UNPKG

@paykit-sdk/core

Version:

The Payment Toolkit for Typescript

220 lines (213 loc) 6.09 kB
'use strict'; var zod = require('zod'); // src/resources/invoice.ts var metadataSchema = zod.z.record(zod.z.string(), zod.z.string()); // src/tools/utils.ts var schema = () => { return (schema2) => schema2; }; var billingAddressSchema = schema()( zod.z.object({ name: zod.z.string().min(1, "Recipient name is required"), line1: zod.z.string().min(1, "Address line 1 is required"), line2: zod.z.string().default("").optional(), city: zod.z.string().min(1, "City is required"), state: zod.z.string().optional(), // Optional because not all countries use states postal_code: zod.z.string().min(1, "Postal code is required"), country: zod.z.string().length(2, "Country must be a 2-letter ISO code").toUpperCase(), phone: zod.z.string().optional() }) ); var billingSchema = schema()( zod.z.object({ address: billingAddressSchema, carrier: zod.z.string().optional(), currency: zod.z.string().min(1, "Currency is required") }) ); schema()( zod.z.object({ id: zod.z.string(), email: zod.z.string().email(), name: zod.z.string(), phone: zod.z.string().nullable(), metadata: metadataSchema.optional(), custom_fields: zod.z.record(zod.z.string(), zod.z.unknown()).optional(), created_at: zod.z.date(), updated_at: zod.z.date().nullable() }) ); var payeeSchema = schema()( zod.z.union([ zod.z.object({ email: zod.z.string().email() }), zod.z.object({ id: zod.z.union([zod.z.string(), zod.z.number()]) }) ]) ); schema()( zod.z.object({ email: zod.z.string().email(), name: zod.z.string().optional(), phone: zod.z.string().nullable().optional(), metadata: metadataSchema.optional(), billing: billingSchema.nullable(), provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional() }) ); schema()( zod.z.object({ email: zod.z.string().email().optional(), name: zod.z.string().optional(), metadata: metadataSchema.optional(), provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional() }) ); schema()( zod.z.object({ id: zod.z.string() }) ); var subscriptionIntervalUnitSchema = zod.z.enum([ "day", "week", "month", "year" ]); var subscriptionBillingIntervalSchema = zod.z.union([ subscriptionIntervalUnitSchema, zod.z.object({ type: zod.z.literal("custom"), durationMs: zod.z.number().int().positive() }) ]); var subscriptionStatusSchema = zod.z.enum([ "active", "past_due", "canceled", "expired", "trialing", "pending" ]); var subscriptionSchema = schema()( zod.z.object({ id: zod.z.string(), customer: payeeSchema.nullable(), amount: zod.z.number(), currency: zod.z.string(), status: subscriptionStatusSchema, current_period_start: zod.z.date(), current_period_end: zod.z.date(), item_id: zod.z.string(), billing_interval: subscriptionBillingIntervalSchema, metadata: metadataSchema.nullable(), custom_fields: zod.z.record(zod.z.string(), zod.z.unknown()).nullable(), requires_action: zod.z.boolean(), payment_url: zod.z.string().nullable() }) ); schema()( zod.z.object({ metadata: metadataSchema, provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional() }) ); schema()( zod.z.object({ id: zod.z.string() }) ); schema()( zod.z.object({ id: zod.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: zod.z.record(zod.z.string(), zod.z.unknown()).optional(), quantity: zod.z.number() }) ); // src/resources/checkout.ts var checkoutSubscriptionSchema = schema()( zod.z.object({ billing_interval: subscriptionBillingIntervalSchema, billing_interval_count: zod.z.number() }) ); var billingModeSchema = zod.z.enum(["one_time", "recurring"]); schema()( zod.z.object({ id: zod.z.string(), customer: payeeSchema.nullable(), payment_url: zod.z.string(), metadata: metadataSchema.nullable(), session_type: billingModeSchema, products: zod.z.array( zod.z.object({ id: zod.z.string(), quantity: zod.z.number() }) ), currency: zod.z.string(), amount: zod.z.number(), subscription: checkoutSubscriptionSchema.nullable().optional() }) ); schema()( zod.z.object({ customer: payeeSchema, metadata: metadataSchema.nullable(), item_id: zod.z.string(), quantity: zod.z.number(), provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional(), billing: billingSchema.optional(), success_url: zod.z.string(), cancel_url: zod.z.string() }) ); var createCheckoutSchema = schema()( zod.z.object({ customer: payeeSchema, metadata: metadataSchema.nullable(), session_type: zod.z.literal(billingModeSchema.enum.one_time), item_id: zod.z.string(), quantity: zod.z.number(), subscription: checkoutSubscriptionSchema.optional(), provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional(), billing: billingSchema.optional(), success_url: zod.z.string(), cancel_url: zod.z.string() }) ); schema()( createCheckoutSchema.partial() ); schema()( zod.z.object({ id: zod.z.string() }) ); // src/resources/invoice.ts var invoiceStatusSchema = zod.z.enum(["paid", "open"]); var invoiceSchema = schema()( zod.z.object({ id: zod.z.string(), customer: payeeSchema.nullable(), subscription_id: zod.z.string().nullable(), billing_mode: billingModeSchema, amount_paid: zod.z.number(), currency: zod.z.string(), status: invoiceStatusSchema, paid_at: zod.z.string().nullable(), line_items: zod.z.array(zod.z.object({ id: zod.z.string(), quantity: zod.z.number() })).nullable(), metadata: metadataSchema.nullable(), custom_fields: zod.z.record(zod.z.string(), zod.z.any()).nullable() }) ); exports.invoiceSchema = invoiceSchema; exports.invoiceStatusSchema = invoiceStatusSchema;