skip to Main Content

I have created an online store with Paystack gateway implemented.

Up till now it’s been successful, but I need a little help with creating a webhook event script, such that if user never redirects to the callback URL to be given value, I can get notified to give user value.

The functionality of my script should throw more light on this..

INITIALIZE.PHP SCRIPT

<?php

session_start();

if(isset($_POST["pay"])){

$curl = curl_init();

$email = $_SESSION["order_details"]["email"];
$amount = $_SESSION["order_details"]["total"];  //the amount in kobo. This value is actually NGN 300

// url to go to after payment
$callback_url = 'http://localhost:8080/phpmyadmin/online_store/order.php';  

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.paystack.co/transaction/initialize",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode([
    'amount'=>$amount,
    'email'=>$email,
    'callback_url' => $callback_url
  ]),
  CURLOPT_HTTPHEADER => [
    "authorization: Bearer sk_test_2563a843c7ddd24e92450fe2ce91f3f18a57ad27", //replace this with your own test key
    "content-type: application/json",
    "cache-control: no-cache"
  ],
));

$response = curl_exec($curl);
$err = curl_error($curl);

if($err){
  // there was an error contacting the Paystack API
  die('Curl returned error: ' . $err);
}

$tranx = json_decode($response, true);

if(!$tranx['status']){
  // there was an error from the API
  print_r('API returned error: ' . $tranx['message']);
}

// comment out this line if you want to redirect the user to the payment page print_r($tranx);
// redirect to page so User can pay
// uncomment this line to allow the user redirect to the payment page

header('Location: ' . 
$tranx['data']['authorization_url']);

}
?>

CALLBACK SCRIPT

$curl = curl_init();
$reference = isset($_GET['reference']) ? $_GET['reference'] : '';

if(!$reference){

  die('No reference supplied');

}

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.paystack.co/transaction/verify/" . rawurlencode($reference),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "accept: application/json",
    "authorization: Bearer sk_test_2563a843c7ddd24e92450fe2ce91f3f18a57ad27",
    "cache-control: no-cache"
  ],
));

$response = curl_exec($curl);
$err = curl_error($curl);

if($err){
    // there was an error contacting the Paystack API
  die('Curl returned error: ' . $err);
}

$tranx = json_decode($response);

if(!$tranx->status){
  // there was an error from the API
  die('API returned error: ' . $tranx->message);
}

if('success' == $tranx->data->status){
  
 // Rest part of the code that gives value to user, adds ordered items and payment info to database, sends email and delete the cart items or unset session (depending if a client or guest)

}

My script allows the user to make payments first by processing the payment through the initialize.php file before inserting Information into the Database through the call back script(order.php)

The problem is something can occur after successful payment and user might not be directed to the callback script to be given value to, so how do I use a webhook to give value to user if he/she wasn’t directed to the callback script ?

Thanks in Advance

2

Answers


  1. Go through this documentation. It has all your answer related to webhooks.

    You can specify your webhook URL (SITEURL/path/to/webhook.php) on your dashboard where paystack would send POST requests to whenever an event occurs.

    All you have to do to receive the event is to create an unauthenticated POST route on your application (SITEURL/path/to/webhook.php). The event object is sent as JSON in the request body.

    You can use this type of code in webhook.php to receive webhook response

    <?php
    
    // Retrieve the request's body and parse it as JSON
    
    $input = @file_get_contents("php://input");
    
    $event = json_decode($input);
    
    // Do something with $event
    
    http_response_code(200); // PHP 5.4 or greater
    
    ?>
    
    Login or Signup to reply.
  2. Between the web hook event and call back event, which one occurs first? If the callback happens first, then with the web hook, a check can be made in the database to know if transact exist.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search