购买单位币种更改后,贝宝会以“ CANNOT_MIX_CURRENCIES”回应

问题描述

我要卖的商品的价格和币种因送货地址而异。

如果用户有两个地址,一个在美国,另一个在英国,则可以通过PayPal更改收货地址。当用户将地区从美国更改为英国时,PayPal会通知我的网站,该地区已经发生更改,并且会回复新的币种和价格,从345美元更改为305英镑。

当PayPal收到此更新时,PayPal向客户显示错误。在网络标签中,您可以找到更多的见解,并且可以发现以下错误CANNOT_MIX_CURRENCIES。但是,项目价格和总价格都都转换为所需的货币,因此没有货币的“混合”。下面是产生错误的请求和相应的响应。

请求:PATCH-> 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","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","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","discount":{
                        "currency_code":"USD","value":"0.00"
                    }
                }
            },"items":[
                {
                    "name":"xxx","currency":"USD","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",{
            "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 npm软件包使用react-paypal-button-v2

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

CANNOT_MIX_CURRENCIES

货币代码无效。所有货币代码都非常匹配。对所有金额对象使用相同的货币代码

解决方法

在您加载SDK的<script>行中,货币默认为USD,而此SDK行货币很重要,因为它用于确定在调用order 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
});

或者,如果您喜欢三个以上行函数的依赖项,则有一个节点包:) https://github.com/paypal/paypal-js