woo-razorpay
Last commit date
css
4 years ago
images
5 years ago
includes
4 weeks ago
js
4 years ago
public
10 months ago
razorpay-sdk
1 year ago
templates
3 years ago
tests
4 weeks ago
.editorconfig
9 years ago
LICENSE
10 years ago
btn-1cc-checkout.js
3 years ago
checkout-block.php
2 years ago
checkout_block.js
2 years ago
composer.json
3 years ago
composer.wp-install.json
2 months ago
debug.md
11 months ago
phpunit.xml
3 years ago
readme.txt
1 week ago
script.js
3 years ago
woo-razorpay.php
1 week ago
btn-1cc-checkout.js
441 lines
| 1 | if (document.readyState !== 'loading') { |
| 2 | btnCheckout(); |
| 3 | } else { |
| 4 | document.addEventListener('DOMContentLoaded', function () { |
| 5 | btnCheckout(); |
| 6 | }); |
| 7 | } |
| 8 | |
| 9 | function btnCheckout(){ |
| 10 | |
| 11 | var btn = document.getElementById('btn-1cc'); |
| 12 | var mobileBtn = document.querySelectorAll('#btn-1cc')[1]; |
| 13 | var btnMini = document.getElementById('btn-1cc-mini-cart'); |
| 14 | var btnPdp = document.getElementById('btn-1cc-pdp'); |
| 15 | var rzpSpinnerBackdrop = document.getElementById('rzp-spinner-backdrop'); |
| 16 | var rzpSpinner = document.getElementById('rzp-spinner'); |
| 17 | var pageURL = jQuery(location).attr('href'); |
| 18 | var url = new URL(pageURL); |
| 19 | var accessToken = new URLSearchParams(url.search).get('wcf_ac_token'); |
| 20 | var referrerDomain = document.referrer.toString(); |
| 21 | var flycartBtn = document.getElementsByClassName("woofc-action-checkout")[0]; |
| 22 | var caddyBtn = document.getElementsByClassName('cc-button cc-button-primary')[0]; |
| 23 | var sidecartBtn = document.getElementsByClassName('xoo-wsc-ft-btn button btn xoo-wsc-ft-btn-checkout')[0]; |
| 24 | rzp1ccCheckoutData.referrerDomain = referrerDomain; |
| 25 | |
| 26 | // event triggered by wc on any cart change |
| 27 | // as input function is the same, duplicate event listeners are NOT called |
| 28 | jQuery(document.body).on('updated_cart_totals', function(event) { |
| 29 | var btn = document.getElementById('btn-1cc'); |
| 30 | if (btn !== null) { |
| 31 | btn.addEventListener('click', openRzpCheckout); |
| 32 | } |
| 33 | |
| 34 | if (mobileBtn != null) { |
| 35 | mobileBtn.addEventListener('click', openRzpCheckout); |
| 36 | } |
| 37 | |
| 38 | var btnMini = document.getElementById('btn-1cc-mini-cart'); |
| 39 | if (btnMini !== null) { |
| 40 | btnMini.addEventListener('click', openRzpCheckout); |
| 41 | } |
| 42 | |
| 43 | var flycartBtn = document.getElementsByClassName("woofc-action-checkout")[0]; |
| 44 | |
| 45 | if (flycartBtn != null) { |
| 46 | flycartBtn.addEventListener('click', openRzpCheckout); |
| 47 | } |
| 48 | |
| 49 | var caddyBtn = document.getElementsByClassName('cc-button cc-button-primary')[0]; |
| 50 | |
| 51 | if (caddyBtn != null) { |
| 52 | caddyBtn.addEventListener('click', openRzpCheckout); |
| 53 | } |
| 54 | |
| 55 | var sidecartBtn = document.getElementsByClassName('xoo-wsc-ft-btn button btn xoo-wsc-ft-btn-checkout')[0]; |
| 56 | if (sidecartBtn != null) { |
| 57 | sidecartBtn.addEventListener('click', openRzpCheckout); |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | function addEventListenerToMinicart(wcEvent) { |
| 62 | jQuery(document.body).on(wcEvent, function(event) { |
| 63 | var btnMini = document.getElementById('btn-1cc-mini-cart'); |
| 64 | if (btnMini !== null) { |
| 65 | btnMini.addEventListener('click', openRzpCheckout); |
| 66 | } |
| 67 | |
| 68 | var flycartBtn = document.getElementsByClassName("woofc-action-checkout")[0]; |
| 69 | |
| 70 | if (flycartBtn != null) { |
| 71 | flycartBtn.addEventListener('click', openRzpCheckout); |
| 72 | } |
| 73 | var caddyBtn = document.getElementsByClassName('cc-button cc-button-primary')[0]; |
| 74 | |
| 75 | if (caddyBtn != null) { |
| 76 | caddyBtn.addEventListener('click', openRzpCheckout); |
| 77 | } |
| 78 | |
| 79 | var sidecartBtn = document.getElementsByClassName('xoo-wsc-ft-btn button btn xoo-wsc-ft-btn-checkout')[0]; |
| 80 | |
| 81 | if (sidecartBtn != null) { |
| 82 | sidecartBtn.addEventListener('click', openRzpCheckout); |
| 83 | } |
| 84 | |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | var stickyBtn = document.querySelectorAll('#btn-1cc-pdp')[1]; |
| 89 | |
| 90 | if (stickyBtn != null) { |
| 91 | |
| 92 | // For attaching event listener to Woodmart's sticky add-to-cart |
| 93 | document.addEventListener('scroll',(e)=>{ |
| 94 | |
| 95 | let i = 0; |
| 96 | while (typeof quantity === 'undefined') { |
| 97 | var quantity = document.getElementsByClassName("qty")[i].value; |
| 98 | i++; |
| 99 | } |
| 100 | |
| 101 | stickyBtn.setAttribute('quantity', quantity); |
| 102 | |
| 103 | jQuery('.qty').on('change',function(e) |
| 104 | { |
| 105 | let x = 0; |
| 106 | while (typeof quantity === 'undefined') { |
| 107 | var quantity = document.getElementsByClassName("qty")[x].value; |
| 108 | x++; |
| 109 | } |
| 110 | |
| 111 | stickyBtn.setAttribute('quantity', quantity); |
| 112 | |
| 113 | if(quantity <= 0) |
| 114 | { |
| 115 | stickyBtn.classList.add("disabled"); |
| 116 | stickyBtn.disabled = true; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | stickyBtn.classList.remove("disabled"); |
| 121 | stickyBtn.disabled = false; |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | (function($){ |
| 126 | |
| 127 | $('form.variations_form').on('show_variation', function(event, data){ |
| 128 | |
| 129 | stickyBtn.classList.remove("disabled"); |
| 130 | stickyBtn.disabled = false; |
| 131 | |
| 132 | stickyBtn.setAttribute('variation_id', data.variation_id); |
| 133 | |
| 134 | var variationArr = {}; |
| 135 | |
| 136 | $.each( data.attributes, function( key, value ) { |
| 137 | variationArr[key] = $("[name="+key+"]").val(); |
| 138 | }); |
| 139 | |
| 140 | stickyBtn.setAttribute('variations', JSON.stringify(variationArr)); |
| 141 | |
| 142 | }).on('hide_variation', function() { |
| 143 | |
| 144 | stickyBtn.classList.add("disabled"); |
| 145 | stickyBtn.disabled = true; |
| 146 | }); |
| 147 | })(jQuery); |
| 148 | |
| 149 | if (stickyBtn != null) { |
| 150 | stickyBtn.onclick = function(){ |
| 151 | |
| 152 | var pdpCheckout = stickyBtn.getAttribute('pdp_checkout'); |
| 153 | var productId = stickyBtn.getAttribute('product_id'); |
| 154 | var quantity = stickyBtn.getAttribute('quantity'); |
| 155 | |
| 156 | rzp1ccCheckoutData.pdpCheckout = pdpCheckout; |
| 157 | rzp1ccCheckoutData.productId = productId; |
| 158 | rzp1ccCheckoutData.quantity = quantity; |
| 159 | |
| 160 | if (btnPdp.getAttribute('variation_id') != null) { |
| 161 | var variationId = stickyBtn.getAttribute('variation_id'); |
| 162 | var variations = stickyBtn.getAttribute('variations'); |
| 163 | |
| 164 | rzp1ccCheckoutData.variationId = variationId; |
| 165 | rzp1ccCheckoutData.variations = variations; |
| 166 | } |
| 167 | |
| 168 | //To support custom product fields plugin. |
| 169 | const customFieldForm = document.getElementsByClassName('wcpa_form_outer'); |
| 170 | |
| 171 | if (customFieldForm && customFieldForm.length > 0) { |
| 172 | |
| 173 | var customProductFieldForm = customFieldForm[0]; |
| 174 | |
| 175 | var fieldValues = customProductFieldForm.getElementsByTagName('input'); |
| 176 | var fieldKey = customProductFieldForm.getElementsByTagName('label'); |
| 177 | var fieldArray = []; |
| 178 | var fieldObj = {}; |
| 179 | |
| 180 | for (i = 0; i < fieldKey.length; i++) { |
| 181 | fieldObj[fieldKey[i].innerText] = fieldValues[i].value; |
| 182 | } |
| 183 | |
| 184 | rzp1ccCheckoutData.fieldObj = fieldObj; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (stickyBtn !== null) { |
| 190 | stickyBtn.addEventListener('click', openRzpCheckout); |
| 191 | } |
| 192 | }) |
| 193 | } |
| 194 | |
| 195 | |
| 196 | addEventListenerToMinicart('wc_fragments_refreshed'); |
| 197 | addEventListenerToMinicart('wc_fragments_loaded'); |
| 198 | addEventListenerToMinicart('added_to_cart'); |
| 199 | |
| 200 | if (btnPdp != null) { |
| 201 | btnPdp.onclick = productInfoHandler; |
| 202 | } |
| 203 | |
| 204 | function productInfoHandler(){ |
| 205 | |
| 206 | var pdpCheckout = btnPdp.getAttribute('pdp_checkout'); |
| 207 | var productId = btnPdp.getAttribute('product_id'); |
| 208 | var quantity = btnPdp.getAttribute('quantity'); |
| 209 | |
| 210 | rzp1ccCheckoutData.pdpCheckout = pdpCheckout; |
| 211 | rzp1ccCheckoutData.productId = productId; |
| 212 | rzp1ccCheckoutData.quantity = quantity; |
| 213 | |
| 214 | if (btnPdp.getAttribute('variation_id') != null) { |
| 215 | var variationId = btnPdp.getAttribute('variation_id'); |
| 216 | var variations = btnPdp.getAttribute('variations'); |
| 217 | |
| 218 | rzp1ccCheckoutData.variationId = variationId; |
| 219 | rzp1ccCheckoutData.variations = variations; |
| 220 | } |
| 221 | |
| 222 | //To support custom product fields plugin. |
| 223 | const customFieldForm = document.getElementsByClassName('wcpa_form_outer'); |
| 224 | |
| 225 | if (customFieldForm && customFieldForm.length > 0) { |
| 226 | |
| 227 | var customProductFieldForm = customFieldForm[0]; |
| 228 | |
| 229 | var fieldValues = customProductFieldForm.getElementsByTagName('input'); |
| 230 | var fieldKey = customProductFieldForm.getElementsByTagName('label'); |
| 231 | var fieldArray = []; |
| 232 | var fieldObj = {}; |
| 233 | |
| 234 | for (i = 0; i < fieldKey.length; i++) { |
| 235 | fieldObj[fieldKey[i].innerText] = fieldValues[i].value; |
| 236 | } |
| 237 | |
| 238 | rzp1ccCheckoutData.fieldObj = fieldObj; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // fetch opts from server and open 1cc modal |
| 243 | var rzp1cc = { |
| 244 | orderApi: rzp1ccCheckoutData.siteurl + '/wp-json/1cc/v1/order/create', |
| 245 | saveAbandonedCartApi: rzp1ccCheckoutData.siteurl + '/wp-json/1cc/v1/abandoned-cart', |
| 246 | makeRequest: function(url, body) { |
| 247 | return new Promise(function(resolve, reject) { |
| 248 | var xhr = new XMLHttpRequest(); |
| 249 | xhr.open('POST', url, true); |
| 250 | xhr.setRequestHeader('Content-Type', 'application/json'); |
| 251 | xhr.setRequestHeader('X-WP-Nonce', rzp1ccCheckoutData.nonce); |
| 252 | xhr.onload = function() { |
| 253 | if (this.status === 200) { |
| 254 | resolve(rzp1cc.parseIfJson(this.response)); |
| 255 | } else { |
| 256 | reject({ status: this.status, response: rzp1cc.parseIfJson(this.response) }); |
| 257 | } |
| 258 | } |
| 259 | xhr.onerror = function () { |
| 260 | reject({ status: this.status, statusText: this.statusText}); |
| 261 | }; |
| 262 | xhr.send(JSON.stringify(body)); |
| 263 | }); |
| 264 | }, |
| 265 | parseIfJson: function (str) { |
| 266 | try { |
| 267 | return JSON.parse(str); |
| 268 | } catch (e) { |
| 269 | return str; |
| 270 | } |
| 271 | }, |
| 272 | setDisabled: function(id, state) { |
| 273 | if (typeof state === 'undefined') { |
| 274 | state = true; |
| 275 | } |
| 276 | var elem = document.getElementById(id); |
| 277 | |
| 278 | if(elem != null) |
| 279 | { |
| 280 | if (state === false) { |
| 281 | elem.removeAttribute('disabled'); |
| 282 | } else { |
| 283 | elem.setAttribute('disabled', state); |
| 284 | } |
| 285 | } |
| 286 | }, |
| 287 | showSpinner: function(state) { |
| 288 | jQuery(document.body).trigger('wc_fragment_refresh'); |
| 289 | if (rzpSpinnerBackdrop == null) { |
| 290 | rzpSpinnerBackdrop = document.getElementById('rzp-spinner-backdrop'); |
| 291 | } |
| 292 | if (rzpSpinner == null) { |
| 293 | rzpSpinner = document.getElementById('rzp-spinner'); |
| 294 | } |
| 295 | if (state === true) { |
| 296 | rzpSpinnerBackdrop.classList.add('show'); |
| 297 | rzpSpinner.classList.add('show'); |
| 298 | } else { |
| 299 | rzpSpinnerBackdrop.classList.remove('show'); |
| 300 | rzpSpinner.classList.remove('show'); |
| 301 | } |
| 302 | }, |
| 303 | handleAbandonmentCart: function(rzpOrderId) { |
| 304 | if(rzpOrderId != null) { |
| 305 | var xhr = new XMLHttpRequest(); |
| 306 | try { |
| 307 | var body = { |
| 308 | order_id: rzpOrderId |
| 309 | }; |
| 310 | xhr.open('POST', rzp1cc.saveAbandonedCartApi, true); |
| 311 | xhr.setRequestHeader('Content-Type', 'application/json'); |
| 312 | xhr.send(JSON.stringify(body)); |
| 313 | } catch (e) { |
| 314 | |
| 315 | } |
| 316 | } |
| 317 | }, |
| 318 | enableCheckoutButtons: function() { |
| 319 | rzp1cc.setDisabled('btn-1cc', false); |
| 320 | rzp1cc.setDisabled('btn-1cc-mini-cart', false); |
| 321 | rzp1cc.setDisabled('btn-1cc-pdp', false); |
| 322 | }, |
| 323 | getBrowserTime: function() { |
| 324 | var dateTime = []; |
| 325 | var date = new Date(), |
| 326 | days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], |
| 327 | months = ['January', 'February', 'March', 'April', 'May', 'June', |
| 328 | 'July', 'August', 'September', 'October', 'November', 'December' |
| 329 | ], |
| 330 | hours = ['00-01', '01-02', '02-03', '03-04', '04-05', '05-06', '06-07', '07-08', |
| 331 | '08-09', '09-10', '10-11', '11-12', '12-13', '13-14', '14-15', '15-16', '16-17', |
| 332 | '17-18', '18-19', '19-20', '20-21', '21-22', '22-23', '23-24' |
| 333 | ]; |
| 334 | dateTime.push(hours[date.getHours()]); |
| 335 | dateTime.push(days[date.getDay()]); |
| 336 | dateTime.push(months[date.getMonth()]); |
| 337 | |
| 338 | rzp1ccCheckoutData.dateTime = dateTime; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (btn !== null) { |
| 343 | btn.addEventListener('click', openRzpCheckout); |
| 344 | } |
| 345 | |
| 346 | if (mobileBtn != null) { |
| 347 | mobileBtn.addEventListener('click', openRzpCheckout); |
| 348 | } |
| 349 | |
| 350 | if (btnMini !== null) { |
| 351 | btnMini.addEventListener('click', openRzpCheckout); |
| 352 | } |
| 353 | |
| 354 | if (btnPdp !== null) { |
| 355 | btnPdp.addEventListener('click', openRzpCheckout); |
| 356 | } |
| 357 | |
| 358 | if (flycartBtn != null) { |
| 359 | flycartBtn.addEventListener('click', openRzpCheckout); |
| 360 | } |
| 361 | |
| 362 | if (caddyBtn != null) { |
| 363 | caddyBtn.addEventListener('click', openRzpCheckout); |
| 364 | } |
| 365 | |
| 366 | if (sidecartBtn != null) { |
| 367 | sidecartBtn.addEventListener('click', openRzpCheckout); |
| 368 | } |
| 369 | |
| 370 | async function openRzpCheckout(e) { |
| 371 | e.preventDefault(); |
| 372 | |
| 373 | if( btnPdp !== null && btnPdp.classList.contains('disabled')){ |
| 374 | return; |
| 375 | } |
| 376 | rzp1cc.showSpinner(true); |
| 377 | |
| 378 | if (accessToken !== null) |
| 379 | { |
| 380 | rzp1ccCheckoutData.token = accessToken; |
| 381 | } |
| 382 | |
| 383 | rzp1cc.getBrowserTime(); |
| 384 | |
| 385 | |
| 386 | var body = rzp1ccCheckoutData; |
| 387 | |
| 388 | rzp1cc.setDisabled('btn-1cc'); |
| 389 | rzp1cc.setDisabled('btn-1cc-mini-cart'); |
| 390 | rzp1cc.setDisabled('btn-1cc-pdp'); |
| 391 | |
| 392 | rzp1cc.makeRequest(rzp1cc.orderApi, body) |
| 393 | .then(data => { |
| 394 | rzp1cc.showSpinner(false); |
| 395 | try { |
| 396 | var razorpayCheckout = new Razorpay({ |
| 397 | ...data, |
| 398 | modal: { |
| 399 | ondismiss: function() { |
| 400 | rzp1cc.handleAbandonmentCart(data.order_id); |
| 401 | rzp1cc.enableCheckoutButtons(); |
| 402 | }, |
| 403 | onload: setTimeout(() => { |
| 404 | rzp1cc.handleAbandonmentCart(data.order_id); |
| 405 | rzp1cc.enableCheckoutButtons(); |
| 406 | }, 25000), |
| 407 | }, |
| 408 | }); |
| 409 | razorpayCheckout.open(); |
| 410 | |
| 411 | } catch (e) { |
| 412 | document.getElementById('error-message').innerHTML = |
| 413 | "<div class='entry-content'><div class='woocommerce'><div class='woocommerce-notices-wrapper'><p class='cart-empty woocommerce-info' style='margin-left: -50px; margin-right: 75px'>Something went wrong, please try again after sometime.</p></div></div></div>"; |
| 414 | |
| 415 | rzp1cc.enableCheckoutButtons(); |
| 416 | rzp1cc.showSpinner(false); |
| 417 | |
| 418 | } |
| 419 | }) |
| 420 | .catch(e => { |
| 421 | // Response sent to the User when cart is empty or order creation fails |
| 422 | if (e.status == 400){ |
| 423 | if (e.response.code == 'BAD_REQUEST_EMPTY_CART'){ |
| 424 | document.getElementById('error-message').innerHTML = "<p style='margin-top: revert;text-color: #e2401c !important;color: #e80707;'>Order could not be placed as your cart is empty.</p>"; |
| 425 | } else if (e.response.code == 'ORDER_CREATION_FAILED'){ |
| 426 | document.getElementById('error-message').innerHTML = "<p style='margin-top: revert;text-color: #e2401c !important;color: #e80707;'>Razorpay Error: Order could not be placed, please try again after sometime.</p>"; |
| 427 | } else if (e.response.code == 'MIN_CART_AMOUNT_CHECK_FAILED' || e.response.code == 'WOOCOMMERCE_ORDER_CREATION_FAILED'){ |
| 428 | document.getElementById('error-message').innerHTML = "<p style='margin-top: revert;text-color: #e2401c !important;color: #e80707;'>"+e.response.message+"</p>"; // nosemgrep: insecure-innerhtml |
| 429 | } else { |
| 430 | document.getElementById('error-message').innerHTML = "<p style='margin-top: revert;text-color: #e2401c !important;color: #e80707;'>Something went wrong, please try again after sometime.</p>"; |
| 431 | } |
| 432 | |
| 433 | } else { |
| 434 | document.getElementById('error-message').innerHTML = "<p style='margin-top: revert;text-color: #e2401c !important;color: #e80707;'>Something went wrong, please try again after sometime.</p>"; |
| 435 | } |
| 436 | |
| 437 | rzp1cc.enableCheckoutButtons(); |
| 438 | rzp1cc.showSpinner(false); |
| 439 | }); |
| 440 | } |
| 441 | } |