import {z} from 'zod' export type CreatePaymentIntentDto = {amount?: number | undefined, autoRecharge?: ({currency?: string | undefined, enabled?: boolean | undefined, maxMonthlySpendMajor?: number | undefined, thresholdAmountMajor?: number | undefined, topupAmountMajor?: number | undefined, triggerType?: 'balance' | undefined}) | undefined, currency?: string | undefined, customerRef: string, description?: string | undefined, planRef?: string | undefined, pricingTier?: string | undefined, productRef?: string | undefined, purpose?: ('product' | 'credit_topup' | 'usage_billing') | undefined}; export type CreateApiV1SdkPaymentIntentsArgs = {'idempotency-key': string, body: CreatePaymentIntentDto}; export type CreateApiV1SdkPaymentIntentsOptions = {baseUrl?: string | undefined, SecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const sdkPaymentIntentResponse = z.object({accountId: z.string().optional(), amount: z.number(), clientSecret: z.string(), createdAt: z.string().optional(), currency: z.string(), customerRef: z.string().optional(), exchangeRate: z.number().optional(), expiresAt: z.string().optional(), originalAmount: z.number().optional(), planRef: z.string().optional(), processorPaymentId: z.string(), publishableKey: z.string(), status: z.enum(["pending", "requires_payment_method", "requires_confirmation", "requires_action", "processing", "succeeded", "failed", "cancelled"]), transactionId: 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 }; /** * Create a payment intent * * POST /v1/sdk/payment-intents */ export const createApiV1SdkPaymentIntents = async (args: CreateApiV1SdkPaymentIntentsArgs, options?: CreateApiV1SdkPaymentIntentsOptions) => { 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 (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (SecretKey !== undefined) headers.set('authorization', `Bearer ${SecretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sdk/payment-intents${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(sdkPaymentIntentResponse, await res.json()) };