import {z} from 'zod' export type LinkType = 'one_time' | 'multiple'; export type CryptoCurrency = 'usdc' | 'usdt' | 'pyusd' | 'eurc' | 'brett' | 'dai' | 'btc' | 'eth' | 'matic' | 'bnb' | 'sol' | 'busd' | 'trx' | 'food' | 'bonk' | 'wif' | 'goat' | 'spx' | 'truf' | 'pengu'; export type CustomFieldType = 'text' | 'numeric' | 'dropdown'; export type CustomFieldLabelType = 'custom'; export type CustomFieldLabelDto = {type?: CustomFieldLabelType | undefined, custom: string}; export type PaymentLinkCustomFieldsDto = {key: string, type: CustomFieldType, label: CustomFieldLabelDto, optional: boolean}; export type PaymentLinkCustomFieldDto = {fields?: Array | undefined}; export type SubmitType = 'pay' | 'book' | 'donate'; export type AfterCompletionLink = 'redirect' | 'hosted_confirmation'; export type PaymentLinkChainDto = {chainId: number, isPreferred: boolean}; export type CreatePaymentLinkDto = {type: LinkType, amount?: number | undefined, currency?: CryptoCurrency | undefined, currencies?: Array | undefined, preferredCurrency?: CryptoCurrency | undefined, title: string, description?: string | undefined, image?: string | undefined, tags: Array, nameCollection: boolean, emailCollection: boolean, phoneNumberCollection: boolean, shippingAddressCollection: boolean, billingAddressCollection: boolean, customFields?: PaymentLinkCustomFieldDto | undefined, submitType: SubmitType, afterCompletion: AfterCompletionLink, afterCompletionRedirectUrl?: string | undefined, afterCompletionConfirmMsg?: string | undefined, suggestedCurrency?: CryptoCurrency | undefined, suggestedAmounts?: Array | undefined, productId?: string | undefined, allowPromotionCodes?: boolean | undefined, allowedChains?: Array | undefined}; export type CreateApiApiV1PaymentLinksArgs = {body: CreatePaymentLinkDto}; export type CreateApiApiV1PaymentLinksOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const linkType = z.enum(["one_time", "multiple"]); export const cryptoCurrency = z.enum(["usdc", "usdt", "pyusd", "eurc", "brett", "dai", "btc", "eth", "matic", "bnb", "sol", "busd", "trx", "food", "bonk", "wif", "goat", "spx", "truf", "pengu"]); export const customFieldType = z.enum(["text", "numeric", "dropdown"]); export const customFieldLabelType = z.literal("custom"); export const customFieldLabelDto = z.object({type: customFieldLabelType.optional(), custom: z.string()}); export const paymentLinkCustomFieldsDto = z.object({key: z.string(), type: customFieldType, label: customFieldLabelDto, optional: z.boolean()}); export const paymentLinkCustomFieldDto = z.object({fields: z.array(paymentLinkCustomFieldsDto).optional()}); export const submitType = z.enum(["pay", "book", "donate"]); export const afterCompletionLink = z.enum(["redirect", "hosted_confirmation"]); export const paymentLinkChainDto = z.object({chainId: z.number(), isPreferred: z.boolean()}); export const interval = z.enum(["day", "week", "month", "year"]); export const priceType = z.enum(["one_time", "recurring", "streaming"]); export const paymentLinkDto = z.object({id: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), organizationId: z.string(), type: linkType, amount: z.number().int().optional(), currency: cryptoCurrency.optional(), currencies: z.array(cryptoCurrency).optional(), preferredCurrency: cryptoCurrency.optional(), title: z.string(), description: z.string().optional(), image: z.string().optional(), publicImage: z.string().optional(), tags: z.array(z.string()), isActive: z.boolean().optional(), nameCollection: z.boolean(), emailCollection: z.boolean(), phoneNumberCollection: z.boolean(), shippingAddressCollection: z.boolean(), billingAddressCollection: z.boolean(), customFields: paymentLinkCustomFieldDto.optional(), submitType: submitType, afterCompletion: afterCompletionLink, afterCompletionRedirectUrl: z.string().optional(), afterCompletionConfirmMsg: z.string().optional(), suggestedCurrency: cryptoCurrency.optional(), suggestedAmounts: z.array(z.string()).optional(), productId: z.string().optional(), allowedChains: z.array(paymentLinkChainDto).optional(), interval: interval.optional(), intervalCount: z.number().optional(), priceType: priceType, allowPromotionCodes: z.boolean().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a payment link * * POST /api/v1/payment-links */ export const createApiApiV1PaymentLinks = async (args: CreateApiApiV1PaymentLinksArgs, options?: CreateApiApiV1PaymentLinksOptions) => { const {baseUrl = "https://api.copperx.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment-links${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paymentLinkDto, await res.json()) };