Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,62 @@ This is a template project with best-practice modules:
- Winterspec for defining the API
- bun testing
- Zustand store with zod definition for database state

## Fake payment API

This project exposes a small fake payment API for exercising bounty and payout
flows without moving real funds.

### Send a payment

```http
POST /payments/send
Content-Type: application/json
```

```json
{
"recipient": "octocat",
"amount": 10,
"currency": "USD",
"bounty_id": "bounty_1",
"issue_number": 1,
"repository": "tscircuit/fake-algora",
"idempotency_key": "optional-retry-key"
}
```

The response includes a `payment` object with `status: "pending"`. If an
`idempotency_key` is provided, repeat sends with the same key return the
existing payment instead of creating a duplicate.

### List payments

```http
GET /payments/list
GET /payments/list?recipient=octocat&status=pending
GET /payments/list?repository=tscircuit/fake-algora&status=completed
```

Supported filters are `recipient`, `repository`, and `status`.

### Get a payment

```http
GET /payments/get?payment_id=payment_0
```

### Complete, cancel, or fail a payment

```http
POST /payments/complete
POST /payments/cancel
POST /payments/fail
Content-Type: application/json
```

```json
{
"payment_id": "payment_0"
}
```
89 changes: 87 additions & 2 deletions lib/db/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { createStore, type StoreApi } from "zustand/vanilla"
import { immer } from "zustand/middleware/immer"
import { hoist, type HoistedStoreApi } from "zustand-hoist"

import { databaseSchema, type DatabaseSchema, type Thing } from "./schema.ts"
import {
databaseSchema,
type DatabaseSchema,
type Payment,
type PaymentStatus,
type Thing,
} from "./schema.ts"
import { combine } from "zustand/middleware"

