如何将PayPal部分退款的主体添加到Axios



我正试图使用Axios向PayPal发布部分退款。如果我用一个空物体作为身体,我可以全额退款。但我不知道如何添加一个可以完成部分退款的主体。这是我当前的代码:

const axios = require('axios');
const qs = require('qs');
const refund = await axios.post("https://api-m.sandbox.paypal.com/v1/payments/capture/" 
+ "myTransactionID" + "/refund", 
qs.stringify({data:{amount:{currency_code:'USD',value:'20.00'}}}), //this works if I just use {}; 
{ 
headers: {
"Content-Type": `application/json`,
"Authorization": `Bearer ${ "myPayPalAccessToken" }`
},     
});

console.log("refund: " + JSON.stringify(refund));

我得到了一个";请求失败,状态代码为"400";当我这样做的时候。我不确定是否有必要使用数据对象。请帮我弄清楚语法。

我想明白了。我应该使用application/json作为内容类型。没有必要把尸体串起来:

const axios = require('axios');
const qs = require('qs');
const PAYPAL_OAUTH_API = 'https://api.sandbox.paypal.com/v1/oauth2/token/';
const PAYPAL_PAYMENTS_API = 'https://api.sandbox.paypal.com/v2/payments/captures/';
const PayPalAuthorization = await axios({ 
method: 'POST', 
url: PAYPAL_OAUTH_API,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Credentials': true
},
data: qs.stringify({
grant_type: 'client_credentials'
}),
auth: {
username: PAYPAL_CLIENT,
password: PAYPAL_SECRET
}
});
const PayPalToken = PayPalAuthorization.data.access_token;
const refund = await axios({
url: PAYPAL_PAYMENTS_API + "myTransactionID" + '/refund',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${ PayPalToken }`
},
data: {
amount: {
value: "10.99",
currency_code: "USD"
},
invoice_id: "INVOICE-123",
note_to_payer: "Defective product"
}
});

如果您正在发布invoice_id,请不要忘记更改后续退款的号码。

还可以查看这些链接:

https://developer.paypal.com/docs/checkout/integration-features/refunds/

https://developer.paypal.com/docs/api/payments/v2#captures_refund

最新更新