import {z} from 'zod' export type CardBrand = 'VISA' | 'MASTERCARD' | 'VERVE'; export type CreateCardAccountRequest = {accountId: string, displayName: string, cardPackageId?: string | undefined, cardBrand?: CardBrand | undefined, initialFundingAmount: number, dailyLimit?: number | undefined, transactionLimit?: number | undefined, whitelistedMccs?: Array | undefined, blacklistedMccs?: Array | undefined, contactlessPaymentEnabled?: boolean | undefined}; export type CreateApiLedgerCardAccountsArgs = {body: CreateCardAccountRequest}; export type CreateApiLedgerCardAccountsOptions = {baseUrl?: string | undefined, HMACAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cardAccountStatus = z.enum(["PENDING_KYC", "PENDING_PROVISIONING", "ACTIVE", "FROZEN", "TERMINATED", "KYC_REJECTED", "CREATION_FAILED"]); export const cardSpendControls = z.object({dailyLimit: z.number().optional(), transactionLimit: z.number().optional(), whitelistedMccs: z.array(z.string()).optional(), blacklistedMccs: z.array(z.string()).optional(), contactlessPaymentEnabled: z.boolean().optional()}); export const cardBrand = z.enum(["VISA", "MASTERCARD", "VERVE"]); export const cardBillingAddress = z.object({address: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), postalCode: z.string().optional(), zipCode: z.string().optional(), houseNo: z.string().optional()}); export const cardDetails = z.object({displayName: z.string().optional(), cardBrand: cardBrand.optional(), cardType: z.string().optional(), cardHolderName: z.string().optional(), maskedCardNumber: z.string().optional(), expiryMonth: z.string().optional(), expiryYear: z.string().optional(), billingAddress: cardBillingAddress.optional(), encryptedCardDetails: z.string().optional()}); export const cardAccount = z.object({id: z.string(), accountId: z.string(), currency: z.string(), status: cardAccountStatus, balance: z.number(), failureReason: z.string().optional(), spendControls: cardSpendControls.optional(), createdDate: z.string(), cardDetails: cardDetails.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 a corporate card. * * POST /ledger/card-accounts */ export const createApiLedgerCardAccounts = async (args: CreateApiLedgerCardAccountsArgs, options?: CreateApiLedgerCardAccountsOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch, HMACAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HMACAuth !== undefined) headers.set("x-api-key", HMACAuth) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/card-accounts${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(cardAccount, await res.json()) };