Callback URL not receiving JSON data

698 Views Asked by At

I have a PHP page that receives JSON responses located at https://example.ac.ke/op/api/mypesa/index.php I have success posting responses using Postman when I post to either https://example.com/op/api/mypesa/ or https://example.com/op/api/mypesa/index.php but not when I post to https://example.com/op/api/mypesa

I have tried redirecting and adding trailing / in htaccess in vain. I need to make https://example.com/op/api/mypesa to be my callback URL. When I post the receiving page is called but it seems like the data is not redirected.

NEED HELP REDIRECTING BOTH PAGE AND POST DATA Since I have success redirecting page but not the data

My .htaccess has the following content

<IfModule mod_rewrite.c>

     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME}  -f [OR]
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^(.*)$ index.php [L,QSA]

</IfModule>

Posting using postman

Posting using postman to the page https://example.com/op/api/mypesa//

database table receiving the json data from page

The database table receiving the json data from page

Posting using postman to the page

  • Posting using postman to the page https://example.com/op/api/mypesa

Content of my index file located in mypesa folder

<?php
   require 'config.php';
    header("Content-Type: application/json");

    $response = '{
        "ResultCode": 0, 
        "ResultDesc": "Confirmation Received Successfully"
    }';

    $mpesaResponse = file_get_contents('php://input');

    $logFile = "M_PESAConfirmationResponse.txt";

    $jsonMpesaResponse = json_decode($mpesaResponse, true); 

    $transaction = array(
            ':TransactionType'      => $jsonMpesaResponse['TransactionType'],
            ':TransID'              => $jsonMpesaResponse['TransID'],
            ':TransTime'            => $jsonMpesaResponse['TransTime'],
            ':TransAmount'          => $jsonMpesaResponse['TransAmount'],
            ':BusinessShortCode'    => $jsonMpesaResponse['BusinessShortCode'],
            ':BillRefNumber'        => $jsonMpesaResponse['BillRefNumber'],
            ':InvoiceNumber'        => $jsonMpesaResponse['InvoiceNumber'],
            ':OrgAccountBalance'    => $jsonMpesaResponse['OrgAccountBalance'],
            ':ThirdPartyTransID'    => $jsonMpesaResponse['ThirdPartyTransID'],
            ':MSISDN'               => $jsonMpesaResponse['MSISDN'],
            ':FirstName'            => $jsonMpesaResponse['FirstName'],
            ':MiddleName'           => $jsonMpesaResponse['MiddleName'],
            ':LastName'             => $jsonMpesaResponse['LastName']
    );

  
    $log = fopen($logFile, "a");
    fwrite($log, $mpesaResponse);
    fclose($log);

    echo $response;

 
    insert_response($transaction);
?>
1

There are 1 best solutions below

0
Joe Mwangi On

Make sure your callback url is using https to get a response from the safaricom api.You can use ngrok! to tunnel your app on the internet. If you have done this and you are using ubuntu you can disable ufw firewall sudo ufw disable and reboot your computer.