【问题标题】:PayPal responding with "CANNOT_MIX_CURRENCIES" when purchase unit currency changed购买单位货币更改时,PayPal 响应“CANNOT_MIX_CURRENCIES”
【发布时间】:2021-01-03 12:24:40
【问题描述】:

我出售的商品价格和币种不同,具体取决于送货地址。

如果用户有两个地址,一个在美国,另一个在英国,他们将能够通过 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",
                                "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"
        }
    ]
}

我通过react-paypal-button-v2 npm 包使用PayPal Orders API v2

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

CANNOT_MIX_CURRENCIES

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

【问题讨论】:

    标签: reactjs paypal paypal-sandbox paypal-rest-sdk


    【解决方案1】:

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

    另外还有一个 node 包,如果你喜欢依赖超过 3 行函数:) https://github.com/paypal/paypal-js

    【讨论】:

    • 感谢您的快速回复。唯一的问题是每次货币变化时,与贝宝的会话都会重新加载,使用户不得不在我们的网站上再次点击“支付”。在与 PayPal 的会话期间,货币会发生变化。
    • 在将用户发送到 PayPal 之前,您应该整理好详细信息。
    • 抱歉,之前的消息不是很清楚。用户可以在 PayPal 上更改他们的地址(以及因此支付的货币),因此之前无法整理此详细信息。我可以将预选区域之外的地址更改标记为无效,但如果可能,我希望允许用户进行更改。
    • 为什么地址更改需要货币更改?无论如何,在显示按钮之前必须知道货币。
    • 谢谢,我想这只是为我节省了几个小时的调试时间
    猜你喜欢
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 2013-03-14
    • 2021-02-21
    • 2014-01-06
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多