import {z} from 'zod' export type PaymentInstrumentMandateDebtorRequest = {iban: string, bic: string, account_holder_name: string}; export type CreatePaymentInstrumentMandateRequest = {type: 'sepa_direct_debit', debtor: PaymentInstrumentMandateDebtorRequest}; export type CreateApiAccountsAccountIdPaymentInstrumentMandatesArgs = {account_id: string, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string, 'Idempotency-Key'?: string | undefined, body: CreatePaymentInstrumentMandateRequest}; export type CreateApiAccountsAccountIdPaymentInstrumentMandatesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentInstrumentMandateType = z.literal("sepa_direct_debit"); export const paymentInstrumentMandateDebtorResponse = z.object({iban: z.string(), bic: z.string().optional(), account_holder_name: z.string()}); export const paymentInstrumentMandateCreditorAddressResponse = z.object({line_1: z.string(), line_2: z.string().optional(), postal_code: z.string(), city: z.string(), country: z.string()}); export const paymentInstrumentMandateCreditorResponse = z.object({identifier: z.string(), name: z.string(), address: paymentInstrumentMandateCreditorAddressResponse}); export const paymentInstrumentMandateScaResponse = z.object({required: z.boolean(), challenge: z.string().optional()}); export const paymentInstrumentMandateStatus = z.enum(["canceled", "confirmed", "created", "rejected"]); export const statusChangePaymentInstrumentMandateStatus = z.object({status: paymentInstrumentMandateStatus, timestamp: z.string()}); export const paymentInstrumentMandateResponse = z.object({id: z.string(), type: paymentInstrumentMandateType, mandate_reference: z.string(), debtor: paymentInstrumentMandateDebtorResponse, creditor: paymentInstrumentMandateCreditorResponse, sca: paymentInstrumentMandateScaResponse.optional(), history: z.array(statusChangePaymentInstrumentMandateStatus)}); 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 Payment Instrument Mandate * * POST /accounts/{account_id}/payment_instrument_mandates */ export const createApiAccountsAccountIdPaymentInstrumentMandates = async (args: CreateApiAccountsAccountIdPaymentInstrumentMandatesArgs, options?: CreateApiAccountsAccountIdPaymentInstrumentMandatesOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['LMG-Data-Privacy-Access-Principal'] != null) headers.set("LMG-Data-Privacy-Access-Principal", String(args['LMG-Data-Privacy-Access-Principal'])) if (args['LMG-Data-Privacy-Access-Justification'] != null) headers.set("LMG-Data-Privacy-Access-Justification", String(args['LMG-Data-Privacy-Access-Justification'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/payment_instrument_mandates${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(paymentInstrumentMandateResponse, await res.json()) };