Reputation: 1595
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:
With PayPal, what I got so far is:
Upvotes: 2
Views: 7415
Reputation: 4575
Because return_url is missing. Here is the below example with array that needs to post in JSON format
Array
(
[intent] => CAPTURE
[purchase_units] => Array
(
[0] => Array
(
[amount] => Array
(
[currency_code] => USD
[value] => 100
)
[payment_instruction] => Array
(
[disbursement_mode] => INSTANT
)
)
)
[payment_source] => Array
(
[paypal] => Array
(
[experience_context] => Array
(
[return_url] => https://example.com/return
[cancel_url] => https://example.com/cancel
)
[name] => Array
(
[given_name] => A
[surname] => B
)
)
)
)
Upvotes: 2
Reputation: 30432
- Send request containing order detail to /v2/checkout/orders
- Get a response containing links
- Redirect my customer to approve link
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 be continue
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)
Upvotes: 4
Reputation: 29
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:
$postData = [
"intent" => "CAPTURE",
"purchase_units" => $purchase_units,
"application_context" => [
"return_url" => "",
"cancel_url" => "",
],
];
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
Upvotes: 1