Search code examples
restpaypal-rest-sdk

Paypal API : how to get order/payment details by custom parameter


I'm using Paypal button to let users do a payment on my site and after payment I get confirmation/payment's details through Paypal PDT.

It works perfect but sometimes it happens that the user doesn't go back to my site after the payment on Paypal. So, I can't read the transaction id in the return url to call the API to get the payment details.

In the end: I have the correct payment on Paypal but I also have a non-paid order on my system.

My Idea is to create a secondary call that gets the payment details to complete the order "manually". But, as I wrote, I don'have an order id or transaction id.

The only way to match the order on my system and its payment on Paypal is to use the custom parameter through which I send the order id, like this:

My question: is it possibile to get the payment details with a API call using the custom parameter as a filter?

I had a look to Paypal REST APIs and I think these calls should do the work:

/v2/checkout/orders/{id} /v2/payments/captures/{capture_id}

In my case, something like this would be awesome:

/v2/checkout/orders/{custom_id=MyOrdeId} /v2/payments/captures/{custom_id=MyOrdeId}

I hope my question is clear.

Thanks


Solution

  • custom_id is an arbitrary metadata field for your own purposes. It is not indexed, and you cannot query by it.

    If you need later asynchronous notifications of completed orders, you can subscribe to webhook events. However this is not a good solution for normal payment processing, and should be reserved for cases where it's needed (such as notification of post-checkout events like a refund or dispute).

    For regular checkouts, what you should do is use the immediate capture API response to receive details about all one-time payments. This is best paired with the JS SDK for an approval flow, only redirecting to a new thank you/success page (if you need a separate page for that) once everything is successful and already recorded.

    Follow the standard checkout integration guide which will call two routes on your server, one to create an order and one to capture it. When the capture route is called, it should (within the server route itself) verify the total amount in the capture response API is correct and you can then save the order in your records immediately, before forwarding the JSON response to the front end caller.

    The frontend caller JS can display a success message on that very same page (a no redirect solution), or redirect only once a successful capture occurs.


    Another thing to note is that custom_id is for arbitrary metadata, but if you have a field that is your own unique checkout/order ID within your system the best field to save it in is invoice_id . This will prevent (block) future payments if there has been a completed one with the same invoice_id, which is useful for preventing duplicate payments, and the invoice_id field being unique is actually indexed and can be searched in the www.paypal.com interface to locate transactions.