Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/contracts/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { oc } from "@orpc/contract";
import { z } from "zod";
import { CheckoutSchema } from "../schemas/checkout";
import { CurrencySchema } from "../schemas/currency";

/**
* Helper to treat empty strings as undefined (not provided).
Expand Down Expand Up @@ -47,7 +48,7 @@ export type CustomerInput = z.infer<typeof CustomerInputSchema>;
export const CreateCheckoutInputSchema = z.object({
nodeId: z.string(),
amount: z.number().optional(),
currency: z.string().optional(),
currency: CurrencySchema.optional(),
products: z.array(z.string()).optional(),
successUrl: z.string().optional(),
allowDiscountCodes: z.boolean().optional(),
Expand Down
4 changes: 3 additions & 1 deletion src/contracts/products.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { oc } from "@orpc/contract";
import { z } from "zod";
import { CurrencySchema } from "../schemas/currency";

export const ProductPriceSchema = z.object({
id: z.string(),
amountType: z.enum(["FIXED", "CUSTOM", "FREE"]),
priceAmount: z.number().nullable(),
currency: CurrencySchema,
});

export const ProductSchema = z.object({
id: z.string(),
name: z.string(),
description: z.string().nullable(),
recurringInterval: z.enum(["MONTH", "QUARTER", "YEAR"]).nullable(),
prices: z.array(ProductPriceSchema),
price: ProductPriceSchema.nullable(),
});

export const ListProductsOutputSchema = z.object({
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type {
} from "./contracts/onboarding";
export type { Checkout } from "./schemas/checkout";
export { CheckoutSchema } from "./schemas/checkout";
export type { Currency } from "./schemas/currency";
export { CurrencySchema } from "./schemas/currency";
export type { Product, ProductPrice } from "./contracts/products";
export {
ProductSchema,
Expand Down
3 changes: 2 additions & 1 deletion src/schemas/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from "zod";
import { CurrencySchema } from "./currency";
import {
BaseInvoiceSchema,
DynamicAmountPendingInvoiceSchema,
Expand Down Expand Up @@ -42,7 +43,7 @@ const BaseCheckoutSchema = z.object({
expiresAt: z.date(),
userMetadata: z.record(z.any()).nullable(),
customFieldData: z.record(z.any()).nullable(),
currency: z.string(),
currency: CurrencySchema,
allowDiscountCodes: z.boolean(),
/**
* Array of customer fields required at checkout.
Expand Down
9 changes: 9 additions & 0 deletions src/schemas/currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from "zod";

/**
* Supported currencies for pricing and payments.
* - USD: US Dollars (amounts in cents)
* - SAT: Satoshis (amounts in whole sats)
*/
export const CurrencySchema = z.enum(["USD", "SAT"]);
export type Currency = z.infer<typeof CurrencySchema>;
3 changes: 2 additions & 1 deletion src/schemas/invoice.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { z } from "zod";
import { CurrencySchema } from "./currency";

export const BaseInvoiceSchema = z.object({
invoice: z.string(),
expiresAt: z.date(),
paymentHash: z.string(),
amountSats: z.number().nullable(),
amountSatsReceived: z.number().nullable(),
currency: z.string(),
currency: CurrencySchema,
fiatAmount: z.number().nullable(),
btcPrice: z.number().nullable(),
});
Expand Down
4 changes: 3 additions & 1 deletion src/schemas/product.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { z } from "zod";
import { CurrencySchema } from "./currency";

export const CheckoutProductPriceSchema = z.object({
id: z.string(),
amountType: z.enum(["FIXED", "CUSTOM", "FREE"]),
priceAmount: z.number().nullable(),
currency: CurrencySchema,
});

export const CheckoutProductSchema = z.object({
id: z.string(),
name: z.string(),
description: z.string().nullable(),
recurringInterval: z.enum(["MONTH", "QUARTER", "YEAR"]).nullable(),
prices: z.array(CheckoutProductPriceSchema),
price: CheckoutProductPriceSchema.nullable(),
});
13 changes: 4 additions & 9 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,12 @@ describe('API Contract Index', () => {
name: 'Test Product',
description: null,
recurringInterval: null,
prices: [{
price: {
id: 'price_123',
amountType: 'FIXED' as const,
priceAmount: 1000,
minimumAmount: null,
maximumAmount: null,
presetAmount: null,
unitAmount: null,
capAmount: null,
meterId: null,
}],
currency: 'USD',
},
}],
providedAmount: null,
totalAmount: null,
Expand Down Expand Up @@ -196,4 +191,4 @@ describe('API Contract Index', () => {
expect(contract.checkout.paymentReceived).toBeDefined();
});
});
});
});
13 changes: 4 additions & 9 deletions tests/schemas/checkout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,12 @@ const mockProduct = {
name: 'Test Product',
description: 'A test product',
recurringInterval: null,
prices: [{
price: {
id: 'price_123',
amountType: 'FIXED' as const,
priceAmount: 1000,
minimumAmount: null,
maximumAmount: null,
presetAmount: null,
unitAmount: null,
capAmount: null,
meterId: null,
}],
currency: 'USD',
},
};

