import {z} from 'zod' export type GetApiV1SdkPurchasesArgs = {status?: ('pending' | 'active' | 'trialing' | 'past_due' | 'cancelled' | 'expired' | 'suspended' | 'refunded') | undefined, productRef?: string | undefined, customerRef?: string | undefined, includeFree?: boolean | undefined}; export type GetApiV1SdkPurchasesOptions = {baseUrl?: string | undefined, SecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const sdkPlanSnapshotDto = z.object({currency: z.string(), features: z.record(z.string(), z.unknown()).nullable().optional(), isMetered: z.boolean().optional(), name: z.string().optional(), options: z.array(z.record(z.string(), z.unknown())).optional(), price: z.number(), reference: z.string().optional()}); export const sdkUsagePeriodDto = z.object({periodEnd: z.string().optional(), periodStart: z.string().optional()}); export const sdkPurchaseResponse = z.object({amount: z.number(), autoRenew: z.boolean().optional(), billingCycle: z.enum(["weekly", "monthly", "quarterly", "yearly"]).optional(), cancellationReason: z.string().optional(), cancelledAt: z.string().optional(), createdAt: z.string(), currency: z.string(), customerEmail: z.string().optional(), customerRef: z.string(), endDate: z.string().optional(), exchangeRate: z.number().optional(), isRecurring: z.boolean(), metadata: z.record(z.string(), z.unknown()).optional(), nextBillingDate: z.string().optional(), origin: z.enum(["paid", "free_default", "manual", "one_time", "credit_topup", "admin_assignment"]).optional(), originalAmount: z.number().optional(), paidAt: z.string().optional(), planRef: z.string().optional(), planSnapshot: sdkPlanSnapshotDto.optional(), productName: z.string().optional(), productRef: z.string().optional(), reference: z.string(), requiresPayment: z.boolean().optional(), startDate: z.string(), status: z.string(), usage: sdkUsagePeriodDto.optional()}); export const getApiV1SdkPurchasesResponse = z.object({purchases: z.array(sdkPurchaseResponse).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 }; /** * List purchases for provider * * GET /v1/sdk/purchases */ export const getApiV1SdkPurchases = async (args: GetApiV1SdkPurchasesArgs, options?: GetApiV1SdkPurchasesOptions) => { const {baseUrl = "https://api.solvapay.com", fetch: fetchFn = fetch, SecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.productRef != null) query.append("productRef", String(args.productRef)) if (args.customerRef != null) query.append("customerRef", String(args.customerRef)) if (args.includeFree != null) query.append("includeFree", String(args.includeFree)) if (SecretKey !== undefined) headers.set('authorization', `Bearer ${SecretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sdk/purchases${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1SdkPurchasesResponse, await res.json()) };