Repository navigation
Conversation
…egative Repeated entries in discounts.codes[] were each applied, so a fixed-amount code listed N times was subtracted N times and the total could go negative through to a completed order. Apply each code once, report repeats with discount_code_already_applied, and cap fixed-amount discounts at the remaining total. Adds cart and cart-to-checkout tests.
damaz91
approved these changes
Oct 27, 2026
damaz91
left a comment
Contributor
There was a problem hiding this comment.
Thank you @sauravGit for reporting #266 and putting together such a clean, well-structured fix!
Extracting the shared helpers into services/discount_rules.py keeps CartService and CheckoutService nicely aligned. In particular:
- Capping
fixed_amountdiscounts against the remaininggrand_totalbefore subtracting (while keeping thediscount_amount > 0guard) ensures the receipt ledger always reconciles (sum(non-total entries) == total) and avoids emitting zero-amountdiscountentries that would violatetotal.json'sexclusiveMaximum: 0constraint. - Preserving
discounts.codesas submitted while trackingseen_codescase-insensitively ensures$.discounts.codes[i]inmessages[]points to the exact repeated entry, andreplace_discount_warningscleanly prevents stale warnings from accumulating across recalculations.
A couple of small, non-blocking suggestions for optional polish if you'd like:
seen_codeswhendiscount_amount == 0(services/cart_service.py:385,services/checkout_service.py:1468): If an earlier discount has already broughtgrand_totalto0(or a percentage discount truncates to0minor units) and a subsequent code incodes[]is also repeated (e.g.,["BIG5000", "EXTRA100", "EXTRA100"]),EXTRA100evaluates to0and is omitted fromdiscounts.applied, but its repeat still emits"Code 'EXTRA100' is already applied". Movingseen_codes.add(code.upper())insideif discount_amount > 0:would ensurediscount_code_already_appliedis only emitted for codes that actually applied.- Direct checkout update test (
cart_test.py): Adding a short test exercisingPUT /checkout-sessions/{id}directly (applying a repeated code, followed by aPUTthat omitsdiscountsto verify warnings don't duplicate, and completing a$0-total checkout) would nicely complement the cart and cart-to-checkout tests.
LGTM!
…ady applied A repeated code was marked seen even when it contributed nothing (the running total was already zero), so its repeat was reported as already applied. Mark a code seen only once it reduces the total. Adds a direct checkout create/update/complete test with a zero total and a cart test for the zero-effect case.
Author
Thanks for the review, @damaz91! Both suggestions are in this commit 43950fd:
|
damaz91
approved these changes
Oct 27, 2026
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.
Fixes #266
Repeated entries in discounts.codes[] were each applied, so a fixed-amount code listed N times was subtracted N times and the total could go negative through to a completed order.
Changes: