Handle Errors
DocsLegacyLast updated: June 20th 2023, @ 6:44:12 pm
Learn about how to handle and trigger errors.
Errors
Buyer checkout error
If an error prevents buyer checkout, alert the user that an error has occurred with the buttons using the onError
callback:
paypal.Buttons({
onError: function (err) {
// For example, redirect to a specific error page
window.location.href = "/your-error-page-here";
}
}).render('#paypal-button-container');
Note: This error handler is a catch-all. Errors at this point are not expected to be handled beyond showing a generic error message or page.
Script not loading error
If null pointer errors prevent the script from loading, provide a different checkout experience:
if (window.paypal && window.paypal.Buttons) {
// render the buttons
} else {
// show a fallback experience
}
Test error conditions
To test your onError
function, you can reject a promise in the createSubscription
and onApprove
callbacks. This can be done on click of the button or when the order is captured.
Button click
paypal.Buttons({
// Set up the transaction
createSubscription: function(data, actions) {
// Manually trigger the OnError callback
throw new Error('force the createSubscription callback to fail');
},
onError: (err) => {
console.error('error from the onError callback', err);
}
}).render('#paypal-button-container');
Finalize transaction
paypal.Buttons({
// Finalize the transaction
onApprove: function(data, actions) {
// Manually trigger the OnError callback
throw new Error('force the onApprove callback to fail');
},
onError: (err) => {
console.error('error from the onError callback', err);
}
}).render('#paypal-button-container');