import {z} from 'zod' export type Amount = {currency: string, value: number}; export type CapabilitySettings = {amountPerIndustry?: {[key: string]: Amount} | undefined, authorizedCardUsers?: boolean | undefined, fundingSource?: (Array<'credit' | 'debit' | 'prepaid'>) | undefined, interval?: ('daily' | 'monthly' | 'weekly') | undefined, maxAmount?: Amount | undefined}; export type CapabilityProblemEntityRecursive = {documents?: Array | undefined, id?: string | undefined, type?: ('BankAccount' | 'Document' | 'LegalEntity') | undefined}; export type CapabilityProblemEntity = {documents?: Array | undefined, id?: string | undefined, owner?: CapabilityProblemEntityRecursive | undefined, type?: ('BankAccount' | 'Document' | 'LegalEntity') | undefined}; export type RemediatingAction = {code?: string | undefined, message?: string | undefined}; export type VerificationErrorRecursive = {capabilities?: (Array<'acceptExternalFunding' | 'acceptPspFunding' | 'acceptTransactionInRestrictedCountries' | 'acceptTransactionInRestrictedCountriesCommercial' | 'acceptTransactionInRestrictedCountriesConsumer' | 'acceptTransactionInRestrictedIndustries' | 'acceptTransactionInRestrictedIndustriesCommercial' | 'acceptTransactionInRestrictedIndustriesConsumer' | 'acquiring' | 'atmWithdrawal' | 'atmWithdrawalCommercial' | 'atmWithdrawalConsumer' | 'atmWithdrawalInRestrictedCountries' | 'atmWithdrawalInRestrictedCountriesCommercial' | 'atmWithdrawalInRestrictedCountriesConsumer' | 'authorisedPaymentInstrumentUser' | 'getGrantOffers' | 'issueBankAccount' | 'issueCard' | 'issueCardCommercial' | 'issueCardConsumer' | 'issueChargeCard' | 'issueChargeCardCommercial' | 'issueCreditLimit' | 'localAcceptance' | 'payout' | 'payoutToTransferInstrument' | 'processing' | 'receiveFromBalanceAccount' | 'receiveFromPlatformPayments' | 'receiveFromThirdParty' | 'receiveFromTransferInstrument' | 'receiveGrants' | 'receivePayments' | 'sendToBalanceAccount' | 'sendToThirdParty' | 'sendToTransferInstrument' | 'thirdPartyFunding' | 'useCard' | 'useCardCommercial' | 'useCardConsumer' | 'useCardInRestrictedCountries' | 'useCardInRestrictedCountriesCommercial' | 'useCardInRestrictedCountriesConsumer' | 'useCardInRestrictedIndustries' | 'useCardInRestrictedIndustriesCommercial' | 'useCardInRestrictedIndustriesConsumer' | 'useChargeCard' | 'useChargeCardCommercial' | 'withdrawFromAtm' | 'withdrawFromAtmCommercial' | 'withdrawFromAtmConsumer' | 'withdrawFromAtmInRestrictedCountries' | 'withdrawFromAtmInRestrictedCountriesCommercial' | 'withdrawFromAtmInRestrictedCountriesConsumer'>) | undefined, code?: string | undefined, message?: string | undefined, type?: ('dataMissing' | 'dataReview' | 'invalidInput' | 'pendingStatus') | undefined, remediatingActions?: Array | undefined}; export type VerificationError = {capabilities?: (Array<'acceptExternalFunding' | 'acceptPspFunding' | 'acceptTransactionInRestrictedCountries' | 'acceptTransactionInRestrictedCountriesCommercial' | 'acceptTransactionInRestrictedCountriesConsumer' | 'acceptTransactionInRestrictedIndustries' | 'acceptTransactionInRestrictedIndustriesCommercial' | 'acceptTransactionInRestrictedIndustriesConsumer' | 'acquiring' | 'atmWithdrawal' | 'atmWithdrawalCommercial' | 'atmWithdrawalConsumer' | 'atmWithdrawalInRestrictedCountries' | 'atmWithdrawalInRestrictedCountriesCommercial' | 'atmWithdrawalInRestrictedCountriesConsumer' | 'authorisedPaymentInstrumentUser' | 'getGrantOffers' | 'issueBankAccount' | 'issueCard' | 'issueCardCommercial' | 'issueCardConsumer' | 'issueChargeCard' | 'issueChargeCardCommercial' | 'issueCreditLimit' | 'localAcceptance' | 'payout' | 'payoutToTransferInstrument' | 'processing' | 'receiveFromBalanceAccount' | 'receiveFromPlatformPayments' | 'receiveFromThirdParty' | 'receiveFromTransferInstrument' | 'receiveGrants' | 'receivePayments' | 'sendToBalanceAccount' | 'sendToThirdParty' | 'sendToTransferInstrument' | 'thirdPartyFunding' | 'useCard' | 'useCardCommercial' | 'useCardConsumer' | 'useCardInRestrictedCountries' | 'useCardInRestrictedCountriesCommercial' | 'useCardInRestrictedCountriesConsumer' | 'useCardInRestrictedIndustries' | 'useCardInRestrictedIndustriesCommercial' | 'useCardInRestrictedIndustriesConsumer' | 'useChargeCard' | 'useChargeCardCommercial' | 'withdrawFromAtm' | 'withdrawFromAtmCommercial' | 'withdrawFromAtmConsumer' | 'withdrawFromAtmInRestrictedCountries' | 'withdrawFromAtmInRestrictedCountriesCommercial' | 'withdrawFromAtmInRestrictedCountriesConsumer'>) | undefined, code?: string | undefined, message?: string | undefined, remediatingActions?: Array | undefined, subErrors?: Array | undefined, type?: ('dataMissing' | 'dataReview' | 'invalidInput' | 'pendingStatus') | undefined}; export type CapabilityProblem = {entity?: CapabilityProblemEntity | undefined, verificationErrors?: Array | undefined}; export type AccountSupportingEntityCapability = {allowed?: boolean | undefined, allowedLevel?: ('high' | 'low' | 'medium' | 'notApplicable') | undefined, enabled?: boolean | undefined, id?: string | undefined, requested?: boolean | undefined, requestedLevel?: ('high' | 'low' | 'medium' | 'notApplicable') | undefined, verificationStatus?: ('invalid' | 'pending' | 'rejected' | 'valid') | undefined}; export type AccountHolderCapability = {allowed?: boolean | undefined, allowedLevel?: ('high' | 'low' | 'medium' | 'notApplicable') | undefined, allowedSettings?: CapabilitySettings | undefined, enabled?: boolean | undefined, problems?: Array | undefined, requested?: boolean | undefined, requestedLevel?: ('high' | 'low' | 'medium' | 'notApplicable') | undefined, requestedSettings?: CapabilitySettings | undefined, transferInstruments?: Array | undefined, verificationStatus?: ('invalid' | 'pending' | 'rejected' | 'valid') | undefined}; export type Address = {city: string, country: string, houseNumberOrName: string, postalCode: string, stateOrProvince?: string | undefined, street: string}; export type Phone = {number: string, type: 'landline' | 'mobile'}; export type ContactDetails = {address: Address, email: string, phone: Phone, webAddress?: string | undefined}; export type AccountHolderInfo = {balancePlatform?: string | undefined, capabilities?: {[key: string]: AccountHolderCapability} | undefined, contactDetails?: ContactDetails | undefined, description?: string | undefined, legalEntityId: string, metadata?: {[key: string]: string} | undefined, migratedAccountHolderCode?: string | undefined, reference?: string | undefined, timeZone?: string | undefined}; export type CreateApiAccountHoldersArgs = {body: AccountHolderInfo}; export type CreateApiAccountHoldersOptions = {baseUrl?: string | undefined, clientKey?: string | undefined, fetch?: typeof fetch | undefined}; export const amount = z.object({currency: z.string(), value: z.number().int()}); export const capabilitySettings = z.object({amountPerIndustry: z.record(z.string(), amount).optional(), authorizedCardUsers: z.boolean().optional(), fundingSource: z.array(z.enum(["credit", "debit", "prepaid"])).optional(), interval: z.enum(["daily", "monthly", "weekly"]).optional(), maxAmount: amount.optional()}); export const capabilityProblemEntityRecursive = z.object({documents: z.array(z.string()).optional(), id: z.string().optional(), type: z.enum(["BankAccount", "Document", "LegalEntity"]).optional()}); export const capabilityProblemEntity = z.object({documents: z.array(z.string()).optional(), id: z.string().optional(), owner: capabilityProblemEntityRecursive.optional(), type: z.enum(["BankAccount", "Document", "LegalEntity"]).optional()}); export const remediatingAction = z.object({code: z.string().optional(), message: z.string().optional()}); export const verificationErrorRecursive = z.object({capabilities: z.array(z.enum(["acceptExternalFunding", "acceptPspFunding", "acceptTransactionInRestrictedCountries", "acceptTransactionInRestrictedCountriesCommercial", "acceptTransactionInRestrictedCountriesConsumer", "acceptTransactionInRestrictedIndustries", "acceptTransactionInRestrictedIndustriesCommercial", "acceptTransactionInRestrictedIndustriesConsumer", "acquiring", "atmWithdrawal", "atmWithdrawalCommercial", "atmWithdrawalConsumer", "atmWithdrawalInRestrictedCountries", "atmWithdrawalInRestrictedCountriesCommercial", "atmWithdrawalInRestrictedCountriesConsumer", "authorisedPaymentInstrumentUser", "getGrantOffers", "issueBankAccount", "issueCard", "issueCardCommercial", "issueCardConsumer", "issueChargeCard", "issueChargeCardCommercial", "issueCreditLimit", "localAcceptance", "payout", "payoutToTransferInstrument", "processing", "receiveFromBalanceAccount", "receiveFromPlatformPayments", "receiveFromThirdParty", "receiveFromTransferInstrument", "receiveGrants", "receivePayments", "sendToBalanceAccount", "sendToThirdParty", "sendToTransferInstrument", "thirdPartyFunding", "useCard", "useCardCommercial", "useCardConsumer", "useCardInRestrictedCountries", "useCardInRestrictedCountriesCommercial", "useCardInRestrictedCountriesConsumer", "useCardInRestrictedIndustries", "useCardInRestrictedIndustriesCommercial", "useCardInRestrictedIndustriesConsumer", "useChargeCard", "useChargeCardCommercial", "withdrawFromAtm", "withdrawFromAtmCommercial", "withdrawFromAtmConsumer", "withdrawFromAtmInRestrictedCountries", "withdrawFromAtmInRestrictedCountriesCommercial", "withdrawFromAtmInRestrictedCountriesConsumer"])).optional(), code: z.string().optional(), message: z.string().optional(), type: z.enum(["dataMissing", "dataReview", "invalidInput", "pendingStatus"]).optional(), remediatingActions: z.array(remediatingAction).optional()}); export const verificationError = z.object({capabilities: z.array(z.enum(["acceptExternalFunding", "acceptPspFunding", "acceptTransactionInRestrictedCountries", "acceptTransactionInRestrictedCountriesCommercial", "acceptTransactionInRestrictedCountriesConsumer", "acceptTransactionInRestrictedIndustries", "acceptTransactionInRestrictedIndustriesCommercial", "acceptTransactionInRestrictedIndustriesConsumer", "acquiring", "atmWithdrawal", "atmWithdrawalCommercial", "atmWithdrawalConsumer", "atmWithdrawalInRestrictedCountries", "atmWithdrawalInRestrictedCountriesCommercial", "atmWithdrawalInRestrictedCountriesConsumer", "authorisedPaymentInstrumentUser", "getGrantOffers", "issueBankAccount", "issueCard", "issueCardCommercial", "issueCardConsumer", "issueChargeCard", "issueChargeCardCommercial", "issueCreditLimit", "localAcceptance", "payout", "payoutToTransferInstrument", "processing", "receiveFromBalanceAccount", "receiveFromPlatformPayments", "receiveFromThirdParty", "receiveFromTransferInstrument", "receiveGrants", "receivePayments", "sendToBalanceAccount", "sendToThirdParty", "sendToTransferInstrument", "thirdPartyFunding", "useCard", "useCardCommercial", "useCardConsumer", "useCardInRestrictedCountries", "useCardInRestrictedCountriesCommercial", "useCardInRestrictedCountriesConsumer", "useCardInRestrictedIndustries", "useCardInRestrictedIndustriesCommercial", "useCardInRestrictedIndustriesConsumer", "useChargeCard", "useChargeCardCommercial", "withdrawFromAtm", "withdrawFromAtmCommercial", "withdrawFromAtmConsumer", "withdrawFromAtmInRestrictedCountries", "withdrawFromAtmInRestrictedCountriesCommercial", "withdrawFromAtmInRestrictedCountriesConsumer"])).optional(), code: z.string().optional(), message: z.string().optional(), remediatingActions: z.array(remediatingAction).optional(), subErrors: z.array(verificationErrorRecursive).optional(), type: z.enum(["dataMissing", "dataReview", "invalidInput", "pendingStatus"]).optional()}); export const capabilityProblem = z.object({entity: capabilityProblemEntity.optional(), verificationErrors: z.array(verificationError).optional()}); export const accountSupportingEntityCapability = z.object({allowed: z.boolean().optional(), allowedLevel: z.enum(["high", "low", "medium", "notApplicable"]).optional(), enabled: z.boolean().optional(), id: z.string().optional(), requested: z.boolean().optional(), requestedLevel: z.enum(["high", "low", "medium", "notApplicable"]).optional(), verificationStatus: z.enum(["invalid", "pending", "rejected", "valid"]).optional()}); export const accountHolderCapability = z.object({allowed: z.boolean().optional(), allowedLevel: z.enum(["high", "low", "medium", "notApplicable"]).optional(), allowedSettings: capabilitySettings.optional(), enabled: z.boolean().optional(), problems: z.array(capabilityProblem).optional(), requested: z.boolean().optional(), requestedLevel: z.enum(["high", "low", "medium", "notApplicable"]).optional(), requestedSettings: capabilitySettings.optional(), transferInstruments: z.array(accountSupportingEntityCapability).optional(), verificationStatus: z.enum(["invalid", "pending", "rejected", "valid"]).optional()}); export const address = z.object({city: z.string().max(3000), country: z.string(), houseNumberOrName: z.string().max(3000), postalCode: z.string(), stateOrProvince: z.string().optional(), street: z.string().max(3000)}); export const phone = z.object({number: z.string(), type: z.enum(["landline", "mobile"])}); export const contactDetails = z.object({address: address, email: z.string(), phone: phone, webAddress: z.string().optional()}); export const verificationDeadline = z.object({capabilities: z.array(z.enum(["acceptExternalFunding", "acceptPspFunding", "acceptTransactionInRestrictedCountries", "acceptTransactionInRestrictedCountriesCommercial", "acceptTransactionInRestrictedCountriesConsumer", "acceptTransactionInRestrictedIndustries", "acceptTransactionInRestrictedIndustriesCommercial", "acceptTransactionInRestrictedIndustriesConsumer", "acquiring", "atmWithdrawal", "atmWithdrawalCommercial", "atmWithdrawalConsumer", "atmWithdrawalInRestrictedCountries", "atmWithdrawalInRestrictedCountriesCommercial", "atmWithdrawalInRestrictedCountriesConsumer", "authorisedPaymentInstrumentUser", "getGrantOffers", "issueBankAccount", "issueCard", "issueCardCommercial", "issueCardConsumer", "issueChargeCard", "issueChargeCardCommercial", "issueCreditLimit", "localAcceptance", "payout", "payoutToTransferInstrument", "processing", "receiveFromBalanceAccount", "receiveFromPlatformPayments", "receiveFromThirdParty", "receiveFromTransferInstrument", "receiveGrants", "receivePayments", "sendToBalanceAccount", "sendToThirdParty", "sendToTransferInstrument", "thirdPartyFunding", "useCard", "useCardCommercial", "useCardConsumer", "useCardInRestrictedCountries", "useCardInRestrictedCountriesCommercial", "useCardInRestrictedCountriesConsumer", "useCardInRestrictedIndustries", "useCardInRestrictedIndustriesCommercial", "useCardInRestrictedIndustriesConsumer", "useChargeCard", "useChargeCardCommercial", "withdrawFromAtm", "withdrawFromAtmCommercial", "withdrawFromAtmConsumer", "withdrawFromAtmInRestrictedCountries", "withdrawFromAtmInRestrictedCountriesCommercial", "withdrawFromAtmInRestrictedCountriesConsumer"])), entityIds: z.array(z.string()).optional(), expiresAt: z.string()}); export const accountHolder = z.object({balancePlatform: z.string().optional(), capabilities: z.record(z.string(), accountHolderCapability).optional(), contactDetails: contactDetails.optional(), description: z.string().max(300).optional(), id: z.string(), legalEntityId: z.string(), metadata: z.record(z.string(), z.string()).optional(), migratedAccountHolderCode: z.string().optional(), primaryBalanceAccount: z.string().optional(), reference: z.string().max(150).optional(), status: z.enum(["active", "closed", "suspended"]).optional(), timeZone: z.string().optional(), verificationDeadlines: z.array(verificationDeadline).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 }; /** * Create an account holder * * POST /accountHolders * * Also accepts auth: BasicAuth, ApiKeyAuth */ export const createApiAccountHolders = async (args: CreateApiAccountHoldersArgs, options?: CreateApiAccountHoldersOptions) => { const {baseUrl = "https://balanceplatform-api-test.adyen.com/bcl/v2", fetch: fetchFn = fetch, clientKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (clientKey !== undefined) query.set("clientKey", clientKey) const url = `${baseUrl.replace(/\/+$/, '')}/accountHolders${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(accountHolder, await res.json()) };