diff --git a/CHANGELOG.md b/CHANGELOG.md index 2569d8a..6004790 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to the API contract are documented here. ## Unreleased +- Checkout accepts orders without a card token (invoice orders). `Order.payment` and + `CheckoutResult.payment` are now nullable. + ## 0.1.0 - Initial schema: products, carts, orders, payments, checkout, and full refunds. diff --git a/schema/schema.graphql b/schema/schema.graphql index d118aa5..1645df6 100644 --- a/schema/schema.graphql +++ b/schema/schema.graphql @@ -66,7 +66,8 @@ type Order { total: Float! "ISO 4217 currency code." currency: String! - payment: Payment! + "Payment for the order. Null for invoice orders that have not been paid yet." + payment: Payment shippingAddress: Address } @@ -128,11 +129,12 @@ input CheckoutInput { cartId: ID! email: String! shippingAddress: AddressInput! - "Tokenized card from the payment form, e.g. tok_visa_4242." - cardToken: String! + "Tokenized card from the payment form, e.g. tok_visa_4242. Omit to place an invoice order that is paid later." + cardToken: String } type CheckoutResult { order: Order! - payment: Payment! + "Null for invoice orders." + payment: Payment } diff --git a/src/data/store.ts b/src/data/store.ts index 7dd725a..4226ba1 100644 --- a/src/data/store.ts +++ b/src/data/store.ts @@ -40,7 +40,8 @@ export interface OrderRecord { createdAt: string; items: LineItemRecord[]; shippingAddress?: AddressRecord; - paymentId: string; + /** Unset for invoice orders that have not been paid yet. */ + paymentId?: string; } export interface PaymentRecord { diff --git a/src/domain/checkout.ts b/src/domain/checkout.ts index f573686..0ca6326 100644 --- a/src/domain/checkout.ts +++ b/src/domain/checkout.ts @@ -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; 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', 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 }; diff --git a/src/domain/refunds.ts b/src/domain/refunds.ts index 87eed51..ee1c707 100644 --- a/src/domain/refunds.ts +++ b/src/domain/refunds.ts @@ -20,8 +20,8 @@ export function refundOrder(store: Store, orderId: string, amount?: number | nul throw badInput('Partial refunds are not supported'); } - const payment = store.payments.get(order.paymentId); - if (!payment) throw notFound('Payment', order.paymentId); + const payment = order.paymentId ? store.payments.get(order.paymentId) : undefined; + if (!payment) throw badInput(`Order ${order.number} has no payment to refund`); order.status = 'REFUNDED'; payment.status = 'REFUNDED'; diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index 75861b1..dd5d554 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -78,11 +78,8 @@ export const resolvers = { tax: (order: OrderRecord) => computeTotals(order.items).tax, total: (order: OrderRecord) => computeTotals(order.items).total, currency: () => 'USD', - payment: (order: OrderRecord, _: unknown, { store }: Context) => { - const payment = store.payments.get(order.paymentId); - if (!payment) throw notFound('Payment', order.paymentId); - return payment; - }, + payment: (order: OrderRecord, _: unknown, { store }: Context) => + order.paymentId ? (store.payments.get(order.paymentId) ?? null) : null, shippingAddress: (order: OrderRecord) => order.shippingAddress ?? null, }, diff --git a/test/checkout.test.ts b/test/checkout.test.ts index bf01a92..7a84b53 100644 --- a/test/checkout.test.ts +++ b/test/checkout.test.ts @@ -57,4 +57,14 @@ describe('checkout', () => { }); expect(errors?.[0].message).toBe('Seed Starter Tray is out of stock'); }); + + it('creates a pending invoice order when no card is given', async () => { + const { execute } = setup(); + const { data, errors } = await execute(CHECKOUT, { + input: { cartId: 'cart_avery', email: 'avery@example.com', shippingAddress: address }, + }); + expect(errors).toBeUndefined(); + expect(data.checkout.order).toMatchObject({ status: 'PENDING', total: 73.07 }); + expect(data.checkout.payment).toBeNull(); + }); }); diff --git a/test/orders.test.ts b/test/orders.test.ts index 486d092..579c73f 100644 --- a/test/orders.test.ts +++ b/test/orders.test.ts @@ -38,13 +38,21 @@ describe('orders', () => { expect(data.order.payment.amount).toBe(data.order.total); }); - it('every order exposes a payment', async () => { + it('returns a null payment for unpaid invoice orders', async () => { const { execute } = setup(); - const { data, errors } = await execute(`{ orders { id payment { id status } } }`); + await execute( + `mutation($input: CheckoutInput!) { checkout(input: $input) { order { id } } }`, + { + input: { + cartId: 'cart_avery', + email: 'avery@example.com', + shippingAddress: { line1: '742 Alder Street', city: 'Portland', region: 'OR', postalCode: '97205', country: 'US' }, + }, + }, + ); + const { data, errors } = await execute(`{ orders(customerId: "cus_avery") { number status payment { id } } }`); expect(errors).toBeUndefined(); - for (const order of data.orders) { - expect(order.payment).not.toBeNull(); - } + expect(data.orders[0]).toEqual({ number: 'TH-1005', status: 'PENDING', payment: null }); }); it('lists orders newest first and filters by customer', async () => {