Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PayPal payments were created but were never executed

Tags:

I am using [email protected] for Node.js. I have created the payments and saved their payerId, token and paymentId, but I never executed those payments.

var paypal = require('paypal-rest-sdk');
paypal.configure({
'mode': 'live', 
'client_id': '_CLIENT_ID_',
'client_secret': '_CLIENT_SECRET_'
});
.
.
let payment = {
        "intent": "sale",
        "transactions": [{
            "item_list": {
                "items": some_items_array,
                "shipping_address": {
                    "recipient_name": "some_recipient_name",
                    "line1": "Fake street",
                    "city": "Fake city",
                    "country_code": "Fake country",
                    "postal_code": "123456",
                    "state": "Fake state",
                    "phone": "0000000"

                }

            },
            "amount": {
                "currency": "AUD",
                "total": "15",
                "details": {
                    "subtotal":"15",
                    "tax": "0",
                    "shipping": "0",
                    "handling_fee": "0",
                    "shipping_discount": "0",
                    "insurance": "0"
                }

            }
        }],
        "redirect_urls": {
            "cancel_url": some_base_url + "/home/cart",
            "return_url": some_base_url + "/home/thank-you?cartid=" + cartid
        },
        "payer": {
            "payment_method": "paypal",
            "payer_info": {
                "email": "[email protected]"
            }
        }
    };
   paypal.payment.create(payment, function (error, response) {
            if (error) {
                console.log(error);
                // HANDLE ERROR
            } else {
                console.log("Create Payment Response");
                console.log(response);
               //SEND SUCCESS RESPONSE
     }
  });

After successful execution of the above lines, the users are redirected to the PayPal website to make their payment. After the successful payment, it redirects them to the 'return_url' link with payload of information, which is:

{
paymentId:'PAY-000000',
PayerID:'SOME_PAYER_ID',
token:'SOME_TOKEN'
}

For many days I have been only saving paymentId, PayerID, & token in the database and NOT BEEN EXECUTING THE PAYMENT, thinking that was not required when 'intent' is set to 'sale'.

Now I have added the following code to execute the payment, which works fine for the orders which are being placed now.

var execute_payment_json = {
            "payer_id": 'THE_PAYER_ID',
            "transactions": [{
                "amount": {
                    "currency": 'AUD',
                    "total": '15'
                }
            }]
        };
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
            if (error) {
                console.log(JSON.stringify(error.response));
                //HANDLE ERROR
            } else {
                console.log("Get Payment Response");
                console.log(JSON.stringify(payment));
               //SEND SUCCESS
            }
        });

For the older orders for which payment.execute was not done after the the successful redirection, I tried to do 'payment.execute' on them and got the following error.

{
"name":"INVALID_RESOURCE_ID",
"message":"The requested resource ID was not found",
"information_link":"https://developer.paypal.com/docs/api/payments/#errors",
"debug_id":"878378979aac7",
"httpStatusCode":404
}

Please help me if I can execute those old payments. I have their payerId, paymentId, and token.

like image 905
vikasThakur.com Avatar asked Aug 05 '18 07:08

vikasThakur.com


1 Answers

It looks like you're doing what used to be called Express Checkout and forgot to perform the final callback.

What likely happened is your tokens expired. While this is from the docs for the legacy system, I have no reason to believe this doesn't apply here.

A timestamped token that indicates Express Checkout processing for the current payment. This token is typically returned in the SetExpressCheckout response, but if you are calling SetExpressCheckout a second time, using the Express Checkout second redirect flow, you can pass the same token back to SetExpressCheckout as a request parameter. PayPal also appends this token as a GET parameter named token to your RETURN URL when redirecting the buyer back to your website from paypal.com. By default, the token expires after three hours.

Even if that's not entirely true for REST, the same principle holds

  1. You set the call
  2. The customer logged in on the PayPal site and authorized it
  3. PayPal sent you back the token
  4. You forgot to "redeem" the token for payment so it expired

To my knowledge that entire transaction is now dead. You would have to start a new one to get a payment now.

like image 107
Machavity Avatar answered Oct 11 '22 19:10

Machavity