Search code examples
node.jspaypalpaypal-sandboxpaypal-adaptive-paymentspaypal-rest-sdk

Paypal batch payout not working in node js api


I am trying to create a batch payout with a test sandbox account. The 'create payment' function is working absolutely fine with correct response:

var paypal_sdk = require('paypal-rest-sdk');

var config_opts = {
    'host': host, //host defined
    'port':'',
    'mode':'sandbox',
    'client_id': client_id, //clientID defined
    'client_secret': client_secret, //clientSecret defined
};

var makePayment = function (req, res, next) {
    var create_payment_json = {
        "intent": "sale",
        "payer": {
            "payment_method": "paypal"
        },
        "redirect_urls": {
            "return_url": "http://mystore.in",
            "cancel_url": "http://mystore.in/contact"
        },
        "transactions": [
            {
                "amount": {
                    "currency": "USD",
                    "total": "1.00"
                },
                "description": "This is the payment description."
            }
        ]
    };

    paypal_sdk.payment.create(create_payment_json,config_opts, function (err, data) {
        if (err) console.log("ERRRRR", err);
        console.log("Create Payment Response");
        console.log(data);
        //res.send('201');
    });
}

makePayment();  //CALLING THE FUNCTION

But, when I am trying to create a new payout, by editing create_payment_json as:

var create_payment_json = {
        "sender_batch_header": {
            "email_subject": "You have a Payout!",
            "recipient_type": "EMAIL"
        },
        "items": [
            {
                "recipient_type": "EMAIL",
                "amount": {
                    "value": "1.0",
                    "currency": "USD"
                },
                "note": "Thanks for your patronage!",
                "sender_item_id": "201403140001",
                "receiver": "[email protected]"
            }
        ]
    };

And main function as:

paypal_sdk.payout.create(create_payment_json,config_opts, function (err, data) {
        if (err) console.log("ERRRRR", err);
        console.log("Create Payment Response");
        console.log(data);
        //res.send('201');
    });

I am receiving an error as follows:

{ [Error: Response Status : 401]
  response: 
   { error: 'invalid_client',
     error_description: 'Client Authentication failed',
     httpStatusCode: 401 },
  httpStatusCode: 401 }

However, I am sure that credentials passed are correct and they are working perfectly in case of payment creation. I am doing all this for the testing first. Also, the payout feature is enabled for this respective test paypal developer account.

Is there any possible solution?


Solution

  • I just got the solution. Well, to add, I must say paypal should provide proper error handling to let the developers know the reason behind each error.

    The error was because of the currency that I had set in the configuration. The testing developer account was of some other country and the currency code was set as "USD". Just make a new account and set currency code in config according to the country you had selected during the creation of that test developer account.