8000 fix(backend): round amounts to cents instead of flooring them by 5eBeKs · Pull Request #70 · aelassas/movinin · GitHub
[go: up one dir, main page]

Skip to content

fix(backend): round amounts to cents instead of flooring them - #70

Merged
aelassas merged 1 commit into
aelassas:mainfrom
5eBeKs:fix/round-cents
Nov 5, 2026
Merged

aelassas merged 1 commit into
aelassas:mainfrom
5eBeKs:fix/round-cents

Conversation

@5eBeKs
@5eBeKs 5eBeKs commented Nov 4, 2026
Copy link
Copy Markdown
Contributor

Amounts are turned into cents with Math.floor(price * 100) (formatPayPalPrice for PayPal orders, and the Stripe Checkout / PaymentIntent amounts and their checks). Booking prices are already rounded to 2 decimals, but many of them aren't exact in floating point: 285.71 * 100 is 28570.999..., so Math.floor drops a cent.

On PayPal this rejects a payment that went through. Example: a Weekly property at 1000, booked for 2 days. calculateTotalPrice gives 285.71, which is stored as the booking price. createOrder sends formatPayPalPrice(285.71) = 285.70 to PayPal, the customer approves and the order is captured, then checkPayPalOrder compares 285.70 with booking.price.toFixed(2) = 285.71 and returns 400 "Payment amount mismatch". The customer has paid, the checkout shows an error, and the booking stays Void until its TTL removes it. It is deterministic for a given amount, so retrying fails the same way. It happens with Weekly, Monthly and Yearly prices (Daily prices with whole numbers are exact): with the seed's monthly prices and stays of 1 to 120 days, about 3% of totals hit it.

On Stripe both sides use the same Math.floor, so the check passes, but the customer is charged one cent less than the booking price for the same amounts.

The fix adds helper.toCents() (Math.round(amount * 100)) and uses it in formatPayPalPrice and in the four Stripe places (Checkout unit_amount and its check in checkCheckoutSession, the PaymentIntent amount and its check in checkout). For amounts that are already at 2 decimals, which is what reaches these functions, rounding gives the intended cent value. formatPayPalPrice now rounds instead of truncating, so an input with three decimals is now rounded to cents (10.006 gives 10.01, not 10.00). The PayPal check already rounds and is unchanged.

Tests: added toCents cases and formatPayPalPrice cases for 1.13 and 285.71, plus a loop checking that for every amount from 0.01 to 1000.00 the PayPal order amount equals the price checkPayPalOrder expects. With formatPayPalPrice still flooring, the test fails (1.12 instead of 1.13). In backend, npm run lint passes and npm run test passes except three booking.test.ts checkout cases. Those call the Stripe API and need a real Stripe test key, which my environment doesn't have; they fail the same way on main.

Note: a Stripe Checkout session created before the deploy and checked after it would compare a floored amount with a rounded one, so a payment for one of these amounts made across the deploy would be rejected. The same applies to a mobile PaymentIntent created before the deploy and verified by checkout after it. Deploying when there are no payments in progress avoids that.

Math.floor(price * 100) drops a cent for amounts like 285.71 or 1.13,
whose product with 100 is just below the integer in floating point.
The PayPal order was created for one cent less than the booking price,
so checkPayPalOrder rejected the captured payment as an amount mismatch.
Stripe amounts were one cent short on both sides.
@aelassas
aelassas merged commit 7227535 into aelassas:main Nov 5, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0