How to make Paypal Payouts on a Node.Js environment

1k Views Asked by At

I am trying to execute a PayPal Payout on Node.Js environment, but I can't seem to wrap my head around it.

Could you please help me get the parameters right, and maybe where I should re-organize the code, and eventually start heading in the right direction?

Thank you all in advance.

var PAYPAL_API = "https://api-m.sandbox.paypal.com";

app.post("/my-api/execute-payment/", function (req, res) {
    // 2. Get the payment ID and the payer ID from the request body.
    var paymentID = req.body.paymentID;
    var payerID = req.body.payerID;

    let requestBody = {
        "sender_batch_header": {
            "sender_batch_id": "Payouts_2018_100007",
            "email_subject": "You have a payout!",
            "email_message": "You have received a payout! Thanks for using our service!",
        },
        "items": [
            {
                "recipient_type": "EMAIL",
                "amount": {
                    "value": "9.87",
                    "currency": "USD",
                },
                "note": "Thanks for your patronage!",
                "sender_item_id": "201403140001",
                "receiver": "[email protected]",
                "alternate_notification_method": {
                    "phone": {
                        "country_code": "91",
                        "national_number": "9999988888",
                    },
                },
                "notification_language": "fr-FR",
            },
            {
                "recipient_type": "PAYPAL_ID",
                "amount": {
                    "value": "5.32",
                    "currency": "USD",
                },
                "note": "Thanks for your patronage!",
                "sender_item_id": "201403140003",
                "receiver": "xXXXXXX-50mQSYBJpg_OWguVkwYYYYYYY-ZAFupw-aaaaa-nnnnnnn",
            },
        ],
    };

    request.post(
        PAYPAL_API + "/v1/payments/payouts",
        {
            auth: {
                user: CLIENT,
                pass: SECRET,
            },
            requestBody,
            json: true,
        },
        function (err, response) {
            if (err) {
                console.error(err);
                return res.sendStatus(500);
            }
            // 4. Return a success response to the client
            res.json({
                status: "success",
            });
        }
    );
});
1

There are 1 best solutions below

0
On

Have you tried the Payouts-NodeJS-SDK ?


The main issue with the custom code above is that you aren't first requesting an OAuth 2.0 access_token, and then using the access_token for your REST API call.

See the documentation: https://developer.paypal.com/docs/api/overview/#get-an-access-token

The SDK abstracts this step for you.