import {z} from 'zod' export type IntermediaryBank = {bic?: string | undefined, routing_number?: string | undefined}; export type AuthorisePaymentRequest = {counterparty_id: number, sender_account_id: string, ccy: string, amount: string, reference: string, reason: string, notes?: string | undefined, preferred_scheme: 'AUTO' | 'BLINC' | 'INTERNAL', nonce: string, beneficiary_account_id?: number | undefined, beneficiary_name?: string | undefined, beneficiary_address_line_1?: string | undefined, beneficiary_address_line_2?: string | undefined, beneficiary_address_line_3?: string | undefined, beneficiary_city?: string | undefined, beneficiary_region?: string | undefined, beneficiary_postcode?: string | undefined, beneficiary_country?: string | undefined, beneficiary_account_number?: string | undefined, beneficiary_sort_code?: string | undefined, beneficiary_bic?: string | undefined, beneficiary_iban?: string | undefined, beneficiary_routing_number?: string | undefined, beneficiary_wire_routing?: string | undefined, beneficiary_swift_code?: string | undefined, beneficiary_national_clearing_code?: string | undefined, beneficiary_institution?: string | undefined, beneficiary_branch?: string | undefined, beneficiary_lei_code?: string | undefined, beneficiary_bank_name?: string | undefined, beneficiary_bank_address?: string | undefined, beneficiary_bank_country?: string | undefined, intermediary_bank?: IntermediaryBank | undefined}; export type CreateApiV5PaymentsAuthoriseArgs = {body: AuthorisePaymentRequest}; export type CreateApiV5PaymentsAuthoriseOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const authorisePaymentResponse = z.object({endToEndIdentification: z.string(), transactionId: z.string(), nonce: z.string(), response: z.string()}); export const createApiV5PaymentsAuthoriseResponse = z.array(authorisePaymentResponse); 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 }; /** * Authorise Payment * * POST /v5/payments/authorise */ export const createApiV5PaymentsAuthorise = async (args: CreateApiV5PaymentsAuthoriseArgs, options?: CreateApiV5PaymentsAuthoriseOptions) => { const {baseUrl = "https://api.bcb.group", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v5/payments/authorise${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(createApiV5PaymentsAuthoriseResponse, await res.json()) };