向PayPal付款 REST API 发送请求会返回 HTTP 代码 204,并且不会返回任何内容



我想调用我的服务器,然后调用PayPal Payouts REST API进行特定付款。我设置了必要的帐户,当我与邮递员一起尝试时,一切正常!我调用 URL:https://api.sandbox.paypal.com/v1/payments/payouts,令牌和Content-Type设置为application/json和正文

{
"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"
}]
}

这将返回类似

{
"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"
}
]
}

HHTP 代码 201(已创建(。这是完全正确的,当使用答案中的payout_batch_id调用适当的 URL 时,我可以看到我的付款状态。

但是,如果我尝试从我的 NodeJS 服务器进行相同的调用,则会出现问题。我得到了令牌并且一切正常,但随后我像这样创建我的请求:

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"
}]
}
};

然后,我使用以下代码使用request模块发送请求:

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);
}
})

但这将导致获得状态代码为 204(无内容(的答案,并且它包含,难怪,没有内容,因此不可能像我使用 Postman 获得的服务器答案那样获得我的付款状态。我的错误在哪里?

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);
}
})

您必须使用您的数据作为第二个参数

正确的答案是像这样链接请求中的正文:

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
};

现在PayPal返回正确的服务器答案 201。

最新更新