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