import {z} from 'zod' export type PaymentLinkId = string; export type GetApiPaymentLinksIdArgs = {id: PaymentLinkId}; export type GetApiPaymentLinksIdOptions = {baseUrl: string, ApiSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; 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 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 getPaymentLinkResponse = z.discriminatedUnion("status", [z.object({id: z.string().min(15).max(15), status: z.literal("Active"), payment_id: z.string().optional(), amount: z.number().int(), currency: z.string(), reference: z.string().optional(), description: z.string().optional(), created_on: z.string(), expires_on: z.string(), processing_channel_id: z.string().optional(), amount_allocations: amountAllocations.optional(), customer: z.object({email: z.string().optional(), name: z.string().optional()}).optional(), shipping: 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)})}).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(), locale: z.enum(["ar", "da-DK", "de-DE", "el", "en-GB", "es-ES", "fi-FI", "fil-PH", "fr-FR", "hi-IN", "id-ID", "it-IT", "ja-JP", "ms-MY", "nb-NO", "nl-NL", "pt-PT", "sv-SE", "th-TH", "vi-VN", "zh-CN", "zh-HK", "zh-TW"]).optional(), return_url: z.string().max(255).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(15).max(15), status: z.literal("Payment Received"), payment_id: z.string(), amount: z.number().int(), currency: z.string(), reference: z.string().optional(), description: z.string().optional(), created_on: z.string(), expires_on: z.string(), processing_channel_id: z.string().optional(), amount_allocations: amountAllocations.optional(), customer: z.object({email: z.string().optional(), name: z.string().optional()}).optional(), shipping: 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)})}).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(), locale: z.enum(["ar", "da-DK", "de-DE", "el", "en-GB", "es-ES", "fi-FI", "fil-PH", "fr-FR", "hi-IN", "id-ID", "it-IT", "ja-JP", "ms-MY", "nb-NO", "nl-NL", "pt-PT", "sv-SE", "th-TH", "vi-VN", "zh-CN", "zh-HK", "zh-TW"]).optional(), return_url: z.string().max(255).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(15).max(15), status: z.literal("Expired"), payment_id: z.string().optional(), amount: z.number().int(), currency: z.string(), reference: z.string().optional(), description: z.string().optional(), created_on: z.string(), expires_on: z.string(), processing_channel_id: z.string().optional(), amount_allocations: amountAllocations.optional(), customer: z.object({email: z.string().optional(), name: z.string().optional()}).optional(), shipping: 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)})}).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(), locale: z.enum(["ar", "da-DK", "de-DE", "el", "en-GB", "es-ES", "fi-FI", "fil-PH", "fr-FR", "hi-IN", "id-ID", "it-IT", "ja-JP", "ms-MY", "nb-NO", "nl-NL", "pt-PT", "sv-SE", "th-TH", "vi-VN", "zh-CN", "zh-HK", "zh-TW"]).optional(), return_url: z.string().max(255).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 Payment Link details * * GET /payment-links/{id} */ export const getApiPaymentLinksId = async (args: GetApiPaymentLinksIdArgs, options: GetApiPaymentLinksIdOptions) => { 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(/\/+$/, '')}/payment-links/${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(getPaymentLinkResponse, await res.json()) };