import {z} from 'zod' export type Notes = {[key: string]: string}; export type PatchApiPaymentLinksIdArgs = {id: string, body: {reference_id?: string | undefined, expire_by?: number | undefined, reminder_enable?: boolean | undefined, accept_partial?: boolean | undefined, notes?: Notes | undefined}}; export type PatchApiPaymentLinksIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const notes = z.record(z.string(), z.string()); export const paymentLink = z.object({id: z.string().optional(), entity: z.literal("payment_link").optional(), amount: z.number().int().optional(), amount_paid: z.number().int().optional(), currency: z.string().optional(), accept_partial: z.boolean().optional(), first_min_partial_amount: z.number().int().optional(), description: z.string().optional(), reference_id: z.string().optional(), expire_by: z.number().int().optional(), expired_at: z.number().int().optional(), cancelled_at: z.number().int().optional(), status: z.enum(["created", "partially_paid", "expired", "cancelled", "paid"]).optional(), upi_link: z.boolean().optional(), short_url: z.string().optional(), callback_url: z.string().optional(), callback_method: z.literal("get").optional(), customer: z.object({name: z.string().optional(), email: z.string().optional(), contact: z.string().optional()}).optional(), notify: z.object({sms: z.boolean().optional(), email: z.boolean().optional()}).optional(), reminder_enable: z.boolean().optional(), payments: z.array(z.object({amount: z.number().int().optional(), payment_id: z.string().optional(), plink_id: z.string().optional(), method: z.enum(["netbanking", "card", "wallet", "upi", "emi", "bank_transfer"]).optional(), status: z.literal("captured").optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional()})).optional(), notes: notes.optional(), order_id: z.string().optional(), user_id: z.string().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().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 }; /** * Update a payment link * * PATCH /payment_links/{id} * * Also accepts auth: oauth2 */ export const patchApiPaymentLinksId = async (args: PatchApiPaymentLinksIdArgs, options?: PatchApiPaymentLinksIdOptions) => { const {baseUrl = "https://api.razorpay.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payment_links/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(paymentLink, await res.json()) };