Try to catch a managed PayPalCheckoutSdk.Orders.OrdersCreateRequest error from PHP server in the JavaScript client side

Viewed 1779

I want to set up PayPal standard payments with :

I use the samples and the code works with a few modifications. I receive the orderId from the server and the processing continues on the client side (display of the payment popup, ...)

Now I want to deal with the case when CreateOrder fails on the server side. In this case, I send back an HTTP error code : header('HTTP/1.1 500 xxxxxx')

On client side, it's impossible to have error-free processing. I always get an error in the console : 'create_order_error' or/and 'click_initiate_payment_reject'.

I've tried to use the catch function on the promise, or return Promise.reject('xxx'), but nothing does anything about it.

Do you have a solution?

1 Answers

If the createOrder on the server fails, simply don't send back an id (or orderId, if you are naming your key that) value. Not having an id results in error enough, although you can also send back separate, additional keys that describe the error condition, such as send back the whole JSON object from the PayPal API (if you want to) for informational purposes. It will be visible in the Network tab and assist with debugging. You don't need to send back an HTTP 500, which may not be appropriate depending on the type of error (a 4xx may be more appropriate), and -- unlike XMLHttpRequest or .ajax() -- fetch ignores such statuses anyway unless you explicitly check status.ok

Where you do need to more concerned about error handling is in the onApprove function, after attempting a capture, as this has business logic consequences. Here is the best JS sample demo of what to do: https://developer.paypal.com/demo/checkout/#/pattern/server

Related