We are using PayPal Checkout Server-side PHP (Orders v2 REST API with PayPal JavaScript SDK). For a long time, we have been suffering a problem, with almost 2-5 % of our orders not successfully redirecting to the success page. Once the payment is successful (on our custom build e-commerce) the redirect happens from JavaScript:
onApprove: function(data, actions) {
return fetch(
'/paypal_checkout/get_order_details',{ method: 'GET'}
).then(function(res) {
return res.json();
}).then(function(res) {
if (res.data.status == "APPROVED") {
// capture order
return fetch(
'/paypal_checkout/capture_order',{method:'POST'})
.then(function(resData) {
return resData.json();
}).then(function(resData) {
let errorDetail = resData.data.hasOwnProperty('details') && Array.isArray(resData.data.details) && resData.data.details[0];
if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
showErrorWithText(errTxt);
}
let captureStatus = resData.data.purchase_units[0].payments.captures[0].status;
if (resData.ack && captureStatus == 'COMPLETED') {
window.location.href = 'kasse.html?schritt=3';
}
}
Can we do this redirect from the server side?
2
Answers
Approval happens on the client side, so redirection must happen on the client side, but what you can do is move where you do the redirect. Essentially:
Then, on that page you redirect to, immediately proceed with the capture and show the result.
(If you want there to be a review step/action at the redirected page before capturing, there is a user_action = CONTINUE parameter you can set in order creation request, but your question doesn’t mention that and so can keep the default PAY_NOW behavior and immediately capture)
The onApprove function can include an XHR call to your server, which can respond with a path. That way you can do server-side work and then send the browser to another page.
Alternatively your onApprove function can redirect to your server which can return a 302 redirect response.