Completed
Pull Request — master (#242)
by Rodrigo
02:24
created

pe_saved_cardsꞌ).change   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
jQuery( function( $ ) {
2
	'use strict';
3
4
	/**
5
	 * Object to handle Stripe admin functions.
6
	 */
7
	var wc_stripe_admin = {
8
		isTestMode: function() {
9
			return $( '#woocommerce_stripe_testmode' ).is( ':checked' );
10
		},
11
12
		getSecretKey: function() {
13
			if ( wc_stripe_admin.isTestMode() ) {
14
				return $( '#woocommerce_stripe_test_secret_key' ).val();
15
			} else {
16
				return $( '#woocommerce_stripe_secret_key' ).val();
17
			}
18
		},
19
20
		/**
21
		 * Initialize.
22
		 */
23
		init: function() {
24
			$( document.body ).on( 'change', '#woocommerce_stripe_testmode', function() {
25
				var test_secret_key = $( '#woocommerce_stripe_test_secret_key' ).parents( 'tr' ).eq( 0 ),
26
					test_publishable_key = $( '#woocommerce_stripe_test_publishable_key' ).parents( 'tr' ).eq( 0 ),
27
					live_secret_key = $( '#woocommerce_stripe_secret_key' ).parents( 'tr' ).eq( 0 ),
28
					live_publishable_key = $( '#woocommerce_stripe_publishable_key' ).parents( 'tr' ).eq( 0 );
29
30
				if ( $( this ).is( ':checked' ) ) {
31
					test_secret_key.show();
32
					test_publishable_key.show();
33
					live_secret_key.hide();
34
					live_publishable_key.hide();
35
				} else {
36
					test_secret_key.hide();
37
					test_publishable_key.hide();
38
					live_secret_key.show();
39
					live_publishable_key.show();
40
				}
41
			} );
42
43
			$( '#woocommerce_stripe_testmode' ).change();
44
45
			// Toggle Stripe Checkout settings.
46
			$( '#woocommerce_stripe_stripe_checkout' ).change( function() {
47
				if ( $( this ).is( ':checked' ) ) {
48
					$( '#woocommerce_stripe_stripe_checkout_locale, #woocommerce_stripe_stripe_bitcoin, #woocommerce_stripe_stripe_checkout_image' ).closest( 'tr' ).show();
49
					$( '#woocommerce_stripe_request_payment_api' ).closest( 'tr' ).hide();
50
				} else {
51
					$( '#woocommerce_stripe_stripe_checkout_locale, #woocommerce_stripe_stripe_bitcoin, #woocommerce_stripe_stripe_checkout_image' ).closest( 'tr' ).hide();
52
					$( '#woocommerce_stripe_request_payment_api' ).closest( 'tr' ).show();
53
				}
54
			}).change();
55
56
			// Toggle Apple Pay settings.
57
			$( '#woocommerce_stripe_apple_pay' ).change( function() {
58
				if ( $( this ).is( ':checked' ) ) {
59
					$( '#woocommerce_stripe_apple_pay_button, #woocommerce_stripe_apple_pay_button_lang' ).closest( 'tr' ).show();
60
				} else {
61
					$( '#woocommerce_stripe_apple_pay_button, #woocommerce_stripe_apple_pay_button_lang' ).closest( 'tr' ).hide();
62
				}
63
			}).change();
64
65
			// Toggle Saved Cards settings
66
			$( '#woocommerce_stripe_saved_cards' ).change( function() {
67
				if ( $( this ).is( ':checked' ) ) {
68
					$( '#woocommerce_stripe_always_save_cards' ).closest( 'tr' ).show();
69
				} else {
70
					$( '#woocommerce_stripe_always_save_cards' ).closest( 'tr' ).hide();
71
				}
72
			}).change();
73
74
			// Validate the keys to make sure it is matching test with test field.
75
			$( '#woocommerce_stripe_secret_key, #woocommerce_stripe_publishable_key' ).on( 'input', function() {
76
				var value = $( this ).val();
77
78
				if ( value.indexOf( '_test_' ) >= 0 ) {
79
					$( this ).css( 'border-color', 'red' ).after( '<span class="description stripe-error-description" style="color:red; display:block;">' + wc_stripe_admin_params.localized_messages.not_valid_live_key_msg + '</span>' );
0 ignored issues
show
Bug introduced by
The variable wc_stripe_admin_params seems to be never declared. If this is a global, consider adding a /** global: wc_stripe_admin_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
80
				} else {
81
					$( this ).css( 'border-color', '' );
82
					$( '.stripe-error-description', $( this ).parent() ).remove();
83
				}
84
			}).trigger( 'input' );
85
86
			// Validate the keys to make sure it is matching live with live field.
87
			$( '#woocommerce_stripe_test_secret_key, #woocommerce_stripe_test_publishable_key' ).on( 'input', function() {
88
				var value = $( this ).val();
89
90
				if ( value.indexOf( '_live_' ) >= 0 ) {
91
					$( this ).css( 'border-color', 'red' ).after( '<span class="description stripe-error-description" style="color:red; display:block;">' + wc_stripe_admin_params.localized_messages.not_valid_test_key_msg + '</span>' );
0 ignored issues
show
Bug introduced by
The variable wc_stripe_admin_params seems to be never declared. If this is a global, consider adding a /** global: wc_stripe_admin_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
92
				} else {
93
					$( this ).css( 'border-color', '' );
94
					$( '.stripe-error-description', $( this ).parent() ).remove();
95
				}
96
			}).trigger( 'input' );
97
		}
98
	};
99
100
	wc_stripe_admin.init();
101
});
102