import {z} from 'zod' export type MoneyFeeV2 = {amount: number, currency: string}; export type AddressCountryCode = string; export type PaymentRuleAddress = {addressLine1: string, addressLine2?: string | undefined, postalCode: string, city: string, country: AddressCountryCode, stateCode?: string | undefined}; export type PaymentRuleIndividual = {type: 'INDIVIDUAL', customerIdentifier: string, firstName: string, lastName: string, dateOfBirth?: string | undefined, address?: PaymentRuleAddress | undefined, relationshipType: 'SELF_OWNED' | 'THIRD_PARTY'}; export type PaymentRuleCompany = {type: 'COMPANY', customerIdentifier: string, legalName: string, registrationNumber?: string | undefined, address?: PaymentRuleAddress | undefined, relationshipType: 'SELF_OWNED' | 'THIRD_PARTY'}; export type BlockchainNetwork = 'TRON' | 'ETHEREUM' | 'LITECOIN' | 'BITCOIN' | 'RIPPLE' | 'DOGECOIN' | 'SOLANA' | 'ALGORAND' | 'CARDANO' | 'BINANCE' | 'POLYGON' | 'BITCOIN_CASH' | 'ARC' | 'ARBITRUM' | 'BASE'; export type PaymentRuleCryptoAddress = {network: BlockchainNetwork, address: string, tag?: string | undefined}; export type CreateApiPaymentV1RulesArgs = {body: {reference: string, trigger: string, walletId: string, fees?: ({customerFee?: MoneyFeeV2 | undefined}) | undefined, beneficiary: {currency?: string | undefined, entity: PaymentRuleIndividual | PaymentRuleCompany, cryptoAddress: PaymentRuleCryptoAddress}}}; export type CreateApiPaymentV1RulesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.record(z.string(), z.string().max(100)); export const createApiPaymentV1RulesResponse = z.object({id: z.string().optional(), reference: z.string().optional(), trigger: z.string().optional(), status: z.string().optional(), fees: z.object({customerFee: z.object({amount: z.number().optional(), currency: z.string().optional()}).optional()}).optional(), originator: z.object({currency: z.string().optional(), walletId: z.string().optional()}).optional(), beneficiary: z.object({currency: z.string().optional(), entity: z.object({legalName: z.string().optional(), type: z.string().optional(), relationshipType: z.string().optional(), registrationNumber: z.string().optional(), address: z.object({addressLine1: z.string().optional(), addressLine2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), postCode: z.string().optional(), country: z.string().optional()}).optional()}).optional(), cryptoAddresses: z.object({network: z.string().optional(), addresses: z.array(z.string()).optional(), tag: z.string().optional()}).optional()}).optional(), metadata: metadata.optional(), updatedAt: z.string().optional(), createdAt: 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 }; /** * Create on-ramp payment rule * * POST /payment/v1/rules */ export const createApiPaymentV1Rules = async (args: CreateApiPaymentV1RulesArgs, options?: CreateApiPaymentV1RulesOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/payment/v1/rules${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(createApiPaymentV1RulesResponse, await res.json()) };