Brak opisu

tokenization-form.js 3.8KB

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