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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / assets / js / frontend / checkout.js
woocommerce / assets / js / frontend Last commit date
add-payment-method.js 8 years ago add-payment-method.min.js 5 years ago add-to-cart-variation.js 5 years ago add-to-cart-variation.min.js 5 years ago add-to-cart.js 6 years ago add-to-cart.min.js 5 years ago address-i18n.js 5 years ago address-i18n.min.js 5 years ago cart-fragments.js 7 years ago cart-fragments.min.js 5 years ago cart.js 5 years ago cart.min.js 5 years ago checkout.js 5 years ago checkout.min.js 5 years ago country-select.js 6 years ago country-select.min.js 5 years ago credit-card-form.js 8 years ago credit-card-form.min.js 8 years ago geolocation.js 6 years ago geolocation.min.js 5 years ago lost-password.js 8 years ago lost-password.min.js 8 years ago password-strength-meter.js 6 years ago password-strength-meter.min.js 6 years ago price-slider.js 7 years ago price-slider.min.js 7 years ago single-product.js 5 years ago single-product.min.js 5 years ago tokenization-form.js 6 years ago tokenization-form.min.js 5 years ago woocommerce.js 5 years ago woocommerce.min.js 5 years ago
checkout.js
717 lines
1 /* global wc_checkout_params */
2 jQuery( function( $ ) {
3
4 // wc_checkout_params is required to continue, ensure the object exists
5 if ( typeof wc_checkout_params === 'undefined' ) {
6 return false;
7 }
8
9 $.blockUI.defaults.overlayCSS.cursor = 'default';
10
11 var wc_checkout_form = {
12 updateTimer: false,
13 dirtyInput: false,
14 selectedPaymentMethod: false,
15 xhr: false,
16 $order_review: $( '#order_review' ),
17 $checkout_form: $( 'form.checkout' ),
18 init: function() {
19 $( document.body ).bind( 'update_checkout', this.update_checkout );
20 $( document.body ).bind( 'init_checkout', this.init_checkout );
21
22 // Payment methods
23 this.$checkout_form.on( 'click', 'input[name="payment_method"]', this.payment_method_selected );
24
25 if ( $( document.body ).hasClass( 'woocommerce-order-pay' ) ) {
26 this.$order_review.on( 'click', 'input[name="payment_method"]', this.payment_method_selected );
27 this.$order_review.on( 'submit', this.submitOrder );
28 this.$order_review.attr( 'novalidate', 'novalidate' );
29 }
30
31 // Prevent HTML5 validation which can conflict.
32 this.$checkout_form.attr( 'novalidate', 'novalidate' );
33
34 // Form submission
35 this.$checkout_form.on( 'submit', this.submit );
36
37 // Inline validation
38 this.$checkout_form.on( 'input validate change', '.input-text, select, input:checkbox', this.validate_field );
39
40 // Manual trigger
41 this.$checkout_form.on( 'update', this.trigger_update_checkout );
42
43 // Inputs/selects which update totals
44 this.$checkout_form.on( 'change', 'select.shipping_method, input[name^="shipping_method"], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type="radio"], .update_totals_on_change input[type="checkbox"]', this.trigger_update_checkout ); // eslint-disable-line max-len
45 this.$checkout_form.on( 'change', '.address-field select', this.input_changed );
46 this.$checkout_form.on( 'change', '.address-field input.input-text, .update_totals_on_change input.input-text', this.maybe_input_changed ); // eslint-disable-line max-len
47 this.$checkout_form.on( 'keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', this.queue_update_checkout ); // eslint-disable-line max-len
48
49 // Address fields
50 this.$checkout_form.on( 'change', '#ship-to-different-address input', this.ship_to_different_address );
51
52 // Trigger events
53 this.$checkout_form.find( '#ship-to-different-address input' ).change();
54 this.init_payment_methods();
55
56 // Update on page load
57 if ( wc_checkout_params.is_checkout === '1' ) {
58 $( document.body ).trigger( 'init_checkout' );
59 }
60 if ( wc_checkout_params.option_guest_checkout === 'yes' ) {
61 $( 'input#createaccount' ).change( this.toggle_create_account ).change();
62 }
63 },
64 init_payment_methods: function() {
65 var $payment_methods = $( '.woocommerce-checkout' ).find( 'input[name="payment_method"]' );
66
67 // If there is one method, we can hide the radio input
68 if ( 1 === $payment_methods.length ) {
69 $payment_methods.eq(0).hide();
70 }
71
72 // If there was a previously selected method, check that one.
73 if ( wc_checkout_form.selectedPaymentMethod ) {
74 $( '#' + wc_checkout_form.selectedPaymentMethod ).prop( 'checked', true );
75 }
76
77 // If there are none selected, select the first.
78 if ( 0 === $payment_methods.filter( ':checked' ).length ) {
79 $payment_methods.eq(0).prop( 'checked', true );
80 }
81
82 // Get name of new selected method.
83 var checkedPaymentMethod = $payment_methods.filter( ':checked' ).eq(0).prop( 'id' );
84
85 if ( $payment_methods.length > 1 ) {
86 // Hide open descriptions.
87 $( 'div.payment_box:not(".' + checkedPaymentMethod + '")' ).filter( ':visible' ).slideUp( 0 );
88 }
89
90 // Trigger click event for selected method
91 $payment_methods.filter( ':checked' ).eq(0).trigger( 'click' );
92 },
93 get_payment_method: function() {
94 return wc_checkout_form.$checkout_form.find( 'input[name="payment_method"]:checked' ).val();
95 },
96 payment_method_selected: function( e ) {
97 e.stopPropagation();
98
99 if ( $( '.payment_methods input.input-radio' ).length > 1 ) {
100 var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) ),
101 is_checked = $( this ).is( ':checked' );
102
103 if ( is_checked && ! target_payment_box.is( ':visible' ) ) {
104 $( 'div.payment_box' ).filter( ':visible' ).slideUp( 230 );
105
106 if ( is_checked ) {
107 target_payment_box.slideDown( 230 );
108 }
109 }
110 } else {
111 $( 'div.payment_box' ).show();
112 }
113
114 if ( $( this ).data( 'order_button_text' ) ) {
115 $( '#place_order' ).text( $( this ).data( 'order_button_text' ) );
116 } else {
117 $( '#place_order' ).text( $( '#place_order' ).data( 'value' ) );
118 }
119
120 var selectedPaymentMethod = $( '.woocommerce-checkout input[name="payment_method"]:checked' ).attr( 'id' );
121
122 if ( selectedPaymentMethod !== wc_checkout_form.selectedPaymentMethod ) {
123 $( document.body ).trigger( 'payment_method_selected' );
124 }
125
126 wc_checkout_form.selectedPaymentMethod = selectedPaymentMethod;
127 },
128 toggle_create_account: function() {
129 $( 'div.create-account' ).hide();
130
131 if ( $( this ).is( ':checked' ) ) {
132 // Ensure password is not pre-populated.
133 $( '#account_password' ).val( '' ).change();
134 $( 'div.create-account' ).slideDown();
135 }
136 },
137 init_checkout: function() {
138 $( document.body ).trigger( 'update_checkout' );
139 },
140 maybe_input_changed: function( e ) {
141 if ( wc_checkout_form.dirtyInput ) {
142 wc_checkout_form.input_changed( e );
143 }
144 },
145 input_changed: function( e ) {
146 wc_checkout_form.dirtyInput = e.target;
147 wc_checkout_form.maybe_update_checkout();
148 },
149 queue_update_checkout: function( e ) {
150 var code = e.keyCode || e.which || 0;
151
152 if ( code === 9 ) {
153 return true;
154 }
155
156 wc_checkout_form.dirtyInput = this;
157 wc_checkout_form.reset_update_checkout_timer();
158 wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.maybe_update_checkout, '1000' );
159 },
160 trigger_update_checkout: function() {
161 wc_checkout_form.reset_update_checkout_timer();
162 wc_checkout_form.dirtyInput = false;
163 $( document.body ).trigger( 'update_checkout' );
164 },
165 maybe_update_checkout: function() {
166 var update_totals = true;
167
168 if ( $( wc_checkout_form.dirtyInput ).length ) {
169 var $required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' );
170
171 if ( $required_inputs.length ) {
172 $required_inputs.each( function() {
173 if ( $( this ).find( 'input.input-text' ).val() === '' ) {
174 update_totals = false;
175 }
176 });
177 }
178 }
179 if ( update_totals ) {
180 wc_checkout_form.trigger_update_checkout();
181 }
182 },
183 ship_to_different_address: function() {
184 $( 'div.shipping_address' ).hide();
185 if ( $( this ).is( ':checked' ) ) {
186 $( 'div.shipping_address' ).slideDown();
187 }
188 },
189 reset_update_checkout_timer: function() {
190 clearTimeout( wc_checkout_form.updateTimer );
191 },
192 is_valid_json: function( raw_json ) {
193 try {
194 var json = $.parseJSON( raw_json );
195
196 return ( json && 'object' === typeof json );
197 } catch ( e ) {
198 return false;
199 }
200 },
201 validate_field: function( e ) {
202 var $this = $( this ),
203 $parent = $this.closest( '.form-row' ),
204 validated = true,
205 validate_required = $parent.is( '.validate-required' ),
206 validate_email = $parent.is( '.validate-email' ),
207 event_type = e.type;
208
209 if ( 'input' === event_type ) {
210 $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-validated' ); // eslint-disable-line max-len
211 }
212
213 if ( 'validate' === event_type || 'change' === event_type ) {
214
215 if ( validate_required ) {
216 if ( 'checkbox' === $this.attr( 'type' ) && ! $this.is( ':checked' ) ) {
217 $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
218 validated = false;
219 } else if ( $this.val() === '' ) {
220 $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
221 validated = false;
222 }
223 }
224
225 if ( validate_email ) {
226 if ( $this.val() ) {
227 /* https://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */
228 var pattern = new RegExp(/^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i); // eslint-disable-line max-len
229
230 if ( ! pattern.test( $this.val() ) ) {
231 $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email' );
232 validated = false;
233 }
234 }
235 }
236
237 if ( validated ) {
238 $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email' ).addClass( 'woocommerce-validated' ); // eslint-disable-line max-len
239 }
240 }
241 },
242 update_checkout: function( event, args ) {
243 // Small timeout to prevent multiple requests when several fields update at the same time
244 wc_checkout_form.reset_update_checkout_timer();
245 wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.update_checkout_action, '5', args );
246 },
247 update_checkout_action: function( args ) {
248 if ( wc_checkout_form.xhr ) {
249 wc_checkout_form.xhr.abort();
250 }
251
252 if ( $( 'form.checkout' ).length === 0 ) {
253 return;
254 }
255
256 args = typeof args !== 'undefined' ? args : {
257 update_shipping_method: true
258 };
259
260 var country = $( '#billing_country' ).val(),
261 state = $( '#billing_state' ).val(),
262 postcode = $( ':input#billing_postcode' ).val(),
263 city = $( '#billing_city' ).val(),
264 address = $( ':input#billing_address_1' ).val(),
265 address_2 = $( ':input#billing_address_2' ).val(),
266 s_country = country,
267 s_state = state,
268 s_postcode = postcode,
269 s_city = city,
270 s_address = address,
271 s_address_2 = address_2,
272 $required_inputs = $( wc_checkout_form.$checkout_form ).find( '.address-field.validate-required:visible' ),
273 has_full_address = true;
274
275 if ( $required_inputs.length ) {
276 $required_inputs.each( function() {
277 if ( $( this ).find( ':input' ).val() === '' ) {
278 has_full_address = false;
279 }
280 });
281 }
282
283 if ( $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ) ) {
284 s_country = $( '#shipping_country' ).val();
285 s_state = $( '#shipping_state' ).val();
286 s_postcode = $( ':input#shipping_postcode' ).val();
287 s_city = $( '#shipping_city' ).val();
288 s_address = $( ':input#shipping_address_1' ).val();
289 s_address_2 = $( ':input#shipping_address_2' ).val();
290 }
291
292 var data = {
293 security : wc_checkout_params.update_order_review_nonce,
294 payment_method : wc_checkout_form.get_payment_method(),
295 country : country,
296 state : state,
297 postcode : postcode,
298 city : city,
299 address : address,
300 address_2 : address_2,
301 s_country : s_country,
302 s_state : s_state,
303 s_postcode : s_postcode,
304 s_city : s_city,
305 s_address : s_address,
306 s_address_2 : s_address_2,
307 has_full_address: has_full_address,
308 post_data : $( 'form.checkout' ).serialize()
309 };
310
311 if ( false !== args.update_shipping_method ) {
312 var shipping_methods = {};
313
314 // eslint-disable-next-line max-len
315 $( 'select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]' ).each( function() {
316 shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
317 } );
318
319 data.shipping_method = shipping_methods;
320 }
321
322 $( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).block({
323 message: null,
324 overlayCSS: {
325 background: '#fff',
326 opacity: 0.6
327 }
328 });
329
330 wc_checkout_form.xhr = $.ajax({
331 type: 'POST',
332 url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'update_order_review' ),
333 data: data,
334 success: function( data ) {
335
336 // Reload the page if requested
337 if ( data && true === data.reload ) {
338 window.location.reload();
339 return;
340 }
341
342 // Remove any notices added previously
343 $( '.woocommerce-NoticeGroup-updateOrderReview' ).remove();
344
345 var termsCheckBoxChecked = $( '#terms' ).prop( 'checked' );
346
347 // Save payment details to a temporary object
348 var paymentDetails = {};
349 $( '.payment_box :input' ).each( function() {
350 var ID = $( this ).attr( 'id' );
351
352 if ( ID ) {
353 if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
354 paymentDetails[ ID ] = $( this ).prop( 'checked' );
355 } else {
356 paymentDetails[ ID ] = $( this ).val();
357 }
358 }
359 });
360
361 // Always update the fragments
362 if ( data && data.fragments ) {
363 $.each( data.fragments, function ( key, value ) {
364 if ( ! wc_checkout_form.fragments || wc_checkout_form.fragments[ key ] !== value ) {
365 $( key ).replaceWith( value );
366 }
367 $( key ).unblock();
368 } );
369 wc_checkout_form.fragments = data.fragments;
370 }
371
372 // Recheck the terms and conditions box, if needed
373 if ( termsCheckBoxChecked ) {
374 $( '#terms' ).prop( 'checked', true );
375 }
376
377 // Fill in the payment details if possible without overwriting data if set.
378 if ( ! $.isEmptyObject( paymentDetails ) ) {
379 $( '.payment_box :input' ).each( function() {
380 var ID = $( this ).attr( 'id' );
381 if ( ID ) {
382 if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
383 $( this ).prop( 'checked', paymentDetails[ ID ] ).change();
384 } else if ( $.inArray( $( this ).attr( 'type' ), [ 'select' ] ) !== -1 ) {
385 $( this ).val( paymentDetails[ ID ] ).change();
386 } else if ( null !== $( this ).val() && 0 === $( this ).val().length ) {
387 $( this ).val( paymentDetails[ ID ] ).change();
388 }
389 }
390 });
391 }
392
393 // Check for error
394 if ( data && 'failure' === data.result ) {
395
396 var $form = $( 'form.checkout' );
397
398 // Remove notices from all sources
399 $( '.woocommerce-error, .woocommerce-message' ).remove();
400
401 // Add new errors returned by this event
402 if ( data.messages ) {
403 $form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-updateOrderReview">' + data.messages + '</div>' ); // eslint-disable-line max-len
404 } else {
405 $form.prepend( data );
406 }
407
408 // Lose focus for all fields
409 $form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).blur();
410
411 wc_checkout_form.scroll_to_notices();
412 }
413
414 // Re-init methods
415 wc_checkout_form.init_payment_methods();
416
417 // Fire updated_checkout event.
418 $( document.body ).trigger( 'updated_checkout', [ data ] );
419 }
420
421 });
422 },
423 handleUnloadEvent: function( e ) {
424 // Modern browsers have their own standard generic messages that they will display.
425 // Confirm, alert, prompt or custom message are not allowed during the unload event
426 // Browsers will display their own standard messages
427
428 // Check if the browser is Internet Explorer
429 if((navigator.userAgent.indexOf('MSIE') !== -1 ) || (!!document.documentMode)) {
430 // IE handles unload events differently than modern browsers
431 e.preventDefault();
432 return undefined;
433 }
434
435 return true;
436 },
437 attachUnloadEventsOnSubmit: function() {
438 $( window ).on('beforeunload', this.handleUnloadEvent);
439 },
440 detachUnloadEventsOnSubmit: function() {
441 $( window ).unbind('beforeunload', this.handleUnloadEvent);
442 },
443 blockOnSubmit: function( $form ) {
444 var form_data = $form.data();
445
446 if ( 1 !== form_data['blockUI.isBlocked'] ) {
447 $form.block({
448 message: null,
449 overlayCSS: {
450 background: '#fff',
451 opacity: 0.6
452 }
453 });
454 }
455 },
456 submitOrder: function() {
457 wc_checkout_form.blockOnSubmit( $( this ) );
458 },
459 submit: function() {
460 wc_checkout_form.reset_update_checkout_timer();
461 var $form = $( this );
462
463 if ( $form.is( '.processing' ) ) {
464 return false;
465 }
466
467 // Trigger a handler to let gateways manipulate the checkout if needed
468 // eslint-disable-next-line max-len
469 if ( $form.triggerHandler( 'checkout_place_order' ) !== false && $form.triggerHandler( 'checkout_place_order_' + wc_checkout_form.get_payment_method() ) !== false ) {
470
471 $form.addClass( 'processing' );
472
473 wc_checkout_form.blockOnSubmit( $form );
474
475 // Attach event to block reloading the page when the form has been submitted
476 wc_checkout_form.attachUnloadEventsOnSubmit();
477
478 // ajaxSetup is global, but we use it to ensure JSON is valid once returned.
479 $.ajaxSetup( {
480 dataFilter: function( raw_response, dataType ) {
481 // We only want to work with JSON
482 if ( 'json' !== dataType ) {
483 return raw_response;
484 }
485
486 if ( wc_checkout_form.is_valid_json( raw_response ) ) {
487 return raw_response;
488 } else {
489 // Attempt to fix the malformed JSON
490 var maybe_valid_json = raw_response.match( /{"result.*}/ );
491
492 if ( null === maybe_valid_json ) {
493 console.log( 'Unable to fix malformed JSON' );
494 } else if ( wc_checkout_form.is_valid_json( maybe_valid_json[0] ) ) {
495 console.log( 'Fixed malformed JSON. Original:' );
496 console.log( raw_response );
497 raw_response = maybe_valid_json[0];
498 } else {
499 console.log( 'Unable to fix malformed JSON' );
500 }
501 }
502
503 return raw_response;
504 }
505 } );
506
507 $.ajax({
508 type: 'POST',
509 url: wc_checkout_params.checkout_url,
510 data: $form.serialize(),
511 dataType: 'json',
512 success: function( result ) {
513 // Detach the unload handler that prevents a reload / redirect
514 wc_checkout_form.detachUnloadEventsOnSubmit();
515
516 try {
517 if ( 'success' === result.result && $form.triggerHandler( 'checkout_place_order_success' ) !== false ) {
518 if ( -1 === result.redirect.indexOf( 'https://' ) || -1 === result.redirect.indexOf( 'http://' ) ) {
519 window.location = result.redirect;
520 } else {
521 window.location = decodeURI( result.redirect );
522 }
523 } else if ( 'failure' === result.result ) {
524 throw 'Result failure';
525 } else {
526 throw 'Invalid response';
527 }
528 } catch( err ) {
529 // Reload page
530 if ( true === result.reload ) {
531 window.location.reload();
532 return;
533 }
534
535 // Trigger update in case we need a fresh nonce
536 if ( true === result.refresh ) {
537 $( document.body ).trigger( 'update_checkout' );
538 }
539
540 // Add new errors
541 if ( result.messages ) {
542 wc_checkout_form.submit_error( result.messages );
543 } else {
544 wc_checkout_form.submit_error( '<div class="woocommerce-error">' + wc_checkout_params.i18n_checkout_error + '</div>' ); // eslint-disable-line max-len
545 }
546 }
547 },
548 error: function( jqXHR, textStatus, errorThrown ) {
549 // Detach the unload handler that prevents a reload / redirect
550 wc_checkout_form.detachUnloadEventsOnSubmit();
551
552 wc_checkout_form.submit_error( '<div class="woocommerce-error">' + errorThrown + '</div>' );
553 }
554 });
555 }
556
557 return false;
558 },
559 submit_error: function( error_message ) {
560 $( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ).remove();
561 wc_checkout_form.$checkout_form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>' ); // eslint-disable-line max-len
562 wc_checkout_form.$checkout_form.removeClass( 'processing' ).unblock();
563 wc_checkout_form.$checkout_form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).blur();
564 wc_checkout_form.scroll_to_notices();
565 $( document.body ).trigger( 'checkout_error' );
566 },
567 scroll_to_notices: function() {
568 var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout' );
569
570 if ( ! scrollElement.length ) {
571 scrollElement = $( '.form.checkout' );
572 }
573 $.scroll_to_notices( scrollElement );
574 }
575 };
576
577 var wc_checkout_coupons = {
578 init: function() {
579 $( document.body ).on( 'click', 'a.showcoupon', this.show_coupon_form );
580 $( document.body ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon );
581 $( 'form.checkout_coupon' ).hide().submit( this.submit );
582 },
583 show_coupon_form: function() {
584 $( '.checkout_coupon' ).slideToggle( 400, function() {
585 $( '.checkout_coupon' ).find( ':input:eq(0)' ).focus();
586 });
587 return false;
588 },
589 submit: function() {
590 var $form = $( this );
591
592 if ( $form.is( '.processing' ) ) {
593 return false;
594 }
595
596 $form.addClass( 'processing' ).block({
597 message: null,
598 overlayCSS: {
599 background: '#fff',
600 opacity: 0.6
601 }
602 });
603
604 var data = {
605 security: wc_checkout_params.apply_coupon_nonce,
606 coupon_code: $form.find( 'input[name="coupon_code"]' ).val()
607 };
608
609 $.ajax({
610 type: 'POST',
611 url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' ),
612 data: data,
613 success: function( code ) {
614 $( '.woocommerce-error, .woocommerce-message' ).remove();
615 $form.removeClass( 'processing' ).unblock();
616
617 if ( code ) {
618 $form.before( code );
619 $form.slideUp();
620
621 $( document.body ).trigger( 'applied_coupon_in_checkout', [ data.coupon_code ] );
622 $( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
623 }
624 },
625 dataType: 'html'
626 });
627
628 return false;
629 },
630 remove_coupon: function( e ) {
631 e.preventDefault();
632
633 var container = $( this ).parents( '.woocommerce-checkout-review-order' ),
634 coupon = $( this ).data( 'coupon' );
635
636 container.addClass( 'processing' ).block({
637 message: null,
638 overlayCSS: {
639 background: '#fff',
640 opacity: 0.6
641 }
642 });
643
644 var data = {
645 security: wc_checkout_params.remove_coupon_nonce,
646 coupon: coupon
647 };
648
649 $.ajax({
650 type: 'POST',
651 url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_coupon' ),
652 data: data,
653 success: function( code ) {
654 $( '.woocommerce-error, .woocommerce-message' ).remove();
655 container.removeClass( 'processing' ).unblock();
656
657 if ( code ) {
658 $( 'form.woocommerce-checkout' ).before( code );
659
660 $( document.body ).trigger( 'removed_coupon_in_checkout', [ data.coupon_code ] );
661 $( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
662
663 // Remove coupon code from coupon field
664 $( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' );
665 }
666 },
667 error: function ( jqXHR ) {
668 if ( wc_checkout_params.debug_mode ) {
669 /* jshint devel: true */
670 console.log( jqXHR.responseText );
671 }
672 },
673 dataType: 'html'
674 });
675 }
676 };
677
678 var wc_checkout_login_form = {
679 init: function() {
680 $( document.body ).on( 'click', 'a.showlogin', this.show_login_form );
681 },
682 show_login_form: function() {
683 $( 'form.login, form.woocommerce-form--login' ).slideToggle();
684 return false;
685 }
686 };
687
688 var wc_terms_toggle = {
689 init: function() {
690 $( document.body ).on( 'click', 'a.woocommerce-terms-and-conditions-link', this.toggle_terms );
691 },
692
693 toggle_terms: function() {
694 if ( $( '.woocommerce-terms-and-conditions' ).length ) {
695 $( '.woocommerce-terms-and-conditions' ).slideToggle( function() {
696 var link_toggle = $( '.woocommerce-terms-and-conditions-link' );
697
698 if ( $( '.woocommerce-terms-and-conditions' ).is( ':visible' ) ) {
699 link_toggle.addClass( 'woocommerce-terms-and-conditions-link--open' );
700 link_toggle.removeClass( 'woocommerce-terms-and-conditions-link--closed' );
701 } else {
702 link_toggle.removeClass( 'woocommerce-terms-and-conditions-link--open' );
703 link_toggle.addClass( 'woocommerce-terms-and-conditions-link--closed' );
704 }
705 } );
706
707 return false;
708 }
709 }
710 };
711
712 wc_checkout_form.init();
713 wc_checkout_coupons.init();
714 wc_checkout_login_form.init();
715 wc_terms_toggle.init();
716 });
717