import {z} from 'zod' export type Notes = {[key: string]: string}; export type CreateApiV2AccountsArgs = {body: {email: string, phone?: string | undefined, legal_business_name?: string | undefined, business_type?: 'route' | undefined, type: 'route', profile: {category?: string | undefined, subcategory?: string | undefined, addresses?: Record | undefined}, legal_info?: ({pan?: string | undefined, gst?: string | undefined}) | undefined, notes?: Notes | undefined}}; export type CreateApiV2AccountsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const notes = z.record(z.string(), z.string()); export const linkedAccount = z.object({id: z.string().optional(), type: z.literal("route").optional(), status: z.enum(["created", "suspended"]).optional(), email: z.string().optional(), phone: z.string().optional(), legal_business_name: z.string().optional(), business_type: z.literal("route").optional(), profile: z.object({category: z.string().optional(), subcategory: z.string().optional(), addresses: z.object({registered: z.object({street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional()}).optional(), operation: z.object({street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional()}).optional()}).optional()}).optional(), legal_info: z.object({pan: z.string().optional(), gst: z.string().optional()}).optional(), contact_name: z.string().optional(), contact_info: z.object({chargeback: z.object({email: z.string().optional(), phone: z.string().optional(), policy_url: z.string().optional()}).optional(), refund: z.object({email: z.string().optional(), phone: z.string().optional(), policy_url: z.string().optional()}).optional(), support: z.object({email: z.string().optional(), phone: z.string().optional(), policy_url: z.string().optional()}).optional()}).optional(), apps: z.object({websites: z.array(z.string()).optional(), android: z.array(z.object({url: z.string().optional(), name: z.string().optional()})).optional(), ios: z.array(z.object({url: z.string().optional(), name: z.string().optional()})).optional()}).optional(), notes: notes.optional(), created_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 }; /** * Create a Linked Account * * POST /v2/accounts * * Also accepts auth: oauth2 */ export const createApiV2Accounts = async (args: CreateApiV2AccountsArgs, options?: CreateApiV2AccountsOptions) => { 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(/\/+$/, '')}/v2/accounts${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(linkedAccount, await res.json()) };