Payments were created but were never executed. Can I execute them after weeks?

vikas_thakur
Contributor
Contributor

I am using paypal-rest-sdk@1.8.1 for nodeJs. 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": "fake_email"
            }
        }
    };   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 paylpal 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.

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.