import {z} from 'zod' export type Currency = 'EUR'; export type VoucherScheme = string; export type VoucherEligibleAmount = {voucherSchemes: Array, amount: number}; export type PaymentCreateRequest = {reference?: string | undefined, bulkId?: string | undefined, amount: number, currency?: Currency | undefined, description?: string | undefined, identifyCallbackUrl?: string | undefined, callbackUrl?: string | undefined, returnUrl?: string | undefined, voucherEligibility?: Array | undefined}; export type CreateApiV3PaymentsArgs = {body: PaymentCreateRequest}; export type CreateApiV3PaymentsOptions = {baseUrl?: string | undefined, apiKeyPaymentProfile?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.literal("EUR"); export const paymentCreditorResponse = z.object({profileId: z.string().optional(), merchantId: z.string().optional(), name: z.string().optional(), iban: z.string().optional(), identifyCallbackUrl: z.string().optional(), callbackUrl: z.string().optional()}); export const link = z.object({href: z.string()}); export const links = z.object({self: link, deeplink: link, qrcode: link, cancel: link.optional(), refund: link.optional(), checkout: link.optional()}); export const paymentCreateResponse = z.object({paymentId: z.string().min(24).max(24), status: z.literal("PENDING"), createdAt: z.string(), expiresAt: z.string(), description: z.string().optional(), reference: z.string().optional(), amount: z.number().int().gt(1).lte(999999999999), currency: currency, creditor: paymentCreditorResponse, _links: links}); 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 payment * * POST /v3/payments * * Also accepts auth: JWS-Request-Signature-Payment */ export const createApiV3Payments = async (args: CreateApiV3PaymentsArgs, options?: CreateApiV3PaymentsOptions) => { const {baseUrl = "https://merchant.api.preprod.bancontact.net", fetch: fetchFn = fetch, apiKeyPaymentProfile} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyPaymentProfile !== undefined) headers.set("Authorization", apiKeyPaymentProfile) const url = `${baseUrl.replace(/\/+$/, '')}/v3/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(paymentCreateResponse, await res.json()) };