UNPKG

@paykit-sdk/core

Version:

The Payment Toolkit for Typescript

199 lines (194 loc) 5.19 kB
import { z } from 'zod'; // src/resources/checkout.ts var metadataSchema = z.record(z.string(), z.string()); // src/tools/utils.ts var schema = () => { return (schema2) => schema2; }; var billingAddressSchema = schema()( z.object({ name: z.string().min(1, "Recipient name is required"), line1: z.string().min(1, "Address line 1 is required"), line2: z.string().default("").optional(), city: z.string().min(1, "City is required"), state: z.string().optional(), // Optional because not all countries use states postal_code: z.string().min(1, "Postal code is required"), country: z.string().length(2, "Country must be a 2-letter ISO code").toUpperCase(), phone: z.string().optional() }) ); var billingSchema = schema()( z.object({ address: billingAddressSchema, carrier: z.string().optional(), currency: z.string().min(1, "Currency is required") }) ); schema()( z.object({ id: z.string(), email: z.string().email(), name: z.string(), phone: z.string().nullable(), metadata: metadataSchema.optional(), custom_fields: z.record(z.string(), z.unknown()).optional(), created_at: z.date(), updated_at: z.date().nullable() }) ); var payeeSchema = schema()( z.union([ z.object({ email: z.string().email() }), z.object({ id: z.union([z.string(), z.number()]) }) ]) ); schema()( z.object({ email: z.string().email(), name: z.string().optional(), phone: z.string().nullable().optional(), metadata: metadataSchema.optional(), billing: billingSchema.nullable(), provider_metadata: z.record(z.string(), z.unknown()).optional() }) ); schema()( z.object({ email: z.string().email().optional(), name: z.string().optional(), metadata: metadataSchema.optional(), provider_metadata: z.record(z.string(), z.unknown()).optional() }) ); schema()( z.object({ id: z.string() }) ); var subscriptionIntervalUnitSchema = z.enum([ "day", "week", "month", "year" ]); var subscriptionBillingIntervalSchema = z.union([ subscriptionIntervalUnitSchema, z.object({ type: z.literal("custom"), durationMs: z.number().int().positive() }) ]); var subscriptionStatusSchema = z.enum([ "active", "past_due", "canceled", "expired", "trialing", "pending" ]); var subscriptionSchema = schema()( z.object({ id: z.string(), customer: payeeSchema.nullable(), amount: z.number(), currency: z.string(), status: subscriptionStatusSchema, current_period_start: z.date(), current_period_end: z.date(), item_id: z.string(), billing_interval: subscriptionBillingIntervalSchema, metadata: metadataSchema.nullable(), custom_fields: z.record(z.string(), z.unknown()).nullable(), requires_action: z.boolean(), payment_url: z.string().nullable() }) ); schema()( z.object({ metadata: metadataSchema, provider_metadata: z.record(z.string(), z.unknown()).optional() }) ); schema()( z.object({ id: z.string() }) ); schema()( z.object({ id: z.string() }) ); schema()( subscriptionSchema.omit({ id: true, status: true, custom_fields: true, current_period_start: true, current_period_end: true, requires_action: true, payment_url: true }).extend({ provider_metadata: z.record(z.string(), z.unknown()).optional(), quantity: z.number() }) ); // src/resources/checkout.ts var checkoutSubscriptionSchema = schema()( z.object({ billing_interval: subscriptionBillingIntervalSchema, billing_interval_count: z.number() }) ); var billingModeSchema = z.enum(["one_time", "recurring"]); var checkoutSchema = schema()( z.object({ id: z.string(), customer: payeeSchema.nullable(), payment_url: z.string(), metadata: metadataSchema.nullable(), session_type: billingModeSchema, products: z.array( z.object({ id: z.string(), quantity: z.number() }) ), currency: z.string(), amount: z.number(), subscription: checkoutSubscriptionSchema.nullable().optional() }) ); var createCheckoutBaseSchema = schema()( z.object({ customer: payeeSchema, metadata: metadataSchema.nullable(), item_id: z.string(), quantity: z.number(), provider_metadata: z.record(z.string(), z.unknown()).optional(), billing: billingSchema.optional(), success_url: z.string(), cancel_url: z.string() }) ); var createCheckoutSchema = schema()( z.object({ customer: payeeSchema, metadata: metadataSchema.nullable(), session_type: z.literal(billingModeSchema.enum.one_time), item_id: z.string(), quantity: z.number(), subscription: checkoutSubscriptionSchema.optional(), provider_metadata: z.record(z.string(), z.unknown()).optional(), billing: billingSchema.optional(), success_url: z.string(), cancel_url: z.string() }) ); var updateCheckoutSchema = schema()( createCheckoutSchema.partial() ); var retrieveCheckoutSchema = schema()( z.object({ id: z.string() }) ); export { billingModeSchema, checkoutSchema, checkoutSubscriptionSchema, createCheckoutBaseSchema, createCheckoutSchema, retrieveCheckoutSchema, updateCheckoutSchema };