Completed
Push — master ( a8a1a1...3b0043 )
by Roy
02:58
created

assets/js/stripe-admin.js   A

Complexity

Total Complexity 20
Complexity/F 1.82

Size

Lines of Code 132
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 20
dl 0
loc 132
rs 10
c 0
b 0
f 0
cc 0
nc 1
mnd 1
bc 26
fnc 11
bpm 2.3636
cpm 1.8181
noi 4
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, #woocommerce_stripe_allow_remember_me' ).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, #woocommerce_stripe_allow_remember_me' ).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, #wc-gateway-stripe-apple-pay-domain' ).closest( 'tr' ).show();
60
				} else {
61
					$( '#woocommerce_stripe_apple_pay_button, #woocommerce_stripe_apple_pay_button_lang, #wc-gateway-stripe-apple-pay-domain' ).closest( 'tr' ).hide();
62
				}
63
			}).change();
64
65
			// Validate the keys to make sure it is matching test with test field.
66
			$( '#woocommerce_stripe_secret_key, #woocommerce_stripe_publishable_key' ).on( 'input', function() {
67
				var value = $( this ).val();
68
69
				if ( value.indexOf( '_test_' ) >= 0 ) {
70
					$( 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...
71
				} else {
72
					$( this ).css( 'border-color', '' );
73
					$( '.stripe-error-description', $( this ).parent() ).remove();
74
				}
75
			}).trigger( 'input' );
76
77
			// Validate the keys to make sure it is matching live with live field.
78
			$( '#woocommerce_stripe_test_secret_key, #woocommerce_stripe_test_publishable_key' ).on( 'input', function() {
79
				var value = $( this ).val();
80
81
				if ( value.indexOf( '_live_' ) >= 0 ) {
82
					$( 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...
83
				} else {
84
					$( this ).css( 'border-color', '' );
85
					$( '.stripe-error-description', $( this ).parent() ).remove();
86
				}
87
			}).trigger( 'input' );
88
89
			// Domain verification is based on the secret key value in real time.
90
			$( '#wc-gateway-stripe-apple-pay-domain' ).click( function( e ) {
91
				e.preventDefault();
92
93
				// Remove any previous messages.
94
				$( '.wc-stripe-apple-pay-domain-message' ).remove();
95
96
				if ( ! wc_stripe_admin.getSecretKey() ) {
97
					$( '#wc-gateway-stripe-apple-pay-domain' ).after( '<p class="wc-stripe-apple-pay-domain-message" style="color:red;">' + wc_stripe_admin_params.localized_messages.missing_secret_key + '</p>' );
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...
98
99
					return;
100
				}
101
102
				var data = {
103
					'nonce': wc_stripe_admin_params.nonce.apple_pay_domain_nonce,
104
					'action': 'wc_stripe_apple_pay_domain',
105
					'secret_key': wc_stripe_admin.getSecretKey()
106
				};
107
108
				$.ajax({
109
					type:    'POST',
110
					data:    data,
111
					url:     wc_stripe_admin_params.ajaxurl,
112
					success: function( response ) {
113
						if ( true === response.success ) {
114
							$( '#wc-gateway-stripe-apple-pay-domain' ).html( wc_stripe_admin_params.localized_messages.re_verify_button_text ).after( '<p class="wc-stripe-apple-pay-domain-message" style="color:green;">' + response.message + '</p>' );
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...
115
116
							$( '.wc-gateway-stripe-apple-pay-domain-set' ).val( 1 );
117
118
						}
119
120
						if ( false === response.success ) {
121
							$( '#wc-gateway-stripe-apple-pay-domain' ).after( '<p class="wc-stripe-apple-pay-domain-message" style="color:red;">' + response.message + '</p>' );
122
123
							$( '.wc-gateway-stripe-apple-pay-domain-set' ).val( 0 );
124
						}
125
					}
126
				});
127
			});
128
		}
129
	};
130
131
	wc_stripe_admin.init();
132
});
133