diff --git a/CHANGELOG.md b/CHANGELOG.md index 2569d8a..44bfaeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to the API contract are documented here. ## Unreleased +- `refundOrder` supports partial refunds. Adds `OrderStatus.PARTIALLY_REFUNDED` and + `Order.refundedAmount`. + ## 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..7d5160f 100644 --- a/schema/schema.graphql +++ b/schema/schema.graphql @@ -16,7 +16,7 @@ type Mutation { removeFromCart(cartId: ID!, productId: ID!): Cart! "Converts a cart into an order and captures payment." checkout(input: CheckoutInput!): CheckoutResult! - "Refunds a paid or fulfilled order. Omit amount to refund the full total." + "Refunds a paid or fulfilled order. Omit amount to refund the remaining balance." refundOrder(orderId: ID!, amount: Float): Order! } @@ -66,6 +66,8 @@ type Order { total: Float! "ISO 4217 currency code." currency: String! + "Total refunded so far, in USD." + refundedAmount: Float! payment: Payment! shippingAddress: Address } @@ -84,6 +86,8 @@ enum OrderStatus { PAID FULFILLED CANCELLED + "Part of the order total has been refunded." + PARTIALLY_REFUNDED REFUNDED } diff --git a/src/data/store.ts b/src/data/store.ts index 7dd725a..1fb1d6a 100644 --- a/src/data/store.ts +++ b/src/data/store.ts @@ -1,4 +1,4 @@ -export type OrderStatus = 'PENDING' | 'PAID' | 'FULFILLED' | 'CANCELLED' | 'REFUNDED'; +export type OrderStatus = 'PENDING' | 'PAID' | 'FULFILLED' | 'CANCELLED' | 'PARTIALLY_REFUNDED' | 'REFUNDED'; export type PaymentStatus = 'AUTHORIZED' | 'CAPTURED' | 'FAILED' | 'REFUNDED'; export interface ProductRecord { @@ -41,6 +41,7 @@ export interface OrderRecord { items: LineItemRecord[]; shippingAddress?: AddressRecord; paymentId: string; + refundedAmount?: number; } export interface PaymentRecord { diff --git a/src/domain/refunds.ts b/src/domain/refunds.ts index 87eed51..9fd6137 100644 --- a/src/domain/refunds.ts +++ b/src/domain/refunds.ts @@ -1,12 +1,13 @@ import type { OrderRecord, Store } from '../data/store'; import { badInput, notFound } from './errors'; -import { computeTotals } from './pricing'; +import { computeTotals, roundCurrency } from './pricing'; -const REFUNDABLE = new Set(['PAID', 'FULFILLED']); +const REFUNDABLE = new Set(['PAID', 'FULFILLED', 'PARTIALLY_REFUNDED']); /** - * Refunds an order in full. Partial refunds are not supported yet; callers - * passing an amount lower than the order total receive BAD_USER_INPUT. + * Refunds part or all of an order. Omitting the amount refunds the remaining + * balance. The order becomes PARTIALLY_REFUNDED until the full total has been + * returned, then REFUNDED. */ export function refundOrder(store: Store, orderId: string, amount?: number | null): OrderRecord { const order = store.orders.get(orderId); @@ -16,14 +17,21 @@ export function refundOrder(store: Store, orderId: string, amount?: number | nul } const { total } = computeTotals(order.items); - if (amount != null && amount < total) { - throw badInput('Partial refunds are not supported'); - } + const alreadyRefunded = order.refundedAmount ?? 0; + const remaining = roundCurrency(total - alreadyRefunded); + const refund = amount ?? remaining; + if (refund <= 0) throw badInput('Refund amount must be greater than zero'); + if (refund > remaining) throw badInput(`Refund exceeds remaining balance of ${remaining}`); const payment = store.payments.get(order.paymentId); if (!payment) throw notFound('Payment', order.paymentId); - order.status = 'REFUNDED'; - payment.status = 'REFUNDED'; + order.refundedAmount = roundCurrency(alreadyRefunded + refund); + if (order.refundedAmount >= total) { + order.status = 'REFUNDED'; + payment.status = 'REFUNDED'; + } else { + order.status = 'PARTIALLY_REFUNDED'; + } return order; } diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index 75861b1..4970766 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -78,6 +78,7 @@ export const resolvers = { tax: (order: OrderRecord) => computeTotals(order.items).tax, total: (order: OrderRecord) => computeTotals(order.items).total, currency: () => 'USD', + refundedAmount: (order: OrderRecord) => order.refundedAmount ?? 0, payment: (order: OrderRecord, _: unknown, { store }: Context) => { const payment = store.payments.get(order.paymentId); if (!payment) throw notFound('Payment', order.paymentId); diff --git a/test/refunds.test.ts b/test/refunds.test.ts index 546cc1d..6ab9caf 100644 --- a/test/refunds.test.ts +++ b/test/refunds.test.ts @@ -5,6 +5,7 @@ const REFUND = /* GraphQL */ ` mutation Refund($orderId: ID!, $amount: Float) { refundOrder(orderId: $orderId, amount: $amount) { status + refundedAmount payment { status } } } @@ -15,13 +16,27 @@ describe('refunds', () => { const { execute } = setup(); const { data, errors } = await execute(REFUND, { orderId: 'ord_1001' }); expect(errors).toBeUndefined(); - expect(data.refundOrder).toEqual({ status: 'REFUNDED', payment: { status: 'REFUNDED' } }); + expect(data.refundOrder).toEqual({ status: 'REFUNDED', refundedAmount: 47.62, payment: { status: 'REFUNDED' } }); }); - it('rejects partial refunds', async () => { + it('partially refunds an order', async () => { const { execute } = setup(); - const { errors } = await execute(REFUND, { orderId: 'ord_1001', amount: 10 }); - expect(errors?.[0].message).toBe('Partial refunds are not supported'); + const { data, errors } = await execute(REFUND, { orderId: 'ord_1001', amount: 10 }); + expect(errors).toBeUndefined(); + expect(data.refundOrder).toEqual({ status: 'PARTIALLY_REFUNDED', refundedAmount: 10, payment: { status: 'CAPTURED' } }); + }); + + it('refunds the remaining balance after a partial refund', async () => { + const { execute } = setup(); + await execute(REFUND, { orderId: 'ord_1001', amount: 10 }); + const { data } = await execute(REFUND, { orderId: 'ord_1001' }); + expect(data.refundOrder).toEqual({ status: 'REFUNDED', refundedAmount: 47.62, payment: { status: 'REFUNDED' } }); + }); + + it('rejects refunds above the remaining balance', async () => { + const { execute } = setup(); + const { errors } = await execute(REFUND, { orderId: 'ord_1001', amount: 100 }); + expect(errors?.[0].message).toBe('Refund exceeds remaining balance of 47.62'); }); it('rejects refunds for pending orders', async () => {