import {z} from 'zod' export type CreateApiOrderPaymentsV3Args = {body: {merchant_api_key?: string | undefined, intent?: string | undefined, mode?: string | undefined, currency?: string | undefined, amount?: number | undefined, entry_mode?: string | undefined, metadata?: ({source?: string | undefined, order_id?: string | undefined}) | undefined}}; export type CreateApiOrderPaymentsV3Options = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const status = z.object({errors: z.array(z.unknown()).optional(), message: z.string().optional()}); export const createApiOrderPaymentsV3Response = z.object({data: z.object({tracker: z.object({token: z.string().optional(), client: z.string().optional(), environment: z.string().optional(), state: z.string().optional(), intent: z.string().optional(), mode: z.string().optional(), entry_mode: z.string().optional(), next_actions: z.object({CYBERSOURCE: z.object({kind: z.string().optional()}).optional(), MPGS: z.object({kind: z.string().optional()}).optional()}).optional(), purchase_totals: z.object({quote_amount: z.object({currency: z.string().optional(), amount: z.number().int().optional()}).optional(), base_amount: z.object({currency: z.string().optional(), amount: z.number().int().optional()}).optional(), conversion_rate: z.object({base_currency: z.string().optional(), quote_currency: z.string().optional(), rate: z.number().int().optional()}).optional()}).optional(), metadata: z.object({}).optional()}).optional(), capabilities: z.object({CYBERSOURCE: z.boolean().optional(), MPGS: z.boolean().optional()}).optional()}).optional(), status: status.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 }; /** * Payment * * POST /order/payments/v3 */ export const createApiOrderPaymentsV3 = async (args: CreateApiOrderPaymentsV3Args, options?: CreateApiOrderPaymentsV3Options) => { const {baseUrl = "https://api.sandbox.safepay.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/order/payments/v3${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(createApiOrderPaymentsV3Response, await res.json()) };