fix(console): account for partial Zen refunds
Summary
- make the Stripe
charge.refundedwebhook deduct the actual refunded amount instead of always deducting the original top-up amount - only mark/deduct once by guarding on
payment.time_refunded is null, so webhook retries do not double-deduct - keep subscription/lite enriched payments from mutating Zen balance
Validation
bun run typecheckfrompackages/console/app
Note: local git push pre-push hook runs full monorepo bun turbo typecheck; that failed outside this change in @opencode-ai/server via packages/core/src/tool/{apply-patch,edit}.ts missing diff type declarations. Branch was pushed with HUSKY=0 after the targeted Console app typecheck passed.
Merge review result: not merging yet.
Validation/status checked:
- GitHub checks are green and GitHub reports the PR as mergeable/clean.
- Local focused validation passed in an isolated worktree:
bun install --frozen-lockfile, thenbun run --cwd packages/console/app typecheck. - Synthetic merge-tree against refreshed
origin/devwas clean.
Merge blockers:
- This PR is still a draft and has no reviews.
- The branch is currently 1 commit ahead and 12 commits behind
origin/dev. - More importantly, the refund accounting change still under-deducts multiple partial refunds.
charge.refundedsends a Charge object, and Stripe'samount_refundedis the cumulative refunded amount on that charge; Stripe also allows multiple partial refunds. This patch marksPaymentTable.timeRefundedon the first refund and then requirestime_refunded is nullplusmarked.rowsAffected > 0before deducting balance. A second partial refund for the same payment will therefore deduct nothing, even though the cumulative refunded amount increased.
Suggested fix: persist the refunded amount already accounted for, or process individual refund events with refund-id idempotency, then deduct only the new delta: min(charge.amount_refunded, payment.amount) - already_accounted_refund_amount. timeRefunded should only be used as a full-refund marker once the payment is fully refunded.
Reference docs:
- https://docs.stripe.com/api/events/types (
charge.refundedincludes partial refunds) - https://docs.stripe.com/api/charges/object (
amount_refundedon Charge) - https://docs.stripe.com/api/refunds/create (partial refunds can happen multiple times)
Merge review result: still not merging.
Validation evidence from a fresh isolated worktree on current origin/dev (61a7f6db3):
- Synthetic merge of
b95318f36aafa092413296cbed8fc80745550b65applied cleanly. bun install --frozen-lockfilepassed.bun run --cwd packages/console/app typecheckpassed.bun run --cwd packages/console/core typecheckpassed.bun run lint -- packages/console/app/src/routes/stripe/webhook.tsexited 0 with existing warnings only.- Existing console app tests
providerUsage.test.tsandrateLimiter.test.tspassed 6/6.
Merge blockers remain:
- The PR is still a draft and has no reviews.
- The branch is now 14 commits behind current
dev. - More importantly, the accounting behavior is still unsafe for multiple partial refunds.
charge.amount_refundedis cumulative on the Stripe Charge object, but this patch setsPaymentTable.timeRefundedon the first refund and then requirestime_refunded is null/rowsAffected > 0before deducting balance. A later partial refund for the same payment will deduct nothing. - A partial refund is also represented as a full refund in the current schema/UI because
timeRefundeddrives the refunded display state.
Needed before merge: store or ledger the already-accounted refunded amount/refund IDs, deduct only the new refund delta up to payment.amount, avoid treating partial refunds as full refunds, and add targeted tests for duplicate events, first partial refund, sequential partial refunds, and full refund idempotency.
Merge review refresh for task 95480452: still not merging.
Current state checked on 2026-06-28:
- PR head is
b95318f36aafa092413296cbed8fc80745550b65; currentdevisdfeb1b5051a05b359bd4af711b204d2c0342c5f4. - Branch is 1 commit ahead and 15 commits behind current
dev. - GitHub reports mergeable/clean and all 10 checks pass.
- Local isolated merge into current
devapplied cleanly. - Focused validation passed:
bun install --frozen-lockfile,bun run --cwd packages/console/app typecheck, andbun run lint -- packages/console/app/src/routes/stripe/webhook.tsexited 0 with existing warnings only.
Merge blockers remain:
- The PR is still a draft and has no reviews.
- The refund accounting is still unsafe for multiple partial refunds. In
packages/console/app/src/routes/stripe/webhook.ts, the patch deductscharge.amount_refundedonly whenPaymentTable.timeRefundedis null. Since Stripe Chargeamount_refundedis cumulative, a second partial refund for the same payment getsrowsAffected === 0and deducts no new refund delta. - The schema/UI still only use
timeRefundedas a boolean refunded marker, so partial refunds are represented like full refunds.
Needed before merge: persist the already-accounted refunded amount or process refund IDs idempotently, deduct only the new delta up to the original payment amount, represent partial vs full refunds accurately, and add targeted tests for duplicate events, first partial refund, sequential partial refunds, and full refund idempotency. Existing follow-up task: 363923ed.
Merge review refresh for task 99f73a07: not merging.
Current state checked on 2026-06-28 at 13:52 +03:00:
- PR head is
b95318f36aafa092413296cbed8fc80745550b65; currentorigin/devis6ee817d0416cc902bdf1e05b0a3db1e94d07434a. - Branch is 1 commit ahead and 16 commits behind current
dev. - GitHub REST reports
mergeable: true/mergeable_state: clean, and all 10 visible checks are green. - Local isolated synthetic merge into current
origin/devapplied cleanly. - Focused validation passed:
bun install --frozen-lockfile,bun run --cwd packages/console/app typecheck, andbun run lint -- packages/console/app/src/routes/stripe/webhook.tsexited 0 with existing warnings only. - A fresh adversarial verifier independently recommended no-merge.
Merge blockers remain:
- The PR is still a draft and has no reviews.
- Maintainer edits are disabled on the PR.
- The accounting behavior is still unsafe for multiple partial refunds. In
packages/console/app/src/routes/stripe/webhook.ts, the patch deductscharge.amount_refundedonly whenPaymentTable.timeRefundedis null. Since Stripe Chargeamount_refundedis cumulative, a second partial refund for the same payment getsrowsAffected === 0and deducts no new refund delta. Example: a $100 top-up with $30 refunded first and $20 refunded later deducts $30, then ignores the later $20. - The current schema/UI still only model
timeRefundedas a refunded marker, so a partial refund is represented like a full refund.
Needed before merge: persist the already-accounted refunded amount or process refund IDs idempotently, deduct only the new delta up to the original payment amount, avoid treating partial refunds as full refunds, and add targeted tests for duplicate events, first partial refund, sequential partial refunds, and full-after-partial refund capping.
Merge review refresh for task ce74ef35: not merging.
Current state checked on 2026-07-05 15:49 EEST:
- PR head is
b95318f36aafa092413296cbed8fc80745550b65; currentorigin/devisb7e4f1ef7433f83ba009eefa2997aeb81017f6ed. - Branch is 1 commit ahead and 250 commits behind current
dev. - GitHub reports
mergeable: true/mergeable_state: clean, but the PR is still a draft, has no reviews, and maintainer edits are disabled. - Visible GitHub checks are green but stale; all 10 completed on 2026-06-27.
- Local synthetic merge into refreshed
origin/devapplied cleanly. - Focused validation on the synthetic merge passed:
BUN_INSTALL_CACHE_DIR=/tmp/bun-cache TMPDIR=/tmp bun run --cwd packages/console/app typecheck. - File-level lint passed with existing warnings only:
BUN_INSTALL_CACHE_DIR=/tmp/bun-cache TMPDIR=/tmp bun run lint -- packages/console/app/src/routes/stripe/webhook.ts. - Independent adversarial review also recommended no-merge.
Merge blockers remain:
- The accounting behavior is still unsafe for multiple partial refunds. The patch deducts
min(charge.amount_refunded, payment.amount)only whenPaymentTable.timeRefunded IS NULL, then markstimeRefunded. Stripe Chargeamount_refundedis cumulative, so a second partial refund for the same payment produces no new balance deduction because the row is already marked refunded. Example: a $100 top-up with a $30 refund followed by another $20 refund deducts $30, then ignores the extra $20. - The schema/UI still model refund state with
timeRefundedonly, which behaves like a full-refund marker and cannot represent the already-accounted partial refund amount.
Needed before merge: persist accounted refunded amount or refund IDs for idempotency, deduct only the new refund delta capped by the original payment amount, avoid treating partial refunds as full refunds, add targeted tests for duplicate events, first partial refund, sequential partial refunds, and full-after-partial refund capping, then mark ready for review and get review approval.