对于PayStack,您应该包含官方的接受支付javascript,它将在支付完成时触发一个事件(无论支付是否成功,都会触发此触发器)
注意:我将首先使用系统生成的“参考”(例如[日期]-[随机字符串])在数据库中插入一条付款记录(包括姓名、电子邮件、金额),然后使用以下 HTML接受付款:
接受付款的 HTML
<form id="paymentForm">
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" id="email-address" required />
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="tel" id="amount" required />
</div>
<div class="form-group">
<label for="first-name">First Name</label>
<input type="text" id="first-name" />
</div>
<div class="form-group">
<label for="last-name">Last Name</label>
<input type="text" id="last-name" />
</div>
<div class="form-submit">
<button type="submit" onclick="payWithPaystack()"> Pay </button>
</div>
</form>
<script src="https://js.paystack.co/v1/inline.js"></script>
js是这样的:
const paymentForm = document.getElementById('paymentForm');
paymentForm.addEventListener("submit", payWithPaystack, false);
function payWithPaystack(e) {
e.preventDefault();
let handler = PaystackPop.setup({
key: 'pk_test_xxxxxxxxxx', // Replace with your public key
email: document.getElementById("email-address").value,
amount: document.getElementById("amount").value * 100,
ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
// label: "Optional string that replaces customer email"
onClose: function(){
alert('Window closed.');
},
callback: function(response){
let message = 'Payment complete! Reference: ' + response.reference;
alert(message);
}
});
handler.openIframe();
}
请确保上面的 ref 使用的是之前生成的 [date]-[random string]。
注意:支付步骤完成后,会触发回调事件。
当然,您不会希望在回调中“提醒消息”,您应该调用(例如通过 ajax)触发 CURL 以“验证/检查”付款状态的 PHP。一个典型的 CRUL 是这样的:
curl https://api.paystack.co/transaction/verify/:reference
-H "Authorization: Bearer YOUR_SECRET_KEY"
-X GET
如果你在PHP中实现curl,会是这样的:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.paystack.co/transaction/verify/:reference');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Authorization: Bearer YOUR_SECRET_KEY';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
现在上述 CURL 返回的 $result 将类似于以下之一:
案例 1:成功
{
"status": true,
"message": "Verification successful",
"data": {
"amount": 27000,
"currency": "NGN",
"transaction_date": "2016-10-01T11:03:09.000Z",
"status": "success",
"reference": "DG4uishudoq90LD",
"domain": "test",
"metadata": 0,
"gateway_response": "Successful",
"message": null,
"channel": "card",
"ip_address": "41.1.25.1",
"log": {
"time_spent": 9,
"attempts": 1,
"authentication": null,
"errors": 0,
"success": true,
"mobile": false,
"input": [],
"channel": null,
"history": [{
"type": "input",
"message": "Filled these fields: card number, card expiry, card cvv",
"time": 7
},
{
"type": "action",
"message": "Attempted to pay",
"time": 7
},
{
"type": "success",
"message": "Successfully paid",
"time": 8
},
{
"type": "close",
"message": "Page closed",
"time": 9
}
]
}
"fees": null,
"authorization": {
"authorization_code": "AUTH_8dfhjjdt",
"card_type": "visa",
"last4": "1381",
"exp_month": "08",
"exp_year": "2018",
"bin": "412345",
"bank": "TEST BANK",
"channel": "card",
"signature": "SIG_idyuhgd87dUYSHO92D",
"reusable": true,
"country_code": "NG",
"account_name": "BoJack Horseman"
},
"customer": {
"id": 84312,
"customer_code": "CUS_hdhye17yj8qd2tx",
"first_name": "BoJack",
"last_name": "Horseman",
"email": "bojack@horseman.com"
},
"plan": "PLN_0as2m9n02cl0kp6",
"requested_amount": 1500000
}
}
案例2:支付失败
{
"status":true,
"message":"Verification successful",
"data": {
"amount":27000,
"currency":"NGN",
"transaction_date":"2016-10-01T11:03:09.000Z",
"status":"failed",
"reference":"djfoidjkdkj41",
"domain":"test",
"metadata":0,
"gateway_response":"Insufficient Funds",
"message":null,
"channel":"card",
"ip_address":"41.1.25.1",
"log": {
"time_spent":9,
"attempts":1,
"authentication":null,
"errors":0,
"success":true,
"mobile":false,
"input":[ ],
"channel":null,
"history": [
{
"type":"input",
"message":"Filled these fields: card number, card expiry, card cvv",
"time":7
},
{
"type":"action",
"message":"Attempted to pay",
"time":7
},
{
"type":"close",
"message":"Page closed",
"time":9
}
]
},
"fees":null,
"authorization":{
"authorization_code":"AUTH_8dfhjjdt",
"card_type":"visa",
"last4":"1381",
"exp_month":"08",
"exp_year":"2018",
"bin":"412345",
"bank":"TEST BANK",
"channel":"card",
"signature": "SIG_idyuhgd87dUYSHO92D",
"reusable":true,
"country_code":"NG",
"account_name": "BoJack Horseman"
},
"customer":{
"id":84312,
"customer_code":"CUS_hdhye17yj8qd2tx",
"first_name":"BoJack",
"last_name":"Horseman",
"email":"bojack@horseman.com"
},
"plan":""
}
}
案例 3:无效
{
"status":false,
"message":"Invalid key"
}
因此,您应该依靠 CURL 返回的状态,根据事务“参考”更新您的数据库(例如,如果数据状态为“成功”,则将您的数据库表的事务记录更新为“PAID”)。
最后,如果数据状态为“成功”,请将用户页面重定向到您的比赛页面。