const mockInvoice = {
Expand Down Expand Up @@ -388,4 +383,4 @@ describe('CheckoutSchema', () => {
expect(result.success).toBe(false);
});
});
});
});
94 changes: 49 additions & 45 deletions tests/schemas/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ const baseProductPriceData = {
id: "price_123",
amountType: "FIXED" as const,
priceAmount: null,
currency: "USD",
};

const baseProductData = {
id: "product_123",
name: "Test Product",
description: null,
recurringInterval: null,
prices: [baseProductPriceData],
price: baseProductPriceData,
};

describe("Product Schemas", () => {
Expand Down Expand Up @@ -144,24 +145,18 @@ describe("Product Schemas", () => {
});
});

test("should validate product with multiple prices", () => {
const productWithMultiplePrices = {
test("should validate product with a custom price", () => {
const productWithCustomPrice = {
...baseProductData,
prices: [
{
id: "price_1",
amountType: "FIXED" as const,
priceAmount: 999,
},
{
id: "price_2",
amountType: "CUSTOM" as const,
priceAmount: null,
},
],
price: {
...baseProductPriceData,
id: "price_2",
amountType: "CUSTOM" as const,
priceAmount: null,
},
};

const result = CheckoutProductSchema.safeParse(productWithMultiplePrices);
const result = CheckoutProductSchema.safeParse(productWithCustomPrice);
expect(result.success).toBe(true);
});

Expand All @@ -185,23 +180,23 @@ describe("Product Schemas", () => {
expect(result.success).toBe(false);
});

test("should reject product without prices array", () => {
const productWithoutPrices = {
test("should reject product without price field", () => {
const productWithoutPrice = {
...baseProductData,
prices: undefined,
price: undefined,
};

const result = CheckoutProductSchema.safeParse(productWithoutPrices);
const result = CheckoutProductSchema.safeParse(productWithoutPrice);
expect(result.success).toBe(false);
});

test("should allow product with empty prices array", () => {
const productWithEmptyPrices = {
test("should allow product with null price", () => {
const productWithNullPrice = {
...baseProductData,
prices: [],
price: null,
};

const result = CheckoutProductSchema.safeParse(productWithEmptyPrices);
const result = CheckoutProductSchema.safeParse(productWithNullPrice);
expect(result.success).toBe(true);
});

Expand All @@ -215,15 +210,13 @@ describe("Product Schemas", () => {
expect(result.success).toBe(false);
});

test("should reject product with invalid price in prices array", () => {
test("should reject product with invalid price object", () => {
const productWithInvalidPrice = {
...baseProductData,
prices: [
{
...baseProductPriceData,
amountType: "INVALID_TYPE" as any,
},
],
price: {
...baseProductPriceData,
amountType: "INVALID_TYPE" as any,
},
};

const result = CheckoutProductSchema.safeParse(productWithInvalidPrice);
Expand Down Expand Up @@ -252,33 +245,44 @@ describe("Product Schemas", () => {
});

describe("Integration scenarios", () => {
test("should validate complete product with all supported price types", () => {
const completeProduct = {
id: "product_complete",
name: "Complete Product",
description: "A product with all types of prices",
recurringInterval: "MONTH" as const,
prices: [
{
test("should validate products with all supported price types", () => {
const products = [
{
...baseProductData,
id: "product_fixed",
price: {
...baseProductPriceData,
id: "price_fixed",
amountType: "FIXED" as const,
priceAmount: 2999,
},
{
},
{
...baseProductData,
id: "product_custom",
price: {
...baseProductPriceData,
id: "price_custom",
amountType: "CUSTOM" as const,
priceAmount: null,
},
{
},
{
...baseProductData,
id: "product_free",
price: {
...baseProductPriceData,
id: "price_free",
amountType: "FREE" as const,
priceAmount: 0,
},
],
};
},
];

const result = CheckoutProductSchema.safeParse(completeProduct);
expect(result.success).toBe(true);
products.forEach((product) => {
const result = CheckoutProductSchema.safeParse(product);
expect(result.success).toBe(true);
});
});
});
});