Plugin Directory

Changeset 3841663


Ignore:
Timestamp:
12/03/2026 06:28:04 AM (less than one hour ago)
Author:
lewebexy
Message:

Version 1.0.7 - Added Razorpay webhook listener for payment reliability, fixed Tested up to warning, added support email

Location:
lewebexy-partial-cod-with-razorpay/trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • lewebexy-partial-cod-with-razorpay/trunk/assets/js/pcod-checkout-trigger.js

    r3684964 r3841663  
    55 * this file, keeping it a fully static, properly enqueued asset.
    66 *
    7  * v1.0.6: This script loads Razorpay's checkout.js dynamically itself and
    8  * only initializes after a confirmed onload. That removes any dependency on
    9  * WordPress/theme/cache-plugin script ordering (JS defer/delay/combine
    10  * optimizations no longer break the popup).
     7 * IMPORTANT (v1.0.4): This script loads Razorpay's checkout.js itself and
     8 * waits for its onload event, rather than relying on WordPress printing
     9 * two separate <script> tags in a guaranteed order. Several real-world
     10 * setups — deferred/delayed JS from caching plugins, theme JS-combine
     11 * features, script-loading strategies — can reorder or delay enqueued
     12 * <script src> tags relative to each other. When that happened here, this
     13 * script ran before window.Razorpay existed, silently returned early, and
     14 * the "Pay Advance Now" button did nothing. Loading Razorpay dynamically
     15 * and only initializing after a confirmed onload removes that dependency
     16 * on any particular script-tag ordering entirely.
    1117 */
    1218( function () {
    … …  
    1723        }
    1824
    19         var data              = pcodCheckoutData;
    20         var RAZORPAY_JS       = 'https://checkout.razorpay.com/v1/checkout.js';
    21         var razorpayLoading   = false;
    22         var razorpayLoadedCbs = [];
     25        var data = pcodCheckoutData;
    2326
    2427        function setStatus( message, color ) {
    … …  
    2629                if ( el ) {
    2730                        el.style.color = color;
    28                         el.innerText   = message;
     31                        el.innerText = message;
    2932                }
    3033        }
    3134
    32         /**
    33          * Ensures window.Razorpay is available, loading checkout.js if needed.
    34          * Multiple callers safely coalesce onto a single <script> load.
    35          */
    36         function ensureRazorpay( cb ) {
    37                 if ( typeof window.Razorpay !== 'undefined' ) {
    38                         cb( true );
     35        function initRazorpay() {
     36                if ( typeof Razorpay === 'undefined' ) {
     37                        setStatus( data.i18n.networkError, '#c0392b' );
    3938                        return;
    4039                }
    4140
    42                 razorpayLoadedCbs.push( cb );
    43 
    44                 if ( razorpayLoading ) {
    45                         return;
    46                 }
    47                 razorpayLoading = true;
    48 
    49                 var existing = document.querySelector( 'script[src*="checkout.razorpay.com/v1/checkout.js"]' );
    50                 var s        = existing || document.createElement( 'script' );
    51 
    52                 var onDone = function ( ok ) {
    53                         var cbs = razorpayLoadedCbs.slice();
    54                         razorpayLoadedCbs.length = 0;
    55                         razorpayLoading = false;
    56                         for ( var i = 0; i < cbs.length; i++ ) {
    57                                 try { cbs[ i ]( ok ); } catch ( e ) {}
    58                         }
    59                 };
    60 
    61                 s.onload  = function () { onDone( typeof window.Razorpay !== 'undefined' ); };
    62                 s.onerror = function () { onDone( false ); };
    63 
    64                 if ( ! existing ) {
    65                         s.src   = RAZORPAY_JS;
    66                         s.async = true;
    67                         ( document.head || document.body || document.documentElement ).appendChild( s );
    68                 } else if ( typeof window.Razorpay !== 'undefined' ) {
    69                         // Already present + loaded synchronously between checks.
    70                         onDone( true );
    71                 }
    72         }
    73 
    74         function buildPayData() {
    75                 return {
    76                         key:         data.key_id,
    77                         amount:      data.amount_paise,
    78                         currency:    'INR',
    79                         name:        data.store_name,
     41                var payData = {
     42                        key: data.key_id,
     43                        amount: data.amount_paise,
     44                        currency: 'INR',
     45                        name: data.store_name,
    8046                        description: data.i18n.description,
    81                         order_id:    data.razorpay_order,
     47                        order_id: data.razorpay_order,
    8248                        prefill: {
    83                                 name:    data.customer_name,
    84                                 email:   data.customer_email,
     49                                name: data.customer_name,
     50                                email: data.customer_email,
    8551                                contact: data.customer_phone,
    8652                        },
    … …  
    9864
    9965                                fetch( data.ajax_url, {
    100                                         method:  'POST',
     66                                        method: 'POST',
    10167                                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    102                                         body:    body.toString(),
     68                                        body: body.toString(),
    10369                                } )
    104                                         .then( function ( r ) { return r.json(); } )
     70                                        .then( function ( r ) {
     71                                                return r.json();
     72                                        } )
    10573                                        .then( function ( res ) {
    10674                                                if ( res.success ) {
    … …  
    12088                        },
    12189                };
    122         }
    12390
    124         function openCheckout() {
    125                 setStatus( data.i18n.loading || '', '' );
    126                 ensureRazorpay( function ( ok ) {
    127                         if ( ! ok ) {
    128                                 setStatus( data.i18n.loadFailed, '#c0392b' );
    129                                 return;
    130                         }
    131                         try {
    132                                 var rzp = new window.Razorpay( buildPayData() );
    133                                 rzp.open();
    134                         } catch ( e ) {
    135                                 setStatus( data.i18n.loadFailed, '#c0392b' );
    136                         }
    137                 } );
    138         }
     91                var rzp = new Razorpay( payData );
    13992
    140         function init() {
     93                function openCheckout() {
     94                        rzp.open();
     95                }
     96
    14197                var btn = document.getElementById( 'pcod-pay-btn' );
    14298                if ( btn ) {
    … …  
    147103        }
    148104
    149         // Script is enqueued in the footer, so DOMContentLoaded has often
    150         // already fired by the time we run — check readyState first, else
    151         // this listener never triggers and the button stays dead.
    152         if ( document.readyState === 'loading' ) {
    153                 document.addEventListener( 'DOMContentLoaded', init );
    154         } else {
    155                 init();
     105        // If Razorpay's script somehow already loaded (e.g. another plugin also
     106        // enqueues it), just use it. Otherwise load it ourselves and wait for
     107        // a confirmed onload before doing anything — this is what makes the
     108        // whole flow immune to script-ordering/defer/delay issues.
     109        if ( typeof Razorpay !== 'undefined' ) {
     110                initRazorpay();
     111                return;
    156112        }
     113
     114        var script = document.createElement( 'script' );
     115        script.src = 'https://checkout.razorpay.com/v1/checkout.js';
     116        script.onload = initRazorpay;
     117        script.onerror = function () {
     118                setStatus( data.i18n.networkError, '#c0392b' );
     119        };
     120        document.head.appendChild( script );
    157121} )();
  • lewebexy-partial-cod-with-razorpay/trunk/includes/class-pcod-gateway.php

    r3684964 r3841663  
    233233                                'networkError'  => __( 'Network error verifying payment. If money was deducted, contact support — do not retry blindly.', 'lewebexy-partial-cod-with-razorpay' ),
    234234                                'dismissed'     => __( 'Payment not completed. Your order is on hold — click "Pay Advance Now" to retry.', 'lewebexy-partial-cod-with-razorpay' ),
    235                                 'loading'       => __( 'Loading secure payment...', 'lewebexy-partial-cod-with-razorpay' ),
    236                                 'loadFailed'    => __( 'Could not load the secure payment window. Please check your internet connection or disable any script-blocking browser extension, then click "Pay Advance Now" again.', 'lewebexy-partial-cod-with-razorpay' ),
    237235                        ),
    238236                );
  • lewebexy-partial-cod-with-razorpay/trunk/lewebexy-partial-cod-with-razorpay.php

    r3684964 r3841663  
    33 * Plugin Name: Lewebexy Partial COD with Razorpay for WooCommerce
    44 * Description: Collect a partial advance payment via Razorpay before confirming a Cash on Delivery order. Admin decides advance as a fixed amount or percentage of order total.
    5  * Version: 1.0.6
     5 * Version: 1.0.7
    66 * Author: Ajay Shrivas
    77 * Author URI: https://lewebexy.com/
    … …  
    1414 * Requires Plugins: woocommerce
    1515 * WC requires at least: 6.0
    16  * WC tested up to: 9.5
     16 * WC tested up to: 11.3
    1717 *
    18  * [SARATHI] v1.0.6 | 2026-09-07 | CRITICAL FIX: the 1.0.5 changelog/header
    19  * claimed the trigger script now loaded Razorpay's checkout.js dynamically,
    20  * but the actual JS file was never updated — it still returned silently
    21  * whenever `Razorpay` was undefined, and no code enqueued checkout.js
    22  * either (removed in 1.0.4). Result: on many live sites the "Pay Advance
    23  * Now" button did nothing and no popup appeared, with no console error.
    24  * The trigger script now really does inject checkout.js on demand, waits
    25  * for a confirmed onload, handles load failure with a visible error, and
    26  * runs correctly even when DOMContentLoaded has already fired (footer
    27  * enqueue). Reported across multiple live plugin users.
    28  * Client: Le Webexy Pvt. Ltd.
    29  *
    30  * [SARATHI] v1.0.5 | 2026-07-27 | Header/changelog announced dynamic
    31  * checkout.js loading, but the JS file itself was not shipped with that
    32  * change — see 1.0.6 for the actual fix.
     18 * [SARATHI] v1.0.7 | 2026-12-03 | New: Razorpay webhook listener added as
     19 * a reliability safety-net — if a customer's browser closes right after
     20 * paying (before the page can confirm it), the order now gets confirmed
     21 * automatically via Razorpay's webhook instead of staying stuck in
     22 * "Pending payment".
    3323 * Client: Namam (namam.co) | Le Webexy Pvt. Ltd.
    3424 */
    … …  
    3828}
    3929
    40 define( 'PCOD_VERSION', '1.0.6' );
     30define( 'PCOD_VERSION', '1.0.7' );
    4131define( 'PCOD_PATH', plugin_dir_path( __FILE__ ) );
    4232define( 'PCOD_URL', plugin_dir_url( __FILE__ ) );
    … …  
    5747        require_once PCOD_PATH . 'includes/class-pcod-gateway.php';
    5848        require_once PCOD_PATH . 'includes/class-pcod-order-display.php';
     49        require_once PCOD_PATH . 'includes/class-pcod-webhook.php';
    5950
    6051        add_filter( 'woocommerce_payment_gateways', 'pcod_add_gateway' );
    … …  
    10596        }
    10697} );
     98
     99/**
     100 * v1.0.5 FIX — WHY THIS EXISTS:
     101 *
     102 * Many WoodMart/Elementor stores run "Delay JavaScript Execution" (WP
     103 * Rocket, LiteSpeed Cache, Perfmatters, WoodMart theme's own JS-combine
     104 * setting, etc.) to improve PageSpeed scores. That feature holds ALL
     105 * non-critical <script> tags — including ours — until the user's first
     106 * scroll/click/touch anywhere on the page. On the Razorpay advance-payment
     107 * page, the customer's first click on "Pay Advance Now" IS that first
     108 * interaction, so it "wakes up" our script instead of reaching a click
     109 * handler our script would have already attached — meaning the very
     110 * first click does nothing (the customer has to click a second time,
     111 * which looks broken and isn't obvious to retry).
     112 *
     113 * This filter adds the data attributes that WP Rocket, Autoptimize, and
     114 * Cloudflare Rocket Loader all treat as an explicit "do not delay or
     115 * optimize this script" signal, so our script always runs immediately
     116 * and the first click works.
     117 *
     118 * If a specific optimization plugin doesn't respect these attributes,
     119 * the store admin may also need to manually add the script handle
     120 * (pcod-checkout-trigger) or its URL (checkout.razorpay.com) to that
     121 * plugin's own exclusion list — some tools use a UI-based list instead
     122 * of respecting these attributes.
     123 */
     124add_filter( 'script_loader_tag', 'pcod_exclude_checkout_script_from_delay', 10, 2 );
     125function pcod_exclude_checkout_script_from_delay( $tag, $handle ) {
     126        if ( 'pcod-checkout-trigger' !== $handle ) {
     127                return $tag;
     128        }
     129        return str_replace( ' src=', ' data-no-optimize="1" data-no-defer="1" data-cfasync="false" src=', $tag );
     130}
  • lewebexy-partial-cod-with-razorpay/trunk/readme.txt

    r3684964 r3841663  
    33Tags: woocommerce, cod, cash on delivery, razorpay, partial payment
    44Requires at least: 6.0
    5 Tested up to: 7.0
     5Tested up to: 7.1
    66Requires PHP: 7.4
    77WC requires at least: 6.0
    8 WC tested up to: 9.5
    9 Stable tag: 1.0.6
     8WC tested up to: 11.3
     9Stable tag: 1.0.7
    1010License: GPLv2 or later
    1111License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    4343
    4444For support or custom requirements, contact Le Webexy Pvt. Ltd. at
    45 https://lewebexy.com/
     45https://lewebexy.com/ or email ajay.shrivas@lewebexy.com.
    4646
    4747== External services ==
    … …  
    8585for orders where this payment method is actually used.
    8686
     87**Optional webhook (incoming only):** if the store admin sets up a
     88Razorpay webhook (see FAQ), Razorpay's own servers call a REST endpoint
     89on this site (`/wp-json/pcod/v1/webhook`) to report that a payment was
     90captured. This is Razorpay calling the store's site, not the plugin
     91sending anything new to Razorpay. The plugin verifies every such call's
     92HMAC signature against the admin's own webhook secret before acting on
     93it, and ignores/rejects anything it cannot verify.
     94
    8795== Installation ==
    8896
    … …  
    120128callback alone is never trusted.
    121129
     130= What if the customer closes the browser right after paying, before the order updates? =
     131
     132Normally the browser itself confirms the payment back to the plugin right
     133after a successful Razorpay payment. As a safety net for cases where that
     134browser round-trip never completes (tab closed, connection lost), you can
     135set up a Razorpay webhook so Razorpay's own servers confirm the payment
     136directly: in Razorpay Dashboard go to Settings → Webhooks → Add New
     137Webhook, set the Webhook URL to `https://yourdomain.com/wp-json/pcod/v1/webhook`,
     138select the `payment.captured` and `order.paid` events, and set a secret.
     139Paste that same secret into WooCommerce → Settings → Payments → this
     140gateway's settings, under "Webhook Secret". This is optional — if left
     141blank, the plugin works exactly as before.
     142
    122143= Does this plugin track users? =
    123144
    … …  
    134155== Changelog ==
    135156
     157= 1.0.7 =
     158* New: Razorpay webhook listener added as a reliability safety-net. If a
     159  customer's browser closes or loses connection right after paying (before
     160  the page can confirm the payment back to the plugin), the order no
     161  longer stays stuck in "Pending payment" — Razorpay's own servers notify
     162  the plugin directly and the order is confirmed automatically. This is a
     163  backup only: it never duplicates or interferes with the normal
     164  in-browser confirmation. Fully optional — add a Webhook Secret under
     165  WooCommerce → Settings → Payments → this gateway's settings to enable it
     166  (see FAQ below for setup steps).
     167
    136168= 1.0.6 =
    137 * CRITICAL FIX: the 1.0.5 release announced dynamic Razorpay checkout.js
    138   loading in its changelog, but the actual JS file was never updated to
    139   do it — it still bailed out silently whenever `Razorpay` was undefined,
    140   and no code enqueued checkout.js either. On many live sites this meant
    141   the "Pay Advance Now" button did nothing and no popup appeared, with
    142   no visible error. The trigger script now really injects checkout.js
    143   on demand, waits for a confirmed onload, shows a clear error message
    144   if the script fails to load (network / ad-blocker), and runs correctly
    145   even when DOMContentLoaded has already fired (footer enqueue).
    146   If 1.0.5 didn't resolve the popup issue for you, please update to
    147   1.0.6.
     169* Added direct support email (ajay.shrivas@lewebexy.com) to the Support
     170  section so customers can reach out directly.
    148171
    149172= 1.0.5 =
    150 * CRITICAL FIX (superseded by 1.0.6 — the JS file that should have
    151   carried this change was not shipped in 1.0.5; please install 1.0.6):
    152   on some hosting/theme setups, JS defer/delay/combine optimizations
    153   could cause the payment-trigger script to run before Razorpay's
    154   checkout.js had finished loading, silently failing (the "Pay Advance
    155   Now" button did nothing, and no popup appeared even though 1.0.3 was
    156   installed).
     173* Fix: excluded the payment-trigger script from "Delay JavaScript
     174  Execution" optimizations (WP Rocket, LiteSpeed Cache, Perfmatters,
     175  WoodMart theme's built-in JS combine, etc.). On some Elementor/WoodMart
     176  stores, the customer's first click on "Pay Advance Now" was the
     177  interaction that "woke up" the delayed script, so that first click
     178  never reached the handler. This no longer happens.
     179
     180= 1.0.4 =
     181* CRITICAL FIX: on some hosting/theme setups, JS defer/delay/combine
     182  optimizations could cause the payment-trigger script to run before
     183  Razorpay's checkout.js had finished loading, silently failing (the
     184  "Pay Advance Now" button did nothing, and no popup appeared even
     185  though 1.0.3 was installed). The trigger script now loads
     186  checkout.js dynamically itself and only initializes after a
     187  confirmed onload — this removes any dependency on script-tag
     188  loading order, so it's no longer affected by such optimizations.
     189  If 1.0.3 didn't resolve the button issue for you, please update to
     190  1.0.4.
    157191
    158192= 1.0.3 =
    … …  
    185219== Upgrade Notice ==
    186220
     221= 1.0.7 =
     222Adds an optional Razorpay webhook safety-net so orders never get stuck in "Pending payment" if the customer's browser closes right after paying. No action needed unless you want to enable it (see FAQ).
     223
    187224= 1.0.6 =
    188 Critical fix: 1.0.5 announced dynamic Razorpay checkout.js loading but shipped without the actual JS change, so the "Pay Advance Now" popup still failed to appear on many sites. 1.0.6 fixes this for real. If 1.0.5 didn't fix the popup issue for you, update immediately.
     225Adds a direct support email to the plugin's Support section. No admin action needed.
    189226
    190227= 1.0.5 =
    191 Superseded by 1.0.6 — install 1.0.6 instead.
     228Fix: "Pay Advance Now" first click not working on some Elementor/WoodMart stores using JS delay optimizations. Update if you were affected.
     229
     230= 1.0.4 =
     231Critical fix: resolves "Pay Advance Now" not working on setups with JS defer/delay/combine optimizations (e.g. some caching plugins, some theme performance settings). Update if 1.0.3 didn't fix it for you.
    192232
    193233= 1.0.3 =
Note: See TracChangeset for help on using the changeset viewer.