使用 paystack 支付网关使用 webhook

时间:2021-01-19 12:58:49

标签: php webhooks

我创建了一个实施了 Paystack 网关的在线商店。

到目前为止它已经成功了,但我需要一些帮助来创建一个 webhook 事件脚本,这样如果用户从不重定向到回调 URL 以获取值,我可以收到通知以提供用户值。

我的脚本的功能应该对此有更多的了解..

初始化.PHP 脚本

<?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']);

}
?>

回调脚本

$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)

}

我的脚本允许用户首先通过 initialize.php 文件处理付款,然后通过回调脚本(order.php)将信息插入数据库中进行付款

问题是在成功付款后可能会发生一些事情,并且用户可能不会被定向到要为其赋值的回调脚本,所以如果他/她没有被定向到用户,我该如何使用 webhook 为用户赋值回调脚本?

提前致谢

1 个答案:

答案 0 :(得分:2)

通过这个documentation。它包含您所有与 webhook 相关的答案。

您可以在您的 dashboard 上指定您的网络钩子 URL (SITEURL/path/to/webhook.php),每当事件发生时,paystack 都会向其中发送 POST 请求。

接收事件所需要做的就是在您的应用程序 (SITEURL/path/to/webhook.php) 上创建一个未经身份验证的 POST 路由。事件对象在请求正文中以 JSON 格式发送。

您可以在 webhook.php 中使用这种类型的代码来接收 webhook 响应

<?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

?>