I am calculating subtotal, tax, shipping and insurance based on the following:
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)
}
The total is fixed with each product.
When I post this to PayPal API, the numbers sometimes add up and sometimes they do not. I am loss for how to fix it.
The following is an example of what is posted and I do not understand why PayPal is rejecting this example:
details {…}
handling_fee 1.00
insurance 0.23
shipping 0.68
shipping_discount 0.00
subtotal 19.30
tax 1.60
total 22.81
All of these numbers were based of 22.81 and the console shows:
tax: 1.5967
shipping: 0.6842999999999999
ins: 0.2281
total: 22.81
subtotal: 19.3009
How can I keep parseFloat().toFixed(2) from changing the numbers? And if I leave the numbers with having more than two decimal place number then PayPal rejects it for being malformatted.
When I use a calculator on these numbers, I receive a result of 22.81
The following is the error that I receive from PayPal - if I comment out the details section, then the post goes through without errors:
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"
}
The following are a couple of screen shots:
For sure, as the amount of products increase, so does the difference in what is added, along with the total given, which leads to my overall question.
How should I go about calculating these items correctly?
Thanks in advance