Plugin Directory

Changeset 3470079


Ignore:
Timestamp:
02/26/2026 09:23:53 AM (5 weeks ago)
Author:
jacobo1
Message:

Speed up KasWare confirmation — direct txid lookup + 3s fast polling

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

Legend:

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

    r3470074 r3470079  
    179179                        } else if (response.success && response.data.status === 'pending_verification') {
    180180                            // Transaction broadcast but not yet confirmed on-chain.
    181                             // The polling system will verify and complete the order.
    182                             updatePaymentStatus('Payment sent! Waiting for blockchain confirmation...', 'checking');
     181                            // Poll aggressively at 3s — Kaspa confirms in ~1s, we just
     182                            // need the API to index it.
     183                            updatePaymentStatus('Payment sent! Confirming on blockchain...', 'checking');
    183184                            var btn = document.getElementById('kaspa-kasware-btn');
    184185                            if (btn) {
     
    188189                            var statusEl = document.getElementById('kaspa-kasware-status');
    189190                            if (statusEl) statusEl.textContent = 'Transaction: ' + txid.substring(0, 16) + '...';
    190                             // Resume polling to catch the confirmation
     191                            // Fast polling — 3s intervals for 30s, then fall back to normal
    191192                            paymentCheckActive = false;
    192                             startPaymentMonitoring();
     193                            startFastPaymentPolling();
    193194                        } else {
    194195                            updatePaymentStatus('Payment sent. Waiting for blockchain confirmation...', 'checking');
     
    208209        var data = 'action=kasppaga_kasware_confirm&order_id=' + orderId + '&txid=' + encodeURIComponent(txid) + '&nonce=' + kaswareNonce;
    209210        xhr.send(data);
     211    }
     212
     213    /**
     214     * Fast polling after KasWare payment — 3s intervals for 30s max,
     215     * then falls back to normal 15s polling if still not confirmed.
     216     */
     217    function startFastPaymentPolling() {
     218        var fastPollCount = 0;
     219        var maxFastPolls = 10; // 10 polls x 3s = 30 seconds
     220
     221        paymentCheckActive = true;
     222        checkPaymentStatus(); // Check immediately
     223
     224        paymentCheckInterval = setInterval(function () {
     225            fastPollCount++;
     226            if (fastPollCount >= maxFastPolls) {
     227                // Switch to normal polling
     228                clearInterval(paymentCheckInterval);
     229                paymentCheckInterval = null;
     230                paymentCheckActive = false;
     231                updatePaymentStatus('Payment sent. Waiting for confirmation...', 'checking');
     232                startPaymentMonitoring();
     233                return;
     234            }
     235            checkPaymentStatus();
     236        }, 3000);
    210237    }
    211238
  • kaspa-payments-gateway-woocommerce/trunk/includes/kaspa-frontend-assets.php

    r3470069 r3470079  
    656656    $order->save();
    657657
    658     // Try to verify the payment on-chain before confirming the order.
    659     // The transaction may not have propagated yet (KasWare returns txid immediately
    660     // after broadcast), so if verification fails we let the existing polling system
    661     // pick it up and verify the amount later.
     658    // Verify the specific transaction by ID — much faster than scanning all
     659    // address transactions. Checks that the tx exists, is accepted, and pays
     660    // the correct address the correct amount.
     661    // If the tx hasn't propagated yet (KasWare returns txid immediately after
     662    // broadcast), the faster client-side polling will catch it within seconds.
    662663    $verified = false;
    663     if ($payment_address && $expected_amount) {
    664         try {
    665             $polling = new KASPPAGA_Transaction_Polling();
    666             $since = $order->get_meta('_kaspa_payment_started');
    667             $result = $polling->check_payment_received($payment_address, floatval($expected_amount), $since ? intval($since) : null);
    668             if (is_array($result) && !empty($result['found'])) {
    669                 $verified = true;
    670             }
    671         } catch (Exception $e) {
    672             // Verification failed — polling will handle it
    673         }
     664    try {
     665        $polling = new KASPPAGA_Transaction_Polling();
     666        $verified = $polling->verify_transaction_by_id($txid, $payment_address, $expected_amount);
     667    } catch (Exception $e) {
     668        // Verification failed — client-side polling will handle it
    674669    }
    675670
  • kaspa-payments-gateway-woocommerce/trunk/includes/kaspa-transaction-polling.php

    r3470074 r3470079  
    5959        );
    6060        return $schedules;
     61    }
     62
     63    /**
     64     * Verify a specific transaction by its ID using the Kaspa API.
     65     * Much faster than scanning all address transactions.
     66     * Returns true if the transaction exists and is accepted.
     67     */
     68    public function verify_transaction_by_id($txid, $expected_address = null, $expected_amount = null)
     69    {
     70        if (!$txid || !preg_match('/^[a-f0-9]{64}$/i', $txid)) {
     71            return false;
     72        }
     73
     74        $url = $this->api_base_url . '/transactions/' . urlencode($txid) . '?inputs=false&outputs=true&resolve_previous_outpoints=no';
     75
     76        $response = wp_remote_get($url, array(
     77            'timeout' => 10,
     78            'headers' => array(
     79                'User-Agent' => 'Kaspa-Payments-Gateway-WooCommerce/1.1',
     80                'Accept' => 'application/json'
     81            )
     82        ));
     83
     84        if (is_wp_error($response)) {
     85            return false;
     86        }
     87
     88        $code = wp_remote_retrieve_response_code($response);
     89        if ($code !== 200) {
     90            return false;
     91        }
     92
     93        $data = json_decode(wp_remote_retrieve_body($response), true);
     94        if (!$data) {
     95            return false;
     96        }
     97
     98        // If we just need to confirm the tx exists and is accepted
     99        if (!$expected_address && !$expected_amount) {
     100            return !empty($data['is_accepted']);
     101        }
     102
     103        // Verify the tx actually pays the expected address the expected amount
     104        if (isset($data['outputs']) && is_array($data['outputs'])) {
     105            foreach ($data['outputs'] as $output) {
     106                $output_address = isset($output['script_public_key_address']) ? $output['script_public_key_address'] : '';
     107                $output_amount = isset($output['amount']) ? $output['amount'] / 100000000 : 0;
     108
     109                $address_matches = !$expected_address || $output_address === $expected_address;
     110                $amount_matches = !$expected_amount || abs($output_amount - floatval($expected_amount)) < 0.00000001;
     111
     112                if ($address_matches && $amount_matches) {
     113                    return true;
     114                }
     115            }
     116        }
     117
     118        return false;
    61119    }
    62120
Note: See TracChangeset for help on using the changeset viewer.