export const createDatabase = () => {
Expand All @@ -11,7 +17,12 @@ export const createDatabase = () => {

export type DbClient = ReturnType<typeof createDatabase>

const initializer = combine(databaseSchema.parse({}), (set) => ({
type CreatePaymentInput = Omit<
Payment,
"payment_id" | "status" | "created_at" | "updated_at"
>

const initializer = combine(databaseSchema.parse({}), (set, get) => ({
addThing: (thing: Omit<Thing, "thing_id">) => {
set((state) => ({
things: [
Expand All @@ -21,4 +32,78 @@ const initializer = combine(databaseSchema.parse({}), (set) => ({
idCounter: state.idCounter + 1,
}))
},
sendPayment: (paymentInput: CreatePaymentInput) => {
const now = new Date().toISOString()
const existingPayment =
paymentInput.idempotency_key !== undefined
? get().payments.find(
(payment) =>
payment.idempotency_key === paymentInput.idempotency_key,
)
: undefined

if (existingPayment) {
return existingPayment
}

const payment: Payment = {
...paymentInput,
payment_id: `payment_${get().paymentIdCounter}`,
status: "pending",
created_at: now,
updated_at: now,
}

set((state) => ({
payments: [...state.payments, payment],
paymentIdCounter: state.paymentIdCounter + 1,
}))

return payment
},
listPayments: (filters?: {
recipient?: string
repository?: string
status?: PaymentStatus
}) => {
return get().payments.filter((payment) => {
if (filters?.recipient && payment.recipient !== filters.recipient) {
return false
}
if (filters?.repository && payment.repository !== filters.repository) {
return false
}
if (filters?.status && payment.status !== filters.status) {
return false
}
return true
})
},
getPayment: (payment_id: string) => {
return get().payments.find((payment) => payment.payment_id === payment_id)
},
updatePaymentStatus: ({
payment_id,
status,
}: {
payment_id: string
status: PaymentStatus
}) => {
let updatedPayment: Payment | undefined
const now = new Date().toISOString()

set((state) => ({
payments: state.payments.map((payment) => {
if (payment.payment_id !== payment_id) return payment
updatedPayment = {
...payment,
status,
updated_at: now,
}
return updatedPayment
}),
}))

return updatedPayment
},
}))
25 changes: 25 additions & 0 deletions lib/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,33 @@ export const thingSchema = z.object({
})
export type Thing = z.infer<typeof thingSchema>

export const paymentStatusSchema = z.enum([
"pending",
"completed",
"failed",
"cancelled",
])
export type PaymentStatus = z.infer<typeof paymentStatusSchema>

export const paymentSchema = z.object({
payment_id: z.string(),
recipient: z.string(),
amount: z.number(),
currency: z.string(),
status: paymentStatusSchema,
bounty_id: z.string().optional(),
issue_number: z.number().optional(),
repository: z.string().optional(),
idempotency_key: z.string().optional(),
created_at: z.string(),
updated_at: z.string(),
})
export type Payment = z.infer<typeof paymentSchema>

export const databaseSchema = z.object({
idCounter: z.number().default(0),
things: z.array(thingSchema).default([]),
paymentIdCounter: z.number().default(0),
payments: z.array(paymentSchema).default([]),
})
export type DatabaseSchema = z.infer<typeof databaseSchema>
23 changes: 23 additions & 0 deletions routes/payments/cancel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { paymentSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

const updatePaymentBodySchema = z.object({
payment_id: z.string().min(1),
})

export default withRouteSpec({
methods: ["POST"],
jsonBody: updatePaymentBodySchema,
jsonResponse: z.object({
payment: paymentSchema.nullable(),
}),
})(async (req, ctx) => {
const { payment_id } = updatePaymentBodySchema.parse(await req.json())
const payment = ctx.db.updatePaymentStatus({
payment_id,
status: "cancelled",
})

return ctx.json({ payment: payment ?? null })
})
23 changes: 23 additions & 0 deletions routes/payments/complete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { paymentSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

const updatePaymentBodySchema = z.object({
payment_id: z.string().min(1),
})

export default withRouteSpec({
methods: ["POST"],
jsonBody: updatePaymentBodySchema,
jsonResponse: z.object({
payment: paymentSchema.nullable(),
}),
})(async (req, ctx) => {
const { payment_id } = updatePaymentBodySchema.parse(await req.json())
const payment = ctx.db.updatePaymentStatus({
payment_id,
status: "completed",
})

return ctx.json({ payment: payment ?? null })
})
23 changes: 23 additions & 0 deletions routes/payments/fail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { paymentSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

const updatePaymentBodySchema = z.object({
payment_id: z.string().min(1),
})

export default withRouteSpec({
methods: ["POST"],
jsonBody: updatePaymentBodySchema,
jsonResponse: z.object({
payment: paymentSchema.nullable(),
}),
})(async (req, ctx) => {
const { payment_id } = updatePaymentBodySchema.parse(await req.json())
const payment = ctx.db.updatePaymentStatus({
payment_id,
status: "failed",
})

return ctx.json({ payment: payment ?? null })
})
15 changes: 15 additions & 0 deletions routes/payments/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { paymentSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["GET"],
jsonResponse: z.object({
payment: paymentSchema.nullable(),
}),
})((req, ctx) => {
const url = new URL(req.url)
const payment = ctx.db.getPayment(url.searchParams.get("payment_id") ?? "")

return ctx.json({ payment: payment ?? null })
})
24 changes: 24 additions & 0 deletions routes/payments/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { paymentSchema, paymentStatusSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["GET"],
jsonResponse: z.object({
payments: z.array(paymentSchema),
}),
})((req, ctx) => {
const url = new URL(req.url)
const statusParam = url.searchParams.get("status")
const status = statusParam
? paymentStatusSchema.parse(statusParam)
: undefined

const payments = ctx.db.listPayments({
recipient: url.searchParams.get("recipient") ?? undefined,
repository: url.searchParams.get("repository") ?? undefined,
status,
})

return ctx.json({ payments })
})
26 changes: 26 additions & 0 deletions routes/payments/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { paymentSchema } from "lib/db/schema"
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().min(1).default("USD"),
bounty_id: z.string().min(1).optional(),
issue_number: z.number().int().positive().optional(),
repository: z.string().min(1).optional(),
idempotency_key: z.string().min(1).optional(),
})

export default withRouteSpec({
methods: ["POST"],
jsonBody: sendPaymentBodySchema,
jsonResponse: z.object({
payment: paymentSchema,
}),
})(async (req, ctx) => {
const body = sendPaymentBodySchema.parse(await req.json())
const payment = ctx.db.sendPayment(body)

return ctx.json({ payment })
})
Loading