import {z} from 'zod' export type IbanBicThirdPartyBeneficiary = {type?: 'IBAN_BIC' | undefined, name: string, iban: string, bic: string, country: string, addressLine1: string, addressLine2?: string | undefined, addressLine3?: string | undefined, city: string, region?: string | undefined, postcode?: string | undefined}; export type AccountBicThirdPartyBeneficiary = {type: 'ACCOUNT_BIC', name: string, accountNumber: string, bic: string, country: string, addressLine1: string, addressLine2?: string | undefined, addressLine3?: string | undefined, city: string, region?: string | undefined, postcode?: string | undefined}; export type AbaThirdPartyBeneficiary = {type: 'ABA', name: string, accountNumber: string, abaRoutingNumber: string, country: string, addressLine1: string, addressLine2?: string | undefined, addressLine3?: string | undefined, city: string, region?: string | undefined, postcode?: string | undefined}; export type ThirdPartyPayment = {currency: string, amount: string, reference: string, nonce: string, reason: 'BKFE' | 'BKIP' | 'BKPP' | 'CBLK' | 'INTC' | 'INTP' | 'DEEPT' | 'ACCTCASH' | 'COLL' | 'CSDB' | 'LIMA' | 'NETT' | 'EXTD' | 'OTCD' | 'REPO' | 'SBSC' | 'SLEB' | 'ARGT' | 'AREN' | 'BEXP' | 'BOCE' | 'COMC' | 'LICF' | 'CPYR' | 'SERV' | 'SUPP' | 'TRAD' | 'SUBS' | 'GDSV' | 'GSCB' | 'ROYA' | 'COMT' | 'CHAR' | 'ECPR' | 'EPAY' | 'COMP' | 'DBTC' | 'CLPR' | 'INTE' | 'LOAN' | 'LOAR' | 'RELG' | 'TRFD' | 'PENO' | 'INPC' | 'INPR' | 'INSC' | 'INSU' | 'LBRI' | 'LIFI' | 'PPTI' | 'HLRP' | 'HLST' | 'PLDS' | 'PDEP' | 'PCOM' | 'PLRF' | 'FORW' | 'FXNT' | 'IVPT' | 'REBT' | 'REFU' | 'CDBL' | 'CPKC' | 'EDUC' | 'FEES' | 'GAMB' | 'LOTT' | 'GIFT' | 'INSM' | 'REOD' | 'FAND' | 'GOVT' | 'TCSC' | 'BDLM' | 'RENT' | 'BNET' | 'CORT' | 'CMDT' | 'DERI' | 'DIVD' | 'FREX' | 'HEDG' | 'INVS' | 'SAVG' | 'SECU' | 'TREA' | 'FNET' | 'FUTR' | 'HLTI' | 'DNTS' | 'LTCF' | 'MDCS' | 'VIEW' | 'SWFP' | 'SWPP' | 'SWRS' | 'SWUF' | 'BECH' | 'BENE' | 'SSBE' | 'PEFC' | 'PENS' | 'ADCS' | 'ALMY' | 'BONU' | 'COMM' | 'HREC' | 'SALA' | 'LREB' | 'LREV' | 'RSTX' | 'HSTX' | 'INTX' | 'PTXP' | 'RDTX' | 'TAXS' | 'VATX' | 'WHLD' | 'TAXR' | 'CBTV' | 'ELEC' | 'GASB' | 'PHON' | 'UTLB' | 'WTER', preferredScheme?: ('AUTO' | 'SEPA' | 'SEPA_INSTANT' | 'TARGET2' | 'SWIFT') | undefined, beneficiary: IbanBicThirdPartyBeneficiary | AccountBicThirdPartyBeneficiary | AbaThirdPartyBeneficiary}; export type CreateApiV1AccountsAccountIdVirtualVirtualAccountIdThirdPartyPaymentArgs = {accountId: number, virtualAccountId: string, body: ThirdPartyPayment}; export type CreateApiV1AccountsAccountIdVirtualVirtualAccountIdThirdPartyPaymentOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const virtualAccountPaymentResponse = z.object({transactionId: z.string(), end2EndId: z.string()}); 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 }; /** * Make a Third Party Payment * * POST /v1/accounts/{accountId}/virtual/{virtualAccountId}/third-party-payment */ export const createApiV1AccountsAccountIdVirtualVirtualAccountIdThirdPartyPayment = async (args: CreateApiV1AccountsAccountIdVirtualVirtualAccountIdThirdPartyPaymentArgs, options?: CreateApiV1AccountsAccountIdVirtualVirtualAccountIdThirdPartyPaymentOptions) => { 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(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.accountId))}/virtual/${encodeURIComponent(String(args.virtualAccountId))}/third-party-payment${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(virtualAccountPaymentResponse, await res.json()) };