import {z} from 'zod' export type CreateApiTransactionsArgs = {paypalFraudId?: string | undefined, 'x-vfi-api-idempotencyKey'?: string | undefined, body: BodyInit}; export type CreateApiTransactionsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const model28 = z.object({id: z.string(), createdAt: z.string(), status: z.enum(["INITIATED", "AUTHORISED"]).optional(), orderId: z.string().optional(), instoreReference: z.string(), approvalUrl: z.string().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 transaction * * POST /transactions * * Also accepts auth: BearerAuth */ export const createApiTransactions = async (args: CreateApiTransactionsArgs, options?: CreateApiTransactionsOptions) => { const {baseUrl = "https://emea.gsc.verifone.cloud/oidc/checkout-service", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "*/*") headers.set('accept', "application/json") if (args.paypalFraudId != null) headers.set("paypalFraudId", String(args.paypalFraudId)) if (args['x-vfi-api-idempotencyKey'] != null) headers.set("x-vfi-api-idempotencyKey", String(args['x-vfi-api-idempotencyKey'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/transactions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(model28, await res.json()) };