How To Verify Paystack Payment Transaction With PHP and jQuery

This is a continuation of my previous tutorial on how to integrate Paystack checkout to your website.

The aim of this tutorial is to check the status of every payment transaction a user made on our website, display it to the user and even save it on our database if we want to(saving transaction details to database is not part of this tutorial).

Requirements

  1. Complete the previous tutorial and then you are ready to go.
  2. Your Paystack SECRET_KEY
  3. Intermediate knowledge of PHP and jQuery
  4. A live server or local server like WAMP, MAMP or LAMP

Creating our PHP file

Lets begin by creating our PHP file and name it verify.php. Our code here would handle only post requests, use the data to make a POST request to Paystack verification endpoint and return the details of a particular transaction using the transaction reference.

The code here can only make this request if it receives a POST request with a POST data. This request can be from jQuery AJAX or POST request.

Our first line of code would check if a POST request is made and also if the data we need(in this case the transaction reference) was sent to this file. If the condition is true then set the post data to a variable, if not we call PHP die() function with error message as its argument.

//check if request was made with the right data
if(!$_SERVER['REQUEST_METHOD'] == 'POST' || !isset($_POST['reference'])){
die("Transaction reference not found");
}
//set reference to a variable @reference
$reference = $_POST['reference'];

Next is to make a POST call to Paystack verification endpoint with our secret key and the transaction reference. But before then lets do some house keeping.

Let’s set the endpoint URL to a variable with transaction reference appended at the end of the URL, open connection, set URL, set HTTP header etc. Our HTTP header will contain our secret key in order for the request to be successful.

//The parameter after verify/ is the transaction reference to be verified
$url = 'https://api.paystack.co/transaction/verify/'.$reference;
//open connection
$ch = curl_init();
//set request parameters 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [’Authorization: Bearer secret_key’]);

Now we can make our post request by calling the curl_exec() function. After that, we close the connection and then declare an array that will contain the data returned from the request if it was successful.

//send request
$request = curl_exec($ch);
//close connection
curl_close($ch);
//declare an array that will contain the result
$result = array();

Check the request and convert it to array using json_decode() function and then assign it to the array declared.

if($request){
$result = json_decode($request, true);
}

Check if the array contains a sub array data and if data contains an element status and if status equals success.

Note that a transaction status can be abandoned, failed, or success according to Paystack documentation. You can also check the format of a verification response here.
if (array_key_exists('data', $result) && array_key_exists('status', $result['data']) && ($result['data']['status'] === 'success')) {
echo "success";
//Perform necessary action
}else{
echo "Transaction was unsuccessful";
}

Here is the link to our verify.php file.


Link jQuery to our index.html file

In order to send POST request with jQuery we need to link the jQuery library to our index.html like we did for app.js files from the previous tutorial.

<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="crossorigin="anonymous"></script>

Sending POST request with jQuery

Using jQuery $.post() we will make a post request to verify.php file. We will make this request inside the callback() function.

The callback() function returns a response data after a user has completed a payment transaction.

This data contains the transaction reference . So we will send the POST request this data.

Inside the callback() function of our jQuery $.post() function we will check the response data returned from our from our verify.php file and alert the user the status of the transaction.

callback: function (response) {
//after the transaction have been completed
//send transaction reference to the server to verify payment
  $.post(
"verify.php",
{reference:response.reference},
function(status){
      if(status == "success"){
//successful transaction
alert('Transaction was successful');
}else{
//transaction failed
alert(status);
}
});
},

Here is the link to our app.js file

Now run the program from your local or live server.

Here is the link to the Github repository of this tutorial

Here is the link to the Paystack’s payment verification reference.

Your feedback will be greatly appreciated.