add-payment-method.js
5 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
5 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
5 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
5 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
5 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
5 years ago
password-strength-meter.min.js
5 years ago
price-slider.js
5 years ago
price-slider.min.js
5 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
tokenization-form.js
118 lines
| 1 | /*global wc_tokenization_form_params */ |
| 2 | jQuery( function( $ ) { |
| 3 | |
| 4 | /** |
| 5 | * WCTokenizationForm class. |
| 6 | */ |
| 7 | var TokenizationForm = function( $target ) { |
| 8 | this.$target = $target; |
| 9 | this.$formWrap = $target.closest( '.payment_box' ); |
| 10 | |
| 11 | // Params. |
| 12 | this.params = $.extend( {}, { |
| 13 | 'is_registration_required': false, |
| 14 | 'is_logged_in' : false |
| 15 | }, wc_tokenization_form_params ); |
| 16 | |
| 17 | // Bind functions to this. |
| 18 | this.onDisplay = this.onDisplay.bind( this ); |
| 19 | this.hideForm = this.hideForm.bind( this ); |
| 20 | this.showForm = this.showForm.bind( this ); |
| 21 | this.showSaveNewCheckbox = this.showSaveNewCheckbox.bind( this ); |
| 22 | this.hideSaveNewCheckbox = this.hideSaveNewCheckbox.bind( this ); |
| 23 | |
| 24 | // When a radio button is changed, make sure to show/hide our new CC info area. |
| 25 | this.$target.on( |
| 26 | 'click change', |
| 27 | ':input.woocommerce-SavedPaymentMethods-tokenInput', |
| 28 | { tokenizationForm: this }, |
| 29 | this.onTokenChange |
| 30 | ); |
| 31 | |
| 32 | // OR if create account is checked. |
| 33 | $( 'input#createaccount' ).change( { tokenizationForm: this }, this.onCreateAccountChange ); |
| 34 | |
| 35 | // First display. |
| 36 | this.onDisplay(); |
| 37 | }; |
| 38 | |
| 39 | TokenizationForm.prototype.onDisplay = function() { |
| 40 | // Make sure a radio button is selected if there is no is_default for this payment method.. |
| 41 | if ( 0 === $( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', this.$target ).length ) { |
| 42 | $( ':input.woocommerce-SavedPaymentMethods-tokenInput:last', this.$target ).prop( 'checked', true ); |
| 43 | } |
| 44 | |
| 45 | // Don't show the "use new" radio button if we only have one method.. |
| 46 | if ( 0 === this.$target.data( 'count' ) ) { |
| 47 | $( '.woocommerce-SavedPaymentMethods-new', this.$target ).remove(); |
| 48 | } |
| 49 | |
| 50 | // Hide "save card" if "Create Account" is not checked and registration is not forced. |
| 51 | var hasCreateAccountCheckbox = 0 < $( 'input#createaccount' ).length, |
| 52 | createAccount = hasCreateAccountCheckbox && $( 'input#createaccount' ).is( ':checked' ); |
| 53 | |
| 54 | if ( createAccount || this.params.is_logged_in || this.params.is_registration_required ) { |
| 55 | this.showSaveNewCheckbox(); |
| 56 | } else { |
| 57 | this.hideSaveNewCheckbox(); |
| 58 | } |
| 59 | |
| 60 | // Trigger change event |
| 61 | $( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', this.$target ).trigger( 'change' ); |
| 62 | }; |
| 63 | |
| 64 | TokenizationForm.prototype.onTokenChange = function( event ) { |
| 65 | if ( 'new' === $( this ).val() ) { |
| 66 | event.data.tokenizationForm.showForm(); |
| 67 | event.data.tokenizationForm.showSaveNewCheckbox(); |
| 68 | } else { |
| 69 | event.data.tokenizationForm.hideForm(); |
| 70 | event.data.tokenizationForm.hideSaveNewCheckbox(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | TokenizationForm.prototype.onCreateAccountChange = function( event ) { |
| 75 | if ( $( this ).is( ':checked' ) ) { |
| 76 | event.data.tokenizationForm.showSaveNewCheckbox(); |
| 77 | } else { |
| 78 | event.data.tokenizationForm.hideSaveNewCheckbox(); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | TokenizationForm.prototype.hideForm = function() { |
| 83 | $( '.wc-payment-form', this.$formWrap ).hide(); |
| 84 | }; |
| 85 | |
| 86 | TokenizationForm.prototype.showForm = function() { |
| 87 | $( '.wc-payment-form', this.$formWrap ).show(); |
| 88 | }; |
| 89 | |
| 90 | TokenizationForm.prototype.showSaveNewCheckbox = function() { |
| 91 | $( '.woocommerce-SavedPaymentMethods-saveNew', this.$formWrap ).show(); |
| 92 | }; |
| 93 | |
| 94 | TokenizationForm.prototype.hideSaveNewCheckbox = function() { |
| 95 | $( '.woocommerce-SavedPaymentMethods-saveNew', this.$formWrap ).hide(); |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * Function to call wc_product_gallery on jquery selector. |
| 100 | */ |
| 101 | $.fn.wc_tokenization_form = function( args ) { |
| 102 | new TokenizationForm( this, args ); |
| 103 | return this; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * Initialize. |
| 108 | */ |
| 109 | $( document.body ).on( 'updated_checkout wc-credit-card-form-init', function() { |
| 110 | // Loop over gateways with saved payment methods |
| 111 | var $saved_payment_methods = $( 'ul.woocommerce-SavedPaymentMethods' ); |
| 112 | |
| 113 | $saved_payment_methods.each( function() { |
| 114 | $( this ).wc_tokenization_form(); |
| 115 | } ); |
| 116 | } ); |
| 117 | } ); |
| 118 |