我根据以下内容计算小计、税收、运费和保险:
public getSubTotal() {
this.subTotal =
document.getElementById("total").value -
(document.getElementById("total").value * 0.07 +
document.getElementById("total").value * 0.03 +
document.getElementById("total").value * 0.01 +
1.0);
document.getElementById("tax").value =
document.getElementById("total").value * 0.07;
document.getElementById("shipping").value =
document.getElementById("total").value * 0.03;
document.getElementById("insurance").value =
document.getElementById("total").value * 0.01;
console.log(
"tax: " +
document.getElementById("total").value * 0.07 +
" shipping: " +
document.getElementById("total").value * 0.03 +
" ins: " +
document.getElementById("total").value * 0.01 +
" total: " +
document.getElementById("total").value +
" subtotal: " +
this.subTotal
);
return this.subTotal.toFixed(2);
}
details: {
subtotal: document.getElementById("subTotal").value,
tax: parseFloat(document.getElementById("tax").value).toFixed(
2
),
shipping: parseFloat(
document.getElementById("shipping").value
).toFixed(2),
handling_fee: "1.00",
shipping_discount: "0.00",
insurance: parseFloat(
document.getElementById("insurance").value
).toFixed(2)
}
每个产品的总数是固定的。
当我将此发布到 PayPal API 时,这些数字有时会加起来,有时不会。我不知道如何解决它。
以下是发布内容的示例,我不明白为什么 PayPal 拒绝此示例:
details {…}
handling_fee 1.00
insurance 0.23
shipping 0.68
shipping_discount 0.00
subtotal 19.30
tax 1.60
total 22.81
所有这些数字都基于 22.81,控制台显示:
tax: 1.5967
shipping: 0.6842999999999999
ins: 0.2281
total: 22.81
subtotal: 19.3009
如何防止 parseFloat().toFixed(2) 更改数字?如果我留下的数字有两个以上的小数位数,那么 PayPal 会因为格式错误而拒绝它。
当我对这些数字使用计算器时,我得到的结果是 22.81
以下是我从 PayPal 收到的错误 - 如果我注释掉详细信息部分,那么帖子会顺利通过:
Error: Request to post https://www.sandbox.paypal.com/v1/payments/payment failed with 400 error. Correlation id: 12d500c6247f1, 12d500c6247f1
{
"name": "VALIDATION_ERROR",
"details": [
{
"field": "transactions[0]",
"issue": "Item amount must add up to specified amount subtotal (or total if amount details not specified)"
}
],
"message": "Invalid request - see details",
"information_link": "https://developer.paypal.com/docs/api/payments/#errors",
"debug_id": "12d500c6247f1"
}
当然,随着产品数量的增加,添加量的差异以及给定的总量也会增加,这导致了我的整体问题。
我应该如何正确计算这些项目?
提前致谢