PayPal REST API - status does not change after purchase

So, I noticed that when checking the status of the payment

GET /v1/payments/payment/<Payment-Id> 

The "state" of a PayPal response does not change from the "created" even after a PayPal user has purchased this item. Only after I made the payment, the status changed to "approved" .

This makes it difficult to distinguish between a payment that has actually been approved by the customer and one that is still in the process, as they are both in the β€œcreated” state. The only difference in json's answer is that it seems to include shipping_address after the payment has actually been approved. I'm not sure if this is the standard way to make the difference.

What is the standard way to find out if a customer really approved a PayPal transaction using the above REST API call?

Note. I already have PHP callback scripts and work. I am working on a security system that constantly checks the database records to make sure that the approved payment is completed (in the event of a system failure).

I thought I could use the IPN script callback. I have a setup from the CLASSIC API , but it seems that the REST API and the CLASSIC API are incompatible, because the IPN callback does not contain a transaction identifier or any necessary information to be useful.

It seems that when using the REST API, if you miss the redirect call to your web server, this transaction will be lost forever.

+5
source share
1 answer

It is true that you do not report the approved payment from the state field in the search API response, instead you would look for the payer object in the JSON body, and this part of the information will indicate the approved payment resource for you.

Here are the JSON responses of the same PAY-ID before / after client redirection (user approval)

  • Search Fee /v1/payments/payment/PAY-9J02491382988403BK3BMC6I (before user approval):

     { "id": "PAY-9J02491382988403BK3BMC6I", "intent": "sale", "state": "created", "cart": "07U14103P0008801U", "transactions": [ { "amount": { "total": "80.00", "currency": "USD" }, "payee": {"email": " USM@email.com "}, "invoice_number": "55a460ff696br", "item_list": { "items": [ { "name": "Test Ticket 1", "sku": "55a460ff65f13", "price": "10.00", "currency": "USD", "quantity": 1 }, { "name": "Test Ticket 2", "sku": "55a460ff66c7a", "price": "20.00", "currency": "USD", "quantity": 2 }, { "name": "Test Ticket 3", "sku": "55a460ff66ce2", "price": "10.00", "currency": "USD", "quantity": 3 } ], "shipping_address": { "recipient_name": "Test Name", "line1": "Main St 1", "city": "San Jose", "state": "CA", "postal_code": "95131", "country_code": "US" } }, "related_resources": [], "notify_url": "https://bt-pduan-1.c9.io/ipn.php" }], "redirect_urls": { "return_url": "http://localhost:80/getpaypal?paymentId=PAY-9J02491382988403BK3BMC6I", "cancel_url": "http://localhost:80/cancel" }, "create_time": "2016-02-16T06:28:08Z", "update_time": "2016-02-16T06:28:08Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-9J02491382988403BK3BMC6I", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-9J02491382988403BK3BMC6I/execute", "rel": "execute", "method": "POST" }, { "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-07U14103P0008801U", "rel": "approval_url", "method": "REDIRECT" } ] } 
  • Look at the payment after user approval (I delete the contents of JSON, for example, transactional / URL arrays for readability):

     { "id": "PAY-9J02491382988403BK3BMC6I", "intent": "sale", "state": "created", "cart": "07U14103P0008801U", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": " USP@email.com ", "first_name": "Payer", "last_name": "US", "payer_id": "8FMFQ2KVYYHTY", "shipping_address": { "recipient_name": "Test Name", "line1": "Main St 1", "city": "San Jose", "state": "CA", "postal_code": "95131", "country_code": "US" }, "phone": "408-743-9795", "country_code": "US", "billing_address": { "line1": "1 Main St", "line2": "", "city": "San Jose", "state": "CA", "postal_code": "95131", "country_code": "US" } } }, "transactions": [], "redirect_urls": {}, "create_time": "2016-02-16T06:28:08Z", "update_time": "2016-02-16T06:28:08Z", "links": [] } 

By checking the recorded PAY-ID, looking for the payer object in the API response, you can save orders and continue making the call if it was missed in the client return redirection.

Additionally, neither IPN nor webhooks will help in this case, since they are asynchronous requests caused by transaction-level events, which means that a notification will not be sent until the payment is completed.

+3
source

Source: https://habr.com/ru/post/1242342/


All Articles