@paykit-sdk/core
Version:
The Payment Toolkit for Typescript
145 lines (139 loc) • 3.82 kB
JavaScript
import { z } from 'zod';
// src/resources/subscription.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")
})
);
// src/resources/customer.ts
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()
})
);
// src/resources/subscription.ts
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()
})
);
var updateSubscriptionSchema = schema()(
z.object({
metadata: metadataSchema,
provider_metadata: z.record(z.string(), z.unknown()).optional()
})
);
var retrieveSubscriptionSchema = schema()(
z.object({
id: z.string()
})
);
var deleteSubscriptionSchema = schema()(
z.object({
id: z.string()
})
);
var createSubscriptionSchema = 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()
})
);
export { createSubscriptionSchema, deleteSubscriptionSchema, retrieveSubscriptionSchema, subscriptionBillingIntervalSchema, subscriptionIntervalUnitSchema, subscriptionSchema, subscriptionStatusSchema, updateSubscriptionSchema };