0

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);
?>
Stephen Ostermiller
  • 21,408
  • 12
  • 81
  • 104
  • What happens when the post doesn't work? Do you get an error message? If there is any code related to this, please edit the post and include it. – kiner_shah Nov 06 '21 at 09:53
  • I don't get any error. It is only that the data cannot be read from the php file wen i use https://xxxxx.com/op/api/mypesa but when i use https://xxxxx.com/op/api/mypesa/ I can be able to read from file and take to database – Shadrack Kipkorir Nov 06 '21 at 16:14
  • Both URL seem to be the same. – kiner_shah Nov 07 '21 at 06:54
  • Sorry the second has a / i.e. https://xxxxx.com/op/api/mypesa// – Shadrack Kipkorir Nov 07 '21 at 07:03
  • Both URLs are still the same, having a `/` has no effect. But that is given, the server handles request with `/` the same way it does it without `/`. If you're owning the server side code, you need to add an additional route with `/` included. – kiner_shah Nov 07 '21 at 07:25
  • Does this answer your question? [Redirect all to index.php using htaccess](https://stackoverflow.com/questions/18406156/redirect-all-to-index-php-using-htaccess) – kiner_shah Nov 07 '21 at 07:36
  • I own the server side code. Payment notifications from a bank need to be sent to https://xxxxx.com/op/api/mypesa. That is the end point i provided them. Since i wasn't receiving the json data i asked them why and they told me my end point is not reachable. They further said https://xxxxx.com/op/api/mypesa and https://xxxxx.com/op/api/mypesa// are not the same. They told me i need to create endpoint by the url i gave them which was https://xxxxx.com/op/api/mypesa – Shadrack Kipkorir Nov 07 '21 at 07:43
  • I have a folder name mypesa and inside i have a index.php file that receives the json data – Shadrack Kipkorir Nov 07 '21 at 07:49
  • You mentioned `.htaccess`, please edit the post and include it's contents. Also, do add the corresponding tag. – kiner_shah Nov 07 '21 at 07:53
  • @kiner_shah, the post has been updated – Shadrack Kipkorir Nov 07 '21 at 08:37
  • Does this answer your question? https://stackoverflow.com/a/17709700. Can you try modifying the `.htaccess` file with this and check if it works. – kiner_shah Nov 07 '21 at 10:52
  • It doesn't. The problem still persists – Shadrack Kipkorir Nov 07 '21 at 13:19
  • What changes did you do? Please edit the post and include the details about the changes you did. – kiner_shah Nov 08 '21 at 03:36
  • Why don't you set the endpoint URL to be `/op/api/mypesa/` to begin with? Does the external service not allow that? – CBroe Nov 08 '21 at 07:50
  • This is likely the [`DirectorySlash`](https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash) directive at work here, which will cause an external redirect that clients follow by making a GET request, and that's how you lose your POST data. But before turning that off, you should read the security warning in the manual. – CBroe Nov 08 '21 at 07:53

0 Answers0