Skip to content

Post placeholder payment and refund atomically - #1822

Merged
stefan-burke merged 2 commits into
mainfrom
split/placeholder-refund-ledger
Jul 14, 2026
Merged

Post placeholder payment and refund atomically#1822
stefan-burke merged 2 commits into
mainfrom
split/placeholder-refund-ledger

Conversation

@stefan-burke

@stefan-burke stefan-burke commented Jul 14, 2026

Copy link
Copy Markdown
Member

A stored-but-refunded placeholder booking records the cash we received and the cash we returned. Before this change, the payment and refund were posted as two separate writes: if the refund leg's reference collided, the payment had already committed and the ledger read as money we still held, even though the provider had already refunded it. The two halves could disagree.

This posts both event groups as one atomic batch through postTransferGroups, so the payment can never land without its completed refund. The refund mapper maps a reversal of the freshly-built payment legs (via asOrderLegs) rather than reading them back from storage, so both groups are computed before either is stored and the batch commits both or neither.

Scope stays within the placeholder refund path. The attendee refund path (recordAttendeeRefund) is untouched. Nothing from the staged-checkout work is brought in: no checkout stages, no held-cash admin guards, no staged activation, no refunding transitions. The shared ledger matching helpers on main (legMatches, postTransferGroups, asOrderLegs) are reused, not paralleled.

Acceptance:

  • A refund-reference collision rolls back the payment too — neither transfer group commits.
  • Payment-only recording (when no provider refund completed) still posts and reports posted: true.
  • Existing recordAttendeeRefund behavior is unchanged; its tests are untouched except for splitting the placeholder suite into its own file for faster, narrower coverage.

The placeholder refund tests now cover: the clean cash round-trip (nets to zero, no sale leg), payment-only when the refund did not complete, refund-reference collision rollback, payment-reference collision logging, and the payment-only conflict path.

Summary by CodeRabbit

  • Bug Fixes
    • Placeholder refunds now post payment and refund ledger entries atomically.
    • Failed or conflicting ledger postings are reported as unsuccessful without creating partial transfers.
    • Refund-only entries correctly net the attendee balance and preserve the provided refund reason.
    • Payment-only placeholder records now maintain the expected attendee balance.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@stefan-burke, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 18 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8d690db7-69a2-45a7-a83d-7e9c9293d4a5

📥 Commits

Reviewing files that changed from the base of the PR and between 39ee1e9 and 1a12dc9.

📒 Files selected for processing (2)
  • TODO.md
  • test/shared/refund-ledger-placeholder.test.ts
📝 Walkthrough

Walkthrough

recordPlaceholderRefund now precomputes payment and refund legs and posts them atomically. Dedicated tests cover completed and incomplete refunds, ledger netting, memo propagation, and posting conflicts.

Changes

Placeholder refund ledger flow

Layer / File(s) Summary
Atomic payment and refund posting
src/shared/refund-ledger.ts
recordPlaceholderRefund uses mapBooking, mapRefund, and postTransferGroups to post required legs atomically, with updated documentation and imports.
Placeholder refund behavior tests
test/shared/refund-ledger-placeholder.test.ts, test/shared/refund-ledger.test.ts
A dedicated suite validates payment-only, payment-plus-refund, netting, memo, and conflict behavior; the former placeholder-refund tests are removed from the general suite.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: atomically posting the placeholder payment and refund together.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch split/placeholder-refund-ledger

Comment @coderabbitai help to get the list of available commands.

@stefan-burke
stefan-burke enabled auto-merge July 14, 2026 18:25
@stefan-burke
stefan-burke added this pull request to the merge queue Jul 14, 2026
@stefan-burke
stefan-burke removed this pull request from the merge queue due to a manual request Jul 14, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/shared/refund-ledger-placeholder.test.ts`:
- Around line 52-66: Replace the presence-only leg-kind checks in the “records
the cash round-trip with no sale leg, netting to zero” test with an exact
assertion matching the expected set of leg kinds, consistent with the sibling
payment-only test. Preserve the existing amount, memo, and balance assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 678338d9-2564-4994-89ac-ccc9abdacdb6

📥 Commits

Reviewing files that changed from the base of the PR and between 32a47a0 and 39ee1e9.

📒 Files selected for processing (3)
  • src/shared/refund-ledger.ts
  • test/shared/refund-ledger-placeholder.test.ts
  • test/shared/refund-ledger.test.ts

Comment thread test/shared/refund-ledger-placeholder.test.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 39ee1e9867

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

}),
]
: [payment];
await postTransferGroups(groups);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve a replay marker when the atomic batch fails

When refunded is true and this atomic post rejects the refund group, postWithoutThrowing returns posted: false but the batch leaves no booking event leg at all. The payment flow uses the ledger as the durable replay guard (replaySessionFromLedger treats bookingLedgerDisposition(...).status === "unrecorded" as fresh), and prunePayments later deletes terminal failure_data rows, so a later webhook/redirect for the same already-refunded session can re-enter processing and create another placeholder/refund attempt instead of being acknowledged as handled. Keep some durable handled marker or surface the failure before acknowledging the terminal payment outcome.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Valid concern — recorded in TODO.md (commit 1a12dc9). The atomic rollback is this PR's core requirement: the split plan says "a conflict in the refund leg must roll back the payment leg as well", and the acceptance criterion is "a refund-reference collision proves neither transfer group is committed." Fixing the replay-marker gap without breaking that requires a durable handled marker outside the prunable processed_payments table — the staged-checkout runtime (deferred foundations item 6 in PR_SPLIT_PLAN.md) carries that machinery.

The gap also partially pre-exists on main: before this PR, if the payment post itself failed (reference conflict), no legs landed either, and the same pruning → re-entry path applied. This PR widens the failure surface from "payment-post failure only" to "payment-post or refund-post failure" (because both are now one atomic batch), which is the designed trade-off.

TODO.md has the full context: the preflight (replaySessionFromLedger in src/features/api/payment-processing/index.ts), the pruner (prunePayments in src/shared/db/prune.ts), and the classification (src/shared/session-ledger.ts) are the starting points.

…w-up

Replace presence-only .some() leg-kind checks with an exact sorted kind-set
assertion, matching the sibling payment-only test and resisting a mutant
that adds an unexpected leg. Record the Codex replay-marker concern (no
durable ledger legs when the atomic batch fails + idempotency row pruned)
in TODO.md for the staged-checkout runtime, since fixing it without
breaking the atomic rollback is deferred to that work.
@stefan-burke
stefan-burke added this pull request to the merge queue Jul 14, 2026
Merged via the queue into main with commit 9e6de21 Jul 14, 2026
3 checks passed
@stefan-burke
stefan-burke deleted the split/placeholder-refund-ledger branch July 14, 2026 20:17
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.

1 participant