首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用paystack支付网关使用webhook

使用paystack支付网关使用webhook
EN

Stack Overflow用户
提问于 2021-01-19 20:58:49
回答 1查看 732关注 0票数 2

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

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

我的脚本的功能应该能更好地说明这一点。

INITIALIZE.PHP脚本

代码语言:javascript
复制
<?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']);

}
?>

回调脚本

代码语言:javascript
复制
$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来为用户赋值?

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2021-01-19 21:23:38

通过此documentation。它提供了与webhooks相关的所有答案。

您可以在dashboard上指定webhook URL (SITEURL/path/ to /webhook.php),无论何时发生事件,paystack都会向其发送POST请求。

要接收该事件,只需在应用程序上创建一个未经身份验证的POST路由(SITEURL/path/ to /webhook.php)。事件对象在请求体中以JSON的形式发送。

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

代码语言:javascript
复制
<?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

?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65791999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档