| 1 |
( function( $, settings ) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
if ( window.LP === undefined ) { |
| 5 |
window.LP = {}; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Checkout |
| 10 |
* |
| 11 |
* @param options |
| 12 |
*/ |
| 13 |
const Checkout = LP.Checkout = function( options ) { |
| 14 |
const $formCheckout = $( '#learn-press-checkout-form' ), |
| 15 |
|
| 16 |
$formLogin = $( '#learn-press-checkout-login' ), |
| 17 |
|
| 18 |
$formRegister = $( '#learn-press-checkout-register' ), |
| 19 |
|
| 20 |
$payments = $( '.payment-methods' ), |
| 21 |
|
| 22 |
$buttonCheckout = $( '#learn-press-checkout-place-order' ), |
| 23 |
|
| 24 |
$checkoutEmail = $( 'input[name="guest_email"]' ); |
| 25 |
|
| 26 |
let selectedMethod = ''; |
| 27 |
|
| 28 |
if ( String.prototype.isEmail === undefined ) { |
| 29 |
String.prototype.isEmail = function() { |
| 30 |
return new RegExp( '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$' ).test( this ); |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
const needPayment = function() { |
| 35 |
return $payments.length > 0; |
| 36 |
}; |
| 37 |
|
| 38 |
const selectedPayment = function() { |
| 39 |
return $payments.find( 'input[name="payment_method"]:checked' ).val(); |
| 40 |
}; |
| 41 |
|
| 42 |
const isLoggedIn = function() { |
| 43 |
return $formCheckout.find( 'input[name="checkout-account-switch-form"]:checked' ).length = 0; |
| 44 |
}; |
| 45 |
|
| 46 |
const getActiveFormData = function() { |
| 47 |
const formName = $formCheckout.find( 'input[name="checkout-account-switch-form"]:checked' ).val(); |
| 48 |
const $form = $( '#checkout-account-' + formName ); |
| 49 |
|
| 50 |
return $form.serializeJSON(); |
| 51 |
}; |
| 52 |
|
| 53 |
const getPaymentData = function() { |
| 54 |
return $( '#checkout-payment' ).serializeJSON(); |
| 55 |
}; |
| 56 |
|
| 57 |
const getPaymentNote = function() { |
| 58 |
return $( '.learn-press-checkout-comment' ).serializeJSON(); |
| 59 |
}; |
| 60 |
|
| 61 |
const showErrors = function( errors ) { |
| 62 |
showMessage( errors ); |
| 63 |
const firstId = Object.keys( errors )[ 0 ]; |
| 64 |
|
| 65 |
$( 'input[name="' + firstId + '"]:visible' ).trigger( 'focus' ); |
| 66 |
}; |
| 67 |
|
| 68 |
const _formSubmit = function( e ) { |
| 69 |
e.preventDefault(); |
| 70 |
|
| 71 |
if ( needPayment() && ! selectedPayment() ) { |
| 72 |
showMessage( 'Please select payment method', true ); |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
let formData = {}; |
| 77 |
|
| 78 |
if ( ! isLoggedIn() ) { |
| 79 |
formData = $.extend( formData, getActiveFormData(), getPaymentNote() ); |
| 80 |
} |
| 81 |
|
| 82 |
formData = $.extend( formData, getPaymentData() ); |
| 83 |
|
| 84 |
removeMessage(); |
| 85 |
|
| 86 |
const btnText = $buttonCheckout.text(); |
| 87 |
|
| 88 |
const urlHandle = new URL( options.ajaxurl ); |
| 89 |
urlHandle.searchParams.set( 'lp-ajax', 'checkout' ); |
| 90 |
|
| 91 |
$.ajax( { |
| 92 |
url: urlHandle, |
| 93 |
dataType: 'html', |
| 94 |
data: formData, |
| 95 |
type: 'POST', |
| 96 |
beforeSend() { |
| 97 |
$( '#learn-press-checkout-place-order' ).addClass( 'loading' ); |
| 98 |
$buttonCheckout.html( options.i18n_processing ); |
| 99 |
}, |
| 100 |
success( response ) { |
| 101 |
response = LP.parseJSON( response ); |
| 102 |
|
| 103 |
if ( response.messages ) { |
| 104 |
showErrors( response.messages ); |
| 105 |
} else if ( response.message ) { |
| 106 |
showMessage( '<div class="learn-press-message error">' + response.message + '</div>' ); |
| 107 |
} |
| 108 |
|
| 109 |
$( '#learn-press-checkout-place-order' ).removeClass( 'loading' ); |
| 110 |
|
| 111 |
if ( 'success' === response.result ) { |
| 112 |
if ( response.redirect && response.redirect.match( /https?/ ) ) { |
| 113 |
$buttonCheckout.html( options.i18n_redirecting ); |
| 114 |
window.location = response.redirect; |
| 115 |
} |
| 116 |
} else { |
| 117 |
$buttonCheckout.html( btnText ); |
| 118 |
} |
| 119 |
}, |
| 120 |
error( jqXHR, textStatus, errorThrown ) { |
| 121 |
$( '#learn-press-checkout-place-order' ).removeClass( 'loading' ); |
| 122 |
|
| 123 |
showMessage( '<div class="learn-press-message error">' + errorThrown + '</div>' ); |
| 124 |
|
| 125 |
$buttonCheckout.html( btnText ); |
| 126 |
|
| 127 |
LP.unblockContent(); |
| 128 |
}, |
| 129 |
} ); |
| 130 |
|
| 131 |
return false; |
| 132 |
}; |
| 133 |
|
| 134 |
const _selectPaymentChange = function() { |
| 135 |
const id = $( this ).val(), |
| 136 |
$selected = $payments.children().filter( '.selected' ).removeClass( 'selected' ), |
| 137 |
buttonText = $selected.find( '#payment_method_' + selectedMethod ).data( 'order_button_text' ); |
| 138 |
|
| 139 |
$selected.find( '.payment-method-form' ).slideUp(); |
| 140 |
$selected.end().filter( '#learn-press-payment-method-' + id ).addClass( 'selected' ).find( '.payment-method-form' ).hide().slideDown(); |
| 141 |
|
| 142 |
selectedMethod = $selected.find( 'payment_method' ).val(); |
| 143 |
|
| 144 |
if ( buttonText ) { |
| 145 |
$buttonCheckout.html( buttonText ); |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
/** |
| 150 |
* Button to switch between mode login/register or place order |
| 151 |
* in case user is not logged in and guest checkout is enabled. |
| 152 |
*/ |
| 153 |
const _guestCheckoutClick = function() { |
| 154 |
const showOrHide = $formCheckout.toggle().is( ':visible' ); |
| 155 |
$formLogin.toggle( ! showOrHide ); |
| 156 |
$formRegister.toggle( ! showOrHide ); |
| 157 |
|
| 158 |
$( '#learn-press-button-guest-checkout' ).toggle( ! showOrHide ); |
| 159 |
}; |
| 160 |
|
| 161 |
/** |
| 162 |
* Append messages into document. |
| 163 |
* |
| 164 |
* @param message |
| 165 |
* @param wrap |
| 166 |
*/ |
| 167 |
const showMessage = function( message, wrap = false ) { |
| 168 |
removeMessage(); |
| 169 |
|
| 170 |
if ( $.isPlainObject( message ) ) { |
| 171 |
Object.keys( message ).reverse().forEach( ( id ) => { |
| 172 |
const m = message[ id ]; |
| 173 |
let msg = Array.isArray( m ) ? m[ 0 ] : m; |
| 174 |
const type = Array.isArray( m ) ? m[ 1 ] : ''; |
| 175 |
msg = '<div class="learn-press-message ' + ( typeof ( type ) === 'string' ? type : '' ) + '">' + msg + '</div>'; |
| 176 |
$formCheckout.prepend( msg ); |
| 177 |
} ); |
| 178 |
|
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
if ( wrap ) { |
| 183 |
message = '<div class="learn-press-message ' + ( typeof ( wrap ) === 'string' ? wrap : '' ) + '">' + message + '</div>'; |
| 184 |
} |
| 185 |
|
| 186 |
if ( Array.isArray( message ) ) { |
| 187 |
message.map( ( msg ) => $formCheckout.prepend( '<div class="learn-press-message error">' + msg + '</div>') ); |
| 188 |
} else { |
| 189 |
$formCheckout.prepend( '<div class="learn-press-message error">' + message + '</div>' ); |
| 190 |
} |
| 191 |
|
| 192 |
$( 'html, body' ).animate( { |
| 193 |
scrollTop: ( $formCheckout.offset().top - 100 ), |
| 194 |
}, 1000 ); |
| 195 |
|
| 196 |
$( document ).trigger( 'learn-press/checkout-error' ); |
| 197 |
}; |
| 198 |
|
| 199 |
/** |
| 200 |
* Callback function for guest email. |
| 201 |
* |
| 202 |
* @private |
| 203 |
*/ |
| 204 |
const _checkEmail = function() { |
| 205 |
if ( ! this.value.isEmail() ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
this.timer && clearTimeout( this.timer ); |
| 210 |
|
| 211 |
$checkoutEmail.addClass( 'loading' ); |
| 212 |
|
| 213 |
this.timer = setTimeout( function() { |
| 214 |
$.post( { |
| 215 |
url: window.location.href, |
| 216 |
data: { |
| 217 |
'lp-ajax': 'checkout-user-email-exists', |
| 218 |
email: $checkoutEmail.val(), |
| 219 |
}, |
| 220 |
success( response ) { |
| 221 |
const res = LP.parseJSON( response ); |
| 222 |
|
| 223 |
$checkoutEmail.removeClass( 'loading' ); |
| 224 |
|
| 225 |
$( '.lp-guest-checkout-output' ).remove(); |
| 226 |
|
| 227 |
if ( res && res.output ) { |
| 228 |
$checkoutEmail.after( res.output ); |
| 229 |
} |
| 230 |
}, |
| 231 |
} ); |
| 232 |
}, 500 ); |
| 233 |
}; |
| 234 |
|
| 235 |
/** |
| 236 |
* Remove all messages |
| 237 |
*/ |
| 238 |
const removeMessage = function() { |
| 239 |
$( '.learn-press-error, .learn-press-notice, .learn-press-message' ).remove(); |
| 240 |
}; |
| 241 |
|
| 242 |
/** |
| 243 |
* Callback function for showing/hiding register form. |
| 244 |
* |
| 245 |
* @param e |
| 246 |
* @param toggle |
| 247 |
*/ |
| 248 |
const _toggleRegisterForm = function( e, toggle ) { |
| 249 |
toggle = $formRegister.find( '.learn-press-form-register' ).toggle( toggle ).is( ':visible' ); |
| 250 |
$formRegister.find( '.checkout-form-register-toggle[data-toggle="show"]' ).toggle( ! toggle ); |
| 251 |
|
| 252 |
e && ( e.preventDefault(), _toggleLoginForm( null, ! toggle ) ); |
| 253 |
}; |
| 254 |
|
| 255 |
/** |
| 256 |
* Callback function for showing/hiding login form. |
| 257 |
* |
| 258 |
* @param e {Event} |
| 259 |
* @param toggle {boolean} |
| 260 |
* @private |
| 261 |
*/ |
| 262 |
const _toggleLoginForm = function( e, toggle ) { |
| 263 |
toggle = $formLogin.find( '.learn-press-form-login' ).toggle( toggle ).is( ':visible' ); |
| 264 |
|
| 265 |
$formLogin.find( '.checkout-form-login-toggle[data-toggle="show"]' ).toggle( ! toggle ); |
| 266 |
|
| 267 |
e && ( e.preventDefault(), _toggleRegisterForm( null, ! toggle ) ); |
| 268 |
}; |
| 269 |
|
| 270 |
/** |
| 271 |
* Place order action |
| 272 |
*/ |
| 273 |
$buttonCheckout.on( 'click', function( e ) { |
| 274 |
|
| 275 |
} ); |
| 276 |
|
| 277 |
$( '.lp-button-guest-checkout' ).on( 'click', _guestCheckoutClick ); |
| 278 |
$( '#learn-press-button-cancel-guest-checkout' ).on( 'click', _guestCheckoutClick ); |
| 279 |
|
| 280 |
$checkoutEmail.on( 'keyup changex', _checkEmail ).trigger( 'changex' ); |
| 281 |
$payments.on( 'change select', 'input[name="payment_method"]', _selectPaymentChange ); |
| 282 |
$formCheckout.on( 'submit', _formSubmit ); |
| 283 |
$payments.children( '.selected' ).find( 'input[name="payment_method"]' ).trigger( 'select' ); |
| 284 |
$formLogin.on( 'click', '.checkout-form-login-toggle', _toggleLoginForm ); |
| 285 |
$formRegister.on( 'click', '.checkout-form-register-toggle', _toggleRegisterForm ); |
| 286 |
|
| 287 |
$formRegister.find( 'input' ).each( function() { |
| 288 |
if ( ( -1 !== $.inArray( $( this ).attr( 'type' ).toLowerCase(), [ 'text', 'email', 'number' ] ) ) && $( this ).val() ) { |
| 289 |
_toggleRegisterForm(); |
| 290 |
|
| 291 |
return false; |
| 292 |
} |
| 293 |
} ); |
| 294 |
|
| 295 |
$formLogin.find( 'input:not([type="hidden"])' ).each( function() { |
| 296 |
if ( ( -1 !== $.inArray( $( this ).attr( 'type' ).toLowerCase(), [ 'text', 'email', 'number' ] ) ) && $( this ).val() ) { |
| 297 |
_toggleLoginForm(); |
| 298 |
|
| 299 |
return false; |
| 300 |
} |
| 301 |
} ); |
| 302 |
|
| 303 |
// Show form if there is only one form Register or Login |
| 304 |
if ( $formRegister.length && ! $formLogin.length ) { |
| 305 |
_toggleRegisterForm(); |
| 306 |
} else if ( ! $formRegister.length && $formLogin.length ) { |
| 307 |
_toggleLoginForm(); |
| 308 |
} |
| 309 |
|
| 310 |
$formCheckout |
| 311 |
.on( 'change', 'input[name="checkout-account-switch-form"]', function() { |
| 312 |
$( this ).next().find( 'input:not([type="hidden"]):visible' ).first().trigger( 'focus' ); |
| 313 |
} ) |
| 314 |
.on( 'change', '#guest_email', function() { |
| 315 |
$formCheckout.find( '#reg_email' ).val( this.value ); |
| 316 |
} ) |
| 317 |
.on( 'change', '#reg_email', function() { |
| 318 |
$formCheckout.find( '#guest_email' ).val( this.value ); |
| 319 |
} ); |
| 320 |
|
| 321 |
setTimeout( function() { |
| 322 |
$formCheckout.find( 'input:not([type="hidden"]):visible' ).first().trigger( 'focus' ); |
| 323 |
}, 300 ); |
| 324 |
}; |
| 325 |
|
| 326 |
$( document ).ready( function() { |
| 327 |
if ( typeof lpCheckoutSettings !== 'undefined' ) { |
| 328 |
LP.$checkout = new Checkout( lpCheckoutSettings ); |
| 329 |
} |
| 330 |
} ); |
| 331 |
}( jQuery ) ); |
| 332 |
|