Javascript 将请求发送到PayPal Payouts REST API将返回HTTP代码204,并且不返回任何内容
我想调用我的服务器,然后它将调用PayPal Payouts REST API来进行特定的支付。我建立了必要的账户,当我和邮递员一起尝试时,一切都很好!我调用URL:Javascript 将请求发送到PayPal Payouts REST API将返回HTTP代码204,并且不返回任何内容,javascript,node.js,paypal,Javascript,Node.js,Paypal,我想调用我的服务器,然后它将调用PayPal Payouts REST API来进行特定的支付。我建立了必要的账户,当我和邮递员一起尝试时,一切都很好!我调用URL:https://api.sandbox.paypal.com/v1/payments/payouts标记和内容类型设置为application/json和正文 { "sender_batch_header": { "sender_batch_id": "Payouts_2018_100013", "email_s
https://api.sandbox.paypal.com/v1/payments/payouts
标记和内容类型设置为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(已创建)。这是完全正确的,我可以在使用答案中的付款批处理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"
}]
}
};
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
};
然后,我使用带有以下代码的请求
模块发送请求:
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);
}
})
您必须将数据用作第二个参数
您必须将数据用作第二个参数正确答案是将请求中的正文链接如下:
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"
}]
}
};
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。正确答案是将请求中的正文链接如下:
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"
}]
}
};
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。这样:{data:}
?这样:{data:}
?