-
-
Notifications
You must be signed in to change notification settings - Fork 10
fix(billing): retry refund correlation backfill 3x + dead-letter #3690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PierreBrisorgueil
merged 7 commits into
master
from
fix/billing-refund-correlation-retry
May 22, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
05b0883
feat(billing): retryWithBackoff helper + BillingFailedBackfill dead-l…
PierreBrisorgueil 3bc6713
fix(billing): retry refund correlation backfill 3x + dead-letter
PierreBrisorgueil 390ddef
docs(billing): runbook for refund correlation backfill failure
PierreBrisorgueil c39b9c3
fix(billing): address code-quality findings on refund correlation retry
PierreBrisorgueil dbb9c25
fix(billing): validate retryWithBackoff options + document makeSession
PierreBrisorgueil 0c5ac16
fix(billing): add err.stack + consistent stripeSessionId key to backf…
PierreBrisorgueil da51b15
Merge remote-tracking branch 'origin/master' into fix/billing-refund-…
PierreBrisorgueil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Retry an async operation with exponential backoff. | ||
| * | ||
| * For default opts (attempts=3, baseMs=200), delays are 200ms then 400ms | ||
| * (no delay after the final attempt). General formula: baseMs * 2^i for | ||
| * each non-final attempt i. | ||
| * | ||
| * Returns the result of the first successful call, or throws the last | ||
| * error after all attempts are exhausted. | ||
| * | ||
| * @param {() => Promise<T>} fn - Async function to attempt. | ||
| * @param {object} [opts] | ||
| * @param {number} [opts.attempts=3] - Maximum number of attempts (including the first call). | ||
| * @param {number} [opts.baseMs=200] - Base delay in ms for the first retry. | ||
| * @returns {Promise<T>} | ||
| */ | ||
| export async function retryWithBackoff(fn, { attempts = 3, baseMs = 200 } = {}) { | ||
| if (!Number.isInteger(attempts) || attempts < 1) { | ||
| throw new TypeError(`retryWithBackoff: attempts must be a positive integer, received ${attempts}`); | ||
| } | ||
| if (!Number.isFinite(baseMs) || baseMs < 0) { | ||
| throw new TypeError(`retryWithBackoff: baseMs must be a non-negative finite number, received ${baseMs}`); | ||
| } | ||
| let lastErr; | ||
| for (let i = 0; i < attempts; i++) { | ||
| try { | ||
| return await fn(); | ||
| } catch (err) { | ||
| lastErr = err; | ||
| if (i < attempts - 1) { | ||
| await new Promise((resolve) => setTimeout(resolve, baseMs * 2 ** i)); | ||
| } | ||
| } | ||
| } | ||
| throw lastErr; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
94 changes: 94 additions & 0 deletions
94
modules/billing/models/billing.failedBackfill.model.mongoose.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /** | ||
| * Module dependencies | ||
| */ | ||
| import mongoose from 'mongoose'; | ||
|
|
||
| const Schema = mongoose.Schema; | ||
|
|
||
| /** | ||
| * BillingFailedBackfill Data Model Mongoose | ||
| * | ||
| * Dead-letter store for PaymentIntent metadata backfill failures. | ||
| * Records are written when the refund-correlation backfill (stripe.paymentIntents.update | ||
| * in handleCheckoutPaymentCompleted) fails after all retry attempts. | ||
| * | ||
| * Kept permanently so operators can manually reconcile unresolved entries. | ||
| * Never auto-expired — resolvedAt is set by the operator after manual fix. | ||
| */ | ||
| const BillingFailedBackfillMongoose = new Schema( | ||
| { | ||
| paymentIntentId: { | ||
| type: String, | ||
| required: true, | ||
| index: true, | ||
| }, | ||
| stripeSessionId: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
| /** | ||
| * Serialised error message from the last failed attempt. | ||
| */ | ||
| error: { | ||
| type: String, | ||
| default: null, | ||
| }, | ||
| /** | ||
| * Timestamp of the first failure (when the record was created). | ||
| */ | ||
| failedAt: { | ||
| type: Date, | ||
| required: true, | ||
| default: () => new Date(), | ||
| }, | ||
| /** | ||
| * Timestamp set by the operator after the PI metadata has been manually patched | ||
| * and the refund correlation risk resolved. | ||
| */ | ||
| resolvedAt: { | ||
| type: Date, | ||
| default: null, | ||
| }, | ||
| /** | ||
| * Operator tag explaining how the record was resolved. | ||
| * E.g. 'admin', 'cron'. | ||
| */ | ||
| resolvedBy: { | ||
| type: String, | ||
| default: null, | ||
| }, | ||
| }, | ||
| { | ||
| collection: 'billing_failed_backfills', | ||
| timestamps: false, | ||
| }, | ||
| ); | ||
|
|
||
| // Partial index — only unresolved documents are indexed, so this stays small | ||
| // even after the collection accumulates many resolved entries. | ||
| // (Sparse would be a no-op here: resolvedAt has default: null, so every document | ||
| // has the field present — sparse skips only docs where the field is absent.) | ||
| BillingFailedBackfillMongoose.index( | ||
| { resolvedAt: 1 }, | ||
| { partialFilterExpression: { resolvedAt: null } }, | ||
| ); | ||
|
|
||
| /** | ||
| * Returns the hex string representation of the document ObjectId. | ||
| * @returns {string} Hex string of the ObjectId. | ||
| */ | ||
| function addID() { | ||
| return this._id.toHexString(); | ||
| } | ||
|
|
||
| /** | ||
| * Model configuration | ||
| */ | ||
| BillingFailedBackfillMongoose.virtual('id').get(addID); | ||
| BillingFailedBackfillMongoose.set('toJSON', { | ||
| virtuals: true, | ||
| }); | ||
|
|
||
| export const BillingFailedBackfill = | ||
| mongoose.models.BillingFailedBackfill ?? | ||
| mongoose.model('BillingFailedBackfill', BillingFailedBackfillMongoose); |
30 changes: 30 additions & 0 deletions
30
modules/billing/repositories/billing.failedBackfill.repository.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
| * Module dependencies | ||
| */ | ||
| import mongoose from 'mongoose'; | ||
|
|
||
| /** | ||
| * @function BillingFailedBackfill | ||
| * @description Lazily resolves the BillingFailedBackfill Mongoose model. | ||
| * Deferred to keep unit tests importable before model registration. | ||
| * @returns {import('mongoose').Model} The registered BillingFailedBackfill model. | ||
| */ | ||
| // biome-ignore lint/correctness/useQwikValidLexicalScope: false positive — Node.js repository, not Qwik | ||
| const BillingFailedBackfill = () => mongoose.model('BillingFailedBackfill'); | ||
|
|
||
| /** | ||
| * @function record | ||
| * @description Write a dead-letter entry for a PaymentIntent metadata backfill failure. | ||
| * Called by billing.webhook.service after all retry attempts are exhausted. | ||
| * @param {object} opts | ||
| * @param {string} opts.paymentIntentId - Stripe PaymentIntent id (pi_*). | ||
| * @param {string} opts.stripeSessionId - Stripe checkout session id (cs_*). | ||
| * @param {string|null} [opts.error] - Serialised error message from the last failed attempt. | ||
| * @param {Date} [opts.failedAt] - Timestamp of the failure (defaults to now). | ||
| * @returns {Promise<import('mongoose').Document>} | ||
| */ | ||
| // biome-ignore lint/correctness/useQwikValidLexicalScope: false positive — Node.js repository, not Qwik | ||
| const record = ({ paymentIntentId, stripeSessionId, error = null, failedAt = new Date() }) => | ||
| BillingFailedBackfill().create({ paymentIntentId, stripeSessionId, error, failedAt }); | ||
|
|
||
| export default { record }; |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.