Repository navigation
Support invoice orders without upfront payment #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,12 +6,13 @@ export interface CheckoutInput { | |
| cartId: string; | ||
| email: string; | ||
| shippingAddress: AddressRecord; | ||
| cardToken: string; | ||
| /** Omitted for invoice orders, which are created PENDING without a payment. */ | ||
| cardToken?: string | null; | ||
| } | ||
|
|
||
| export interface CheckoutResult { | ||
| order: OrderRecord; | ||
| payment: PaymentRecord; | ||
| payment: PaymentRecord | null; | ||
| } | ||
|
|
||
| /** Extracts the last four digits from a token such as `tok_visa_4242`. */ | ||
|
|
@@ -22,7 +23,8 @@ export function last4FromToken(token: string): string { | |
| } | ||
|
|
||
| /** | ||
| * Converts a cart into a paid order. Prices are snapshotted onto the order's | ||
| * Converts a cart into an order. With a card token the payment is captured | ||
| * immediately; without one the order is created as an unpaid invoice. Prices are snapshotted onto the order's | ||
| * line items so later catalog changes do not alter historical orders. | ||
| */ | ||
| export function checkout(store: Store, input: CheckoutInput, now = new Date()): CheckoutResult { | ||
|
|
@@ -37,37 +39,40 @@ export function checkout(store: Store, input: CheckoutInput, now = new Date()): | |
| return { id: store.nextId('li'), productId: product.id, quantity: item.quantity, unitPrice: product.price }; | ||
| }); | ||
|
|
||
| const last4 = last4FromToken(input.cardToken); | ||
| const last4 = input.cardToken ? last4FromToken(input.cardToken) : null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject an empty card token instead of placing an invoice order. If a caller supplies
|
||
| const { total } = computeTotals(items); | ||
| const orderId = store.nextId('ord'); | ||
| const paymentId = store.nextId('pay'); | ||
| const createdAt = now.toISOString(); | ||
|
|
||
| const order: OrderRecord = { | ||
| id: orderId, | ||
| number: store.nextOrderNumber(), | ||
| customerId: cart.customerId, | ||
| status: 'PAID', | ||
| status: last4 ? 'PAID' : 'PENDING', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| createdAt, | ||
| items, | ||
| shippingAddress: input.shippingAddress, | ||
| paymentId, | ||
| }; | ||
| const payment: PaymentRecord = { | ||
| id: paymentId, | ||
| orderId, | ||
| status: 'CAPTURED', | ||
| method: 'card', | ||
| last4, | ||
| amount: total, | ||
| capturedAt: createdAt, | ||
| }; | ||
|
|
||
| let payment: PaymentRecord | null = null; | ||
| if (last4) { | ||
| payment = { | ||
| id: store.nextId('pay'), | ||
| orderId, | ||
| status: 'CAPTURED', | ||
| method: 'card', | ||
| last4, | ||
| amount: total, | ||
| capturedAt: createdAt, | ||
| }; | ||
| order.paymentId = payment.id; | ||
| store.payments.set(payment.id, payment); | ||
| } | ||
|
|
||
| for (const item of items) { | ||
| store.products.get(item.productId)!.inventory -= item.quantity; | ||
| } | ||
| store.orders.set(order.id, order); | ||
| store.payments.set(payment.id, payment); | ||
| cart.items = []; | ||
|
|
||
| return { order, payment }; | ||
|
|
||
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.
Preserve the existing payment output guarantees until clients migrate.
Both
Payment!→Paymentchanges are breaking. The schema-diff CI job already rejects them.thicket-web’sOrderDetailQueryandCheckoutMutation, andthicket-mobile’sOrderCardFragmentandOrderReceiptQuery, still type payment as required. Their renderers dereference it without a null check, so an invoice order can crash those views. Update and ship both clients before exposing null payments. Use a new invoice-safe field if the existing non-null fields must remain available during migration. (raw.githubusercontent.com)As per coding guidelines, “Do not weaken non-null outputs.” As per path instructions, “changing
T!toTweakens a guarantee clients rely on.”Also applies to: 139-139
Sources: Coding guidelines, Path instructions, Linked repositories, Pipeline failures