Skip to content

Add promo code support to billing API - #685

Open
alisawavezen12 wants to merge 20 commits into
masterfrom
feat/promocodes
Open

alisawavezen12 wants to merge 20 commits into
masterfrom
feat/promocodes

Conversation

@alisawavezen12

@alisawavezen12 alisawavezen12 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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

Comment thread src/billing/types/paymentData.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved

@neSpecc neSpecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 put promo: { ... } 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 plus if (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.

Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/billing/promoCodes/redemptions.ts Resolved
Comment thread src/billing/promoCodes/redemptions.ts Resolved
Comment thread src/billing/promoCodes/redemptions.ts Resolved
Comment thread src/billing/promoCodes/redemptions.ts Resolved
Comment thread src/billing/promoCodes/redemptions.ts Resolved

@neSpecc neSpecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/billing/cloudpayments.ts Resolved
Comment thread src/models/promoCodesFactory.ts Resolved
Comment thread src/models/promoCodesFactory.ts Outdated
Comment on lines +59 to +82
* 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,
},
};
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Comment thread src/billing/promoCodes/validation.ts Resolved
Comment thread src/billing/promoCodes/redemptions.ts Resolved
Comment thread src/billing/types/response.ts Resolved
Comment thread src/billing/types/paymentData.ts Resolved
alisawavezen12 and others added 5 commits November 18, 2026 16:49
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.
Comment on lines +21 to +22
discount: PromoCodeDiscount;
isDisabled?: boolean;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add docs please

Comment thread src/billing/promoCodes/validation.ts Outdated
Comment on lines +154 to +159
if (recurrent && (
recurrent.amount !== monthlyCharge || recurrent.period !== 1 ||
recurrent.interval !== (isDebug ? 'Day' : 'Month') ||
!data.nextPaymentDate ||
Date.parse(recurrent.startDate || '') !== Date.parse(data.nextPaymentDate)
)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too complex statement, please separate it to few variables for better maintainability

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