import {z} from 'zod' export type PaymentInitiationInput = {bankId: string, paymentScheme?: ('SEPA_CREDIT_TRANSFER' | 'MULTIBANCO' | 'DANISH_DOMESTIC_CREDIT_TRANSFER' | 'FASTER_PAYMENTS' | 'BACS' | 'BECS' | 'ACH' | null) | undefined, clientEvidence?: ({fingerprint?: string | undefined, timezone?: string | undefined, checkout_page_version?: string | undefined, payment_terms_agreement_id?: string | undefined, payment_page_url?: string | undefined, authorization_language?: string | undefined}) | undefined} | {customerBankAccountId: string, paymentScheme?: ('SEPA_CREDIT_TRANSFER' | 'MULTIBANCO' | 'DANISH_DOMESTIC_CREDIT_TRANSFER' | 'FASTER_PAYMENTS' | 'BACS' | 'BECS' | 'ACH' | null) | undefined, clientEvidence?: ({fingerprint?: string | undefined, timezone?: string | undefined, checkout_page_version?: string | undefined, payment_terms_agreement_id?: string | undefined, payment_page_url?: string | undefined, authorization_language?: string | undefined}) | undefined}; export type CreateApiApiV1EmbeddedPaymentInitiationArgs = {body: PaymentInitiationInput}; export type CreateApiApiV1EmbeddedPaymentInitiationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentInitiationResponse = z.object({success: z.boolean(), data: z.object({order_id: 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 }; /** * Initiate a payment for an embedded flow using payment_token (query or body) and selected bank * * POST /api/v1/embedded/payment-initiation */ export const createApiApiV1EmbeddedPaymentInitiation = async (args: CreateApiApiV1EmbeddedPaymentInitiationArgs, options?: CreateApiApiV1EmbeddedPaymentInitiationOptions) => { const {baseUrl = "https://core.quidkey.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(/\/+$/, '')}/api/v1/embedded/payment-initiation${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(paymentInitiationResponse, await res.json()) };