1. คุณจะต้องได้รับ userId และ checkStateToken.
ใน
API การชำระเงิน ค่านี้จะถูกส่งมาในคำขอเริ่มต้นเพื่อสร้างการชำระเงิน คุณสามารถแทรกค่าดังกล่าวลงในพารามิเตอร์ของหน้าการชำระเงินของคุณได้ทันที
2. ในหน้าการประมวลผลการชำระเงินของคุณ ให้เพิ่มสคริปต์ต่อไปนี้:
<script src="https://app.bukza.com/static/js/bukzaCheckState.js"></script>
3. เมื่อ DOM พร้อมแล้ว ให้รันโค้ดต่อไปนี้:
var bukzaCheckState = new BukzaCheckState({
userId: 12345,
token: 'eyJVc2VySWQiOjIsIk9yZGVyS...Ws9In0%3D',
handler: function (result) {
console.log(result); //{isFinished:true, isValid:true, amount:99.99}
}
});
bukzaCheckState.bind();
สคริปต์นี้จะตรวจสอบสถานะคำสั่งซื้อ ทุก ๆ 10 วินาที จะได้รับผลลัพธ์และเรียกใช้ฟังก์ชัน handler ของคุณ ใน handler คุณสามารถตรวจสอบผลลัพธ์และดำเนินการที่เหมาะสม: แสดงข้อผิดพลาด พาผู้ใช้กลับไปหน้าก่อนหน้า หรือแสดงหน้าสำเร็จ
หากไม่ได้ระบุค่า userId และ token สคริปต์จะดึงค่าดังกล่าวจาก query parameters ของหน้า: bukzaUserId และ bukzaCheckStateToken
ตัวอย่างจริงสามารถดูได้ในซอร์สโค้ดของหน้า https://app.bukza.com/paw.html โดยใช้ handler ดังนี้:
handler: function (result) {
if (result) {
if (result.isFinished) {
if (result.isValid) {
//order is completed succesfully
window.location.href = 'https://app.bukza.com/result.html?result=success&culture=en';
} else {
//order is completed but for some reason it is not valid
window.location.href = 'https://app.bukza.com/result.html?result=warning&culture=en';
}
} else {
if (result.isValid) {
if (result.amount != window.amount) {
//จำนวนเงินที่ต้องชำระเปลี่ยนแปลง กลับไปหน้าก่อนหน้า
window.history.go(-1);
} else {
//ทุกอย่างปกติ รอการชำระเงิน
}
} else {
//คำสั่งซื้อไม่ถูกต้อง กลับไปหน้าก่อนหน้า
window.history.go(-1);
}
}
}
else {
window.location.href = 'https://app.bukza.com/result.html?result=error&culture=en';
}
}