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 RiskFlightLeg = {flightNumber?: string | undefined, departureAirportCode?: string | undefined, departureDate?: string | undefined, arrivalAirportCode?: string | undefined, carrierCode?: string | undefined}; export type RiskFlightPassenger = {firstName?: string | undefined, lastName?: string | undefined, email?: string | undefined}; export type RiskFlightBooking = {agentCode?: string | undefined, agentName?: string | undefined, reservationCode?: string | undefined, legData?: Array | undefined, passengerData?: Array | undefined}; export type RiskPayDirectInitiate = {customerData?: Customer | undefined, shippingData?: Shipping | undefined, orderData?: Array | undefined, flightData?: 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 Card = {number?: string | undefined, expiryMonth?: string | undefined, expiryYear?: string | undefined, securityCode?: string | undefined, type?: string | undefined}; export type PaymentPayDirectInitiateCard = {totalAmount: string, txnCurrency: string, billingData?: Billing | undefined, cardData: Card}; export type SIDataPayDirectOnDemand = {maxAmount: string, amount: string, numberOfPayments: string, frequency: 'ONDEMAND' | 'WEEKLY' | 'BIWEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'HALFYEARLY' | 'YEARLY', type: 'FIXED' | 'VARIABLE'}; export type StandingInstructionPayDirectOnDemand = {data: SIDataPayDirectOnDemand}; export type TransactionRequestPayDirectSIOnDemand = {merchantTxnId: string, merchantUniqueId?: string | undefined, riskData?: RiskPayDirectInitiate | undefined, merchantCallbackURL: string, paymentData: PaymentPayDirectInitiateCard, standingInstruction: StandingInstructionPayDirectOnDemand}; export type CreateApiGlV1PaymentsInitiateArgs = {body: TransactionRequestPayDirectSIOnDemand}; export type CreateApiGlV1PaymentsInitiateOptions = {baseUrl?: string | undefined, JwsTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const initiateResponseDataSI = z.object({redirectUrl: z.string().optional(), statusUrl: z.string().optional(), merchantTxnId: z.string().optional(), mandateId: z.string().optional()}); export const apiResponsePayDirectInitiateSI = z.object({gid: z.string().optional(), status: z.string().optional(), message: z.string().optional(), timestamp: z.string().optional(), reasonCode: z.string().optional(), data: initiateResponseDataSI.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 }; /** * PayDirect on-demand SI — register mandate (request body) * * POST /gl/v1/payments/initiate */ export const createApiGlV1PaymentsInitiate = async (args: CreateApiGlV1PaymentsInitiateArgs, options?: CreateApiGlV1PaymentsInitiateOptions) => { 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${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(apiResponsePayDirectInitiateSI, await res.json()) };