【问题标题】:PayPal Developer Issues With CommaPayPal 开发人员的逗号问题
【发布时间】:2019-04-23 11:57:21
【问题描述】:

我遇到了一个错误。我正在使用https://developer.paypal.com/docs/checkout/integrate/ 在我的 ASP.NET MVC 项目中实现 PayPal 付款。我的货币是浮动的,该项目的价格为 150,99。每当这个价格通过时,它会说价格是 99,00 欧元。它只读取逗号后面的内容。每当价格为 190,00 时,它会正确地说价格为 190,00 欧元。我该如何解决?

前端的 JavaScript 目前是这样的:

<script>
    var totalPrice = (@ViewBag.totalPrice);
</script>
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>
    // Render the PayPal button
    paypal.Button.render({
        // Set your environment
        env: 'sandbox', // sandbox | production

        // Specify the style of the button
        style: {
            layout: 'horizontal',  // horizontal | vertical
            size: 'large',    // medium | large | responsive
            shape: 'pill',      // pill | rect
            color: 'black'       // gold | blue | silver | white | black
        },

        // Specify allowed and disallowed funding sources
        //
        // Options:
        // - paypal.FUNDING.CARD
        // - paypal.FUNDING.CREDIT
        // - paypal.FUNDING.ELV
        funding: {
            allowed: [
                paypal.FUNDING.CARD,
                paypal.FUNDING.CREDIT
            ],
            disallowed: []
        },

        // Enable Pay Now checkout flow (optional)
        commit: true,

        // PayPal Client IDs - replace with your own
        // Create a PayPal app: https://developer.paypal.com/developer/applications/create
        client: {
            sandbox: '<removed>',
            production: '<insert production client id>'
        },

        payment: function (data, actions) {
            return actions.payment.create({
                payment: {
                    transactions: [
                        {
                            amount: {
                                total: totalPrice,
                                currency: 'EUR'
                            }
                        }
                    ]
                }
            });
        },

        onAuthorize: function (data, actions) {
            return actions.payment.execute()
                .then(function () {
                    window.alert('Payment Complete!');
                });
        }
    }, '#paypal-button-container');
</script>

价格的后端是这样的:

        [HttpGet]
        public IActionResult Index()
        {
            ...
            float totalPrice = 0;
            float sendcost = 2.95f;
            ...
                        foreach(ShoppingCartModel item in model)
                        {
                            item.subtotal = item.qty * item.price;
                            totalPrice += item.subtotal;
                        }
                        if(totalPrice < 100)
                        {
                            ViewBag.totalPrice = totalPrice + sendcost;;
                        }
                        else
                        {
                            ViewBag.totalPrice = totalPrice;
                        }
                    }
                }
                ...
        }

【问题讨论】:

  • 在文档的示例代码中,所有价格的格式都像'150.99'var totalPrice = (@ViewBag.totalPrice);的最终输出是什么?您是否尝试过使用句点作为小数分隔符?
  • @ChrisG 在 JavaScript 中,我在total 尝试了输入 20.99 和 20,99。对于 20.99,它在 PayPal 付款视图上正确显示 20,99 欧元,当我输入 20,99 时,付款屏幕不会出现。 var totalPrice = (@ViewBag.totalPrice); 的最终输出是任何产品价格,可以是 20,99 或 20,00。
  • 最终输出是指源代码在您的浏览器中的样子。我假设它是var totalPrice = '150,99';,因为这是产生我猜的问题的唯一方法?无论如何,问题在于英语以及所有编程语言和 API 在其浮点数中使用.。所以如果你以'150,99'结尾,你只需要replace()这个逗号加上句号。
  • 对不起,我第三次问你:最后的输出是什么?在浏览器的源代码视图中,var totalPrice = 旁边的内容是什么?
  • 对,这就是造成这种情况的原因。 (443) 计算结果为 443,但 (150,99) 计算结果为 99 see here 快速而肮脏的修复是 var totalPrice = parseFloat("@ViewBag.totalPrice".replace(",", ".")); 但正确的修复是设置您的后端,以便使用句点输出浮点值小数分隔符。

标签: javascript c# asp.net-mvc paypal


【解决方案1】:

官方的回答是您的网络应用程序的语言设置会导致服务器输出带有逗号作为小数分隔符的数值。所以

var totalPrice = (@ViewBag.totalPrice);

结束

var totalPrice = (150,99);

在浏览器中,由于 JavaScript 有一个叫做 comma operator 的东西,totalPrice 现在是 99

要在服务器端解决此问题,您可以通过添加将用于格式化的语言环境更改为英语

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-US");

Startup.cs 中的Configure() 方法。

【讨论】:

  • 这应该可行,但应该注意的是,更改 AppDomain 中所有线程的区域性可能会产生其他不希望的效果。另一种选择是使用接受文化作为第二个参数的Single.ToString(string format, IFormatProvider provider) 重载
  • 感谢@Stijn 提供有用的信息!如果我尝试将文化更改为 en-US,它确实会对服务器端的其他数据产生一些影响。
猜你喜欢
  • 2010-12-25
  • 2017-01-08
  • 2012-09-19
  • 2015-11-02
  • 2020-06-06
  • 1970-01-01
  • 2013-11-28
  • 2021-12-24
  • 1970-01-01
相关资源
最近更新 更多