import {z} from 'zod' export type PayInDetailDto = {currency: string, network?: string | undefined, protocol?: string | undefined}; export type PayRequestCurrencyOptionDto = {code: string, protocols?: Array | undefined}; export type EntityTypeEnumDto = 'INDIVIDUAL' | 'COMPANY'; export type IndividualPartyDetailDto = {type: string, entityType: EntityTypeEnumDto, firstName: string, lastName: string, dateOfBirth: string, relationshipType: string, countryCode: string, regionCode?: unknown | undefined}; export type CompanyPartyDetailDto = {type: string, entityType: EntityTypeEnumDto, legalName: string, relationshipType: string, countryCode: string, regionCode?: unknown | undefined, registrationNumber: string}; export type PartyDetailDto = IndividualPartyDetailDto | CompanyPartyDetailDto; export type ComplianceDetailDto = {requesterIpAddress?: string | undefined, contactId?: string | undefined, partyDetails: Array}; export type Metadata = {[key: string]: string}; export type PayRequestInDto = {walletId: string, merchantId?: string | undefined, amount: number, expiryMinutes?: number | undefined, currency: string, returnUrl?: string | undefined, reference: string, customerId: string, type: 'IN', payInDetails: PayInDetailDto, currencyOptions?: Array | undefined, complianceDetails: ComplianceDetailDto, metadata?: Metadata | undefined, embeddedCustomerDetails?: ({reference?: string | undefined}) | undefined}; export type PayOutDetailDto = {code: string, currency: string, address: string, tag?: string | undefined, network: string, protocol?: string | undefined}; export type PayRequestOutDto = {walletId: string, merchantId?: string | undefined, amount: number, expiryMinutes?: number | undefined, currency: string, returnUrl?: string | undefined, reference: string, customerId: string, type: 'OUT', twoStep?: boolean | undefined, flow?: 'EMBEDDED_CRYPTO' | undefined, fees?: ({customerFee?: {currency: string, amount: number} | undefined}) | undefined, payOutDetails?: PayOutDetailDto | undefined, complianceDetails: ComplianceDetailDto, metadata?: Metadata | undefined, embeddedCustomerDetails?: {reference: string} | undefined}; export type PayRequestDto = PayRequestInDto | PayRequestOutDto; export type CreateApiApiV1PaySummaryDryRunArgs = {body: PayRequestDto}; export type CreateApiApiV1PaySummaryDryRunOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const directionDto = z.enum(["IN", "OUT"]); export const paymentStatusDto = z.enum(["PENDING", "PROCESSING", "CANCELLED", "COMPLETE", "UNDERPAID", "OVERPAID", "EXPIRED"]); export const payAmountsDto = z.object({currency: z.string().optional(), amount: z.number().optional(), actual: z.number().optional()}); export const exchangeRateDto = z.object({base: z.string().optional(), counter: z.string().optional(), rate: z.number().optional()}); export const alternativeAddressDto = z.object({network: z.string().optional(), protocol: z.string().optional(), address: z.string().optional(), tag: z.string().optional(), uri: z.string().optional()}); export const cryptoAddressDto = z.object({address: z.string().optional(), tag: z.string().optional(), network: z.string().optional(), protocol: z.string().optional(), uri: z.string().optional(), alternatives: z.array(alternativeAddressDto).optional()}); export const gatewayTransactionDto = z.object({dateCreated: z.number().int().optional(), dateConfirmed: z.number().int().optional(), hash: z.string().optional(), amount: z.number().optional(), risk: z.object({}).optional(), networkFeeCurrency: z.string().optional(), networkFeeAmount: z.number().optional(), sources: z.array(z.string()).optional(), displayRate: exchangeRateDto.optional(), exchangeRate: exchangeRateDto.optional()}); export const currencyOptions = z.object({transaction: z.string().optional(), explorer: z.string().optional(), address: z.string().optional(), confirmations: z.number().int().optional()}); export const summaryPaymentDto = z.object({uuid: z.string().max(50).optional(), merchantDisplayName: z.string().max(255).optional(), walletId: z.string().max(255).optional(), dateCreated: z.number().int().optional(), expiryDate: z.number().int().optional(), quoteExpiryDate: z.number().int().optional(), acceptanceExpiryDate: z.number().int().optional(), quoteStatus: z.string().max(50).optional(), reference: z.string().min(6).max(255).optional(), type: directionDto.optional(), subType: z.enum(["merchantPayIn", "merchantPayOut", "merchantRefund"]).max(50).optional(), status: paymentStatusDto.optional(), displayCurrency: payAmountsDto.optional(), walletCurrency: payAmountsDto.optional(), paidCurrency: payAmountsDto.optional(), feeCurrency: payAmountsDto.optional(), displayRate: exchangeRateDto.optional(), exchangeRate: exchangeRateDto.optional(), address: cryptoAddressDto.optional(), returnUrl: z.string().optional(), redirectUrl: z.string().optional(), transactions: z.array(gatewayTransactionDto).optional(), refund: z.object({}).optional(), refunds: z.array(z.object({})).optional(), currencyOptions: currencyOptions.optional(), networkFeeCurrency: z.object({currency: z.string().optional(), amount: z.number().optional(), actual: z.number().optional()}).optional(), flow: 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 }; /** * Preview payment * * POST /api/v1/pay/summary/dry-run */ export const createApiApiV1PaySummaryDryRun = async (args: CreateApiApiV1PaySummaryDryRunArgs, options?: CreateApiApiV1PaySummaryDryRunOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/pay/summary/dry-run${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(summaryPaymentDto, await res.json()) };