Webhook response

How do I make october receive a webhook (json) response from a bank payment?

Take a look at this routing article to build an endpoint for your payment provider:

You could also create something using a CMS page:

Thank you very much. I’m not able to return the json to the ajaxHandler

{% do response(ajaxHandler(‘onResponse’)) %}
– to
public function onResponse()
{
//retrieve the value here
}

Hi,

url = "/example-api"
==
public function onResponse()
{
    return [
        "key_1" => "value_1",
        "key_2" => "value_2"
    ];
}
==
{% do response(ajaxHandler('onResponse')) %}

gives

image


If the function is inside a component, make sure to include your component:

url = "/example-api"
[component1]
==
{% do response(ajaxHandler('onResponse')) %}

with:

class Component1 extends ComponentBase {

    public function onResponse()
    {
        return [
            "key_1" => "value_1",
            "key_2" => "value_2"
        ];
    }

}

Good morning!
How do you do it in the other direction? I need to manipulate the json data I receive from the bank.

the bank sends to my endpoint:

{
   "id": "ch_Qym35AmhvcZ5goJV",
   "code": "J3X6U97A6O",
   "gateway_id": "16232821",
   "amount": 350,
   "paid_amount": 350,
   "status": "paid",
   "currency": "BRL",
...
}

When I receive the ‘paid’ status, I create the customer’s coupons using the function

Hi,

Here is an option.

You can create a routes.php file in your plugin at root level.

<?php

    use Hatalias\Plugin1\Http\Controllers\OrderController;
    use Hatalias\Plugin1\Http\Middleware\EnsureTokenIsValid;;

    Route::prefix('api/v1/hatalias/plugin1')->middleware(EnsureTokenIsValid::class)->group(function () {

        // Create or update a new order entry
        Route::post('order/{id}', OrderController::class . '@createOrUpdateV1');
        
    });
?>

Then create these two:

plugins\hatalias\plugin1\http\controllers\OrderController.php
plugins\hatalias\plugin1\http\controllers\EnsureTokenIsValid.php

OrderController.php could look like this:

<?php namespace Hatalias\Plugin1\Http\Controllers;

use Backend\Classes\Controller;
use View;
use Illuminate\Http\JsonResponse;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Hatalias\Plugin1\Http\Resources\OrderResource;
use Hatalias\Plugin1\Models\Order;
use Illuminate\Http\Request;

class OrderController extends Controller {


    /**
     * V1 - Create or update a new order
    */
    public function createOrUpdateV1(Request $request, string $id)
    {
        try {
           
            // Validate and process the request data
            $requestData = $request->all();
           
            // Do your logic : Find or create the new order, check the status, create a coupon, update the order...

            return new JsonResponse([
                'success' => true,
                'data' => new OrderResource($order),
            ]);
        } catch (Exception $e) {
            // Handle any exception
            return new JsonResponse(['success' => false, 'message' => $e->getMessage()], 500);
        }   
    }
}

?>

EnsureTokenIsValid.php could look like this:

<?php namespace Hatalias\Plugin1\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Http\JsonResponse;

class EnsureTokenIsValid
{
    /**
     * Handle an incoming request.
     *
     * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $token = $request->bearerToken();

        if (!$token) {
            // Return a JSON response for missing bearer token
            return new JsonResponse(['success' => false, 'message' => 'You need to supply a Bearer Token'], 401);
        }

        if (!isTokenValid()) {
            // Return a JSON response for invalid bearer token
            return new JsonResponse(['success' => false, 'message' => 'Invalid Bearer Token'], 401);
        }

        return $next($request);
    }
}

2 Likes

I love you! saved my life! Thank you very much!!!

2 Likes