skip to Main Content

I want to make a call to my server, which will then call the PayPal Payouts REST API to make a specific payment. I set up the necessary accounts and when I try it with Postman everything works great! I call the URL: https://api.sandbox.paypal.com/v1/payments/payouts with the token and Content-Type set to application/json and the body

{
  "sender_batch_header": {
    "sender_batch_id": "Payouts_2018_100013",
    "email_subject": "You have a payout!",
    "email_message": "You have received a payout from ****! Thanks for using our service!"
  },
  "items": [
    {
      "recipient_type": "EMAIL",
      "amount": {
        "value": "9.87",
        "currency": "EUR"
      },
      "receiver": "****@*****.com"
    }]
}

This will return something like

{
    "batch_header": {
        "payout_batch_id": "F5YLETFEWFLUS",
        "batch_status": "PENDING",
        "sender_batch_header": {
            "sender_batch_id": "Payouts_2018_100012",
            "email_subject": "You have a payout!",
            "email_message": "You have received a payout from realnote! Thanks for using our service!"
        }
    },
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v1/payments/payouts/F5YLETFEWFLUS",
            "rel": "self",
            "method": "GET",
            "encType": "application/json"
        }
    ]
}

with the HHTP Code 201 (Created). This is completely correct and I can see the status of my payment when calling the appropriate URL with the payout_batch_id from the answer.

However, if I try the same call from my NodeJS server there are problems. I get the token and everything works but then I create my request like this:

const options = {
    url: "https://api.sandbox.paypal.com/v1/payments/payouts",
    headers: {
      'Content-Type': 'application/json'
    },
    auth: {
      'bearer': token
    },
    form: {
      "sender_batch_header": {
        "sender_batch_id": "***_payment_***",
        "email_subject": "You have a payout!",
        "email_message": "You have received a payout from ****! Thanks for using our service!"
      },
      "items": [
        {
          "recipient_type": "EMAIL",
          "amount": {
            "value": "10.0",
            "currency": "EUR"
          },
          "receiver": "*****@*****.com"
        }]
    }
  };

I then send the request using the request module with this code:

request.post(options, function(err,httpResponse,body){
    if (err) {
      console.error('Sending money failed:', err);
    } else {
      console.log('Response from PayPal successful!  Server responded with:', body);

      //var payPalAnswer = JSON.parse(body);
    }
  })

But this will result in getting an answer with the status code 204 (No content) and it contains, no wonder, no content, so there is no possibility to get the status of my payment like with the server answer I got using Postman. Where is my error?

2

Answers


  1. Chosen as BEST ANSWER

    The correct answer was to link the body in the request like this:

    var dataObject =  {"sender_batch_header": {
          "sender_batch_id": "payment_",
          "email_subject": "You have a payout!",
          "email_message": "You have received a payout from *****! Thanks for using our service!"
        },
        "items": [
          {
            "recipient_type": "EMAIL",
            "amount": {
              "value": '10.0',
              "currency": "EUR"
            },
            "receiver": "*******@personal.example.com"
          }]
      };
    
    const options = {
        url: "https://api.sandbox.paypal.com/v1/payments/payouts",
        headers: {
          'Content-Type': 'application/json',
          'Content-Length': dataObject.length
        },
        auth: {
          'bearer': token
        },
        body: dataObject,
        json: true
      };
    

    Now PayPal returns the correct server answer 201.


  2. request.post(options, {data: "..."},function(err,httpResponse,body){
        if (err) {
          console.error('Sending money failed:', err);
        } else {
          console.log('Response from PayPal successful!  Server responded with:', body);
    
          //var payPalAnswer = JSON.parse(body);
        }
      })
    

    You have to use your data as second parameter

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search