-
Notifications
You must be signed in to change notification settings - Fork 50
Add fake payments API #43
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
firewine
wants to merge
2
commits into
tscircuit:main
Choose a base branch
from
firewine:bounty-1-fake-payments-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 1 commit
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
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,61 @@ | ||
| import { paymentSchema, paymentStatusSchema } from "lib/db/schema" | ||
| import { z } from "zod" | ||
|
|
||
| export const sendPaymentRequestSchema = z | ||
| .object({ | ||
| recipient_email: z.string().email(), | ||
| amount_cents: z.number().int().positive().optional(), | ||
| amount_usd: z.number().positive().optional(), | ||
| currency: z.string().min(1).default("usd"), | ||
| bounty_issue_url: z.string().url().optional(), | ||
| note: z.string().optional(), | ||
| idempotency_key: z.string().min(1).optional(), | ||
| }) | ||
| .refine( | ||
| (body) => body.amount_cents !== undefined || body.amount_usd !== undefined, | ||
| "amount_cents or amount_usd is required", | ||
| ) | ||
|
|
||
| export const paymentResponseSchema = z.object({ | ||
| payment: paymentSchema, | ||
| }) | ||
|
|
||
| export const errorResponseSchema = z.object({ | ||
| error: z.unknown(), | ||
| }) | ||
|
|
||
| export const paymentRouteResponseSchema = z.union([ | ||
| paymentResponseSchema, | ||
| errorResponseSchema, | ||
| ]) | ||
|
|
||
| export const paymentListResponseSchema = z.object({ | ||
| payments: z.array(paymentSchema), | ||
| }) | ||
|
|
||
| export const getPaymentRequestSchema = z.object({ | ||
| payment_id: z.string().min(1), | ||
| }) | ||
|
|
||
| export const updatePaymentStatusRequestSchema = z.object({ | ||
| payment_id: z.string().min(1), | ||
| }) | ||
|
|
||
| export const updatePaymentStatusResponseSchema = z.object({ | ||
| payment: paymentSchema, | ||
| }) | ||
|
|
||
| export const updatePaymentStatusRouteResponseSchema = z.union([ | ||
| updatePaymentStatusResponseSchema, | ||
| errorResponseSchema, | ||
| ]) | ||
|
|
||
| export type SendPaymentRequest = z.infer<typeof sendPaymentRequestSchema> | ||
| export type PaymentStatus = z.infer<typeof paymentStatusSchema> | ||
|
|
||
| export function toAmountCents(body: SendPaymentRequest): number { | ||
| if (body.amount_cents !== undefined) return body.amount_cents | ||
| return Math.round(body.amount_usd! * 100) | ||
| } | ||
|
|
||
| export { paymentStatusSchema } |
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,20 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { | ||
| updatePaymentStatusRequestSchema, | ||
| updatePaymentStatusRouteResponseSchema, | ||
| } from "lib/payments/schemas" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: updatePaymentStatusRequestSchema, | ||
| jsonResponse: updatePaymentStatusRouteResponseSchema, | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = await req.json() | ||
| const payment = ctx.db.updatePaymentStatus(payment_id, "canceled") | ||
|
|
||
| if (!payment) { | ||
| return ctx.json({ error: "Payment not found" }, { status: 404 }) | ||
| } | ||
|
|
||
| return ctx.json({ 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,20 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { | ||
| updatePaymentStatusRequestSchema, | ||
| updatePaymentStatusRouteResponseSchema, | ||
| } from "lib/payments/schemas" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: updatePaymentStatusRequestSchema, | ||
| jsonResponse: updatePaymentStatusRouteResponseSchema, | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = await req.json() | ||
| const payment = ctx.db.updatePaymentStatus(payment_id, "completed") | ||
|
|
||
| if (!payment) { | ||
| return ctx.json({ error: "Payment not found" }, { status: 404 }) | ||
| } | ||
|
|
||
| return ctx.json({ 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,20 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { | ||
| updatePaymentStatusRequestSchema, | ||
| updatePaymentStatusRouteResponseSchema, | ||
| } from "lib/payments/schemas" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: updatePaymentStatusRequestSchema, | ||
| jsonResponse: updatePaymentStatusRouteResponseSchema, | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = await req.json() | ||
| const payment = ctx.db.updatePaymentStatus(payment_id, "failed") | ||
|
|
||
| if (!payment) { | ||
| return ctx.json({ error: "Payment not found" }, { status: 404 }) | ||
| } | ||
|
|
||
| return ctx.json({ 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,16 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { paymentRouteResponseSchema } from "lib/payments/schemas" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["GET"], | ||
| jsonResponse: paymentRouteResponseSchema, | ||
| })((req, ctx) => { | ||
| const url = new URL(req.url) | ||
| const payment = ctx.db.getPayment(url.searchParams.get("payment_id") ?? "") | ||
|
|
||
| if (!payment) { | ||
| return ctx.json({ error: "Payment not found" }, { status: 404 }) | ||
| } | ||
|
|
||
| return ctx.json({ 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,23 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { | ||
| paymentListResponseSchema, | ||
| paymentStatusSchema, | ||
| } from "lib/payments/schemas" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["GET"], | ||
| jsonResponse: paymentListResponseSchema, | ||
| })((req, ctx) => { | ||
| const url = new URL(req.url) | ||
| const status = paymentStatusSchema | ||
| .nullable() | ||
| .catch(null) | ||
| .parse(url.searchParams.get("status")) | ||
|
|
||
| const payments = ctx.db.listPayments({ | ||
| recipient_email: url.searchParams.get("recipient_email"), | ||
| status, | ||
| }) | ||
|
|
||
| return ctx.json({ 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,35 @@ | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { | ||
| paymentRouteResponseSchema, | ||
| sendPaymentRequestSchema, | ||
| toAmountCents, | ||
| } from "lib/payments/schemas" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: sendPaymentRequestSchema, | ||
| jsonResponse: paymentRouteResponseSchema, | ||
| })(async (req, ctx) => { | ||
| const body = await req.json() | ||
| const parsed = sendPaymentRequestSchema.safeParse(body) | ||
|
|
||
| if (!parsed.success) { | ||
| return ctx.json( | ||
| { error: parsed.error.flatten() }, | ||
| { | ||
| status: 400, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| const payment = ctx.db.sendPayment({ | ||
| recipient_email: parsed.data.recipient_email, | ||
| amount_cents: toAmountCents(parsed.data), | ||
| currency: parsed.data.currency, | ||
| bounty_issue_url: parsed.data.bounty_issue_url, | ||
| note: parsed.data.note, | ||
| idempotency_key: parsed.data.idempotency_key, | ||
| }) | ||
|
|
||
| return ctx.json({ payment }) | ||
| }) |
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.
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.
Using
paymentStatusSchema.nullable().catch(null)turns any invalidstatusquery value intonull, so requests likeGET /payments/list?status=completesilently drop the filter and return all payments instead of surfacing input error. This can mislead callers and break workflows that depend on strict filtering; invalid enum values should be rejected (e.g., 400) rather than treated as “no filter.”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.
Fixed in e5a39dc by validating
statuswithsafeParseand returning 400 for unknown values. I also added a regression test for/payments/list?status=complete.