-
Notifications
You must be signed in to change notification settings - Fork 29
Add payment lifecycle API with idempotent send support #21
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
Open
JYZ-LESLIE
wants to merge
1
commit into
tscircuit:main
Choose a base branch
from
JYZ-LESLIE:codex/add-payment-lifecycle-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,46 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| const updatePaymentSchema = z.object({ | ||
| payment_id: z.string().min(1), | ||
| }) | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: updatePaymentSchema, | ||
| jsonResponse: z.object({ | ||
| ok: z.boolean(), | ||
| payment: z | ||
| .object({ | ||
| payment_id: z.string(), | ||
| recipient: z.string(), | ||
| amount: z.number(), | ||
| currency: z.string(), | ||
| status: z.enum(["pending", "completed", "canceled", "failed"]), | ||
| idempotency_key: z.string().optional(), | ||
| bounty_issue: z.string().optional(), | ||
| metadata: z.record(z.string(), z.string()), | ||
| created_at: z.string(), | ||
| updated_at: z.string(), | ||
| }) | ||
| .nullable(), | ||
| error: z.string().optional(), | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = updatePaymentSchema.parse(await req.json()) | ||
| const payment = ctx.db.findPaymentById(payment_id) | ||
| if (!payment) { | ||
| return ctx.json({ | ||
| ok: false, | ||
| payment: null, | ||
| error: "payment_not_found", | ||
| }) | ||
| } | ||
|
|
||
| ctx.db.updatePaymentStatus(payment_id, "canceled") | ||
|
|
||
| return ctx.json({ | ||
| ok: true, | ||
| payment: ctx.db.findPaymentById(payment_id)!, | ||
| }) | ||
| }) |
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,46 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| const updatePaymentSchema = z.object({ | ||
| payment_id: z.string().min(1), | ||
| }) | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: updatePaymentSchema, | ||
| jsonResponse: z.object({ | ||
| ok: z.boolean(), | ||
| payment: z | ||
| .object({ | ||
| payment_id: z.string(), | ||
| recipient: z.string(), | ||
| amount: z.number(), | ||
| currency: z.string(), | ||
| status: z.enum(["pending", "completed", "canceled", "failed"]), | ||
| idempotency_key: z.string().optional(), | ||
| bounty_issue: z.string().optional(), | ||
| metadata: z.record(z.string(), z.string()), | ||
| created_at: z.string(), | ||
| updated_at: z.string(), | ||
| }) | ||
| .nullable(), | ||
| error: z.string().optional(), | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = updatePaymentSchema.parse(await req.json()) | ||
| const payment = ctx.db.findPaymentById(payment_id) | ||
| if (!payment) { | ||
| return ctx.json({ | ||
| ok: false, | ||
| payment: null, | ||
| error: "payment_not_found", | ||
| }) | ||
| } | ||
|
|
||
| ctx.db.updatePaymentStatus(payment_id, "completed") | ||
|
|
||
| return ctx.json({ | ||
| ok: true, | ||
| payment: ctx.db.findPaymentById(payment_id)!, | ||
| }) | ||
| }) |
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,45 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| const getPaymentBodySchema = z.object({ | ||
| payment_id: z.string().min(1), | ||
| }) | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: getPaymentBodySchema, | ||
| jsonResponse: z.object({ | ||
| ok: z.boolean(), | ||
| payment: z | ||
| .object({ | ||
| payment_id: z.string(), | ||
| recipient: z.string(), | ||
| amount: z.number(), | ||
| currency: z.string(), | ||
| status: z.enum(["pending", "completed", "canceled", "failed"]), | ||
| idempotency_key: z.string().optional(), | ||
| bounty_issue: z.string().optional(), | ||
| metadata: z.record(z.string(), z.string()), | ||
| created_at: z.string(), | ||
| updated_at: z.string(), | ||
| }) | ||
| .nullable(), | ||
| error: z.string().optional(), | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = getPaymentBodySchema.parse(await req.json()) | ||
| const payment = ctx.db.findPaymentById(payment_id) | ||
|
|
||
| if (!payment) { | ||
| return ctx.json({ | ||
| ok: false, | ||
| payment: null, | ||
| error: "payment_not_found", | ||
| }) | ||
| } | ||
|
|
||
| return ctx.json({ | ||
| ok: true, | ||
| payment, | ||
| }) | ||
| }) |
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,26 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["GET"], | ||
| jsonResponse: z.object({ | ||
| payments: z.array( | ||
| z.object({ | ||
| payment_id: z.string(), | ||
| recipient: z.string(), | ||
| amount: z.number(), | ||
| currency: z.string(), | ||
| status: z.enum(["pending", "completed", "canceled", "failed"]), | ||
| idempotency_key: z.string().optional(), | ||
| bounty_issue: z.string().optional(), | ||
| metadata: z.record(z.string(), z.string()), | ||
| created_at: z.string(), | ||
| updated_at: z.string(), | ||
| }), | ||
| ), | ||
| }), | ||
| })(async (_req, ctx) => { | ||
| return ctx.json({ | ||
| payments: ctx.db.getState().payments, | ||
| }) | ||
| }) |
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,55 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| const sendPaymentBodySchema = z.object({ | ||
| recipient: z.string().min(1), | ||
| amount: z.number().positive(), | ||
| currency: z.string().default("USD"), | ||
| idempotency_key: z.string().optional(), | ||
| bounty_issue: z.string().optional(), | ||
| metadata: z.record(z.string(), z.string()).optional(), | ||
| }) | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: sendPaymentBodySchema, | ||
| jsonResponse: z.object({ | ||
| ok: z.boolean(), | ||
| payment: z.object({ | ||
| payment_id: z.string(), | ||
| recipient: z.string(), | ||
| amount: z.number(), | ||
| currency: z.string(), | ||
| status: z.enum(["pending", "completed", "canceled", "failed"]), | ||
| idempotency_key: z.string().optional(), | ||
| bounty_issue: z.string().optional(), | ||
| metadata: z.record(z.string(), z.string()), | ||
| created_at: z.string(), | ||
| updated_at: z.string(), | ||
| }), | ||
| idempotent_replay: z.boolean().optional(), | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const body = sendPaymentBodySchema.parse(await req.json()) | ||
|
|
||
| if (body.idempotency_key) { | ||
| const existing = ctx.db.findPaymentByIdempotencyKey(body.idempotency_key) | ||
| if (existing) { | ||
| return ctx.json({ | ||
| ok: true, | ||
| payment: existing, | ||
| idempotent_replay: true, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| ctx.db.addPayment(body) | ||
| const payment = body.idempotency_key | ||
| ? ctx.db.findPaymentByIdempotencyKey(body.idempotency_key) | ||
| : ctx.db.getState().payments.at(-1) | ||
|
|
||
| return ctx.json({ | ||
| ok: true, | ||
| payment: payment!, | ||
| }) | ||
| }) | ||
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,49 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { getTestServer } from "tests/fixtures/get-test-server" | ||
|
|
||
| test("payment lifecycle routes work and support idempotent send", async () => { | ||
| const { axios } = await getTestServer() | ||
|
|
||
| const sendBody = { | ||
| recipient: "dev@example.com", | ||
| amount: 30, | ||
| currency: "USD", | ||
| idempotency_key: "issue-1-send-001", | ||
| bounty_issue: "#1", | ||
| metadata: { | ||
| source: "algora", | ||
| }, | ||
| } | ||
|
|
||
| const firstSend = await axios.post("/payments/send", sendBody) | ||
| expect(firstSend.data.ok).toBe(true) | ||
| expect(firstSend.data.payment.status).toBe("pending") | ||
|
|
||
| const replaySend = await axios.post("/payments/send", sendBody) | ||
| expect(replaySend.data.ok).toBe(true) | ||
| expect(replaySend.data.idempotent_replay).toBe(true) | ||
| expect(replaySend.data.payment.payment_id).toBe( | ||
| firstSend.data.payment.payment_id, | ||
| ) | ||
|
|
||
| const paymentId = firstSend.data.payment.payment_id | ||
|
|
||
| const listRes = await axios.get("/payments/list") | ||
| expect(listRes.data.payments).toHaveLength(1) | ||
|
|
||
| const getRes = await axios.post("/payments/get", { payment_id: paymentId }) | ||
| expect(getRes.data.ok).toBe(true) | ||
| expect(getRes.data.payment.payment_id).toBe(paymentId) | ||
|
|
||
| const completeRes = await axios.post("/payments/complete", { | ||
| payment_id: paymentId, | ||
| }) | ||
| expect(completeRes.data.ok).toBe(true) | ||
| expect(completeRes.data.payment.status).toBe("completed") | ||
|
|
||
| const cancelRes = await axios.post("/payments/cancel", { | ||
| payment_id: paymentId, | ||
| }) | ||
| expect(cancelRes.data.ok).toBe(true) | ||
| expect(cancelRes.data.payment.status).toBe("canceled") | ||
| }) |
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.
The replay guard uses a truthiness check (
if (body.idempotency_key)), but the request schema allowsidempotency_key: ""because it isz.string().optional(). In that case, the deduplication branch is skipped and repeatedPOST /payments/sendcalls with the same empty key create duplicate payments, which breaks the endpoint’s advertised idempotent retry behavior for this input.Useful? React with 👍 / 👎.