import {z} from 'zod' export type PaymentCheckoutDto = {name?: string | undefined}; export type PaymentOptionsDto = {checkout?: PaymentCheckoutDto | undefined}; export type PaymentCustomerDto = {name?: string | undefined, contact?: string | undefined, email?: string | undefined}; export type PaymentNotifyDto = {sms?: boolean | undefined, email?: boolean | undefined, whatsapp?: boolean | undefined}; export type PaymentNotesDto = {metadata?: string | undefined, tenantName?: string | undefined, orgId?: string | undefined}; export type PaymentRequestDto = {amount?: number | undefined, currency?: string | undefined, accept_partial?: boolean | undefined, first_min_partial_amount?: number | undefined, reference_id?: string | undefined, description?: string | undefined, reminder_enable?: boolean | undefined, callback_url?: string | undefined, callback_method?: string | undefined, options?: PaymentOptionsDto | undefined, customer?: PaymentCustomerDto | undefined, notify?: PaymentNotifyDto | undefined, notes?: PaymentNotesDto | undefined}; export type RcPaymentRequestDto = {command?: string | undefined, currency?: string | undefined, amount?: string | undefined, uiRedirectUrl?: string | undefined, uiCancelUrl?: string | undefined, uiFailedUrl?: string | undefined, jobInstanceId?: string | undefined}; export type BasePaymentRequestDto = {pgType?: string | undefined, razorPayReq?: PaymentRequestDto | undefined, rcPayReq?: RcPaymentRequestDto | undefined}; export type CreateApiApiV1PaymentCreateArgs = {body: BasePaymentRequestDto}; export type CreateApiApiV1PaymentCreateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commonResponseObject = z.object({data: z.unknown(), statusCode: z.number().int().optional(), messages: z.array(z.string()).optional(), validationErrors: z.unknown()}); 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 }; /** * POST /api/v1/payment/create */ export const createApiApiV1PaymentCreate = async (args: CreateApiApiV1PaymentCreateArgs, options?: CreateApiApiV1PaymentCreateOptions) => { const {baseUrl = "https://api-docs.gosure.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.hal+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment/create${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(commonResponseObject, await res.json()) };