import {z} from 'zod' export type PaymentIntent = {publishable_key?: string | undefined, amount: number, gateway: string, link: number, customer?: Record | undefined, metadata?: Record | undefined, return_url?: (string | null) | undefined, authorization_url?: (string | null) | undefined, status?: ('failed' | 'pending' | 'succeeded') | undefined, status_description?: (string | null) | undefined, status_reason?: (string | null) | undefined, created?: string | undefined, updated?: string | undefined}; export type CreateApiPublishablePaymentLinksIntentsArgs = {body: PaymentIntent}; export type CreateApiPublishablePaymentLinksIntentsOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentIntent = z.object({publishable_key: z.string().optional(), amount: z.number(), gateway: z.string(), link: z.number().int(), customer: z.object({}).optional(), metadata: z.object({}).optional(), return_url: z.string().nullable().optional(), authorization_url: z.string().nullable().optional(), status: z.enum(["failed", "pending", "succeeded"]).optional(), status_description: z.string().nullable().optional(), status_reason: z.string().nullable().optional(), created: z.string().optional(), updated: z.string().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 payment intent (publishable) * * POST /publishable/payment_links/intents/ */ export const createApiPublishablePaymentLinksIntents = async (args: CreateApiPublishablePaymentLinksIntentsArgs, options?: CreateApiPublishablePaymentLinksIntentsOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/publishable/payment_links/intents/${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(paymentIntent, await res.json()) };