tokenization-form.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ( function( $ ) {
  2. $( function() {
  3. var wcTokenizationForm = (function() {
  4. function wcTokenizationForm( target ) {
  5. var $target = $( target ),
  6. $formWrap = $target.closest( '.payment_box' ),
  7. $wcTokenizationForm = this;
  8. this.onTokenChange = function() {
  9. if ( 'new' === $( this ).val() ) {
  10. $wcTokenizationForm.showForm();
  11. $wcTokenizationForm.showSaveNewCheckbox();
  12. } else {
  13. $wcTokenizationForm.hideForm();
  14. $wcTokenizationForm.hideSaveNewCheckbox();
  15. }
  16. };
  17. this.onCreateAccountChange = function() {
  18. if ( $( this ).is( ':checked' ) ) {
  19. $wcTokenizationForm.showSaveNewCheckbox();
  20. } else {
  21. $wcTokenizationForm.hideSaveNewCheckbox();
  22. }
  23. };
  24. this.onDisplay = function() {
  25. // Make sure a radio button is selected if there is no is_default for this payment method..
  26. if ( 0 === $( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', $target ).length ) {
  27. $( ':input.woocommerce-SavedPaymentMethods-tokenInput:last', $target ).prop( 'checked', true );
  28. }
  29. // Don't show the "use new" radio button if we only have one method..
  30. if ( 0 === $target.data( 'count' ) ) {
  31. $( '.woocommerce-SavedPaymentMethods-new', $target ).hide();
  32. }
  33. // Trigger change event
  34. $( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', $target ).trigger( 'change' );
  35. // Hide "save card" if "Create Account" is not checked.
  36. // Check that the field is shown in the form - some plugins and force create account remove it
  37. if ( $( 'input#createaccount' ).length && ! $('input#createaccount').is( ':checked' ) ) {
  38. $wcTokenizationForm.hideSaveNewCheckbox();
  39. }
  40. };
  41. this.hideForm = function() {
  42. $( '.wc-payment-form', $formWrap ).hide();
  43. };
  44. this.showForm = function() {
  45. $( '.wc-payment-form', $formWrap ).show();
  46. };
  47. this.showSaveNewCheckbox = function() {
  48. $( '.woocommerce-SavedPaymentMethods-saveNew', $formWrap ).show();
  49. };
  50. this.hideSaveNewCheckbox = function() {
  51. $( '.woocommerce-SavedPaymentMethods-saveNew', $formWrap ).hide();
  52. };
  53. // When a radio button is changed, make sure to show/hide our new CC info area
  54. $( ':input.woocommerce-SavedPaymentMethods-tokenInput', $target ).change( this.onTokenChange );
  55. // OR if create account is checked
  56. $ ( 'input#createaccount' ).change( this.onCreateAccountChange );
  57. this.onDisplay();
  58. }
  59. return wcTokenizationForm;
  60. })();
  61. $( document.body ).on( 'updated_checkout wc-credit-card-form-init', function() {
  62. // Loop over gateways with saved payment methods
  63. var $saved_payment_methods = $( 'ul.woocommerce-SavedPaymentMethods' );
  64. $saved_payment_methods.each( function() {
  65. new wcTokenizationForm( this );
  66. } );
  67. } );
  68. });
  69. })( jQuery );