import {z} from 'zod' export type GetApiV1GatewayPaymentsPageIdArgs = {pageId: string}; export type GetApiV1GatewayPaymentsPageIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getGatewayPaymentResponse = z.object({pageId: z.string(), paymentId: z.string(), status: z.enum(["OPEN", "EXPIRED", "FIAT_CONFIRMED", "AUTHORIZING", "PENDING_CAPTURE", "CONFIRMING", "CONFIRMED", "REFUNDED", "REFUND_PROCESSING", "SETTLING", "SETTLED", "FAILED", "SETTLE_FAILED", "SUBSCRIPTION_CANCELLED", "MANUAL_INSPECTION_REQUIRED", "CANCELLED", "ACTION_REQUIRED"]), amount: z.number(), currency: z.string(), description: z.string().optional(), createdAt: z.number(), updatedAt: z.number().optional(), customerId: z.string().optional(), savedPaymentMethodId: z.string().optional(), schemeTransactionId: z.string().optional(), nextAction: z.object({type: z.enum(["REDIRECT_TO_URL", "AUTHENTICATION_REQUIRED"]), redirectUrl: z.string().optional()}).optional(), metadata: z.record(z.string(), z.string()).optional(), taxDetails: z.object({amount: z.number(), mode: z.literal("merchant_calculated"), location: z.object({country: z.string(), postalCode: z.string().optional()})}).optional(), errorCode: z.string().optional(), failureReason: z.string().optional(), adviceCode: z.string().optional()}); export const getApiV1GatewayPaymentsPageIdResponse = z.object({status: z.literal("SUCCEEDED"), data: getGatewayPaymentResponse}); 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 a payment * * GET /v1/gateway/payments/{pageId} */ export const getApiV1GatewayPaymentsPageId = async (args: GetApiV1GatewayPaymentsPageIdArgs, options?: GetApiV1GatewayPaymentsPageIdOptions) => { const {baseUrl = "https://api.breeze.cash", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/gateway/payments/${encodeURIComponent(String(args.pageId))}${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(getApiV1GatewayPaymentsPageIdResponse, await res.json()) };