如何将 PayPal 部分退款的正文添加到 Axios?

How do I add body for PayPal partial refund to Axios?

我正在尝试使用 Axios post 向 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 作为 Content-Type。不需要对正文进行字符串化:

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