import {z} from 'zod' export type PublicPaymentCustomer = {name: string, email: string, phone: string, fingerprint: string, docType?: ('cpf' | 'cnpj') | undefined, docNumber?: string | undefined, birthDate?: string | undefined, ip?: string | undefined}; export type PublicPaymentItem = {offerId: string, quantity?: number | undefined, offerType?: 'main' | undefined}; export type PublicPaymentAddress = {country?: string | undefined, state?: string | undefined, city?: string | undefined, zipcode?: string | undefined, street?: string | undefined, neighborhood?: string | undefined, number?: string | undefined, complement?: string | undefined}; export type PublicPaymentMetadata = {utm_source?: string | undefined, utm_medium?: string | undefined, utm_campaign?: string | undefined, utm_term?: string | undefined, utm_content?: string | undefined, sck?: string | undefined}; export type PublicPaymentCreateRequest = {productId: string, paymentMethod: 'pix' | 'pix_auto' | 'boleto', customer: PublicPaymentCustomer, items: Array, address?: PublicPaymentAddress | undefined, affiliateShortId?: string | undefined, coupon?: string | undefined, metadata?: PublicPaymentMetadata | undefined, dueDate?: string | undefined, pixExpiresIn?: number | undefined}; export type CreateApiPublicApiPaymentsArgs = {'X-Idempotency-Key': string, body: PublicPaymentCreateRequest}; export type CreateApiPublicApiPaymentsOptions = {baseUrl?: string | undefined, OAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const publicPaymentResponseProduct = z.object({id: z.string().optional(), short_id: z.string().optional(), name: z.string().optional()}); export const publicPaymentResponseOffer = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), price: z.number().nullable().optional()}); export const publicPaymentResponsePix = z.object({qrCode: z.string().optional(), qrCodeBase64: z.string().optional(), expirationDate: z.string().optional()}); export const publicPaymentResponseBoleto = z.object({barcode: z.string().optional(), pdfUrl: z.string().optional(), dueDate: z.string().optional()}); export const publicPaymentResponse = z.object({id: z.string().optional(), refId: z.string().optional(), status: z.string().optional(), paymentMethod: z.enum(["pix", "pix_auto", "boleto"]).optional(), amount: z.string().optional(), baseAmount: z.string().nullable().optional(), discount: z.string().nullable().optional(), fees: z.string().nullable().optional(), externalId: z.string().nullable().optional(), checkoutUrl: z.string().nullable().optional(), createdAt: z.string().optional(), product: publicPaymentResponseProduct.optional(), offer: publicPaymentResponseOffer.optional(), pix: publicPaymentResponsePix.optional(), boleto: publicPaymentResponseBoleto.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 }; /** * Cria uma cobrança transacional Pix ou Boleto vinculada a um produto e oferta da * * POST /public_api/payments/ */ export const createApiPublicApiPayments = async (args: CreateApiPublicApiPaymentsArgs, options?: CreateApiPublicApiPaymentsOptions) => { const {baseUrl = "https://api.cakto.com.br", fetch: fetchFn = fetch, OAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Idempotency-Key'] != null) headers.set("X-Idempotency-Key", String(args['X-Idempotency-Key'])) if (OAuthToken !== undefined) headers.set('authorization', `Bearer ${OAuthToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/public_api/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(publicPaymentResponse, await res.json()) };