Need to receive a JSON callback from URL and process it in PHP

    I am using a payment gateway which upon payment completion sends a JSON file through a URL to my website, specifically a PHP file.  I have coded the PHP file as follows:

$json = file_get_contents('php://input');
    $obj = json_decode($json, TRUE);
	if ($obj) {
   			   $to = '';
               $subject = "It exists";

    $message .= $smart_id. "\r\n\r\n";
    $message .= $bbct_id. "\r\n\r\n";

   mail( $to, $subject, $message); 
} else {
   			   $to = '';
   $subject = "Nothing here";

    $message .= $smart_id. "\r\n\r\n";
    $message .= $bbct_id. "\r\n\r\n";

   mail( $to, $subject, $message); 

Open in new window

The trouble is.... when I make a payment, the payment gateway says the callback has completed successfully and been acknowledged, but I don't receive an email.  Do I need to do something else to get the callback URL to trigger the PHP page it is calling?

So in a nutshell... I make a payment through the third party gateway, all looks good.  My PHP page is triggered, but I never receive an email from the test above.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Before worrying about the Mail functionality, make sure you script is working  and decoding the JSON correctly. First off, turn on error reporting right the start of your script. Without this any errors will make your script fail silently.

ini_set('display_errors', 1);

Open in new window

Figure out how and what the Payment Gateway is sending to your URL - POST / GET etc, and then test your script by sending similar test data to it (Postman is great for this - You can then run some tests and var_dump your decoded JSON. Could be as simple as:

$obj = json_decode($_POST['myData', TRUE);

Open in new window

Once you've got that, you can move on to coding and testing your mail functionality.

Couple of other things of note in your code. You're never initialising your $message variable. Also, you are trying to send variables called $smart_id and $bbct_id but you're never setting them anywhere. I'm guessing they're supposed to be members of your JSON $obj, in which case you would access them like so:

$message = "";
$message .= $obj['smart_id'] . "\r\n\r\n";
$message .= $obj['bbct_id'] . "\r\n\r\n";

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
damianb123Author Commented:
Hi Chris,
     Thanks for this.  Sorry I should have said it's a POSTed JSON..... as this is done behind the scenes, do I need to do anything to get the PHP file to fire, or will thje fact a JSON file is being posted to the PHP file, be enough to awaken it and run the script through?

I hope that makes sense.

Dave BaldwinFixer of ProblemsCommented:
Since it is being done "behind the scenes" which is normal, you need to save it to a file or a database to see it.  It is not going to run in your browser at the time that it happens.
Chris StanyonWebDevCommented:
Hey Damian,

The trick here is to try and bring it from 'behind the scenes' to a place that you can see what's actually going on. The reason I mentioned POSTMAN is because it makes it easy to test your API calls. By using that, you can simulate your Payment Gateway's request. You can then immediately see the response from your script.

If you were going to use POSTMAN, you would enter the URL of the page your Payment Gateway talks to on your server (or create a simple test script). You will also need some sample data that the Payment Gateway sends. Easiest way to find that is to push a payment through and then using the Network tab of the built-in developer tools (press F12 in Firefox / Chrome) you should see the request coming through. You can copy the request body to your POSTMAN test. Now when you click on SEND in POSTMAN it will send the same data to your script and show you the response. From there you can properly start to debug your script. You can keep sending the request from POSTMAN until your script is working properly.

As I said, turn on error reporting and then var_dump the php://input. You will then see exactly what your script is receiving and how it's handling it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.