import {z} from 'zod' export type SchemasAcceptorKeyRef = string; export type SchemasMerchantKey = string; export type SchemasAcceptorRoute = string; export type AcceptorResolver = {merchantAcceptorKey: SchemasAcceptorKeyRef} | {merchantKey: SchemasMerchantKey, route?: SchemasAcceptorRoute | undefined}; export type Webhook = string; export type SchemasAmountValue = number; export type ComponentsSchemasCurrencyCode = 'EUR'; export type SchemasAmount = {value: SchemasAmountValue, currency: ComponentsSchemasCurrencyCode}; export type CompletionUrl = string; export type AuthenticationDescription = string; export type InitiateBEPRequest = {merchantAcceptorResolver: AcceptorResolver, webhook: Webhook, amount: SchemasAmount, completionUrl?: CompletionUrl | undefined, description?: AuthenticationDescription | undefined}; export type CreateApiBepArgs = {body: InitiateBEPRequest}; export type CreateApiBepOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const schemasVersion = z.number().int(); export const initiateBEPResponse = z.object({bepKey: z.string(), qrCode: z.string(), bancontactIntent: z.string(), expires: z.string(), created: z.string(), version: schemasVersion}); 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 BEP Authentication Intent * * POST /bep * * Also accepts auth: BearerToken */ export const createApiBep = async (args: CreateApiBepArgs, options?: CreateApiBepOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/bep${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(initiateBEPResponse, await res.json()) };