import {z} from 'zod' export type GetApiGlV1PaymentsIdStatusArgs = {id: string}; export type GetApiGlV1PaymentsIdStatusOptions = {baseUrl?: string | undefined, JwsTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionStatusData = z.object({transactionCreationTime: z.string().optional(), gid: z.string().optional(), "payment-method": z.string().optional(), Amount: z.string().optional(), txnCurrency: z.string().optional(), merchantTxnId: z.string().optional(), merchantLogo: z.string().nullable().optional(), detailedMessage: z.string().optional(), Currency: z.string().optional(), reasonCode: z.string().optional(), status: z.string().optional(), CardBrand: z.string().optional(), CardType: z.string().optional(), Country: z.string().optional(), processor: z.string().optional(), authApprovalCode: z.string().optional(), upiTxnAmount: z.string().optional(), upiTxnCurrency: z.string().optional()}); export const apiResponseTransactionStatus = z.object({gid: z.string().optional(), status: z.string().optional(), message: z.string().optional(), timestamp: z.string().optional(), reasonCode: z.string().optional(), data: transactionStatusData.optional(), errors: z.object({}).nullable().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 Transaction Status * * GET /gl/v1/payments/{id}/status */ export const getApiGlV1PaymentsIdStatus = async (args: GetApiGlV1PaymentsIdStatusArgs, options?: GetApiGlV1PaymentsIdStatusOptions) => { const {baseUrl = "https://api.onboard.payglocal.in", fetch: fetchFn = fetch, JwsTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (JwsTokenAuth !== undefined) headers.set("x-gl-token-external", JwsTokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/gl/v1/payments/${encodeURIComponent(String(args.id))}/status${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(apiResponseTransactionStatus, await res.json()) };