0

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?

0

1 Answer 1

0

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:

onApprove: function(data, actions) {
   window.location.href = 'kasse.html?schritt=3&orderid=' + data.id; 
}

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)

5
  • I modified the code, now you can check the whole onApprove: function which contains 'order_creation' and then 'order_capture'. I can not directly redirect once the onApprove function calls coz it does 2 fetch() calls. (also both order creation and capture happen while the Paypal popup is active)
    – mrana
    Commented Mar 8 at 8:44
  • The point is to redirect before the fetch calls, and do those operations at the redirected URL. Commented Mar 8 at 8:45
  • If I redirect before the fetch, is not it the Paypal popup gone? because all the fetches are happening from Paypal popup actions? (once the PP popup opens it creates the 'order fetch' and after login and payment approval the 'capture fetch')
    – mrana
    Commented Mar 8 at 10:26
  • Yes, it is gone. Approval is finished. What remains to be done is to capture the order. Do that at the redirected URL. And do not use a fetch for it; do it as part of the page load and show success/failure on page load, as well as whatever other important thing you are wanting to do at that redirected URL. Commented Mar 8 at 11:10
  • Yes, I understand your idea. I will try that and let you know. thank you
    – mrana
    Commented Mar 8 at 11:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.