2016-05-26 11:42:29 +00:00
|
|
|
( function( $ ) {
|
2016-05-24 16:52:32 +00:00
|
|
|
$( function() {
|
|
|
|
var wcTokenizationForm = (function() {
|
|
|
|
function wcTokenizationForm( target ) {
|
|
|
|
var $target = $( target ),
|
|
|
|
$formWrap = $target.closest( '.payment_box' ),
|
|
|
|
$wcTokenizationForm = this;
|
|
|
|
|
|
|
|
this.onTokenChange = function() {
|
|
|
|
if ( 'new' === $( this ).val() ) {
|
|
|
|
$wcTokenizationForm.showForm();
|
2016-05-26 10:48:02 +00:00
|
|
|
$wcTokenizationForm.showSaveNewCheckbox();
|
2016-05-24 16:52:32 +00:00
|
|
|
} else {
|
|
|
|
$wcTokenizationForm.hideForm();
|
|
|
|
$wcTokenizationForm.hideSaveNewCheckbox();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onCreateAccountChange = function() {
|
|
|
|
if ( $( this ).is( ':checked' ) ) {
|
|
|
|
$wcTokenizationForm.showSaveNewCheckbox();
|
|
|
|
} else {
|
|
|
|
$wcTokenizationForm.hideSaveNewCheckbox();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onDisplay = function() {
|
|
|
|
// Make sure a radio button is selected if there is no is_default for this payment method..
|
2016-05-26 11:42:29 +00:00
|
|
|
if ( 0 === $( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', $target ).length ) {
|
|
|
|
$( ':input.woocommerce-SavedPaymentMethods-tokenInput:last', $target ).prop( 'checked', true );
|
2016-05-24 16:52:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 10:48:02 +00:00
|
|
|
// Don't show the "use new" radio button if we only have one method..
|
|
|
|
if ( 0 === $target.data( 'count' ) ) {
|
2016-05-26 11:42:29 +00:00
|
|
|
$( '.woocommerce-SavedPaymentMethods-new', $target ).hide();
|
2016-05-24 16:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger change event
|
2016-05-26 11:42:29 +00:00
|
|
|
$( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', $target ).trigger( 'change' );
|
2016-06-22 09:37:36 +00:00
|
|
|
|
|
|
|
// Hide "save card" if "Create Account" is not checked.
|
2016-06-22 09:39:03 +00:00
|
|
|
// Check that the field is shown in the form - some plugins and force create account remove it
|
2016-06-24 11:39:13 +00:00
|
|
|
if ( $( 'input#createaccount' ).length && ! $('input#createaccount').is( ':checked' ) ) {
|
|
|
|
$wcTokenizationForm.hideSaveNewCheckbox();
|
2016-10-14 11:39:19 +00:00
|
|
|
}
|
2016-06-24 11:39:13 +00:00
|
|
|
|
2016-05-24 16:52:32 +00:00
|
|
|
};
|
2016-02-15 16:28:46 +00:00
|
|
|
|
2016-05-24 16:52:32 +00:00
|
|
|
this.hideForm = function() {
|
|
|
|
$( '.wc-payment-form', $formWrap ).hide();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.showForm = function() {
|
|
|
|
$( '.wc-payment-form', $formWrap ).show();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.showSaveNewCheckbox = function() {
|
2016-05-26 11:42:29 +00:00
|
|
|
$( '.woocommerce-SavedPaymentMethods-saveNew', $formWrap ).show();
|
2016-05-24 16:52:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.hideSaveNewCheckbox = function() {
|
2016-05-26 11:42:29 +00:00
|
|
|
$( '.woocommerce-SavedPaymentMethods-saveNew', $formWrap ).hide();
|
2016-05-24 16:52:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// When a radio button is changed, make sure to show/hide our new CC info area
|
2016-05-26 11:42:29 +00:00
|
|
|
$( ':input.woocommerce-SavedPaymentMethods-tokenInput', $target ).change( this.onTokenChange );
|
2016-05-24 16:52:32 +00:00
|
|
|
|
|
|
|
// OR if create account is checked
|
|
|
|
$ ( 'input#createaccount' ).change( this.onCreateAccountChange );
|
|
|
|
|
|
|
|
this.onDisplay();
|
2016-02-15 16:28:46 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:52:32 +00:00
|
|
|
return wcTokenizationForm;
|
|
|
|
})();
|
2016-02-15 16:28:46 +00:00
|
|
|
|
2016-10-14 11:39:19 +00:00
|
|
|
$( document.body ).on( 'updated_checkout wc-credit-card-form-init', function() {
|
2016-05-24 16:52:32 +00:00
|
|
|
// Loop over gateways with saved payment methods
|
2016-05-26 11:42:29 +00:00
|
|
|
var $saved_payment_methods = $( 'ul.woocommerce-SavedPaymentMethods' );
|
2016-02-15 16:28:46 +00:00
|
|
|
|
2016-05-24 16:52:32 +00:00
|
|
|
$saved_payment_methods.each( function() {
|
|
|
|
new wcTokenizationForm( this );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
});
|
2016-05-26 10:48:02 +00:00
|
|
|
})( jQuery );
|