arrow_back_ios

จะตรวจสอบสถานะคำสั่งซื้อขณะประมวลผลการชำระเงินบนเว็บไซต์ของฉันได้อย่างไร?

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 สคริปต์จะนำค่าเหล่านี้มาจากพารามิเตอร์คิวรีของหน้า: 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) {
                    //amount to pay has changed, going to the previous page
                    window.history.go(-1);
                } else {
                    //everything is ok, waiting for payment
                }
            } else {
                //order became invalid, going to the previous page
                window.history.go(-1);
            }
        }
    }
    else {
        window.location.href = 'https://app.bukza.com/result.html?result=error&culture=en';
    }
}