import type { CreateTransactionParams, TransactionListParams, PayloadBuilderResponse, ExecuteOptions, CompletePreAuthParams, CompletePreAuthWithPayoutParams, CancelPreAuthParams, ReturnType, PaywayPaymentStatusCheckResponse } from "./types.js"; /** * PayWay API Client for ABA PayWay payment gateway * * This client builds request payloads with HMAC-SHA512 signatures. * It does NOT make HTTP requests - you use the returned payload to: * 1. Create an HTML form on the client-side, OR * 2. Make server-to-server API calls * * @class PayWayClient */ export declare class PayWayClient { readonly base_url: string; readonly merchant_id: string; readonly api_key: string; readonly rsa_public_key?: string; /** * Creates a new PayWayClient instance * @param base_url - Base URL of the PayWay API (e.g., https://checkout-sandbox.payway.com.kh/) * @param merchant_id - Your merchant ID from ABA Bank * @param api_key - Your API key from ABA Bank * @param rsa_public_key - Optional RSA public key from ABA Bank (required for pre-auth operations) */ constructor(base_url: string, merchant_id: string, api_key: string, rsa_public_key?: string); /** * Creates HMAC-SHA512 hash for request signing * @param values - Array of strings to hash * @returns Base64 encoded hash */ create_hash(values: string[]): string; /** * Normalizes RSA public key to proper PEM format * * Handles common formatting issues: * - Literal \n escape sequences (from copy-paste or database storage) * - Missing or incorrect line breaks * - Extra whitespace * * @param key - RSA public key string (may be malformed) * @returns Properly formatted PEM key * @throws Error if key is missing required markers * @private */ private normalizePublicKey; /** * Encrypts data with RSA public key in 117-byte chunks * * Used for pre-auth operations where sensitive data (mc_id, tran_id, complete_amount) * must be encrypted using ABA Bank's RSA public key. * * @param data - Object to encrypt (will be JSON encoded) * @returns Base64 encoded encrypted data * @throws Error if RSA public key is not configured * @private */ private encryptWithRSA; /** * Creates payload fields with hash signature * @param body - Request body parameters * @param date - Date for the request (defaults to current date) * @returns Plain object with all fields including hash * @private */ private create_payload; /** * Builds a payment transaction payload * * Use this to create a client-side form that submits directly to ABA PayWay. * The returned payload contains all fields (including hash) and the URL. * * @param params - Transaction parameters * @returns Payload with fields, hash, and URL for form submission * * @example * ```typescript * // Server-side (Next.js API route) * const payload = client.buildTransactionPayload({ * payment_option: "abapay", * amount: 100, * tran_id: "ORDER-123", * return_url: "https://mysite.com/callback" * }); * * // Send to client * return Response.json(payload); * * // Client-side: Create and submit form * const form = document.createElement('form'); * form.method = payload.method; * form.action = payload.url; * for (const [key, value] of Object.entries(payload.fields)) { * const input = document.createElement('input'); * input.type = 'hidden'; * input.name = key; * input.value = value; * form.appendChild(input); * } * document.body.appendChild(form); * form.submit(); * ``` */ buildTransactionPayload(params?: CreateTransactionParams): PayloadBuilderResponse; /** * Builds a check transaction payload * * Use this for server-to-server API calls to check transaction status. * * @param tran_id - Transaction ID to check * @returns Payload with fields, hash, and URL * * @example * ```typescript * const payload = client.buildCheckTransactionPayload("ORDER-123"); * * // Make server-to-server request * const formData = new FormData(); * for (const [key, value] of Object.entries(payload.fields)) { * formData.append(key, value); * } * * const response = await fetch(payload.url, { * method: payload.method, * body: formData * }); * const result = await response.json(); * ``` */ buildCheckTransactionPayload(tran_id: string): PayloadBuilderResponse; /** * Builds a transaction list payload * * Use this for server-to-server API calls to retrieve transaction lists. * * @param params - Filter parameters * @returns Payload with fields, hash, and URL * * @example * ```typescript * const payload = client.buildTransactionListPayload({ * from_date: "20240101000000", * to_date: "20240131235959", * status: "APPROVED" * }); * * // Make server-to-server request * const formData = new FormData(); * for (const [key, value] of Object.entries(payload.fields)) { * formData.append(key, value); * } * * const response = await fetch(payload.url, { * method: payload.method, * body: formData * }); * const result = await response.json(); * ``` */ buildTransactionListPayload(params?: TransactionListParams): PayloadBuilderResponse; /** * Builds a complete pre-auth transaction payload * * Use this to capture funds from a pre-authorized transaction. * The pre-auth must be in valid state (not expired or already completed). * * For card payments: You can complete with up to 10% more than the original amount. * * @param params - Complete pre-auth parameters * @returns Payload with fields, hash, and URL * * @example * ```typescript * // Complete with the authorized amount * const payload = client.buildCompletePreAuthPayload({ * tran_id: "ORDER-123", * complete_amount: 100 // Required * }); * * // Complete with increased amount (+10% allowed for cards) * const payload = client.buildCompletePreAuthPayload({ * tran_id: "ORDER-123", * complete_amount: 110 // Original was 100, can add up to 10% * }); * * // Execute the completion * const result = await client.execute(payload); * console.log('Status:', result.transaction_status); // "COMPLETED" * ``` */ buildCompletePreAuthPayload(params: CompletePreAuthParams): PayloadBuilderResponse; /** * Builds a complete pre-auth transaction with payout payload * * Use this to capture funds and distribute them according to payout rules. * Useful for marketplace scenarios where funds need to be split. * * @param params - Complete pre-auth with payout parameters * @returns Payload with fields, hash, and URL * * @example * ```typescript * const payload = client.buildCompletePreAuthWithPayoutPayload({ * tran_id: "ORDER-123", * complete_amount: 100, * payout: [ * { acc: "123456", amt: 80 }, * { acc: "789012", amt: 20 } * ] * }); * * const result = await client.execute(payload); * ``` */ buildCompletePreAuthWithPayoutPayload(params: CompletePreAuthWithPayoutParams): PayloadBuilderResponse; /** * Builds a cancel pre-auth transaction payload * * Use this to release reserved funds from a pre-authorized transaction. * The pre-auth must be in valid state (not expired or already completed/cancelled). * * @param params - Cancel pre-auth parameters * @returns Payload with fields, hash, and URL * * @example * ```typescript * const payload = client.buildCancelPreAuthPayload({ * tran_id: "ORDER-123" * }); * * const result = await client.execute(payload); * console.log('Status:', result.transaction_status); // "CANCELLED" * ``` */ buildCancelPreAuthPayload(params: CancelPreAuthParams): PayloadBuilderResponse; /** * Execute a server-to-server API call * * This method takes a payload and makes the HTTP request for you. * Useful for server-to-server calls like check_transaction and transaction_list. * * WARNING: For create_transaction with payment_option "abapay", use client-side * form submission instead, as it returns HTML. This method will throw an error * if you try to execute with payment_option "abapay" (unless allowHtml is true). * * @param payload - Payload from any build method * @param options - Execution options * @returns JSON response from ABA PayWay API * * @throws {PayWayAPIError} If HTTP request fails - includes status, statusText, and response body * @throws {Error} If payment_option is "abapay" and allowHtml is false * @throws {Error} If response is HTML and allowHtml is false * * @example * ```typescript * // Check transaction status (always server-to-server) * const result = await client.execute( * client.buildCheckTransactionPayload("ORDER-123") * ); * console.log('Status:', result.status); * * // With error handling * try { * const result = await client.execute( * client.buildCompletePreAuthPayload({ * tran_id: 'ORDER-123', * complete_amount: 100 * }) * ); * } catch (error: any) { * console.error('Error:', error.message); * console.error('Status:', error.status); * console.error('Details:', error.body); * } * * // Get transaction list * const transactions = await client.execute( * client.buildTransactionListPayload({ status: 'APPROVED' }) * ); * ``` */ execute(payload: PayloadBuilderResponse, options?: ExecuteOptions): Promise; } //# sourceMappingURL=client.d.ts.map