Plugin Directory

Changeset 3470285


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

Speed up KasWare confirmation: 2s txid polling instead of 3s balance check

File:
1 edited

Legend:

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

    r3470198 r3470285  
    1414    let paymentCheckActive = false;
    1515    let kaswarePaymentInProgress = false;
     16    let kaswareTxid = null;
    1617
    1718    // Get data from WordPress
     
    135136
    136137            // Step 3: Payment sent — confirm with server
     138            kaswareTxid = txid;
    137139            btn.className = 'kaspa-kasware-btn success';
    138140            btn.textContent = 'Payment sent!';
     
    218220
    219221    /**
    220      * Fast polling after KasWare payment — 3s intervals for 30s max,
    221      * then falls back to normal 15s polling if still not confirmed.
     222     * Fast polling after KasWare payment — 2s intervals for 20s max,
     223     * uses direct txid verification for speed, then falls back to normal polling.
    222224     */
    223225    function startFastPaymentPolling() {
    224226        var fastPollCount = 0;
    225         var maxFastPolls = 10; // 10 polls x 3s = 30 seconds
     227        var maxFastPolls = 10; // 10 polls x 2s = 20 seconds
    226228
    227229        paymentCheckActive = true;
    228         checkPaymentStatus(); // Check immediately
     230        fastCheckTxid(); // Check immediately
    229231
    230232        paymentCheckInterval = setInterval(function () {
    231233            fastPollCount++;
    232234            if (fastPollCount >= maxFastPolls) {
    233                 // Switch to normal polling
    234235                clearInterval(paymentCheckInterval);
    235236                paymentCheckInterval = null;
    236237                paymentCheckActive = false;
    237                 updatePaymentStatus('Payment sent. Waiting for confirmation...', 'checking');
    238238                startPaymentMonitoring();
    239239                return;
    240240            }
     241            fastCheckTxid();
     242        }, 2000);
     243    }
     244
     245    /**
     246     * Fast txid verification — retries the direct txid lookup instead of
     247     * scanning address balance. Much faster for KasWare payments.
     248     */
     249    function fastCheckTxid() {
     250        if (!kaswareTxid) {
    241251            checkPaymentStatus();
    242         }, 3000);
     252            return;
     253        }
     254
     255        var xhr = new XMLHttpRequest();
     256        xhr.open('POST', ajaxUrl, true);
     257        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     258        xhr.onreadystatechange = function () {
     259            if (xhr.readyState === 4 && xhr.status === 200) {
     260                try {
     261                    var response = JSON.parse(xhr.responseText);
     262                    if (response.success && response.data.status === 'completed') {
     263                        handlePaymentConfirmed({ txid: kaswareTxid, status: 'completed' });
     264                    }
     265                    // If still pending, do nothing — next poll will retry
     266                } catch (e) {}
     267            }
     268        };
     269        var data = 'action=kasppaga_kasware_confirm&order_id=' + orderId + '&txid=' + encodeURIComponent(kaswareTxid) + '&nonce=' + kaswareNonce;
     270        xhr.send(data);
    243271    }
    244272
Note: See TracChangeset for help on using the changeset viewer.