@paykit-sdk/core
Version:
The Payment Toolkit for Typescript
350 lines (343 loc) • 11 kB
JavaScript
'use strict';
var zod = require('zod');
// src/resources/checkout.ts
var PAYKIT_METADATA_KEY = "__paykit";
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")
})
);
var customerSchema = 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()]) })
])
);
var createCustomerSchema = 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()
})
);
var updateCustomerSchema = 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()
})
);
var retrieveCustomerSchema = schema()(
zod.z.object({
id: zod.z.string()
})
);
var isEmailCustomer = (customer) => {
return typeof customer === "object" && customer !== null && "email" in customer;
};
var isIdCustomer = (customer) => {
return typeof customer === "object" && customer !== null && "id" in customer;
};
var parseCustomerName = (params) => {
const full = params.name?.trim() || params.email.split("@")[0];
const parts = full.split(/\s+/);
const first = parts[0] || "";
const last = parts.slice(1).join(" ") || "";
return {
fullName: full,
firstName: first,
lastName: last,
initials: `${first[0] || ""}${last[0] || ""}`.toUpperCase()
};
};
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()
})
);
var updateSubscriptionSchema = schema()(
zod.z.object({
metadata: metadataSchema,
provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
})
);
var retrieveSubscriptionSchema = schema()(
zod.z.object({
id: zod.z.string()
})
);
var deleteSubscriptionSchema = schema()(
zod.z.object({
id: zod.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: 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"]);
var checkoutSchema = 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()
})
);
var createCheckoutBaseSchema = 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()
})
);
var updateCheckoutSchema = schema()(
createCheckoutSchema.partial()
);
var retrieveCheckoutSchema = schema()(
zod.z.object({
id: zod.z.string()
})
);
// src/resources/webhook.ts
var paykitEvent$InboundSchema = (event) => event;
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()
})
);
var paymentStatusSchema = zod.z.enum([
"pending",
"processing",
"requires_action",
"requires_capture",
"succeeded",
"canceled",
"failed"
]);
var paymentSchema = schema()(
zod.z.object({
id: zod.z.string(),
amount: zod.z.number().min(0),
currency: zod.z.string(),
customer: payeeSchema.nullable(),
status: paymentStatusSchema,
metadata: metadataSchema,
item_id: zod.z.string().nullable(),
requires_action: zod.z.boolean(),
payment_url: zod.z.string().nullable()
})
);
var createPaymentSchema = schema()(
paymentSchema.omit({
id: true,
status: true,
metadata: true,
requires_action: true,
payment_url: true
}).extend({
metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
billing: billingSchema.optional(),
capture_method: zod.z.enum(["automatic", "manual"])
})
);
var updatePaymentSchema = schema()(
paymentSchema.partial().extend({
provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
})
);
var retrievePaymentSchema = schema()(
paymentSchema.pick({ id: true })
);
var deletePaymentSchema = schema()(
paymentSchema.pick({ id: true })
);
var capturePaymentSchema = schema()(
zod.z.object({
amount: zod.z.number()
})
);
var refundReasonSchema = schema()(
zod.z.string().nullable()
);
var refundSchema = schema()(
zod.z.object({
id: zod.z.string(),
amount: zod.z.number(),
currency: zod.z.string(),
reason: refundReasonSchema,
metadata: metadataSchema.nullable()
})
);
var createRefundSchema = schema()(
zod.z.object({
payment_id: zod.z.string(),
reason: refundReasonSchema,
amount: zod.z.number().min(0),
provider_metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
metadata: metadataSchema.nullable()
})
);
exports.PAYKIT_METADATA_KEY = PAYKIT_METADATA_KEY;
exports.billingAddressSchema = billingAddressSchema;
exports.billingModeSchema = billingModeSchema;
exports.billingSchema = billingSchema;
exports.capturePaymentSchema = capturePaymentSchema;
exports.checkoutSchema = checkoutSchema;
exports.checkoutSubscriptionSchema = checkoutSubscriptionSchema;
exports.createCheckoutBaseSchema = createCheckoutBaseSchema;
exports.createCheckoutSchema = createCheckoutSchema;
exports.createCustomerSchema = createCustomerSchema;
exports.createPaymentSchema = createPaymentSchema;
exports.createRefundSchema = createRefundSchema;
exports.createSubscriptionSchema = createSubscriptionSchema;
exports.customerSchema = customerSchema;
exports.deletePaymentSchema = deletePaymentSchema;
exports.deleteSubscriptionSchema = deleteSubscriptionSchema;
exports.invoiceSchema = invoiceSchema;
exports.invoiceStatusSchema = invoiceStatusSchema;
exports.isEmailCustomer = isEmailCustomer;
exports.isIdCustomer = isIdCustomer;
exports.metadataSchema = metadataSchema;
exports.parseCustomerName = parseCustomerName;
exports.payeeSchema = payeeSchema;
exports.paykitEvent$InboundSchema = paykitEvent$InboundSchema;
exports.paymentSchema = paymentSchema;
exports.paymentStatusSchema = paymentStatusSchema;
exports.refundSchema = refundSchema;
exports.retrieveCheckoutSchema = retrieveCheckoutSchema;
exports.retrieveCustomerSchema = retrieveCustomerSchema;
exports.retrievePaymentSchema = retrievePaymentSchema;
exports.retrieveSubscriptionSchema = retrieveSubscriptionSchema;
exports.subscriptionBillingIntervalSchema = subscriptionBillingIntervalSchema;
exports.subscriptionIntervalUnitSchema = subscriptionIntervalUnitSchema;
exports.subscriptionSchema = subscriptionSchema;
exports.subscriptionStatusSchema = subscriptionStatusSchema;
exports.updateCheckoutSchema = updateCheckoutSchema;
exports.updateCustomerSchema = updateCustomerSchema;
exports.updatePaymentSchema = updatePaymentSchema;
exports.updateSubscriptionSchema = updateSubscriptionSchema;