I’ve never used PayPal before, it’s not really popular here, and I’m confused by how the order and payment works. Can anyone explain it to me? I’ve read the documentation and I’m still confused.
To complete payer approval, use the approve link to redirect the
payer. The API caller has 3 hours (default setting, this which can be
changed by your account manager to 24/48/72 hours to accommodate your
use case) from the time the order is created, to redirect your payer.
Once redirected, the API caller has 3 hours for the payer to approve
the order and either authorize or capture the order.
I have created a working PHP curl call for this API in sandbox env according to the sample
https://developer.paypal.com/docs/api/orders/v2/#orders_create
The APIs I created before work like this:
- Send a request to 3rd party payment
- Get a response containing a checkout link
- Redirect customer to link
- Customer complete payment
- 3rd party send a request containing order and payment detail to a file/path on my server
- My server receives the request and updates the order status/payment in my database
- 3rd party redirect the customer to my webpage
With PayPal, what I got so far is:
- Send request containing order detail to /v2/checkout/orders
- Get a response containing various links
- Redirect my customer to the rel:approve checkout link
- Log in to my sandbox customer account and pay using PayPal balance, click Continue
- A popup message is shown: We’re sending you back to xxx’s Test Store to complete this purchase
- The popup message is closed and I’m still at the same payment page with the Continue button as seen in the image
2
Answers
Step (3) is an old integration method, for websites using an old redirect-based flow. The preferred way to integrate PayPal uses no redirects. At all. Your website remains loaded in the background. Do the following:
Follow the Set up standard payments guide and make 2 routes on your server, one for ‘Create Order’ and one for ‘Capture Order’, documented here. Both routes should return only JSON data (no HTML or text). Inside the 2nd route, when the capture API is successful you should 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.Pair those 2 routes with the frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server
If, for some odd and inexplicable reason, you insist on using the legacy flow with a full page redirect instead of what I just described above, you must include a
redirect_url
in your orders creation request, so that PayPal has somewhere to return to after the order is approved. Immediately when the return happens, capture the order with an API call and show the success/failure result to the customer. If you want an intervening order review step before capture, you can do this, but you must also edit your initial order creation request to change the verbiage of the last button at PayPal from "Pay Now" to "Continue" so that the user is clicking on something that corresponds to what the next step will be.application_context.user_action
needs to becontinue
for this change.Capturing an order will return a v2/payments object which is the completed transaction with its own ID for accounting and refund purposes. (The order ID is only used during payer approval, and unimportant otherwise)
I had this issue too, and I eventually got a solution from the documentation.
You have to add application_context.return_url to your request.
Attached is an example in PHP:
You can make a whole lot of customisations to the PayPal Payment page by adding the Application Context option, For a full list of the possible customisations, you can check up the official documentation Application Context Documentation