i have successful created ajax code to send data to external api (payment gateway).
The problem is how can i get data after they pay and show "waiting payment" button before display "Thank you" container ?
Below is my code for ajax post data :
$.ajax({
url: 'creating_bill.php',
data: {
paid_amount : JSON.stringify(jumlah_semua),
email : emel,
mobile : telefon,
name : nama
},
type: "POST",
dataType: "json",
success: function (data) {
confirm('Terima Kasih ! Sila buat pembayaran dengan segera.');
console.log(data)
window.open(data.url, '_blank');
setTimeout(function()
{
window.location = 'index.html';
},10000);
},
async: false,
error: function(data) {
handleRequestError(data);
}
})
}
Here is the api doc link for payment completion : BillPlz doc
But i have no idea how its work. How can i post the data and get the data back in same ajax request ?
Basically my system process is like this.
The code i post above is a code that i use to post customer data to payment gateway api. My problem now is, how can i show "waiting" message while waiting for customer to complete their payment and show "Thank you" message when payment has been completed.
</div>
So you will have to make a separate request to check if the user has completed paying the bill. Basically you create a function which:
Also removing async: false
is probably a good idea since it blocks the browser while the request is running.
Your code should be along the lines of:
function checkBillStatus() {
$.ajax({
...
// Compose the ajax request to check the bill status
...
success: function (data) {
// Here you need to check if the bill is paid
if (data.isBillPaid) {
console.log('Remove waiting for the payment message');
console.log('Show thank you for the payment message');
// Payment is done, redirecting to index
setTimeout(function() {
window.location = 'index.html';
},10000);
} else {
// Repeat the request after a while
setTimeout(checkBillStatus, 1000);
}
}
});
}
$.ajax({
...
success: function (data) {
confirm('Terima Kasih ! Sila buat pembayaran dengan segera.');
console.log('Add waiting for the payment message');
console.log('User confirmed the payment, redirecting to gateway');
window.open(data.url, '_blank');
setTimeout(checkBillStatus, 1000);
},
async: true,
...
});
So after the confirm
you should show the "waiting" message, then the code opens a gateway page and sets a timeout to check the bill status in 1 second. The checkBillStatus
function itself performs a bill's status check and if it is not paid, it sets timeout to check the bill status again in 1 second. And so on until the bill is paid. When it is, it displays the 'thank you' message and redirects to index.
You will have to rely on the gateway to close the opened window.