Advertisement
RodzynBWA

Checkout Woo - PayU

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