Search code examples
paypal

PayPal - merchant canceled transaction


I'm new to PayPal integrations. I have an application in node.js and my client asked for PayPal integration. What I've done so far :

  1. call /v1/oauth2/token to obtain the token
  2. call /v2/checkout/orders to obtain the approve link
  3. redirect to the url obtained at step 2
  4. follow the steps on paypal to pay
  5. the success_url is called, I suppose everything worked fine

Problem: the money does not go to the merchant. Sometimes they are taken from my card and and after few minutes they are send back with the message: merchant cancel your transaction. Sometimes the money are not taken from my card. I'm using a live client_id and secret.

Does anybody have this issue?


Solution

  • I suppose everything worked fine

    You suppose, but the process is not complete. The payer approved the order, but no payment takes place until you capture the order. You are missing the capture API step. When using such a redirect integration, it is expected that your "success_url" (as you call it, though the API parameter is "redirect_url") should show an order review page, and when final confirmation is given your system should then execute a v2/checkout/orders Capture API call, to complete the payment, and show the success (thank you) or failure (there was an error) result of this capture API call.


    Moreover, redirecting away from your website is an old PayPal integration flow, for old websites. Current PayPal Checkout integrations use no redirects. At all.

    Instead, make 2 routes (url paths) on your server, one for 'Create Order' and one for 'Capture Order'. Both of these routes should return only JSON data (no HTML or text). Inside the 2nd route, when the capture API is successful you should verify the amount was correct and store its resulting payment details in your database (particularly purchase_units[0].payments.captures[0].id, which is the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) immediately before forwarding your return JSON to the frontend caller. In the event of an error forward the JSON details of it as well, since the frontend must handle such cases.

    Pair those 2 routes with this frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server . (If you need to send any additional data from the client to the server, such as an items array or selected options, add a body parameter to the fetch with a value that is a JSON string or object)


    Since you're using node, the first link has a full stack example in node. Just be sure to extend the onApprove function with full client-side error handling (see second link) as the node sample doesn't include that level of detail at the moment.