当购买单位货币更改时,PayPal 以 "CANNOT_MIX_CURRENCIES" 响应

PayPal responding with "CANNOT_MIX_CURRENCIES" when purchase unit currency changed

我要销售的商品根据送货地址的不同而具有不同的价格和货币。

如果用户有两个地址,一个在美国,另一个在英国,他们将能够通过 PayPal 更改收货地址。当用户将地区从美国更改为英国时,PayPal 会通知我的网站发生了地区更改,它会回复新的货币和价格,从 345 美元更改为 305 英镑。

当 PayPal 收到此更新时,PayPal 会向客户显示一条错误消息。在网络选项卡中有更多的洞察力,可以发现以下错误:CANNOT_MIX_CURRENCIES。但是,项目价格和总价都转换为所需的货币,因此不存在货币“混合”。以下是生成错误的请求和随附的响应。

请求:补丁 -> www.sandbox.paypal.com/smart/api/order/890595684S747592L/patch

{
    "data":{
        "patch":[
            {
                "op":"replace",
                "path":"/purchase_units/@reference_id=='xxx'",
                "value":{
                    "reference_id":"xxx",
                    "invoice_id":"xxx",
                    "custom_id":1,
                    "description":"xxx",
                    "amount":{
                        "currency_code":"GBP",
                        "value":"305.00",
                        "breakdown":{
                            "item_total":{
                                "currency_code":"GBP",
                                "value":"305.00"
                            },
                            "shipping":{
                                "currency_code":"GBP",
                                "value":"0.00"
                            },
                            "tax_total":{
                                "currency_code":"GBP",
                                "value":"0.00"
                            },
                            "discount":{
                                "currency_code":"GBP",
                                "value":"0.00"
                            }
                        }
                    },
                    "items":[
                        {
                            "name":"xxx",
                            "sku":"xxx",
                            "currency":"GBP",
                            "quantity":1,
                            "category":"PHYSICAL_GOODS",
                            "unit_amount":{
                                "currency_code":"GBP",
                                "value":"305.00"
                            }
                        }
                    ]
                }
            }
        ]
    }

}

回复:

{
  "ack":"contingency",
  "contingency":"UNPROCESSABLE_ENTITY",
  "data": {
    "name":"UNPROCESSABLE_ENTITY",
    "details":[{ 
      "location":"body",
      "issue":"CANNOT_MIX_CURRENCIES",
      "description":"CANNOT_MIX_CURRENCIES"
    }],
    "message":"The requested action could not be performed, semantically incorrect, or failed business validation.",
    "debug_id":"xxx",
    "links":[{
      "href":"https://developer.paypal.com/docs/api/orders/v2/#error-CANNOT_MIX_CURRENCIES",
      "rel":"information_link",
      "method":"GET"
    }]
  },
  "meta":{"calc":"xxx","rlog":"xxx"},
  "server":"xxx"
}

下面是创建上述采购单位的请求(和响应)。

请求:POST -> POST -> www.sandbox.paypal.com/v2/checkout/orders

{
    "intent":"CAPTURE",
    "purchase_units":[
        {
            "reference_id":"xxx",
            "invoice_id":"xxx",
            "custom_id":1,
            "description":"xxx",
            "amount":{
                "currency_code":"USD",
                "value":"345.00",
                "breakdown":{
                    "item_total":{
                        "currency_code":"USD",
                        "value":"345.00"
                    },
                    "shipping":{
                        "currency_code":"USD",
                        "value":"0.00"
                    },
                    "tax_total":{
                        "currency_code":"USD",
                        "value":"0.00"
                    },
                    "discount":{
                        "currency_code":"USD",
                        "value":"0.00"
                    }
                }
            },
            "items":[
                {
                    "name":"xxx",
                    "sku":"xxx",
                    "currency":"USD",
                    "quantity":1,
                    "category":"PHYSICAL_GOODS",
                    "unit_amount":{
                        "currency_code":"USD",
                        "value":"345.00"
                    }
                }
            ]
        }
    ],
    "application_context":{
        "shipping_preference":"GET_FROM_FILE"
    }
}

回复:

{
    "id":"xxx",
    "status":"CREATED",
    "links":[
        {
            "href":"https://api.sandbox.paypal.com/v2/checkout/orders/xxx",
            "rel":"self",
            "method":"GET"
        },
        {
            "href":"https://www.sandbox.paypal.com/checkoutnow?token=xxx",
            "rel":"approve",
            "method":"GET"
        },
        {
            "href":"https://api.sandbox.paypal.com/v2/checkout/orders/xxx",
            "rel":"update",
            "method":"PATCH"
        },
        {
            "href":"https://api.sandbox.paypal.com/v2/checkout/orders/xxx/capture",
            "rel":"capture",
            "method":"POST"
        }
    ]
}

我正在使用 PayPal Orders API v2, via the react-paypal-button-v2 npm 包。

“CANNOT_MIX_CURRENCIES”的文档提到采购单位中的所有货币必须相同,但没有提到不可能更改采购单位的货币。令人困惑的是,我只能在 Billing Agreements API v1.

中找到提到这个错误(“CANNOT_MIX_CURRENCIES”)

CANNOT_MIX_CURRENCIES

The currency code is not valid. All currency codes much match. Use same currency code for all amount objects.

在您加载 SDK 的 <script> 行中,货币默认为美元,此 SDK 行货币很重要,因为它用于确定在订单 v2 对象之前可以显示哪些按钮打电话。

如果您需要在页面加载后更改货币,您可以异步重新加载 SDK。

function loadAsync(url, callback) {
    var s = document.createElement('script');
    s.setAttribute('src', url); s.onload = callback;
    document.head.insertBefore(s, document.head.firstElementChild);
}

// Usage:

loadAsync('https://www.paypal.com/sdk/js?client-id=sb&currency=USD', function() {
  paypal.Buttons({
    createOrder: function(data, actions) {
        //your code here
    },
    onApprove: function(data, actions) {
        //your code here
    }
  }).render('body');  // Replace with selector to render in
});

或者有一个节点包,如果你喜欢依赖超过 3 行函数:) https://github.com/paypal/paypal-js