This question is about paypal v2 API on how to relaod the page once transaction completed.
With my actual code I can perform the transaction and it is saved successfuly in my database.
the only part is how to reload after full execution ?
here my code
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value:''.$amount.'',
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
return fetch("/__path_to_save_db.php", {
method: "post",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({
orderID: data.orderID,
paymentID: data.paymentID,
payerID: data.payerID,
})
});
});
}
}).render("#paypal-button-container");
I tried to add a javascript reload but it reloads before the end of execution 🙁
here
return actions.order.capture().then(function(details) {
location.reload();
return fetch("/__path_to_save_db.php", {
it seems that location.reload() is triggered too quickly…. how to make it execute only once the paypal answer is fully loaded ?
2
Answers
I found a solution that works for me
the "confirm" after a 1 second delay to have a dialog that when clicked, it reloads
You should not be capturing payments on the client side and then depending on the client to send this information to a server.
Change this to a proper client server payment integration. Create two routes, one for ‘Set Up Transaction’ and one for ‘Capture Transaction’, documented here.
Pair your two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server
Your code to display a success message — or do a page "reload" or redirect, if you are feeling like an old-fashioned web 2.0 designer — should go in the case that is handled when the server returns a successful capture.