I am working on a website with a PayPal (checkout/REST API) integration that requires data from a form be saved to a database table. As far as I know, it is not possible to have PayPal send data gathered in a form to a webhook in order to save it to the DB. I am getting around this by first saving as pending, with the orderID, and then, in the webhook updating the record with the same orderID to not be pending.
However, this approach relies on the fact that the webhook is invoked after the saving to the database as pending (which is invoked on the client side via AJAX). This indeed seems to always be the case. Can I rely on the webhook being called second?
Here is the JavaScript code similar to what I am using (details specfic to the site have been removed: this is from the PayPal developer docs):
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '0.01'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name);
// Call your server to save the transaction
return fetch('/paypal-transaction-complete', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
});
});
}
}).render('#paypal-button-container');
</script>
Will the AJAX call in onApprove
reliably happen before the webhook is invoked?
Thank you for reading.