import {z} from 'zod' export type SimulateSubscriptionFlowDto = {event: 'pre_charge_reminder' | 'due_charge' | 'payment_failed' | 'retry_due' | 'retry_failed_cancel' | 'manual_invoice_due' | 'manual_overdue_reminder', redirectUrl?: string | undefined}; export type CreateApiV1SubscriptionsIdSimulateArgs = {id: string, body: SimulateSubscriptionFlowDto}; export type CreateApiV1SubscriptionsIdSimulateOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const publicPlanDto = z.object({id: z.string(), name: z.string(), amount: z.number(), currency: z.string(), interval: z.string().optional(), isActive: z.boolean().optional(), createdAt: z.string().optional()}); export const publicSubscriptionDto = z.object({id: z.string(), status: z.string(), plan: publicPlanDto.optional(), customer: z.record(z.string(), z.unknown()).optional(), nextChargeAt: z.string().optional(), createdAt: z.string().optional()}); export const publicTransactionMetaDto = z.object({amount: z.number(), symbol: z.string(), currency: z.string(), fxRate: z.number().nullable().optional()}); export const publicFeeBreakdownDto = z.object({fee: z.number(), vat: z.number(), processorFee: z.number(), stampDuty: z.number(), allInclusiveKyshiFee: z.number(), totalFees: z.number(), others: z.record(z.string(), z.unknown())}); export const publicTransactionDto = z.object({id: z.string(), amount: z.number(), type: z.enum(["DR", "CR"]), status: z.enum(["PENDING", "SUCCESS", "IN_REVIEW", "FAILED", "REVERSAL", "COLLECTED"]), reference: z.string(), currency: z.string(), channel: z.string().optional(), processor: z.enum(["PROVIDUS_BANK", "STERLING", "VFD", "PAYSTACK", "FLUTTERWAVE", "CHOICE", "STITCH", "PAGA", "STRIPE", "ASSET_MFB", "MOMENTPAY", "PAWAPAY"]).optional(), feeBearer: z.enum(["MERCHANT", "CUSTOMER"]).optional(), meta: publicTransactionMetaDto.optional(), feeBreakdown: publicFeeBreakdownDto.optional(), createdAt: z.string(), updatedAt: z.string()}); export const createApiV1SubscriptionsIdSimulateResponse = z.object({status: z.boolean(), message: z.string(), code: z.number(), data: z.object({success: z.boolean(), subscription: publicSubscriptionDto.optional(), transaction: publicTransactionDto.optional(), message: 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 }; /** * Simulate subscription event * * POST /v1/subscriptions/{id}/simulate */ export const createApiV1SubscriptionsIdSimulate = async (args: CreateApiV1SubscriptionsIdSimulateArgs, options?: CreateApiV1SubscriptionsIdSimulateOptions) => { const {baseUrl = "https://api.kyshi.co", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/subscriptions/${encodeURIComponent(String(args.id))}/simulate${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(createApiV1SubscriptionsIdSimulateResponse, await res.json()) };