import {z} from 'zod' export type OneTimePaymentsPostRequestBodyContentApplicationJsonSchemaOneOf0ItemsItemsCurrency = 'GTQ' | 'USD'; export type OneTimePaymentsPostRequestBodyContentApplicationJsonSchemaOneOf0ItemsItems = {name?: string | undefined, currency?: OneTimePaymentsPostRequestBodyContentApplicationJsonSchemaOneOf0ItemsItemsCurrency | undefined, amount_in_cents?: number | undefined, image_url?: string | undefined, quantity?: number | undefined, product_id?: string | undefined}; export type OneTimePaymentsPostRequestBodyContentApplicationJsonSchemaOneOf0TransferSetupsItemsPurpose = 'fund_split' | 'platform_commission'; export type OneTimePaymentsPostRequestBodyContentApplicationJsonSchemaOneOf0TransferSetupsItems = {amount_in_cents: number, recipient_id?: string | undefined, purpose?: OneTimePaymentsPostRequestBodyContentApplicationJsonSchemaOneOf0TransferSetupsItemsPurpose | undefined}; export type OneTimePaymentsCreateOneTimePaymentRequest0 = {payment_method_id: string, items: Array, transfer_setups?: Array | undefined}; export type OneTimePaymentsPostRequestBodyContentApplicationJsonSchemaOneOf1Mode = 'now' | 'next_cycle'; export type OneTimePaymentsCreateOneTimePaymentRequest1 = {subscription_id: string, amount_in_cents: number, description: string, mode?: OneTimePaymentsPostRequestBodyContentApplicationJsonSchemaOneOf1Mode | undefined}; export type OneTimePaymentsCreateOneTimePaymentRequest = OneTimePaymentsCreateOneTimePaymentRequest0 | OneTimePaymentsCreateOneTimePaymentRequest1; export type CreateApiOneTimePaymentsArgs = {body: OneTimePaymentsCreateOneTimePaymentRequest}; export type CreateApiOneTimePaymentsOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transferSetupStatus = z.enum(["pending", "completed", "cancelled", "reversed"]); export const transferSetupPurpose = z.enum(["fund_split", "platform_commission"]); export const transferSetup = z.object({id: z.string().optional(), status: transferSetupStatus.optional(), amount_in_cents: z.number().int().nullable().optional(), amount_percent: z.number().nullable().optional(), currency: z.string().optional(), recipient_id: z.string().optional(), purpose: transferSetupPurpose.optional()}); export const oneTimePaymentsCreateOneTimePaymentResponse200 = z.object({object: z.string().optional(), id: z.string().optional(), status: z.string().optional(), transfer_setups: z.array(transferSetup).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 }; /** * Cobrar un método de pago guardado * * POST /one_time_payments */ export const createApiOneTimePayments = async (args: CreateApiOneTimePaymentsArgs, options?: CreateApiOneTimePaymentsOptions) => { const {baseUrl = "https://app.recurrente.com/api", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (secretKey !== undefined) headers.set("X-SECRET-KEY", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/one_time_payments${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(oneTimePaymentsCreateOneTimePaymentResponse200, await res.json()) };