'POST /api/mpesa/callbackurl 502 Bad Gateway in ngrok in mpesa integration

i am integrating mpesa to m laravel app.I have been able to simulate a transaction and a user can perfectly make a payment. after the payment I want the details of the payment to be stored in the database,through the callback url.i have made an api route that call the function which encodes and save the data in the db.i am using ngrok to tunnel my localhost to the callback url.whenever i execute the function in postman and successfully makes the payment,i get an error on ngrok "POST /api/mpesa/callbackurl 502 Bad Gateway"..i have researched and found it a server error but i have channelled the localhost well in ngrok..how can i fix this..

here is my stkpush function

public function stkpush(Request $request)
{
    $url='https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';

    $curl_post_data=[
        'BusinessShortCode'=>174379,
        'Password'=>$this->lipanampesapassword(),
        'Timestamp'=>Carbon::rawParse('now')->format('YmdHms'),

        'TransactionType'=> "CustomerPayBillOnline",
        'Amount'=>1,
        'PartyA'=>254712345678,
        'PartyB'=>174379,
        'PhoneNumber'=>254712345678,
        'CallBackURL'=>'https://89af-196-202-210-53.eu.ngrok.io/api/mpesa/callbackurl',
        'AccountReference'=>'Waweru Enterprises',
        'TransactionDesc'=>'Paying for Products Bought'
    ];

    $data_string=json_encode($curl_post_data);

    $curl=curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type:application/json','Authorization:Bearer '.$this->newaccesstoken()));
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl,CURLOPT_POST,true);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data_string);

    $curl_response=curl_exec($curl);
    return $curl_response;
}

the callback url route in the api.php

Route::post('/mpesa/callbackurl', [MpesatransactionController::class,'mpesaresponse'])->name('mpesaresponse');

the mpesa response function

public function mpesaresponse(Request $request)
{
    $response=$request->getContent();

    $transaction=new mpesatransaction;
    $transaction->response=json_encode($response);
    $transaction->save();

}

the ngrok panel ngrok panel



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source