1jQuery(document).ready(function () { 2 let sessionIdPromise = null; 3 let manualCloseInitiated = false; 4 let skipGoKwikCheckout = localStorage.getItem('skipGoKwikCheckout') === 'true'; 5 6 function fetchUserSessionId() { 7 if (gc_data.session_id && gc_data.session_id !== '') { 8 return Promise.resolve(gc_data.session_id); 9 } 10 if (sessionIdPromise) { 11 return sessionIdPromise; 12 } 13 sessionIdPromise = new Promise((resolve, reject) => { 14 jQuery.ajax({ 15 type: 'POST', 16 dataType: 'json', 17 url: `${gc_data.ajaxurl}?action=gc_fetch_user_session_id`, 18 data: { action: 'gc_fetch_user_session_id' } 19 }).done(data => { 20 resolve(data.session_id); 21 }).fail((jqXHR, textStatus, errorThrown) => { 22 console.error('Failed to fetch session ID:', { status: jqXHR.status, textStatus, errorThrown }); 23 reject(errorThrown); 24 }).always(() => { 25 sessionIdPromise = null; 26 }); 27 }); 28 return sessionIdPromise; 29 } 30 31 function handleCheckoutFailure() { 32 if (typeof gokwikSdk !== 'undefined' && typeof gokwikSdk.close === 'function') { 33 manualCloseInitiated = true; 34 gokwikSdk.close(); 35 } 36 if (!gc_data.is_checkout_page) { 37 skipGoKwikCheckout = true; 38 localStorage.setItem('skipGoKwikCheckout', 'true'); 39 window.location.href = gc_data.checkout_url; 40 } else { 41 jQuery('#gcCustomCSS-inline-css').remove(); 42 jQuery('body').append('<style>.woocommerce form.woocommerce-checkout { display: block !important; }</style>'); 43 } 44 } 45 46 function processGoKwikCheckout(sessionId) { 47 if (skipGoKwikCheckout) { 48 jQuery('#gcCustomCSS-inline-css').remove(); 49 jQuery('body').append('<style>.woocommerce form.woocommerce-checkout { display: block !important; }</style>'); 50 return; 51 } 52 53 if (typeof gokwikSdk === 'undefined' || typeof gokwikSdk.initCheckout !== 'function') { 54 console.error("GoKwik SDK or initCheckout function not available", { gokwikSdk, initCheckout: gokwikSdk?.initCheckout }); 55 handleCheckoutFailure(); 56 return; 57 } 58 59 let referrer = getCookie("origin_referrer") || getCookie("_orig_referrer"); 60 let utmSource = getCookie("ad_source"); 61 let landingPage = getCookie("pys_landing_page") || getCookie("_landing_page"); 62 63 const gcObj = { 64 environment: gc_data.environment, 65 type: "merchantInfo", 66 mid: gc_data.mid, 67 merchantParams: { 68 merchantCheckoutId: sessionId 69 } 70 }; 71 72 if (referrer) gcObj.origin_referrer = referrer; 73 if (utmSource) gcObj.ad_source = utmSource; 74 if (landingPage) gcObj.landing_page = landingPage; 75 76 if (!gc_data.is_cart_empty) { 77 gokwikSdk.initCheckout(gcObj); 78 return; 79 } 80 81 const cartItemsCookie = document.cookie.split('; ').find(row => row.startsWith('woocommerce_items_in_cart=')); 82 83 if (cartItemsCookie && parseInt(cartItemsCookie.split('=')[1]) > 0) { 84 gokwikSdk.initCheckout(gcObj); 85 } else { 86 jQuery.ajax({ 87 type: 'POST', 88 dataType: 'json', 89 url: `${gc_data.ajaxurl}?action=gc_check_cart_status`, 90 data: { action: 'gc_check_cart_status' } 91 }).done(response => { 92 if (response.status === 'success' && !response.cart_empty) { 93 gokwikSdk.initCheckout(gcObj); 94 } else { 95 handleCheckoutFailure(); 96 } 97 }).fail((jqXHR, textStatus, errorThrown) => { 98 console.error('Failed to check cart status:', { status: jqXHR.status, textStatus, errorThrown }); 99 handleCheckoutFailure(); 100 }); 101 } 102 } 103 104 function initGoKwikCheckout(sessionId) { 105 if (!sessionId) { 106 fetchUserSessionId() 107 .then(newSessionId => { 108 if (newSessionId && newSessionId.trim() !== '') { 109 processGoKwikCheckout(newSessionId); 110 } else { 111 throw new Error('Empty or invalid session ID received.'); 112 } 113 }) 114 .catch(error => { 115 console.error("Failed to fetch session ID:", error); 116 handleCheckoutFailure(); 117 }); 118 } else { 119 processGoKwikCheckout(sessionId); 120 } 121 } 122 123 function setupMiniCartCheckoutButton(selector, sessionId) { 124 jQuery(selector).each(function() { 125 126 const $original = jQuery(this); 127 if ($original.hasClass('gc-button-replaced')) { 128 return; 129 } 130 131 const originalBtn = jQuery(selector); 132 const gcButtonClass = jQuery(this).attr("class") ?? ''; 133 const gcButtonId = jQuery(this).attr("id") ?? ''; 134 const gcButton = jQuery('<a>', { 135 href: "#", 136 class: gcButtonClass, 137 id: gcButtonId, 138 text: 'Checkout' 139 }); 140 jQuery(this).replaceWith(gcButton); 141 142 if (!gc_data.replace_woocommerce_native_checkout_with_gokwik) { 143 const priceSpan = originalBtn.find('.woocommerce-Price-amount').clone(); 144 const nativeBtn = jQuery('<a>', { 145 href: "/checkout/", 146 class: `${gcButtonClass} gc-gokwik-btn gc-button-replaced`, 147 id: gcButtonId, 148 text: 'Checkout' 149 }); 150 nativeBtn.append(priceSpan); 151 gcButton.after(nativeBtn); 152 } 153 gcButton.addClass('gc-button-replaced'); 154 155 gcButton.on('click', function (event) { 156 event.preventDefault(); 157 gcButton.html('Loading...'); 158 setTimeout(() => { 159 gcButton.html('Checkout'); 160 }, 3000); 161 initGoKwikCheckout(sessionId); 162 }); 163 }); 164 } 165 166 function renderCheckoutButtons() { 167 if (gc_data.is_international_user) { 168 return; 169 } 170 171 if (gc_data.replace_woocommerce_native_checkout_with_gokwik) { 172 const gcCheckoutBtnText = 'Place Order <img src="https://cdn.gokwik.co/v4/images/upi-icons.svg" class="gokwik_logo">'; 173 const gcCheckoutBtn = jQuery('.wc-proceed-to-checkout .checkout-button').not('.native_checkout_button'); 174 if (gcCheckoutBtn.length) { 175 const newGcCheckoutBtn = jQuery('<a>', { 176 href: "#", 177 class: gcCheckoutBtn.attr("class"), 178 id: gcCheckoutBtn.attr("id"), 179 html: gcCheckoutBtnText 180 }); 181 gcCheckoutBtn.replaceWith(newGcCheckoutBtn); 182 fetchUserSessionId().then(sessionId => { 183 newGcCheckoutBtn.on('click', e => { 184 e.preventDefault(); 185 newGcCheckoutBtn.html('Loading...'); 186 setTimeout(() => { 187 newGcCheckoutBtn.html(gcCheckoutBtnText); 188 }, 3000); 189 initGoKwikCheckout(sessionId); 190 }); 191 }); 192 } 193 } 194 195 if (gc_data.enable_gokwik_checkout_on_cart_page && !gc_data.replace_woocommerce_native_checkout_with_gokwik) { 196 const gcCheckoutBtnText = 'Place Order <img src="https://cdn.gokwik.co/v4/images/upi-icons.svg" class="gokwik_logo">'; 197 const gcCheckoutBtn = jQuery('.wc-proceed-to-checkout .checkout-button').not('.native_checkout_button'); 198 199 if (gcCheckoutBtn.length && jQuery('.gokwik_checkout_button').length === 0) { 200 const newGcCheckoutBtn = gcCheckoutBtn.clone(); 201 202 newGcCheckoutBtn 203 .removeClass('native_checkout_button') 204 .addClass('gokwik_checkout_button') 205 .attr('id', 'gokwik-checkout-button') 206 .html(gcCheckoutBtnText); 207 208 gcCheckoutBtn.after(newGcCheckoutBtn); 209 210 fetchUserSessionId().then(sessionId => { 211 newGcCheckoutBtn.on('click', e => { 212 e.preventDefault(); 213 newGcCheckoutBtn.html('Loading...'); 214 setTimeout(() => { 215 newGcCheckoutBtn.html(gcCheckoutBtnText); 216 }, 3000); 217 initGoKwikCheckout(sessionId); 218 }); 219 }); 220 } 221 } 222 223 if (gc_data.enable_gokwik_checkout_on_side_cart) { 224 const sideCartSelectors = [ 225 ".xoo-wsc-ft-btn-checkout", 226 ".elementor-button--checkout", 227 "a.button.checkout", 228 "a.btn.checkout", 229 "#fkcart-checkout-button", 230 ".xoo-wsc-ft-btn.xoo-wsc-cart", 231 "a.button.checkout-button:not(.wc-proceed-to-checkout .button.checkout-button)" 232 ]; 233 for (const selector of sideCartSelectors) { 234 if (jQuery(selector).length) { 235 fetchUserSessionId().then(sessionId => { 236 setupMiniCartCheckoutButton(selector, sessionId); 237 }); 238 break; 239 } 240 } 241 } 242 } 243 244 function getCookie(name) { 245 let match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); 246 return match ? decodeURIComponent(match[2]) : ""; 247 } 248 249 if (gc_data.is_checkout_page && !gc_data.is_order_received && !gc_data.is_cart_empty && gc_data.overwrite_native_checkout && !gc_data.is_international_user) { 250 fetchUserSessionId().then(sessionId => { 251 initGoKwikCheckout(sessionId); 252 }); 253 gokwikSdk.on('checkout-close', () => { 254 if (!manualCloseInitiated) { 255 window.location = gc_data.cart_url; 256 } 257 manualCloseInitiated = false;
258 }); 259 } else { 260 jQuery(document.body).on('wc_fragments_loaded wc_fragments_refreshed added_to_cart removed_from_cart updated_cart_totals updated_wc_div updated_checkout fkcart_fragments_refreshed', renderCheckoutButtons); 261 renderCheckoutButtons(); 262 } 263 264 gokwikSdk.on('order-complete', order => { 265 jQuery.ajax({ 266 type: 'POST', 267 dataType: 'json', 268 url: `${gc_data.ajaxurl}?action=gc_cart_clear_and_redirect`, 269 data: { 270 action: 'gc_cart_clear_and_redirect', 271 merchant_order_id: order.merchant_order_id 272 } 273 }).done(data => { 274 if (data.status === 'success') { 275 window.location = data.url; 276 } else { 277 console.error('Failed to clear cart:', data); 278 } 279 }).fail((jqXHR, textStatus, errorThrown) => { 280 console.error('Order-Completion Ajax request failed:', { status: jqXHR.status, textStatus, errorThrown }); 281 }); 282 }); 283 284 ['native-redirection', 'checkout-initiation-failure'].forEach(event => { 285 gokwikSdk.on(event, handleCheckoutFailure); 286 }); 287 288 if (!skipGoKwikCheckout) { 289 localStorage.removeItem('skipGoKwikCheckout'); 290 } else { 291 localStorage.setItem('skipGoKwikCheckout', 'false'); 292 } 293 294 if (!gc_data.gokwik_buy_now_enabled) { 295 return; 296 } 297 298 const quantitySelector = 'input.qty'; 299 const buttonSelector = 'button.gk-buy-now-btn'; 300 301 const buyNowButton = jQuery(buttonSelector); 302 if (buyNowButton.length === 0) return; 303 304 const updateButton = () => { 305 const form = jQuery('form.cart'); 306 const allSelected = form.hasClass('variations_form') 307 ? jQuery('.variations_form select').toArray().every(select => jQuery(select).val() !== '') 308 : true; 309 buyNowButton.toggleClass('disabled', !allSelected).attr('aria-disabled', !allSelected); 310 }; 311 312 jQuery(document).on('input change', quantitySelector, updateButton); 313 jQuery('.variations_form').on('woocommerce_variation_has_changed show_variation change', 'select', updateButton); 314 315 buyNowButton.on('click', function (e) { 316 if (jQuery(this).hasClass('disabled')) { 317 alert('Please select all product options before purchasing.'); 318 return; 319 } 320 321 buyNowButton.text('Loading...').addClass('disabled').attr('aria-disabled', true); 322 setTimeout(() => { 323 buyNowButton.text("Buy Now").removeClass('disabled').attr('aria-disabled', false); 324 }, 3000); 325 326 const form = jQuery('form.cart'); 327 const formData = new FormData(form.get(0)); 328 let productID = null; 329 330 const submitButton = form.find('button[type="submit"][name="add-to-cart"]'); 331 if (submitButton.length > 0) { 332 productID = submitButton.val(); 333 } 334 335 if (!productID) { 336 const productData = form.serializeArray(); 337 for (const item of productData) { 338 if (item.name === 'productID' || item.name === 'add-to-cart') { 339 if (item.value) { 340 productID = item.value; 341 break; 342 } 343 } 344 } 345 } 346 347 if (!productID && form.attr('action')) { 348 const match = form.attr('action').match(/add-to-cart=([0-9]+)/); 349 productID = match ? match[1] : null; 350 } 351 352 if (buyNowButton.attr('name') && buyNowButton.attr('value')) { 353 formData.append(buyNowButton.attr('name'), buyNowButton.attr('value')); 354 } 355 356 if (productID) { 357 formData.append('add-to-cart', productID); 358 } 359 360 formData.append('action', 'gc_add_to_cart'); 361 jQuery(document.body).trigger('adding_to_cart', [buyNowButton, formData]); 362 363 jQuery.ajax({ 364 type: 'POST', 365 url: wc_add_to_cart_params.wc_ajax_url.replace('%%endpoint%%', 'gc_add_to_cart'), 366 data: formData, 367 cache: false, 368 processData: false, 369 contentType: false, 370 success: function (response) { 371 if (response.fragments) { 372 fetchUserSessionId() 373 .then(sessionId => { 374 processGoKwikCheckout(sessionId); 375 }) 376 .catch(error => { 377 console.error('Failed to fetch user session ID:', error); 378 handleCheckoutFailure(); 379 }); 380 } 381 }, 382 error: function (jqXHR, textStatus, errorThrown) { 383 console.error('Failed to add product to cart:', { status: jqXHR.status, textStatus, errorThrown }); 384 handleCheckoutFailure(); 385 } 386 }); 387 }); 388 389 updateButton(); 390});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.