import {z} from 'zod' export type PutAutoRechargeSdkDto = {currency: string, customerEmail?: string | undefined, customerName?: string | undefined, customerRef: string, deferSetupIntent?: boolean | undefined, enabled: boolean, maxMonthlySpendMajor?: number | undefined, thresholdAmountMajor?: number | undefined, topupAmountMajor?: number | undefined, triggerType: 'balance'}; export type UpdateApiV1SdkAutoRechargeArgs = {body: PutAutoRechargeSdkDto}; export type UpdateApiV1SdkAutoRechargeOptions = {baseUrl?: string | undefined, SecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const autoRechargeTopupDto = z.object({amountMinor: z.number(), currency: z.string(), mode: z.literal("fixed")}); export const autoRechargeTriggerDto = z.object({thresholdAmountMinor: z.number(), type: z.literal("balance")}); export const autoRechargeConfigDto = z.object({enabled: z.boolean(), failureCount: z.number(), fundingSourceType: z.enum(["saved_card", "tokenized_card"]), inFlightPaymentIntentId: z.string().optional(), lastChargeAt: z.string().optional(), lockAcquiredAt: z.string().optional(), maxMonthlySpendMinor: z.number().optional(), monthlySpendMinor: z.number(), monthlySpendPeriod: z.string().optional(), paymentMethodId: z.string().optional(), status: z.enum(["active", "disabled", "failed", "pending_setup"]), topup: autoRechargeTopupDto, trigger: autoRechargeTriggerDto, updatedAt: z.string()}); export const autoRechargeDisplayFormattedDto = z.object({threshold: z.string(), topup: z.string()}); export const autoRechargeDisplayDto = z.object({currency: z.string(), exchangeRate: z.number(), formatted: autoRechargeDisplayFormattedDto, rateSource: z.enum(["parity", "db", "fallback"]), thresholdAmountMajor: z.number(), topupAmountMajor: z.number()}); export const saveAutoRechargeResponse = z.object({config: autoRechargeConfigDto, display: autoRechargeDisplayDto.optional(), publishableKey: z.string().optional(), setupClientSecret: z.string().optional(), stripeAccountId: 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 }; /** * Set or update auto-recharge config for a customer * * PUT /v1/sdk/auto-recharge */ export const updateApiV1SdkAutoRecharge = async (args: UpdateApiV1SdkAutoRechargeArgs, options?: UpdateApiV1SdkAutoRechargeOptions) => { const {baseUrl = "https://api.solvapay.com", fetch: fetchFn = fetch, SecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SecretKey !== undefined) headers.set('authorization', `Bearer ${SecretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sdk/auto-recharge${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(saveAutoRechargeResponse, await res.json()) };