Master Of Disaster
Master Of Disaster

Reputation: 387

Laravel 5.8 callback API

Am developing a web application using laravel 5.8 and am stuck before final step.

The full scenario is:
Because am trying mobile money payment service aggregation with a thirdy party company, when i use ajax to send a request with phone number and amount to /requestpayment API they gave me, i get a "pending" JSON response in chrome console which means it works fine.

But they say i have to give them a callback URL to be able to receive the JSON response in my laravel app so that i can for example save a payment record into DB.


Now, the documentation says this regarding responses:

xyz acts as a client and Invokes the App by sending it the status of the pending transaction via an HTTP post. Which can either be successfull or failed. Partners are required to provide an end point url (Callback URL) to which the xyz gateway will submit the request.
Below is an example in Python programming language:

data = {
'requesttransactionid':''4522233',
'transactionid':'6004994884',
'responsecode' :'01',
'status':'Successfull',
'statusdesc':'Successfully Processed Transaction',
'referenceno':'312333883'
 }

r = requests.post(url, json={'jsonpayload':data},headers={'contenttype':"application/json"},verify=False)


So, My question is: How am i going to capture above JSON in my laravel 5.8 app to get that 'responsecode' and 'status'??

Am i going to create an API posting to a controller and then what? Help please!!

Upvotes: 1

Views: 1745

Answers (1)

Christophe Hubert
Christophe Hubert

Reputation: 2951

You need to create a new route in your application to accept this data from your Remote API.

 Route::post('/endpoint', function (Request $request) {
    dd($request->all());
    /*
    [
        'requesttransactionid' => '4522233',
        'transactionid' => '6004994884',
        'responsecode' => '01',
        'status' => 'Successfull',
        'statusdesc' => 'Successfully Processed Transaction',
        'referenceno' => '312333883'
    ]
    */
});       

Don't forget to add the POST exception in your VerifyCsrfToken Middleware

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        '/endpoint',
    ];
}

Once this is done provide the URL of the route to your Provider: https://myapp.com/endpoint

Upvotes: 2

Related Questions