【发布时间】: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)计算结果为99see here 快速而肮脏的修复是var totalPrice = parseFloat("@ViewBag.totalPrice".replace(",", "."));但正确的修复是设置您的后端,以便使用句点输出浮点值小数分隔符。
标签: javascript c# asp.net-mvc paypal