-
-
Notifications
You must be signed in to change notification settings - Fork 3
Post placeholder payment and refund atomically #1822
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,110 @@ | ||
| import { expect } from "@std/expect"; | ||
| import { it as test } from "@std/testing/bdd"; | ||
| import { attendeeAccount, WORLD } from "#shared/accounting/accounts.ts"; | ||
| import { bookingEventGroup } from "#shared/accounting/mappers.ts"; | ||
| import { transfersByAccount } from "#shared/accounting/queries.ts"; | ||
| import { legReference } from "#shared/accounting/refs.ts"; | ||
| import { postTransfers } from "#shared/accounting/store.ts"; | ||
| import { balanceOf } from "#shared/ledger/project.ts"; | ||
| import { recordPlaceholderRefund } from "#shared/refund-ledger.ts"; | ||
| import { describeWithEnv } from "#test-utils/db.ts"; | ||
| import { setupErrorSpy } from "#test-utils/error-spy.ts"; | ||
| import { BOOKING_AT } from "./refund-ledger/helpers.ts"; | ||
|
|
||
| const PLACEHOLDER = { | ||
| amount: 5000, | ||
| attendeeId: 7, | ||
| eventId: "ph-sess-1", | ||
| listingId: 1, | ||
| occurredAt: BOOKING_AT, | ||
| }; | ||
|
|
||
| const paymentReference = (): Promise<string> => | ||
| legReference(["booking", PLACEHOLDER.eventId, "payment"]); | ||
|
|
||
| const refundReference = async (): Promise<string> => | ||
| legReference([ | ||
| "refund", | ||
| await bookingEventGroup(PLACEHOLDER.eventId), | ||
| await paymentReference(), | ||
| ]); | ||
|
|
||
| describeWithEnv("refund-ledger > recordPlaceholderRefund", { db: true }, () => { | ||
| const errors = setupErrorSpy(); | ||
|
|
||
| const blockLeg = async ( | ||
| eventGroup: string, | ||
| reference: string, | ||
| ): Promise<void> => { | ||
| await postTransfers([ | ||
| { | ||
| amount: 100, | ||
| destination: attendeeAccount(99), | ||
| eventGroup, | ||
| kind: "payment", | ||
| occurredAt: BOOKING_AT, | ||
| reference, | ||
| source: WORLD, | ||
| }, | ||
| ]); | ||
| }; | ||
|
|
||
| test("records the cash round-trip with no sale leg, netting to zero", async () => { | ||
| expect( | ||
| await recordPlaceholderRefund(PLACEHOLDER, "price_changed", true), | ||
| ).toEqual({ posted: true }); | ||
| const legs = await transfersByAccount( | ||
| attendeeAccount(PLACEHOLDER.attendeeId), | ||
| ); | ||
| const cash = legs.filter((leg) => leg.kind === "refund_cash"); | ||
| expect(legs.map((leg) => leg.kind).sort()).toEqual([ | ||
| "payment", | ||
| "refund_cash", | ||
| ]); | ||
| expect(cash.length).toBe(1); | ||
| expect(cash[0]!.amount).toBe(PLACEHOLDER.amount); | ||
| expect(cash[0]!.memo).toBe("price_changed"); | ||
| expect(balanceOf(attendeeAccount(PLACEHOLDER.attendeeId))(legs)).toBe(0); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test("posts only the payment when no refund completed", async () => { | ||
| expect( | ||
| await recordPlaceholderRefund(PLACEHOLDER, "charge_mismatch", false), | ||
| ).toEqual({ posted: true }); | ||
| const legs = await transfersByAccount( | ||
| attendeeAccount(PLACEHOLDER.attendeeId), | ||
| ); | ||
| expect(legs.map((leg) => leg.kind)).toEqual(["payment"]); | ||
| expect(balanceOf(attendeeAccount(PLACEHOLDER.attendeeId))(legs)).toBe( | ||
| PLACEHOLDER.amount, | ||
| ); | ||
| }); | ||
|
|
||
| test("rolls back the payment when the refund reference conflicts", async () => { | ||
| await blockLeg("refund-blocker", await refundReference()); | ||
|
|
||
| expect( | ||
| await recordPlaceholderRefund(PLACEHOLDER, "sold_out", true), | ||
| ).toEqual({ posted: false }); | ||
| expect( | ||
| await transfersByAccount(attendeeAccount(PLACEHOLDER.attendeeId)), | ||
| ).toEqual([]); | ||
| expect(errors.lastMessage()).toContain("E_LEDGER_POST"); | ||
| }); | ||
|
|
||
| test("logs and does not throw when the payment reference conflicts", async () => { | ||
| await blockLeg("payment-blocker", await paymentReference()); | ||
| expect( | ||
| await recordPlaceholderRefund(PLACEHOLDER, "sold_out", true), | ||
| ).toEqual({ posted: false }); | ||
| expect(errors.lastMessage()).toContain("E_LEDGER_POST"); | ||
| }); | ||
|
|
||
| test("reports a payment-only conflict as not posted", async () => { | ||
| await blockLeg("payment-only-blocker", await paymentReference()); | ||
| expect( | ||
| await recordPlaceholderRefund(PLACEHOLDER, "charge_mismatch", false), | ||
| ).toEqual({ posted: false }); | ||
| expect(errors.lastMessage()).toContain("E_LEDGER_POST"); | ||
| }); | ||
| }); | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
refundedis true and this atomic post rejects the refund group,postWithoutThrowingreturnsposted: falsebut the batch leaves no booking event leg at all. The payment flow uses the ledger as the durable replay guard (replaySessionFromLedgertreatsbookingLedgerDisposition(...).status === "unrecorded"as fresh), andprunePaymentslater deletes terminalfailure_datarows, 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 👍 / 👎.
There was a problem hiding this comment.
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_paymentstable — 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 (
replaySessionFromLedgerinsrc/features/api/payment-processing/index.ts), the pruner (prunePaymentsinsrc/shared/db/prune.ts), and the classification (src/shared/session-ledger.ts) are the starting points.