Money and Currency Arithmetic
Monetary amounts in the OXID shop domain are IEEE-754 floats. Comparing them
with ==, <= or >= accumulates binary-rounding noise, and converting
them for a PSP with (int) ($amount * 100) is wrong twice over: it truncates
instead of rounding, and it assumes every currency has two decimal places.
Payment Base collects the correct arithmetic in src/Math/Money/ so PSP
adapters do not each re-derive it.
Comparing amounts
Money provides comparison helpers with one shared tolerance of half a cent
(Money::HALF_CENT_EPSILON). That is tight enough never to collapse a real
partial amount into a full one, and loose enough to absorb drift from
accumulated float arithmetic.
use OxidEsales\PaymentBase\Math\Money\Money;
Money::equals($captured, $total); // equal within tolerance
Money::greaterThan($refunded, $captured); // strictly greater, beyond tolerance
Money::atLeast($captured, $requested); // ≥, allowing a sub-tolerance shortfall
Money::atMost($refunded, $captured); // ≤, allowing a sub-tolerance overshoot
Use these instead of bare float comparisons whenever you decide whether an amount is fully captured, fully refunded, or still partial. That decision drives refund routing (see Refund Intent Routing), so a false negative there sends the wrong verb to the PSP.
Warning
Do not introduce a local epsilon constant in a PSP module. A second tolerance that disagrees with this one produces contract states that payment-base and the adapter read differently.
Converting to minor units
PSPs expect amounts in the smallest currency unit. How many that is depends on the currency:
Currency type |
Decimals |
Example |
|---|---|---|
Two-decimal (EUR, USD, GBP, …) |
2 |
19.99 EUR → |
Zero-decimal (JPY, KRW, …) |
0 |
¥1000 → |
Three-decimal (BHD, KWD, …) |
3 |
1.234 BHD → |
MinorUnitConverter is a currency-aware static converter:
use OxidEsales\PaymentBase\Math\Money\MinorUnitConverter;
MinorUnitConverter::toMinorUnits(19.99, 'EUR'); // 1999
MinorUnitConverter::toMinorUnits(1000.0, 'JPY'); // 1000
MinorUnitConverter::decimalsFor('BHD'); // 3
Currency matching is case-insensitive. An unknown or empty currency code falls back to two decimals — a shop-agnostic default that is deliberately not hard-wired to EUR.
Conversion rounds rather than truncates. This matters:
(int) (19.99 * 100) // 1998 — wrong: 19.99 * 100 is 1998.9999… in float
(int) round(19.99 * 100) // 1999 — correct
Per-line totals
LineItemAmount holds the gross, net and VAT total for a single basket
line, and builds them from per-unit prices:
use OxidEsales\PaymentBase\Math\Money\LineItemAmount;
$line = LineItemAmount::forQuantity(
unitPrice: 19.99,
netPrice: 16.80,
vatValue: 3.19,
quantity: 3
);
$line->totalPrice; // 59.97
$line->netPrice; // 50.40
$line->vatValue; // 9.57
The per-unit values come from OXID Price objects and are already rounded to
the shop’s currency precision, so forQuantity() introduces no rounding of
its own — it only multiplies.
Important
OXID’s totals win. The order total and the VAT total are the ones the shop calculates. If a PSP needs line items that add up exactly to the transmitted total, the PSP module absorbs the rounding residual in its own payload. The shop’s arithmetic is never bent to fit a provider.
The former setting blPaymentBasePerLineVat, which re-rounded VAT per
line, was removed in v1.7.2 (see Module Settings).