Repository navigation
fix(backend): round amounts to cents instead of flooring them - #70
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Amounts are turned into cents with
Math.floor(price * 100)(formatPayPalPricefor 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 * 100is28570.999..., soMath.floordrops a cent.On PayPal this rejects a payment that went through. Example: a Weekly property at 1000, booked for 2 days.
calculateTotalPricegives 285.71, which is stored as the booking price.createOrdersendsformatPayPalPrice(285.71)=285.70to PayPal, the customer approves and the order is captured, thencheckPayPalOrdercompares 285.70 withbooking.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 informatPayPalPriceand in the four Stripe places (Checkoutunit_amountand its check incheckCheckoutSession, the PaymentIntent amount and its check incheckout). For amounts that are already at 2 decimals, which is what reaches these functions, rounding gives the intended cent value.formatPayPalPricenow rounds instead of truncating, so an input with three decimals is now rounded to cents (10.006 gives10.01, not10.00). The PayPal check already rounds and is unchanged.Tests: added
toCentscases andformatPayPalPricecases 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 pricecheckPayPalOrderexpects. WithformatPayPalPricestill flooring, the test fails (1.12instead of1.13). Inbackend,npm run lintpasses andnpm run testpasses except threebooking.test.tscheckout 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
checkoutafter it. Deploying when there are no payments in progress avoids that.