import {z} from 'zod' export type CreateGatewayPaymentRequest = {amount: number, currency: 'USD' | 'AUD' | 'CAD' | 'CHF' | 'DKK' | 'EUR' | 'GBP' | 'HKD' | 'JPY' | 'NOK' | 'NZD' | 'PLN' | 'SEK' | 'SGD' | 'TWD' | 'KRW', clientReferenceId: string, description?: string | undefined, customer: {id: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined, merchantTrustSignal?: ({playerId: string, hoursPlayed?: (number | null) | undefined, playerLevel?: (number | null) | undefined, trustStatus?: ('GOOD' | 'BAD' | 'null' | null) | undefined}) | undefined} | {id: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined, merchantTrustSignal?: ({playerId: string, hoursPlayed?: (number | null) | undefined, playerLevel?: (number | null) | undefined, trustStatus?: ('GOOD' | 'BAD' | 'null' | null) | undefined}) | undefined} | {id: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined, merchantTrustSignal?: ({playerId: string, hoursPlayed?: (number | null) | undefined, playerLevel?: (number | null) | undefined, trustStatus?: ('GOOD' | 'BAD' | 'null' | null) | undefined}) | undefined} | {id: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined, merchantTrustSignal?: ({playerId: string, hoursPlayed?: (number | null) | undefined, playerLevel?: (number | null) | undefined, trustStatus?: ('GOOD' | 'BAD' | 'null' | null) | undefined}) | undefined} | {referenceId: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined, merchantTrustSignal?: ({playerId: string, hoursPlayed?: (number | null) | undefined, playerLevel?: (number | null) | undefined, trustStatus?: ('GOOD' | 'BAD' | 'null' | null) | undefined}) | undefined} | {referenceId: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined, merchantTrustSignal?: ({playerId: string, hoursPlayed?: (number | null) | undefined, playerLevel?: (number | null) | undefined, trustStatus?: ('GOOD' | 'BAD' | 'null' | null) | undefined}) | undefined} | {referenceId: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined, merchantTrustSignal?: ({playerId: string, hoursPlayed?: (number | null) | undefined, playerLevel?: (number | null) | undefined, trustStatus?: ('GOOD' | 'BAD' | 'null' | null) | undefined}) | undefined} | {referenceId: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined, merchantTrustSignal?: ({playerId: string, hoursPlayed?: (number | null) | undefined, playerLevel?: (number | null) | undefined, trustStatus?: ('GOOD' | 'BAD' | 'null' | null) | undefined}) | undefined} | {email: string, firstName: string, lastName: string, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined} | {email: string, firstName: string, lastName: string, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined} | {email: string, firstName: string, lastName: string, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined} | {email: string, firstName: string, lastName: string, phone?: {countryCode: string, number: string} | undefined, address?: ({line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}) | undefined}, paymentMethod: {type: 'RAW_PAN', card: {number: string, expiryMonth: string, expiryYear: string, scheme?: ('VISA' | 'MASTERCARD' | 'AMEX' | 'DISCOVER' | 'DINERS' | 'JCB' | 'CARTES_BANCAIRES') | undefined, issuer?: string | undefined, issuerCountry?: string | undefined, cvv?: string | undefined}, storeFutureUsage?: ('on_session' | 'off_session') | undefined} | {type: 'DECRYPTED_APPLE_PAY', card: {number: string, expiryMonth: string, expiryYear: string, scheme?: ('VISA' | 'MASTERCARD' | 'AMEX' | 'DISCOVER' | 'DINERS' | 'JCB' | 'CARTES_BANCAIRES') | undefined, issuer?: string | undefined, issuerCountry?: string | undefined}, network?: ({eci?: string | undefined, cryptogram?: string | undefined}) | undefined, storeFutureUsage?: ('on_session' | 'off_session') | undefined} | {type: 'DECRYPTED_GOOGLE_PAY', card: {number: string, expiryMonth: string, expiryYear: string, scheme?: ('VISA' | 'MASTERCARD' | 'AMEX' | 'DISCOVER' | 'DINERS' | 'JCB' | 'CARTES_BANCAIRES') | undefined, issuer?: string | undefined, issuerCountry?: string | undefined}, network: {tokenType: 'PAN_ONLY'}, storeFutureUsage?: ('on_session' | 'off_session') | undefined} | {type: 'DECRYPTED_GOOGLE_PAY', card: {number: string, expiryMonth: string, expiryYear: string, scheme?: ('VISA' | 'MASTERCARD' | 'AMEX' | 'DISCOVER' | 'DINERS' | 'JCB' | 'CARTES_BANCAIRES') | undefined, issuer?: string | undefined, issuerCountry?: string | undefined}, network: {tokenType: 'CRYPTOGRAM_3DS', eci?: string | undefined, cryptogram?: string | undefined}, storeFutureUsage?: ('on_session' | 'off_session') | undefined} | {type: 'NETWORK_TOKEN', card: {number: string, expiryMonth: string, expiryYear: string, issuer?: string | undefined, issuerCountry?: string | undefined, scheme?: ('VISA' | 'MASTERCARD' | 'AMEX' | 'DISCOVER' | 'DINERS' | 'JCB' | 'CARTES_BANCAIRES' | 'VISA' | 'MASTERCARD') | undefined}, network?: ({eci?: string | undefined, cryptogram?: string | undefined}) | undefined, storeFutureUsage?: ('on_session' | 'off_session') | undefined} | {type: 'SAVED_CARD', card: {savedPaymentMethodId: string}}, transactionContext?: ({initiator: 'CUSTOMER' | 'MERCHANT', mitType?: ('SUBSCRIPTION' | 'INSTALLMENT' | 'UNSCHEDULED') | undefined, originalTransaction?: {schemeTransactionId: string} | undefined}) | undefined, threeDs?: ({mode: 'MERCHANT_PROVIDED', version?: string | undefined, eci?: string | undefined, authenticationValue?: string | undefined, dsTransactionId?: string | undefined} | {mode: 'BREEZE_HOSTED', successReturnUrl: string, failReturnUrl: string}) | undefined, risk?: ({sessionId?: string | undefined, ipAddress?: (string | string) | undefined, userAgent?: string | undefined, userAgentClientHint?: string | undefined, acceptHeader?: string | undefined, language?: string | undefined, timezone?: string | undefined, screenWidth?: number | undefined, screenHeight?: number | undefined, colorDepth?: number | undefined, javaEnabled?: boolean | undefined, javascriptEnabled?: boolean | undefined, timestamp?: number | undefined}) | undefined, taxDetails?: ({amount: number, mode: 'merchant_calculated', location: {country: string, postalCode?: string | undefined}}) | undefined, metadata?: Record | undefined}; export type CreateApiV1GatewayPaymentsArgs = {body: CreateGatewayPaymentRequest}; export type CreateApiV1GatewayPaymentsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createGatewayPaymentResponse = 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 createApiV1GatewayPaymentsResponse = z.object({status: z.literal("SUCCEEDED"), data: createGatewayPaymentResponse}); 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 }; /** * Create a payment * * POST /v1/gateway/payments */ export const createApiV1GatewayPayments = async (args: CreateApiV1GatewayPaymentsArgs, options?: CreateApiV1GatewayPaymentsOptions) => { const {baseUrl = "https://secure.breeze.cash", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/gateway/payments${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(createApiV1GatewayPaymentsResponse, await res.json()) };