feat(betterzeriya): add Universal Commerce Protocol (UCP) checkout endpoints#21
Open
feat(betterzeriya): add Universal Commerce Protocol (UCP) checkout endpoints#21
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a basic Universal Commerce Protocol (UCP) discovery document and REST checkout-session endpoints to betterzeriya, backed by an in-memory session store for development/integration flows.
Changes:
- Introduces an in-memory UCP checkout session store with create/get/complete helpers.
- Adds UCP discovery at
/.well-known/ucpadvertising a REST binding under/api/ucp. - Implements
POST /api/ucp/checkout-sessionsandGET/POST /api/ucp/checkout-sessions/:idendpoints.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| bun.lock | Lockfile update from environment runs. |
| apps/betterzeriya/src/lib/server/ucp.ts | Adds in-memory UCP checkout session storage and helpers. |
| apps/betterzeriya/src/routes/.well-known/ucp/+server.ts | Adds UCP capability discovery endpoint. |
| apps/betterzeriya/src/routes/api/ucp/checkout-sessions/+server.ts | Adds endpoint to create UCP checkout sessions and return URLs. |
| apps/betterzeriya/src/routes/api/ucp/checkout-sessions/[id]/+server.ts | Adds endpoints to fetch and complete a checkout session by ID. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+17
to
+22
|
|
||
| return json( | ||
| { | ||
| checkout_session: session, | ||
| checkout_url: `${url.origin}/sessions/${session.id}`, | ||
| status_url: `${url.origin}/api/ucp/checkout-sessions/${session.id}`, |
Comment on lines
+4
to
+15
| export const POST: RequestHandler = async ({ request, url }) => { | ||
| const body = await request.json().catch(() => null) | ||
| const lineItems = body?.line_items | ||
|
|
||
| if (!Array.isArray(lineItems) || lineItems.length === 0) { | ||
| return json({ error: 'line_items is required' }, { status: 400 }) | ||
| } | ||
|
|
||
| const session = createUcpCheckoutSession({ | ||
| line_items: lineItems, | ||
| buyer: body?.buyer, | ||
| currency: body?.currency, |
Comment on lines
+30
to
+57
| const sessions = new Map<string, UcpCheckoutSession>() | ||
|
|
||
| export const createUcpCheckoutSession = (input: CreateCheckoutSessionInput) => { | ||
| const now = new Date().toISOString() | ||
| const session: UcpCheckoutSession = { | ||
| id: crypto.randomUUID(), | ||
| status: 'requires_action', | ||
| currency: input.currency ?? 'JPY', | ||
| buyer: input.buyer, | ||
| line_items: input.line_items, | ||
| created_at: now, | ||
| updated_at: now, | ||
| } | ||
| sessions.set(session.id, session) | ||
| return session | ||
| } | ||
|
|
||
| export const getUcpCheckoutSession = (id: string) => sessions.get(id) | ||
|
|
||
| export const completeUcpCheckoutSession = (id: string) => { | ||
| const session = sessions.get(id) | ||
| if (!session) return undefined | ||
| const updated = { | ||
| ...session, | ||
| status: 'completed' as const, | ||
| updated_at: new Date().toISOString(), | ||
| } | ||
| sessions.set(id, updated) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
Description
apps/betterzeriya/src/lib/server/ucp.tswithcreateUcpCheckoutSession,getUcpCheckoutSession, andcompleteUcpCheckoutSession./.well-known/ucp(apps/betterzeriya/src/routes/.well-known/ucp/+server.ts) that advertises thedev.ucp.shopping.checkoutcapability and a REST binding withbase_url: /api/ucp.POST /api/ucp/checkout-sessions(apps/betterzeriya/src/routes/api/ucp/checkout-sessions/+server.ts) to validateline_items, create a UCP checkout session, and returncheckout_urlandstatus_url.GETandPOSTon/api/ucp/checkout-sessions/:id(apps/betterzeriya/src/routes/api/ucp/checkout-sessions/[id]/+server.ts) to fetch session status and to mark a session completed, returning 404 for unknown IDs.bun.lock) as part of environment runs.Testing
bun run test, which succeeded: 7 test files, 24 tests passed.bun check:fix, which failed in this environment due to a TypeScript/Vite config runtime mismatch when loadingvite.config.ts, causing formatting to be skipped.Codex Task