import {z} from 'zod' export type HostedPaymentId = string; export type GetApiHostedPaymentsIdArgs = {id: HostedPaymentId}; export type GetApiHostedPaymentsIdOptions = {baseUrl: string, ApiSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentInterfacesProducts = z.array(z.object({reference: z.string().max(255).optional(), url: z.string().max(1024).optional(), name: z.string().max(255), quantity: z.number().int().gte(1), price: z.number().int().gte(0)})); export const amountAllocations = z.array(z.object({id: z.string(), amount: z.number().int().gte(0).lte(9999999999), reference: z.string().max(50).optional(), commission: z.object({amount: z.number().int().gte(0).optional(), percentage: z.number().gte(0).lte(100).optional()}).optional()})); export const getHostedPaymentsResponse = z.discriminatedUnion("status", [z.object({id: z.string().min(16).max(16), status: z.literal("Payment Pending"), payment_id: z.string().optional(), amount: z.number().int(), currency: z.string(), reference: z.string().optional(), description: z.string().optional(), customer: z.object({email: z.string().optional(), name: z.string().optional()}).optional(), billing: z.object({address: z.object({address_line1: z.string().max(200).optional(), address_line2: z.string().max(200).optional(), city: z.string().max(50).optional(), state: z.string().max(3).optional(), zip: z.string().max(50).optional(), country: z.string().min(2).max(2)}), phone: z.object({country_code: z.string().min(1).max(7).optional(), number: z.string().max(25)}).optional()}), products: paymentInterfacesProducts.optional(), metadata: z.object({}).optional(), success_url: z.string(), cancel_url: z.string(), failure_url: z.string(), amount_allocations: amountAllocations.optional(), _links: z.object({self: z.object({href: z.string()}), redirect: z.object({href: z.string()}), payment: z.object({href: z.string()}).optional(), payment_actions: z.object({href: z.string()}).optional()})}), z.object({id: z.string().min(16).max(16), status: z.literal("Payment Received"), payment_id: z.string(), amount: z.number().int(), currency: z.string(), reference: z.string().optional(), description: z.string().optional(), customer: z.object({email: z.string().optional(), name: z.string().optional()}).optional(), billing: z.object({address: z.object({address_line1: z.string().max(200).optional(), address_line2: z.string().max(200).optional(), city: z.string().max(50).optional(), state: z.string().max(3).optional(), zip: z.string().max(50).optional(), country: z.string().min(2).max(2)}), phone: z.object({country_code: z.string().min(1).max(7).optional(), number: z.string().max(25)}).optional()}), products: paymentInterfacesProducts.optional(), metadata: z.object({}).optional(), success_url: z.string(), cancel_url: z.string(), failure_url: z.string(), amount_allocations: amountAllocations.optional(), _links: z.object({self: z.object({href: z.string()}), redirect: z.object({href: z.string()}), payment: z.object({href: z.string()}).optional(), payment_actions: z.object({href: z.string()}).optional()})}), z.object({id: z.string().min(16).max(16), status: z.literal("Expired"), payment_id: z.string().optional(), amount: z.number().int(), currency: z.string(), reference: z.string().optional(), description: z.string().optional(), customer: z.object({email: z.string().optional(), name: z.string().optional()}).optional(), billing: z.object({address: z.object({address_line1: z.string().max(200).optional(), address_line2: z.string().max(200).optional(), city: z.string().max(50).optional(), state: z.string().max(3).optional(), zip: z.string().max(50).optional(), country: z.string().min(2).max(2)}), phone: z.object({country_code: z.string().min(1).max(7).optional(), number: z.string().max(25)}).optional()}), products: paymentInterfacesProducts.optional(), metadata: z.object({}).optional(), success_url: z.string(), cancel_url: z.string(), failure_url: z.string(), amount_allocations: amountAllocations.optional(), _links: z.object({self: z.object({href: z.string()}), redirect: z.object({href: z.string()}), payment: z.object({href: z.string()}).optional(), payment_actions: z.object({href: 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 }; /** * Get Hosted Payments Page details * * GET /hosted-payments/{id} */ export const getApiHostedPaymentsId = async (args: GetApiHostedPaymentsIdArgs, options: GetApiHostedPaymentsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiSecretKey !== undefined) headers.set("Authorization", ApiSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/hosted-payments/${encodeURIComponent(String(args.id))}${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(getHostedPaymentsResponse, await res.json()) };