import {z} from 'zod' export type GetApiPaymentPaymentsPaymentIdOperationsArgs = {paymentId: string, includeLatency?: boolean | undefined, includeLogs?: boolean | undefined}; export type GetApiPaymentPaymentsPaymentIdOperationsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPaymentPaymentsPaymentIdOperationsResponse = z.object({results: z.array(z.object({id: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), paymentId: z.string().optional(), type: z.enum(["Authorize", "Preauthorize", "Capture", "Cancel", "Refund", "Credit", "SendNotification", "CaptureNotification", "RefundNotification", "CancelNotification", "AuthorizeNotification", "ChargebackNotification", "ChargebackReverseNotification", "Get", "Search", "GetToken", "Tokenize", "DisableToken", "Register", "Generate3DS", "Validate3DS", "PayerAuthInit", "PayerAuthCheckEnroll", "PayerAuthValidate", "RefundFailedNotification", "RefundReversedNotification", "CaptureFailedNotification", "ValidateRedirect", "RefundNotificationUpdated"]).optional(), reason: z.string().optional(), providerReference: z.string().optional(), paymentStatus: z.string().optional(), amount: z.object({value: z.string(), currency: z.string()}).optional(), result: z.enum(["Success", "Accepted", "Pending", "HTTPRedirectRequired", "FormRedirectRequired", "Unknown", "UnexpectedProviderResponse", "ProviderUnknownError", "ProviderConnectionError", "Timeout", "ProviderTimeout", "GenericRejection", "FraudRisk", "DuplicateOperation", "OperationNotAllowed", "InstrumentNotAllowed", "InvalidInstrument", "InsufficientBalance", "BlockedInstrument", "ExpiredInstrument", "ValidationError", "ParamsError", "ProviderConfigError", "InvalidSignature", "InternalServerError", "PayerCanceled", "AuthenticationError", "AuthenticationRequired", "LimitExceeded", "PaymentMethodNotSupported", "AuthorizationRevoked"]), responseCode: z.string().optional(), requiredActionDetails: z.object({redirectUrl: z.string().optional(), redirectMethod: z.enum(["GET", "POST"]).optional(), parameters: z.record(z.string(), z.string()).optional()}).optional(), errorDetails: z.string().optional(), acquirerReference: z.string().optional(), workspaceId: z.string().optional(), operationLogs: z.array(z.object({id: z.string().optional(), createdAt: z.string().optional(), operationId: z.string().optional(), input: z.string().optional(), output: z.string().optional(), workspaceId: z.string().optional()})).optional(), providerLatency: z.string().optional(), totalLatency: z.string().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 }; /** * Get Payment Operations by Payment ID * * GET /payment/payments/{paymentId}/operations */ export const getApiPaymentPaymentsPaymentIdOperations = async (args: GetApiPaymentPaymentsPaymentIdOperationsArgs, options?: GetApiPaymentPaymentsPaymentIdOperationsOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeLatency != null) query.append("includeLatency", String(args.includeLatency)) if (args.includeLogs != null) query.append("includeLogs", String(args.includeLogs)) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/payment/payments/${encodeURIComponent(String(args.paymentId))}/operations${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(getApiPaymentPaymentsPaymentIdOperationsResponse, await res.json()) };