import {z} from 'zod' export type GetApiChargesArgs = {'Accept-Language'?: ('es' | 'en') | undefined, 'X-Child-Company-Id'?: string | undefined, limit?: number | undefined, search?: string | undefined, next?: string | undefined, previous?: string | undefined}; export type GetApiChargesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const chargeResponseChannel = z.object({segment: z.string().optional(), checkout_request_id: z.string().optional(), checkout_request_type: z.string().optional(), id: z.string().optional()}); export const orderNextActionResponseRedirectToUrl = z.object({url: z.string().optional(), return_url: z.string().optional()}); export const orderNextActionResponse = z.object({redirect_to_url: orderNextActionResponseRedirectToUrl.optional(), type: z.string().optional()}); export const chargeResponsePaymentMethod = z.discriminatedUnion("object", [z.object({type: z.string().optional(), object: z.string(), agreement: z.string().optional(), auth_code: z.number().int().nullable().optional(), cashier_id: z.string().nullable().optional(), reference: z.string().optional(), barcode_url: z.string().optional(), expires_at: z.number().int().optional(), product_type: z.string().optional(), service_name: z.string().optional(), store: z.string().nullable().optional(), store_name: z.string().optional(), customer_ip_address: z.string().optional()}), z.object({type: z.string().optional(), object: z.string(), account_type: z.string().optional(), auth_code: z.string().optional(), brand: z.string().optional(), contract_id: z.string().optional(), country: z.string().optional(), exp_month: z.string().optional(), exp_year: z.string().optional(), fraud_indicators: z.array(z.unknown()).optional(), issuer: z.string().optional(), last4: z.string().optional(), name: z.string().optional(), customer_ip_address: z.string().optional()}), z.object({type: z.string().optional(), object: z.string(), bank: z.string().optional(), clabe: z.string().optional(), description: z.string().nullable().optional(), executed_at: z.string().nullable().optional(), expires_at: z.number().int().optional(), issuing_account_bank: z.string().nullable().optional(), issuing_account_number: z.string().nullable().optional(), issuing_account_holder_name: z.string().nullable().optional(), issuing_account_tax_id: z.string().nullable().optional(), payment_attempts: z.array(z.unknown()).optional(), receiving_account_holder_name: z.string().nullable().optional(), receiving_account_number: z.string().optional(), receiving_account_bank: z.string().optional(), receiving_account_tax_id: z.string().nullable().optional(), reference_number: z.string().nullable().optional(), tracking_code: z.string().nullable().optional(), customer_ip_address: z.string().optional()}), z.object({type: z.string().optional(), object: z.literal("bnpl_payment"), cancel_url: z.string().optional(), expires_at: z.number().int(), failure_url: z.string().optional(), product_type: z.string(), redirect_url: z.string().optional(), success_url: z.string().optional()}), z.object({type: z.string().optional(), object: z.string(), deep_link: z.string(), expires_at: z.number().int().gt(0), product_type: z.string(), redirect_url: z.string(), reference: z.string()})]); export const chargeResponseRefundsData = z.object({amount: z.number().int(), auth_code: z.string().optional(), created_at: z.number().int(), expires_at: z.number().int().optional(), id: z.string(), object: z.string(), status: z.string().optional()}); export const chargeResponseRefunds = z.object({has_more: z.boolean(), object: z.string(), next_page_url: z.string().nullable().optional(), previous_page_url: z.string().nullable().optional(), data: z.array(chargeResponseRefundsData).optional()}); export const chargebackFileResponse = z.object({id: z.string().optional(), file_name: z.string().optional(), url: z.string().nullable().optional(), created_at: z.number().int().optional()}); export const chargebackResponse = z.object({id: z.string().optional(), status: z.enum(["won", "lost", "action_required", "pending_review", "under_review", "covered", "rt", "rt_sent", "represented"]).optional(), reason: z.enum(["unrecognized", "unauthorized", "unauthorized_by_client", "unrecognized_like_fraud", "insufficient_evidence", "illegible_evidence", "general", "late_evidence", "incorrect_exchange", "fraudulent_business", "billing", "twice_transaction", "transaction_not_refunded", "bank_doc_request", "unrecognized_purchase", "subscription_canceled", "copy_of_purchase", "differents_amounts", "subscription_not_requested", "damaged_products", "show_documents", "duplicate", "fraudulent", "service_canceled", "product_unacceptable", "product_not_received", "incorrect_amount", "chip_liabiliaty_shift", "credit_not_processed"]).optional(), note: z.string().nullable().optional(), followup_status: z.enum(["customer_uncontacted", "customer_contacted", "customer_unreachable"]).nullable().optional(), response_from_client: z.string().nullable().optional(), files: z.array(chargebackFileResponse).optional(), object: z.string().optional(), charge_id: z.string().optional(), created_at: z.number().int().optional(), evidence_due_by: z.number().int().optional()}).nullable(); export const chargeResponse = z.object({amount: z.number().int().optional(), channel: chargeResponseChannel.optional(), conekta_account_id: z.string().nullable().optional(), created_at: z.number().int(), currency: z.string(), customer_id: z.string().optional(), description: z.string().optional(), device_fingerprint: z.string().optional(), failure_code: z.string().optional(), failure_message: z.string().optional(), id: z.string(), livemode: z.boolean(), next_action: orderNextActionResponse.optional(), object: z.string(), order_id: z.string(), paid_at: z.number().int().nullable().optional(), payment_method: chargeResponsePaymentMethod.optional(), reference_id: z.string().nullable().optional(), refunds: chargeResponseRefunds.optional(), chargeback: chargebackResponse.optional(), status: z.string()}); export const getChargesResponse = z.object({has_more: z.boolean(), object: z.string(), next_page_url: z.string().nullable().optional(), previous_page_url: z.string().nullable().optional(), data: z.array(chargeResponse).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 }; /** * Get A List of Charges * * GET /charges */ export const getApiCharges = async (args: GetApiChargesArgs, options?: GetApiChargesOptions) => { const {baseUrl = "https://api.conekta.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.conekta-v2.3.0+json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args['X-Child-Company-Id'] != null) headers.set("X-Child-Company-Id", String(args['X-Child-Company-Id'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.search != null) query.append("search", String(args.search)) if (args.next != null) query.append("next", String(args.next)) if (args.previous != null) query.append("previous", String(args.previous)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/charges${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getChargesResponse, await res.json()) };