admin
1 month ago
builder
1 month ago
stripe
1 month ago
admin.js
1 month ago
checkout-fields-manager.js
1 month ago
checkout.js
1 month ago
clipboard.min.js
1 month ago
content-control.js
1 month ago
create-form.js
1 month ago
frontend.js
1 month ago
frontend.min.js
1 month ago
index.php
1 month ago
jcarousel.min.js
1 month ago
jquery.blockUI.js
1 month ago
mailoptin.js
1 month ago
member-directory.js
1 month ago
member-directory.min.js
1 month ago
pp-wp-editor.js
1 month ago
checkout.js
412 lines
| 1 | import $ from 'jquery'; |
| 2 | |
| 3 | export default function () { |
| 4 | |
| 5 | let _this = this; |
| 6 | |
| 7 | window.ppressCheckoutForm = this; |
| 8 | |
| 9 | this.init = function () { |
| 10 | |
| 11 | if (pp_ajax_form.is_checkout === '0' || $('#ppress_checkout_main_form').length === 0) return; |
| 12 | |
| 13 | $(document).on('click', '.ppress-checkout-show-login-form', this.toggle_login_form); |
| 14 | $(document).on('click', '.ppress-login-submit-btn input[type="submit"]', this.process_login); |
| 15 | $(document).on('click', '.ppress-coupon-code-link', this.toggle_discount_code_reveal); |
| 16 | $(document).on('click', '.ppress-apply-discount-btn', this.apply_discount_code); |
| 17 | $(document).on('click', '#ppress-remove-applied-coupon', this.remove_applied_discount_code); |
| 18 | $(document).on('submit', '#ppress_mb_checkout_form', this.process_checkout); |
| 19 | |
| 20 | $(document).on('click', '.ppress-terms-and-conditions-link', function (e) { |
| 21 | var cache = $('.ppress-checkout-form__terms_condition__content'); |
| 22 | if (cache.length > 0) { |
| 23 | e.preventDefault(); |
| 24 | cache.slideToggle(); |
| 25 | } |
| 26 | }); |
| 27 | |
| 28 | $(document).on('ppress_update_checkout', this.update_checkout); |
| 29 | |
| 30 | if (pp_ajax_form.is_checkout_tax_enabled === '1') { |
| 31 | $(document).on('change', '#ppress_mb_checkout_form .ppress_billing_country, #ppress_mb_checkout_form .ppress_billing_state, #ppress_mb_checkout_form .ppress_vat_number', _this.debounce(function () { |
| 32 | $(document.body).trigger('ppress_update_checkout'); |
| 33 | }, 200)); |
| 34 | } else { |
| 35 | $(document).on('change', '#ppress_mb_checkout_form .ppress_billing_country', _this.contextual_state_field); |
| 36 | } |
| 37 | |
| 38 | // Update payment method change |
| 39 | $(document.body).on('change', '#ppress_checkout_payment_methods [name=ppress_payment_method]', function () { |
| 40 | $(document.body).trigger('ppress_update_checkout'); |
| 41 | }); |
| 42 | |
| 43 | // Update on autorenewal checkbox check |
| 44 | $(document.body).on('change', '#ppress_mb_checkout_form #ppress-checkout-renewal', function () { |
| 45 | $(document.body).trigger('ppress_update_checkout'); |
| 46 | }); |
| 47 | |
| 48 | // Update group selection change |
| 49 | $(document.body).on('change', '#ppress_mb_checkout_form [name=group_selector]', function () { |
| 50 | _this.update_checkout(); |
| 51 | }); |
| 52 | |
| 53 | // Update on page load. |
| 54 | $(document.body).trigger('ppress_update_checkout'); |
| 55 | |
| 56 | $(document).ajaxError(function () { |
| 57 | _this.remove_spinner(); |
| 58 | }); |
| 59 | }; |
| 60 | |
| 61 | this.debounce = function (fun, mil) { |
| 62 | let timer; |
| 63 | mil = mil || 600; |
| 64 | return function () { |
| 65 | clearTimeout(timer); |
| 66 | timer = setTimeout(function () { |
| 67 | fun(); |
| 68 | }, mil); |
| 69 | }; |
| 70 | }; |
| 71 | |
| 72 | this.contextual_state_field = function () { |
| 73 | |
| 74 | let state_field = $('.ppress_billing_state'); |
| 75 | |
| 76 | let data = { |
| 77 | 'action': 'ppress_contextual_state_field', |
| 78 | 'country': $(this).val(), |
| 79 | 'name': state_field.prop('name'), |
| 80 | 'id': state_field.prop('id'), |
| 81 | 'class': state_field.prop('class'), |
| 82 | 'csrf': $('#ppress_checkout_nonce').val() |
| 83 | }; |
| 84 | |
| 85 | $.post(pp_ajax_form.ajaxurl, data, function (response) { |
| 86 | state_field.replaceWith(response.data); |
| 87 | }); |
| 88 | }; |
| 89 | |
| 90 | this.scroll_to_notices = function (scrollElement) { |
| 91 | |
| 92 | if (pp_ajax_form.is_checkout_autoscroll_enabled === 'true') { |
| 93 | |
| 94 | scrollElement = scrollElement || $('.ppress-checkout-alert'); |
| 95 | if (scrollElement.length) { |
| 96 | $('html, body').animate({ |
| 97 | scrollTop: (scrollElement.offset().top - 100) |
| 98 | }, 1000); |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | this.update_checkout = function (ignoreChangePlanRefresh) { |
| 104 | |
| 105 | ignoreChangePlanRefresh = ignoreChangePlanRefresh || false; |
| 106 | |
| 107 | let isChangePlanUpdate = $('#ppress_mb_checkout_form [name=group_selector]').length > 0; |
| 108 | |
| 109 | _this.removeAllAlerts(); |
| 110 | |
| 111 | _this.add_spinner(); |
| 112 | |
| 113 | let data = { |
| 114 | 'action': 'ppress_update_order_review', |
| 115 | 'plan_id': $('#ppress-checkout-plan-id').val(), |
| 116 | 'ppress_payment_method': $('#ppress_checkout_payment_methods [name=ppress_payment_method]:checked').val(), |
| 117 | 'csrf': $('#ppress_checkout_nonce').val(), |
| 118 | 'address': $('.ppress-checkout-form__payment_method.ppress-active .ppress_billing_address').val(), |
| 119 | 'city': $('.ppress-checkout-form__payment_method.ppress-active .ppress_billing_city').val(), |
| 120 | 'country': $('.ppress-checkout-form__payment_method.ppress-active .ppress_billing_country').val(), |
| 121 | 'state': $('.ppress-checkout-form__payment_method.ppress-active .ppress_billing_state').val(), |
| 122 | 'postcode': $('.ppress-checkout-form__payment_method.ppress-active .ppress_billing_postcode').val(), |
| 123 | 'phone': $('.ppress-checkout-form__payment_method.ppress-active .ppress_billing_phone').val(), |
| 124 | 'vat_number': $('#ppress_checkout_main_form .ppress_vat_number').val(), |
| 125 | 'post_data': $('#ppress_mb_checkout_form').serialize() |
| 126 | }; |
| 127 | |
| 128 | if (isChangePlanUpdate === true) { |
| 129 | data['isChangePlanUpdate'] = 'true'; |
| 130 | } |
| 131 | |
| 132 | $.post(pp_ajax_form.ajaxurl, data, function (response) { |
| 133 | |
| 134 | // Save payment details to a temporary object |
| 135 | let paymentDetails = {}; |
| 136 | |
| 137 | $('.ppress-checkout-form__payment_method :input, .ppress-checkout_account_info-wrap :input').each(function () { |
| 138 | |
| 139 | let ID = $(this).attr('id'); |
| 140 | |
| 141 | if (ID) { |
| 142 | if ($.inArray($(this).attr('type'), ['checkbox', 'radio']) !== -1) { |
| 143 | paymentDetails[ID] = $(this).prop('checked'); |
| 144 | } else { |
| 145 | paymentDetails[ID] = $(this).val(); |
| 146 | } |
| 147 | } |
| 148 | }); |
| 149 | |
| 150 | // Always update the fragments |
| 151 | if ('data' in response && typeof response.data.fragments !== 'undefined') { |
| 152 | $.each(response.data.fragments, function (key, value) { |
| 153 | if (!_this.fragments || _this.fragments[key] !== value) { |
| 154 | $(key).replaceWith(value); |
| 155 | } |
| 156 | }); |
| 157 | _this.fragments = data.fragments; |
| 158 | } |
| 159 | |
| 160 | // Fill in the payment details if possible without overwriting data if set. |
| 161 | if (!$.isEmptyObject(paymentDetails)) { |
| 162 | $('.ppress-checkout-form__payment_method :input, .ppress-checkout_account_info-wrap :input').each(function () { |
| 163 | let ID = $(this).attr('id'); |
| 164 | if (ID) { |
| 165 | if ($.inArray($(this).attr('type'), ['checkbox', 'radio']) !== -1) { |
| 166 | $(this).prop('checked', paymentDetails[ID]); |
| 167 | } else if ($.inArray($(this).attr('type'), ['select']) !== -1) { |
| 168 | $(this).val(paymentDetails[ID]); |
| 169 | } else { |
| 170 | $(this).val(paymentDetails[ID]); |
| 171 | } |
| 172 | } |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | // Check for error |
| 177 | if ('success' in response && false === response.success) { |
| 178 | |
| 179 | let $checkout_form_section = $('#ppress_checkout_main_form'); |
| 180 | |
| 181 | if (response.data) { |
| 182 | $checkout_form_section.prepend(response.data); |
| 183 | } |
| 184 | |
| 185 | // Lose focus for all fields |
| 186 | $checkout_form_section.find('.input-text, select, input:checkbox').trigger('blur'); |
| 187 | } |
| 188 | |
| 189 | // Fire updated_checkout event. |
| 190 | $(document.body).trigger('ppress_updated_checkout', [response]); |
| 191 | |
| 192 | let scrollToSelector = $('.ppress-checkout_order_summary__bottom_details'), cache; |
| 193 | |
| 194 | if ((cache = $('.ppress-checkout-alert')).length > 0) { |
| 195 | scrollToSelector = cache; |
| 196 | } |
| 197 | |
| 198 | _this.scroll_to_notices(scrollToSelector); |
| 199 | |
| 200 | _this.remove_spinner(); |
| 201 | |
| 202 | // it's important we re-init checkout to keep things working |
| 203 | if (isChangePlanUpdate === true && ignoreChangePlanRefresh !== true) { |
| 204 | _this.update_checkout(true); |
| 205 | } |
| 206 | }); |
| 207 | }; |
| 208 | |
| 209 | this.toggle_login_form = function (e) { |
| 210 | e.preventDefault(); |
| 211 | $('#ppress_checkout_account_info .ppress-main-checkout-form__login_form_wrap').slideToggle(); |
| 212 | }; |
| 213 | |
| 214 | this.toggle_discount_code_reveal = function (e) { |
| 215 | e.preventDefault(); |
| 216 | $('#ppress-checkout-coupon-code-wrap').slideToggle(); |
| 217 | }; |
| 218 | |
| 219 | this.apply_discount_code = function (e) { |
| 220 | e.preventDefault(); |
| 221 | |
| 222 | _this.removeAllAlerts(); |
| 223 | |
| 224 | _this.add_spinner(); |
| 225 | |
| 226 | let data = { |
| 227 | 'action': 'ppress_checkout_apply_discount', |
| 228 | 'plan_id': $('#ppress-checkout-plan-id').val(), |
| 229 | 'coupon_code': $('#apply-discount').val(), |
| 230 | 'ppress_checkout_nonce': $('#ppress_checkout_nonce').val(), |
| 231 | }; |
| 232 | |
| 233 | $.post(pp_ajax_form.ajaxurl, data, function (response) { |
| 234 | if ('success' in response && response.success === true) { |
| 235 | $(document.body).trigger('ppress_update_checkout'); |
| 236 | } else { |
| 237 | $('.ppress-checkout_order_summary-wrap').before(response.data); |
| 238 | |
| 239 | _this.remove_spinner(); |
| 240 | } |
| 241 | }); |
| 242 | }; |
| 243 | |
| 244 | this.remove_applied_discount_code = function (e) { |
| 245 | e.preventDefault(); |
| 246 | |
| 247 | _this.removeAllAlerts(); |
| 248 | |
| 249 | _this.add_spinner(); |
| 250 | |
| 251 | let data = { |
| 252 | 'action': 'ppress_checkout_remove_discount', |
| 253 | 'plan_id': $('#ppress-checkout-plan-id').val(), |
| 254 | 'ppress_checkout_nonce': $('#ppress_checkout_nonce').val(), |
| 255 | }; |
| 256 | |
| 257 | $.post(pp_ajax_form.ajaxurl, data, function (response) { |
| 258 | if ('success' in response && response.success === true) { |
| 259 | $(document.body).trigger('ppress_update_checkout'); |
| 260 | } else { |
| 261 | $('.ppress-checkout_order_summary-wrap').before(response.data); |
| 262 | |
| 263 | _this.remove_spinner(); |
| 264 | } |
| 265 | }); |
| 266 | }; |
| 267 | |
| 268 | this.process_login = function (e) { |
| 269 | |
| 270 | e.preventDefault(); |
| 271 | |
| 272 | _this.removeAllAlerts(); |
| 273 | |
| 274 | _this.add_spinner(); |
| 275 | |
| 276 | let data = { |
| 277 | 'action': 'ppress_process_checkout_login', |
| 278 | 'ppmb_user_login': $('#ppress_mb_checkout_form #ppmb_user_login').val(), |
| 279 | 'ppmb_user_pass': $('#ppress_mb_checkout_form #ppmb_user_pass').val(), |
| 280 | 'ppress_checkout_nonce': $('#ppress_checkout_nonce').val(), |
| 281 | }; |
| 282 | |
| 283 | $.post(pp_ajax_form.ajaxurl, data, function (response) { |
| 284 | if ('success' in response) { |
| 285 | if (response.success === true) { |
| 286 | window.location.reload(); |
| 287 | } else if ('data' in response) { |
| 288 | $('#ppress_mb_checkout_form .ppress-login-submit-btn').prepend(response.data); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | _this.remove_spinner(); |
| 293 | }); |
| 294 | }; |
| 295 | |
| 296 | this.process_checkout = function (e) { |
| 297 | |
| 298 | if (typeof this.checkValidity === 'function' && false === this.checkValidity()) { |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | e.preventDefault(); |
| 303 | |
| 304 | _this.removeAllAlerts(); |
| 305 | |
| 306 | _this.add_spinner(); |
| 307 | |
| 308 | var $form = $(this), $payment_method = _this.get_payment_method(); |
| 309 | |
| 310 | if ($form.triggerHandler('ppress_checkout_place_order_' + $payment_method) !== false) { |
| 311 | |
| 312 | let formData = new FormData(this); |
| 313 | formData.append("action", "ppress_process_checkout"); |
| 314 | formData.append("ppress_checkout_nonce", $('#ppress_checkout_nonce').val()); |
| 315 | |
| 316 | $.post({ |
| 317 | url: pp_ajax_form.ajaxurl, |
| 318 | data: formData, |
| 319 | cache: false, |
| 320 | contentType: false, |
| 321 | enctype: 'multipart/form-data', |
| 322 | processData: false, |
| 323 | dataType: 'json', |
| 324 | success: function (response) { |
| 325 | |
| 326 | $(document.body).trigger('ppress_process_checkout_success_callback', [response]); |
| 327 | |
| 328 | if ('success' in response) { |
| 329 | |
| 330 | if (response.success === true) { |
| 331 | |
| 332 | if ($form.triggerHandler('ppress_process_checkout_' + $payment_method, [response, $payment_method]) !== false) { |
| 333 | |
| 334 | if ('redirect_url' in response && typeof response.redirect_url !== 'undefined' && response.redirect_url.length > 0) { |
| 335 | window.location.assign(response.redirect_url); |
| 336 | } else { |
| 337 | |
| 338 | $(document.body).trigger('ppress_checkout_success', [response, $payment_method]); |
| 339 | |
| 340 | window.location.assign(response.order_success_url); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return; |
| 345 | |
| 346 | } |
| 347 | |
| 348 | if ('error_message' in response) { |
| 349 | return _this.createAlertMessage(response.error_message); |
| 350 | } |
| 351 | |
| 352 | if ('data' in response && typeof response.data == 'string') { |
| 353 | return _this.createAlertMessage(response.data); |
| 354 | } |
| 355 | |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | _this.remove_spinner(); |
| 360 | }, |
| 361 | error: function (jqXHR, textStatus, errorThrown) { |
| 362 | $(document.body).trigger('ppress_process_checkout_error_callback', [jqXHR, textStatus, errorThrown]); |
| 363 | _this.createAlertMessage(errorThrown); |
| 364 | } |
| 365 | }, 'json',); |
| 366 | } |
| 367 | }; |
| 368 | |
| 369 | this.get_payment_method = function () { |
| 370 | return $('#ppress_mb_checkout_form').find('input[name="ppress_payment_method"]:checked').val(); |
| 371 | }; |
| 372 | |
| 373 | this.createAlertMessage = function (message, type) { |
| 374 | type = type || 'error'; |
| 375 | var is_marked_up = typeof message.indexOf !== 'undefined' && message.indexOf('ppress-checkout-alert') !== -1, |
| 376 | msg = ''; |
| 377 | |
| 378 | if (!is_marked_up) msg = '<div class="ppress-checkout-alert ppress-' + type + '"><p>'; |
| 379 | |
| 380 | msg += message; |
| 381 | |
| 382 | if (!is_marked_up) msg += '</p></div>'; |
| 383 | |
| 384 | $('#ppress_checkout_main_form').prepend(msg); |
| 385 | |
| 386 | ppressCheckoutForm.scroll_to_notices(); |
| 387 | ppressCheckoutForm.remove_spinner(); |
| 388 | |
| 389 | $(document.body).trigger('ppress_checkout_error', [message]); |
| 390 | }; |
| 391 | |
| 392 | this.removeAllAlerts = function () { |
| 393 | $('.ppress-checkout-alert').remove(); |
| 394 | }; |
| 395 | |
| 396 | this.add_spinner = function () { |
| 397 | _this.remove_spinner(); |
| 398 | $('.ppress-checkout__form').prepend('<div class="ppress-checkout__form__preloader"><div class="ppress-checkout__form__spinner"></div></div>') |
| 399 | }; |
| 400 | |
| 401 | this.remove_spinner = function () { |
| 402 | $('.ppress-checkout__form .ppress-checkout__form__preloader').remove() |
| 403 | }; |
| 404 | |
| 405 | this.is_var_defined = function (val) { |
| 406 | return typeof val !== 'undefined' && val !== null; |
| 407 | }; |
| 408 | |
| 409 | this.get_obj_val = function (val, $default) { |
| 410 | return this.is_var_defined(val) && val !== "" ? val : $default; |
| 411 | }; |
| 412 | } |