import {z} from 'zod' export type CreateApiPurchasesArgs = {'Content-Type'?: string | undefined, body: {amount: number, reference: string, customer_ip: string, currency: string, card_number: string, card_holder: string, card_expiry?: string | undefined, cryptogram?: string | undefined, network_eci?: string | undefined, test?: boolean | undefined, metadata?: Record | undefined, addendum_data?: string | undefined, capture?: boolean | undefined, extra?: ({ecm?: string | undefined}) | undefined}}; export type CreateApiPurchasesOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiPurchasesResponse = z.object({successful: z.boolean().optional(), response: z.object({authorization: z.string().optional(), id: z.string().optional(), card_number: z.string().optional(), card_holder: z.string().optional(), card_expiry: z.string().optional(), card_token: z.string().optional(), card_type: z.string().optional(), card_category: z.string().optional(), card_subcategory: z.string().optional(), amount: z.number().int().optional(), decimal_amount: z.number().optional(), successful: z.boolean().optional(), message: z.string().optional(), reference: z.string().optional(), currency: z.string().optional(), transaction_id: z.string().optional(), settlement_date: z.string().optional(), transaction_date: z.string().optional(), response_code: z.string().optional(), captured: z.boolean().optional(), captured_amount: z.number().int().optional(), rrn: z.string().optional(), cvv_match: z.string().optional(), metadata: z.object({}).optional(), addendum_data: z.object({}).optional()}).optional(), errors: z.array(z.unknown()).optional(), test: 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 purchase using a network token passthrough * * POST /purchases/ ​ */ export const createApiPurchases = async (args: CreateApiPurchasesArgs, options?: CreateApiPurchasesOptions) => { const {baseUrl = "https://gateway.pmnts-sandbox.io/v1.0", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/purchases/ ​${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(createApiPurchasesResponse, await res.json()) };