import {z} from 'zod' export type Card = {number?: string | undefined, expiryMonth?: string | undefined, expiryYear?: string | undefined, securityCode?: string | undefined, type?: string | undefined}; export type Billing = {firstName?: string | undefined, lastName?: string | undefined, emailId?: string | undefined, callingCode?: string | undefined, phoneNumber?: string | undefined, addressStreet1?: string | undefined, addressStreet2?: string | undefined, addressCity?: string | undefined, addressState?: string | undefined, addressStateCode?: string | undefined, addressPostalCode?: string | undefined, addressCountry?: string | undefined}; export type Authentication = {type?: ('3DS' | 'NO_THREE_DS') | undefined}; export type Payment = {totalAmount: string, txnCurrency: string, cardData?: Card | undefined, billingData?: Billing | undefined, authenticationData?: Authentication | undefined}; export type Customer = {merchantAssignedCustomerId?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, emailId?: string | undefined, callingCode?: string | undefined, mobileNumber?: string | undefined}; export type Shipping = {firstName?: string | undefined, lastName?: string | undefined, emailId?: string | undefined, addressStreet1?: string | undefined, addressStreet2?: string | undefined, addressCity?: string | undefined, addressState?: string | undefined, addressPostalCode?: string | undefined, addressCountry?: string | undefined}; export type RiskItem = {itemId?: string | undefined, itemName?: string | undefined, itemQuantity?: number | undefined, itemPrice?: string | undefined, itemCategory?: string | undefined}; export type Risk = {customerData?: Customer | undefined, shippingData?: Shipping | undefined, orderData?: Array | undefined}; export type SIData = {maxAmount?: string | undefined, amount?: string | undefined, numberOfPayments: string, frequency: 'ONDEMAND' | 'WEEKLY' | 'BIWEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'HALFYEARLY' | 'YEARLY', type: 'FIXED' | 'VARIABLE', mode: 'REGISTER' | 'PAYREGISTER', startDate?: string | undefined, endDate?: string | undefined}; export type StandingInstruction = {data: SIData, action?: ('REVOKE' | 'PAUSE' | 'RESUME') | undefined, mandateId?: string | undefined, schedulerId?: string | undefined, scheduledId?: string | undefined}; export type CreateApiGlV1PaymentsGidRefundArgs = {gid: string, body: {merchantTxnId?: string | undefined, merchantUniqueId?: string | undefined, refundType: 'F' | 'P', refundAction?: string | undefined, captureType?: ('F' | 'P') | undefined, captureTxn?: boolean | undefined, paymentData?: Payment | undefined, riskData?: Risk | undefined, standingInstruction?: StandingInstruction | undefined, merchantCallbackURL?: string | undefined, merchantCustomPayload?: {[key: string]: string} | undefined, dcc_indicator?: boolean | undefined, dccReferenceGid?: string | undefined}}; export type CreateApiGlV1PaymentsGidRefundOptions = {baseUrl?: string | undefined, JwsTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiResponse = z.object({gid: z.string().optional(), status: z.string().optional(), message: z.string().optional(), timestamp: z.string().optional(), reasonCode: z.string().optional(), data: z.record(z.string(), z.unknown()).optional(), errors: z.record(z.string(), 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 }; /** * Refund Transaction * * POST /gl/v1/payments/{gid}/refund */ export const createApiGlV1PaymentsGidRefund = async (args: CreateApiGlV1PaymentsGidRefundArgs, options?: CreateApiGlV1PaymentsGidRefundOptions) => { const {baseUrl = "https://api.onboard.payglocal.in", fetch: fetchFn = fetch, JwsTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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.gid))}/refund${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(apiResponse, await res.json()) };