0

我正在尝试使用 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”。我不确定是否需要使用数据对象。请帮我弄清楚语法。

4

1 回答 1

0

我想到了。我应该一直使用 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

于 2020-11-20T21:25:59.210 回答