我创建了一个实现了 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)将信息插入数据库问题是成功付款后可能会发生一些事情,并且用户可能不会被定向到回调脚本以获取值(value),那么如果他/她没有被定向到回调脚本,我该如何使用 webhook 为用户提供值(value)?
提前致谢
最佳答案
通过这个documentation .它包含与 webhooks 相关的所有答案。
您可以在 dashboard 上指定您的 webhook 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
?>
关于php - 使用 paystack 支付网关使用 webhook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65791999/