Changeset 3841663
- Timestamp:
- 12/03/2026 06:28:04 AM (less than one hour ago)
- Location:
- lewebexy-partial-cod-with-razorpay/trunk
- Files:
-
- 1 added
- 4 edited
-
assets/js/pcod-checkout-trigger.js (modified) (6 diffs)
-
includes/class-pcod-gateway.php (modified) (1 diff)
-
includes/class-pcod-webhook.php (added)
-
lewebexy-partial-cod-with-razorpay.php (modified) (5 diffs)
-
readme.txt (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lewebexy-partial-cod-with-razorpay/trunk/assets/js/pcod-checkout-trigger.js
r3684964 r3841663 5 5 * this file, keeping it a fully static, properly enqueued asset. 6 6 * 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. 11 17 */ 12 18 ( function () { … … 17 23 } 18 24 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; 23 26 24 27 function setStatus( message, color ) { … … 26 29 if ( el ) { 27 30 el.style.color = color; 28 el.innerText = message;31 el.innerText = message; 29 32 } 30 33 } 31 34 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' ); 39 38 return; 40 39 } 41 40 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, 80 46 description: data.i18n.description, 81 order_id: data.razorpay_order,47 order_id: data.razorpay_order, 82 48 prefill: { 83 name: data.customer_name,84 email: data.customer_email,49 name: data.customer_name, 50 email: data.customer_email, 85 51 contact: data.customer_phone, 86 52 }, … … 98 64 99 65 fetch( data.ajax_url, { 100 method: 'POST',66 method: 'POST', 101 67 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 102 body: body.toString(),68 body: body.toString(), 103 69 } ) 104 .then( function ( r ) { return r.json(); } ) 70 .then( function ( r ) { 71 return r.json(); 72 } ) 105 73 .then( function ( res ) { 106 74 if ( res.success ) { … … 120 88 }, 121 89 }; 122 }123 90 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 ); 139 92 140 function init() { 93 function openCheckout() { 94 rzp.open(); 95 } 96 141 97 var btn = document.getElementById( 'pcod-pay-btn' ); 142 98 if ( btn ) { … … 147 103 } 148 104 149 // Script is enqueued in the footer, so DOMContentLoaded has often150 // already fired by the time we run — check readyState first, else151 // 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; 156 112 } 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 ); 157 121 } )(); -
lewebexy-partial-cod-with-razorpay/trunk/includes/class-pcod-gateway.php
r3684964 r3841663 233 233 'networkError' => __( 'Network error verifying payment. If money was deducted, contact support — do not retry blindly.', 'lewebexy-partial-cod-with-razorpay' ), 234 234 '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' ),237 235 ), 238 236 ); -
lewebexy-partial-cod-with-razorpay/trunk/lewebexy-partial-cod-with-razorpay.php
r3684964 r3841663 3 3 * Plugin Name: Lewebexy Partial COD with Razorpay for WooCommerce 4 4 * 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. 65 * Version: 1.0.7 6 6 * Author: Ajay Shrivas 7 7 * Author URI: https://lewebexy.com/ … … 14 14 * Requires Plugins: woocommerce 15 15 * WC requires at least: 6.0 16 * WC tested up to: 9.516 * WC tested up to: 11.3 17 17 * 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". 33 23 * Client: Namam (namam.co) | Le Webexy Pvt. Ltd. 34 24 */ … … 38 28 } 39 29 40 define( 'PCOD_VERSION', '1.0. 6' );30 define( 'PCOD_VERSION', '1.0.7' ); 41 31 define( 'PCOD_PATH', plugin_dir_path( __FILE__ ) ); 42 32 define( 'PCOD_URL', plugin_dir_url( __FILE__ ) ); … … 57 47 require_once PCOD_PATH . 'includes/class-pcod-gateway.php'; 58 48 require_once PCOD_PATH . 'includes/class-pcod-order-display.php'; 49 require_once PCOD_PATH . 'includes/class-pcod-webhook.php'; 59 50 60 51 add_filter( 'woocommerce_payment_gateways', 'pcod_add_gateway' ); … … 105 96 } 106 97 } ); 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 */ 124 add_filter( 'script_loader_tag', 'pcod_exclude_checkout_script_from_delay', 10, 2 ); 125 function 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 3 3 Tags: woocommerce, cod, cash on delivery, razorpay, partial payment 4 4 Requires at least: 6.0 5 Tested up to: 7. 05 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 7 WC requires at least: 6.0 8 WC tested up to: 9.59 Stable tag: 1.0. 68 WC tested up to: 11.3 9 Stable tag: 1.0.7 10 10 License: GPLv2 or later 11 11 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 43 43 44 44 For support or custom requirements, contact Le Webexy Pvt. Ltd. at 45 https://lewebexy.com/ 45 https://lewebexy.com/ or email ajay.shrivas@lewebexy.com. 46 46 47 47 == External services == … … 85 85 for orders where this payment method is actually used. 86 86 87 **Optional webhook (incoming only):** if the store admin sets up a 88 Razorpay webhook (see FAQ), Razorpay's own servers call a REST endpoint 89 on this site (`/wp-json/pcod/v1/webhook`) to report that a payment was 90 captured. This is Razorpay calling the store's site, not the plugin 91 sending anything new to Razorpay. The plugin verifies every such call's 92 HMAC signature against the admin's own webhook secret before acting on 93 it, and ignores/rejects anything it cannot verify. 94 87 95 == Installation == 88 96 … … 120 128 callback alone is never trusted. 121 129 130 = What if the customer closes the browser right after paying, before the order updates? = 131 132 Normally the browser itself confirms the payment back to the plugin right 133 after a successful Razorpay payment. As a safety net for cases where that 134 browser round-trip never completes (tab closed, connection lost), you can 135 set up a Razorpay webhook so Razorpay's own servers confirm the payment 136 directly: in Razorpay Dashboard go to Settings → Webhooks → Add New 137 Webhook, set the Webhook URL to `https://yourdomain.com/wp-json/pcod/v1/webhook`, 138 select the `payment.captured` and `order.paid` events, and set a secret. 139 Paste that same secret into WooCommerce → Settings → Payments → this 140 gateway's settings, under "Webhook Secret". This is optional — if left 141 blank, the plugin works exactly as before. 142 122 143 = Does this plugin track users? = 123 144 … … 134 155 == Changelog == 135 156 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 136 168 = 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. 148 171 149 172 = 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. 157 191 158 192 = 1.0.3 = … … 185 219 == Upgrade Notice == 186 220 221 = 1.0.7 = 222 Adds 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 187 224 = 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.225 Adds a direct support email to the plugin's Support section. No admin action needed. 189 226 190 227 = 1.0.5 = 191 Superseded by 1.0.6 — install 1.0.6 instead. 228 Fix: "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 = 231 Critical 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. 192 232 193 233 = 1.0.3 =
Note:
See TracChangeset
for help on using the changeset viewer.