Plugin Directory

Changeset 3470315


Ignore:
Timestamp:
02/26/2026 01:35:12 PM (5 weeks ago)
Author:
jacobo1
Message:

Faster KasWare confirmation: 1s polling, 3s API timeout, fire-and-forget notify

Location:
kaspa-payments-gateway-woocommerce/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • kaspa-payments-gateway-woocommerce/trunk/assets/kaspa-checkout.js

    r3470285 r3470315  
    135135            }
    136136
    137             // Step 3: Payment sent — confirm with server
     137            // Step 3: Payment sent — start fast verification
    138138            kaswareTxid = txid;
    139139            btn.className = 'kaspa-kasware-btn success';
     
    144144            }
    145145
    146             // Stop polling — we have the txid
     146            // Stop any existing polling
    147147            stopPaymentChecking();
    148             updatePaymentStatus('Payment sent via KasWare. Verifying on blockchain...', 'checking');
    149 
    150             // Notify server and verify
    151             confirmKasWarePayment(txid);
     148            updatePaymentStatus('Payment sent! Confirming on blockchain...', 'checking');
     149
     150            // Notify server of txid (fire-and-forget, don't wait)
     151            notifyServerTxid(txid);
     152
     153            // Start fast 1s polling immediately — don't wait for server response
     154            startFastPaymentPolling();
    152155
    153156        } catch (err) {
     
    163166            } else if (errorMsg.toLowerCase().includes('insufficient')) {
    164167                if (statusEl) statusEl.textContent = 'Insufficient funds in your KasWare wallet.';
     168            } else if (errorMsg.toLowerCase().includes('storage mass')) {
     169                if (statusEl) statusEl.textContent = 'Wallet has too many small UTXOs. Send your full balance to yourself in KasWare to consolidate, then try again.';
    165170            } else {
    166171                if (statusEl) statusEl.textContent = 'Error: ' + errorMsg;
     
    169174    }
    170175
    171     function confirmKasWarePayment(txid) {
     176    /**
     177     * Fire-and-forget: notify server of the KasWare txid so it's stored on the order.
     178     * Does not block — fast polling handles verification separately.
     179     */
     180    function notifyServerTxid(txid) {
    172181        var xhr = new XMLHttpRequest();
    173182        xhr.open('POST', ajaxUrl, true);
    174183        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    175 
    176         xhr.onreadystatechange = function () {
    177             if (xhr.readyState === 4) {
    178                 if (xhr.status === 200) {
    179                     try {
    180                         var response = JSON.parse(xhr.responseText);
    181                         if (response.success && response.data.status === 'completed') {
    182                             // Verified on-chain — order confirmed
    183                             handlePaymentConfirmed({ txid: txid, status: 'completed' });
    184                         } else if (response.success && response.data.status === 'pending_verification') {
    185                             // Transaction broadcast but not yet confirmed on-chain.
    186                             // Poll aggressively at 3s — Kaspa confirms in ~1s, we just
    187                             // need the API to index it.
    188                             updatePaymentStatus('Payment sent! Confirming on blockchain...', 'checking');
    189                             var btn = document.getElementById('kaspa-kasware-btn');
    190                             if (btn) {
    191                                 btn.className = 'kaspa-kasware-btn success';
    192                                 btn.textContent = 'Payment sent!';
    193                             }
    194                             var statusEl = document.getElementById('kaspa-kasware-status');
    195                             if (statusEl) {
    196                                 statusEl.className = 'kaspa-kasware-status verifying';
    197                                 statusEl.innerHTML = '<span class="kaspa-kasware-spinner"></span> Confirming: ' + txid.substring(0, 16) + '...';
    198                             }
    199                             // Fast polling — 3s intervals for 30s, then fall back to normal
    200                             paymentCheckActive = false;
    201                             startFastPaymentPolling();
    202                         } else {
    203                             updatePaymentStatus('Payment sent. Waiting for blockchain confirmation...', 'checking');
    204                             startPaymentMonitoring();
    205                         }
    206                     } catch (e) {
    207                         updatePaymentStatus('Payment sent. Waiting for blockchain confirmation...', 'checking');
    208                         startPaymentMonitoring();
    209                     }
    210                 } else {
    211                     updatePaymentStatus('Payment sent. Waiting for blockchain confirmation...', 'checking');
    212                     startPaymentMonitoring();
    213                 }
    214             }
    215         };
    216 
     184        // No onreadystatechange — fire and forget
    217185        var data = 'action=kasppaga_kasware_confirm&order_id=' + orderId + '&txid=' + encodeURIComponent(txid) + '&nonce=' + kaswareNonce;
    218186        xhr.send(data);
     
    220188
    221189    /**
    222      * Fast polling after KasWare payment — 2s intervals for 20s max,
     190     * Fast polling after KasWare payment — 1s intervals for 15s max,
    223191     * uses direct txid verification for speed, then falls back to normal polling.
    224192     */
    225193    function startFastPaymentPolling() {
    226194        var fastPollCount = 0;
    227         var maxFastPolls = 10; // 10 polls x 2s = 20 seconds
     195        var maxFastPolls = 15; // 15 polls x 1s = 15 seconds
    228196
    229197        paymentCheckActive = true;
     
    240208            }
    241209            fastCheckTxid();
    242         }, 2000);
     210        }, 1000);
    243211    }
    244212
  • kaspa-payments-gateway-woocommerce/trunk/includes/kaspa-transaction-polling.php

    r3470079 r3470315  
    7575
    7676        $response = wp_remote_get($url, array(
    77             'timeout' => 10,
     77            'timeout' => 3,
    7878            'headers' => array(
    79                 'User-Agent' => 'Kaspa-Payments-Gateway-WooCommerce/1.1',
     79                'User-Agent' => 'Kaspa-Payments-Gateway-WooCommerce/1.2',
    8080                'Accept' => 'application/json'
    8181            )
Note: See TracChangeset for help on using the changeset viewer.