PayPal express checkout: how should I check if the transaction is done and money is transferred?

827 Views Asked by At

I have PayPal express checkout on the client side and after the callback is fired with payment ID I want to check that id on the NodeJS side. I make a call to RESTfull API using JS SDK:

https://developer.paypal.com/docs/api/payments/v1/#payment_get

and I get such object below.

state is approved but transactions[0].related_resources[0].sale.state === pending

So how should I check if the transaction is done and money is transferred to the seller to activate the service he/she paid for?

from the callback, I have buyer email, payerId, paymentID, paymentToken so maybe I need some other endpoint?

{
            "id": "PAY-7SN4959762125513LLPOWTDA",
            "intent": "sale",
            "state": "approved",
            "cart": "2A9434180T1061602",
            "payer": {
                "payment_method": "paypal",
                "status": "VERIFIED",
                "payer_info": {
                    "email": "[email protected]",
                    "first_name": "Some",
                    "last_name": "Surname",
                    "payer_id": "E76V3DRUQZYZW",
                    "shipping_address": {
                        "recipient_name": "Some Surname"
                    },
                    "phone": "0455346902",
                    "country_code": "FR"
                }
            },
            "transactions": [
                {
                    "amount": {
                        "total": "27.96",
                        "currency": "EUR",
                        "details": {
                            "subtotal": "27.96"
                        }
                    },
                    "payee": {
                        "merchant_id": "FJ96CP2E7QWMY"
                    },
                    "description": "The payment for services.",
                    "custom": "ws-1XWCLNRbVdWavMVZgH2cSE",
                    "invoice_number": "in-1h7KSgdCh35Gdpi_Yk7PO_",
                    "item_list": {
                        "items": [],
                        "shipping_address": {
                            "recipient_name": "Some Surname"
                        }
                    },
                    "related_resources": [
                        {
                            "sale": {
                                "id": "7UB08136N5814360K",
                                "state": "pending",
                                "amount": {
                                    "total": "27.96",
                                    "currency": "EUR",
                                    "details": {
                                        "subtotal": "27.96"
                                    }
                                },
                                "payment_mode": "INSTANT_TRANSFER",
                                "reason_code": "RECEIVING_PREFERENCE_MANDATES_MANUAL_ACTION",
                                "protection_eligibility": "ELIGIBLE",
                                "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
                                "parent_payment": "PAY-7SN4959762125513LLPOWTDA",
                                "create_time": "2018-11-03T09:25:53Z",
                                "update_time": "2018-11-03T09:25:53Z",
                                "links": [
                                    {
                                        "href": "https://api.sandbox.paypal.com/v1/payments/sale/7UB08136N5814360K",
                                        "rel": "self",
                                        "method": "GET"
                                    },
                                    {
                                        "href": "https://api.sandbox.paypal.com/v1/payments/sale/7UB08136N5814360K/refund",
                                        "rel": "refund",
                                        "method": "POST"
                                    },
                                    {
                                        "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7SN4959762125513LLPOWTDA",
                                        "rel": "parent_payment",
                                        "method": "GET"
                                    }
                                ]
                            }
                        }
                    ]
                }
            ],
            "create_time": "2018-11-03T09:25:32Z",
            "links": [
                {
                    "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7SN4959762125513LLPOWTDA",
                    "rel": "self",
                    "method": "GET"
                }
            ],
            "httpStatusCode": 200
        }
2

There are 2 best solutions below

0
On BEST ANSWER

That's what I finally found out:

The "approved" state just means the buyer logged into their account and approved the payment. You still need to execute the payment request, which once you do check the "state" shown in the "related_resources" section and it should say "completed", which indicates the transaction amount has been captured. The "id" shown below is the transaction identifier.

https://www.paypal-community.com/t5/REST-APIs/IPN-vs-actions-payment-execute-result/td-p/1564037

0
On
<PayPalButtons
        createOrder = {async(data, actions) => {
            return actions.order.create({
                purchase_units: [
                    {
                      
                        amount: {
                            value:'value',
                        },
                    },
                ],
            });
        }}

        onApprove = {async (data, actions) => {
            //Ignore Capture on Client side and do it on server
            //Send Data to Server (mostly data.orderId and data.accessToken)
        }}
    />


// Backend
const fetch = require('node-fetch');
// capture Function
const capturePayment = async (orderId, accessToken) => { //orderId and accessToken from frontend
  
   const url = 'https://api-m.sandbox.paypal.com/v2/checkout/orders/${orderId}/capture';
   const response = await fetch(url,{
      method:"post",
      headers: {
         "Content-Type": "application/json",
         Authorization:`Bearer ${accessToken}`, 
      },
   });
   const data = await response.json();
   console.log(data)
   return data;
}