Repository navigation
Add promo code support to billing API - #685
alisawavezen12 wants to merge 20 commits into
Conversation
neSpecc
left a comment
There was a problem hiding this comment.
Overall, the promo logic itself mostly works and is covered by tests, but a few things need to change before merging.
1. The PR changes the regular payment flow, not only promo. Regular /pay and /recurrent were rewritten (see the inline comments). This is the main risk, so please revert them to the master logic. If regular payments need idempotency or handling of out-of-order callbacks, let's do that in a separate PR with its own tests. The existing integration tests in test/integration/cases/billing pass, but they don't cover subscription replacement and cancellation, so green CI doesn't prove the regular flow is safe.
2. Suggested shape of the feature. A promo code should only change the price and reuse the existing flow:
composePayment: get a quote from a promo code service and putpromo: { ... }into the checksum;/check:expectedAmount = data.promo ? data.promo.finalAmount : plan.monthlyCharge, plus a check that the code can still be used. The business operation is created through the factory as usual;/pay: the regular flow plusif (data.promo) redeem(...)(safe to repeat thanks to the unique index). The receipt uses the amount actually paid;/fail: no changes.
DB access should go through PromoCodesFactory / PromoCodeRedemptionsFactory, like the other models, and domain functions shouldn't receive req / res. This removes finishPromoPayment and its own lease mechanism, which duplicates /pay and /fail (plan change, unblock, notifications, receipt, card saving) and will drift from them with every change.
3. payWithCard ignores the promo code. It charges the full price, so /check rejects the payment with WRONG_AMOUNT. Garage doesn't call it now, but let's explicitly reject promo checksums there.
neSpecc
left a comment
There was a problem hiding this comment.
Thanks, all previous comments are addressed. The regular /check, /pay, /fail and /recurrent flow now matches master apart from the separate data.promo blocks, and the promo logic goes through the factories.
A few things left, see inline. The main one is payload.userId in cloudpayments.ts, the rest are naming and descriptions.
| * Read legacy percentage documents without changing stored data. | ||
| * | ||
| * @param query - indexed code or identifier lookup | ||
| * @param tariffPlanId - selected plan | ||
| */ | ||
| private async findActivePromoCode(query: Filter<PromoCode>, tariffPlanId: ObjectId): Promise<PromoCode> { | ||
| const promoCode = await this.collection.findOne(query); | ||
|
|
||
| ensureActivePromoCode(promoCode, tariffPlanId); | ||
|
|
||
| /* | ||
| * shortcut: support discountPercent until existing documents have been migrated to discount. | ||
| */ | ||
| const legacy = promoCode as PromoCode & { discountPercent?: number }; | ||
|
|
||
| if (promoCode.discount === undefined && typeof legacy.discountPercent === 'number') { | ||
| return { | ||
| ...promoCode, | ||
| discount: { | ||
| type: 'percent', | ||
| value: legacy.discountPercent, | ||
| }, | ||
| }; | ||
| } |
There was a problem hiding this comment.
The promoCodes collection is created by this PR, so production has no discountPercent documents. If there are some on stage, let's update them by hand and remove this shim.
Then the description can say what the method is for, e.g. "Find a promo code that can be applied to the selected plan, or throw".
Normalize release names, expose first-seen and lifecycle metadata with request-scoped `isNew` lookups, and expand coverage. Refine promo callback validation and update the package version to 1.7.0.
| discount: PromoCodeDiscount; | ||
| isDisabled?: boolean; |
| if (recurrent && ( | ||
| recurrent.amount !== monthlyCharge || recurrent.period !== 1 || | ||
| recurrent.interval !== (isDebug ? 'Day' : 'Month') || | ||
| !data.nextPaymentDate || | ||
| Date.parse(recurrent.startDate || '') !== Date.parse(data.nextPaymentDate) | ||
| )) { |
There was a problem hiding this comment.
too complex statement, please separate it to few variables for better maintainability
Add promo code validation, plan restrictions, and discounted payment amounts. Reserve promo codes for payments and handle successful and failed payment callbacks.
Related changes in Web: codex-team/hawk.garage#927