import {z} from 'zod' 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 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 PaymentPayCollectInitiateHostedSI = {totalAmount: string, txnCurrency: string, billingData?: Billing | undefined}; export type SIDataOnDemand = {maxAmount: string, amount: string, numberOfPayments: string, frequency: 'ONDEMAND' | 'WEEKLY' | 'BIWEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'HALFYEARLY' | 'YEARLY', type: 'FIXED' | 'VARIABLE'}; export type StandingInstructionOnDemand = {data: SIDataOnDemand}; export type TransactionRequestPayCollectSIOnDemand = {merchantTxnId: string, merchantUniqueId?: string | undefined, riskData?: Risk | undefined, merchantCallbackURL: string, paymentData: PaymentPayCollectInitiateHostedSI, standingInstruction: StandingInstructionOnDemand}; export type CreateApiGlV1PaymentsInitiatePaycollectArgs = {body: TransactionRequestPayCollectSIOnDemand}; export type CreateApiGlV1PaymentsInitiatePaycollectOptions = {baseUrl?: string | undefined, JwsTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const initiateResponseDataPayCollectSI = z.object({redirectUrl: z.string().optional(), statusUrl: z.string().optional(), merchantTxnId: z.string().optional(), mandateId: z.string().optional()}); export const apiResponsePayCollectInitiateSI = z.object({gid: z.string().optional(), status: z.string().optional(), message: z.string().optional(), timestamp: z.string().optional(), reasonCode: z.string().optional(), data: initiateResponseDataPayCollectSI.optional(), errors: z.record(z.string(), z.string()).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 }; /** * PayCollect on-demand SI — register mandate (request body) * * POST /gl/v1/payments/initiate/paycollect */ export const createApiGlV1PaymentsInitiatePaycollect = async (args: CreateApiGlV1PaymentsInitiatePaycollectArgs, options?: CreateApiGlV1PaymentsInitiatePaycollectOptions) => { 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/initiate/paycollect${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(apiResponsePayCollectInitiateSI, await res.json()) };