DEV Community

Payneteasy
Payneteasy

Posted on

Your amount helper assumes every currency has two decimals

Found this when a merchant selling in Japan reported that orders cost 100x too much. Most cards were declined on amount. The few that passed were charged ¥100,000 for a ¥1,000 item.

Our amount helper did what most tutorials do: Math.round(price * 100). That works for USD, EUR and GBP. It breaks for JPY and KRW, which have zero decimals in ISO 4217. It also breaks the other way for BHD, KWD and JOD, which have three. A dinar price of 1.500 sent as 150 reaches the gateway as 0.150.

Sandbox accepted everything, because every test amount was in USD. The first signal was an approval rate drop on a single currency. We read it as an issuer problem for a full day.

What we changed:

  • The exponent lives in one table keyed by ISO code, not in a constant.
  • Amounts are integers in minor units from the cart onward, with no floats after the first conversion.
  • One test pushes the same price of 10 through USD, JPY and BHD and asserts 1000, 10 and 10000.
  • If the input has more decimals than the currency allows, we reject it instead of rounding silently.

Do you validate the exponent on your side, or do you rely on the gateway to reject a bad amount?

Payneteasy handles this kind of payment flow for merchants and PSPs — see how it works.

Top comments (1)

Collapse
 
mickyarun profile image
arun rajkumar •

The sentence that matters here is the one about sandbox. Every test amount was in USD, so the exponent was never exercised. The bug was in the fixture; the helper is only where it surfaced.

To answer your question: validate on your side, and do not take ISO 4217 as the source of truth. Take the table your gateway uses. They are not always the same table. HUF has two decimals in ISO and several acquirers expect it sent with zero, so if you treat ISO as canonical you ship a 100x on a currency where your code is technically correct. The exponent is a property of the route, not of the currency, which is why it belongs in config next to the credentials rather than in a shared constants file.

Two places the same bug comes back after the cart is fixed:

Partial refunds. A refund computed as a percentage of a minor-unit integer gives you a float again, and the rounding direction decides whether you can ever refund the last unit. Refund in minor units and carry the remainder somewhere explicit.

Reconciliation. The settlement file arrives in the scheme's minor units and your side reads it with your table. A mismatch there declines nothing. It makes two numbers disagree by a factor of 100, and whoever reconciles reaches for FX first.

Reject rather than round is the right call and I would make it loud. A silent round is how you get a dispute nobody can reproduce.