import {z} from 'zod' export type CancelRequest = {orderId: string}; export type CreateApiPaymentOpenOtcApiRechargeCancelArgs = {'X-GatePay-Certificate-ClientId': string, 'X-GatePay-Signature': string, 'X-GatePay-Timestamp': string, 'X-GatePay-Nonce': string, 'X-GatePay-On-Behalf-Of'?: string | undefined, body: CancelRequest}; export type CreateApiPaymentOpenOtcApiRechargeCancelOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const otcRechargeCancelResponse = z.object({code: z.string().optional(), status: z.string().optional(), errorMessage: z.string().optional(), data: z.object({success: z.boolean().optional()}).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 }; /** * Cancel OTC Recharge Order * * POST /payment/open/otc/api/recharge/cancel */ export const createApiPaymentOpenOtcApiRechargeCancel = async (args: CreateApiPaymentOpenOtcApiRechargeCancelArgs, options?: CreateApiPaymentOpenOtcApiRechargeCancelOptions) => { const {baseUrl = "https://openplatform.gateapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-GatePay-Certificate-ClientId'] != null) headers.set("X-GatePay-Certificate-ClientId", String(args['X-GatePay-Certificate-ClientId'])) if (args['X-GatePay-Signature'] != null) headers.set("X-GatePay-Signature", String(args['X-GatePay-Signature'])) if (args['X-GatePay-Timestamp'] != null) headers.set("X-GatePay-Timestamp", String(args['X-GatePay-Timestamp'])) if (args['X-GatePay-Nonce'] != null) headers.set("X-GatePay-Nonce", String(args['X-GatePay-Nonce'])) if (args['X-GatePay-On-Behalf-Of'] != null) headers.set("X-GatePay-On-Behalf-Of", String(args['X-GatePay-On-Behalf-Of'])) const url = `${baseUrl.replace(/\/+$/, '')}/payment/open/otc/api/recharge/cancel${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(otcRechargeCancelResponse, await res.json()) };