Cancelled
Push — master ( 86817a...a519f9 )
by Roy
38s queued 38s
created

assets/js/stripe.js   A

Complexity

Total Complexity 34
Complexity/F 2.83

Size

Lines of Code 195
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 0
wmc 34
c 3
b 0
f 0
nc 3840
mnd 3
bc 26
fnc 12
dl 0
loc 195
rs 9.2
bpm 2.1666
cpm 2.8333
noi 5

1 Function

Rating   Name   Duplication   Size   Complexity  
B stripe.js ➔ jQuery 0 193 1
1
/* global wc_stripe_params */
2
Stripe.setPublishableKey( wc_stripe_params.key );
0 ignored issues
show
Bug introduced by
The variable Stripe seems to be never declared. If this is a global, consider adding a /** global: Stripe */ 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...
3
4
jQuery( function( $ ) {
5
	'use strict';
6
7
	/* Open and close for legacy class */
8
	$( 'form.checkout, form#order_review' ).on( 'change', 'input[name="wc-stripe-payment-token"]', function() {
9
		if ( 'new' === $( '.stripe-legacy-payment-fields input[name="wc-stripe-payment-token"]:checked' ).val() ) {
10
			$( '.stripe-legacy-payment-fields #stripe-payment-data' ).slideDown( 200 );
11
		} else {
12
			$( '.stripe-legacy-payment-fields #stripe-payment-data' ).slideUp( 200 );
13
		}
14
	} );
15
16
	/**
17
	 * Object to handle Stripe payment forms.
18
	 */
19
	var wc_stripe_form = {
20
21
		/**
22
		 * Initialize event handlers and UI state.
23
		 */
24
		init: function() {
25
			// checkout page
26
			if ( $( 'form.woocommerce-checkout' ).length ) {
27
				this.form = $( 'form.woocommerce-checkout' );
28
			}
29
30
			$( 'form.woocommerce-checkout' )
31
				.on(
32
					'checkout_place_order_stripe',
33
					this.onSubmit
34
				);
35
36
			// pay order page
37
			if ( $( 'form#order_review' ).length ) {
38
				this.form = $( 'form#order_review' );
39
			}
40
41
			$( 'form#order_review' )
42
				.on(
43
					'submit',
44
					this.onSubmit
45
				);
46
47
			// add payment method page
48
			if ( $( 'form#add_payment_method' ).length ) {
49
				this.form = $( 'form#add_payment_method' );
50
			}
51
52
			$( 'form#add_payment_method' )
53
				.on(
54
					'submit',
55
					this.onSubmit
56
				);
57
58
			$( document )
59
				.on(
60
					'change',
61
					'#wc-stripe-cc-form :input',
62
					this.onCCFormChange
63
				)
64
				.on(
65
					'stripeError',
66
					this.onError
67
				)
68
				.on(
69
					'checkout_error',
70
					this.clearToken
71
				);
72
		},
73
74
		isStripeChosen: function() {
75
			return $( '#payment_method_stripe' ).is( ':checked' ) && ( ! $( 'input[name="wc-stripe-payment-token"]:checked' ).length || 'new' === $( 'input[name="wc-stripe-payment-token"]:checked' ).val() );
76
		},
77
78
		hasToken: function() {
79
			return 0 < $( 'input.stripe_token' ).length;
80
		},
81
82
		block: function() {
83
			wc_stripe_form.form.block({
84
				message: null,
85
				overlayCSS: {
86
					background: '#fff',
87
					opacity: 0.6
88
				}
89
			});
90
		},
91
92
		unblock: function() {
93
			wc_stripe_form.form.unblock();
94
		},
95
96
		onError: function( e, responseObject ) {
97
			var message = responseObject.response.error.message;
98
99
			// Customers do not need to know the specifics of the below type of errors
100
			// therefore return a generic localizable error message.
101
			if ( 
102
				'invalid_request_error' === responseObject.response.error.type ||
103
				'api_connection_error'  === responseObject.response.error.type ||
104
				'api_error'             === responseObject.response.error.type ||
105
				'authentication_error'  === responseObject.response.error.type ||
106
				'rate_limit_error'      === responseObject.response.error.type
107
			) {
108
				message = wc_stripe_params.invalid_request_error;
109
			}
110
111
			if ( 'card_error' === responseObject.response.error.type && wc_stripe_params.hasOwnProperty( responseObject.response.error.code ) ) {
112
				message = wc_stripe_params[ responseObject.response.error.code ];
113
			}
114
115
			$( '.wc-stripe-error, .stripe_token' ).remove();
116
			$( '#stripe-card-number' ).closest( 'p' ).before( '<ul class="woocommerce_error woocommerce-error wc-stripe-error"><li>' + message + '</li></ul>' );
117
			wc_stripe_form.unblock();
118
		},
119
120
		onSubmit: function( e ) {
121
			if ( wc_stripe_form.isStripeChosen() && ! wc_stripe_form.hasToken() ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if wc_stripe_form.isStripeC..._stripe_form.hasToken() is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
122
				e.preventDefault();
123
				wc_stripe_form.block();
124
125
				var card       = $( '#stripe-card-number' ).val(),
126
					cvc        = $( '#stripe-card-cvc' ).val(),
127
					expires    = $( '#stripe-card-expiry' ).payment( 'cardExpiryVal' ),
128
					first_name = $( '#billing_first_name' ).length ? $( '#billing_first_name' ).val() : wc_stripe_params.billing_first_name,
129
					last_name  = $( '#billing_last_name' ).length ? $( '#billing_last_name' ).val() : wc_stripe_params.billing_last_name,
130
					data       = {
131
						number   : card,
132
						cvc      : cvc,
133
						exp_month: parseInt( expires.month, 10 ) || 0,
134
						exp_year : parseInt( expires.year, 10 ) || 0
135
					};
136
137
				if ( first_name && last_name ) {
138
					data.name = first_name + ' ' + last_name;
139
				}
140
141
				if ( $( '#billing_address_1' ).length > 0 ) {
142
					data.address_line1   = $( '#billing_address_1' ).val();
143
					data.address_line2   = $( '#billing_address_2' ).val();
144
					data.address_state   = $( '#billing_state' ).val();
145
					data.address_city    = $( '#billing_city' ).val();
146
					data.address_zip     = $( '#billing_postcode' ).val();
147
					data.address_country = $( '#billing_country' ).val();
148
				} else if ( wc_stripe_params.billing_address_1 ) {
149
					data.address_line1   = wc_stripe_params.billing_address_1;
150
					data.address_line2   = wc_stripe_params.billing_address_2;
151
					data.address_state   = wc_stripe_params.billing_state;
152
					data.address_city    = wc_stripe_params.billing_city;
153
					data.address_zip     = wc_stripe_params.billing_postcode;
154
					data.address_country = wc_stripe_params.billing_country;
155
				}
156
157
				Stripe.createToken( data, wc_stripe_form.onStripeResponse );
0 ignored issues
show
Bug introduced by
The variable Stripe seems to be never declared. If this is a global, consider adding a /** global: Stripe */ 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...
158
159
				// Prevent form submitting
160
				return false;
161
			}
162
		},
163
164
		onCCFormChange: function() {
165
			$( '.wc-stripe-error, .stripe_token' ).remove();
166
		},
167
168
		onStripeResponse: function( status, response ) {
169
			if ( response.error ) {
170
				$( document ).trigger( 'stripeError', { response: response } );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
171
			} else {
172
				// check if we allow prepaid cards
173
				if ( 'no' === wc_stripe_params.allow_prepaid_card && 'prepaid' === response.card.funding ) {
174
					response.error = { message: wc_stripe_params.no_prepaid_card_msg };
175
176
					$( document ).trigger( 'stripeError', { response: response } );
177
					
178
					return false;
179
				}
180
181
				// token contains id, last4, and card type
182
				var token = response.id;
183
184
				// insert the token into the form so it gets submitted to the server
185
				wc_stripe_form.form.append( "<input type='hidden' class='stripe_token' name='stripe_token' value='" + token + "'/>" );
186
				wc_stripe_form.form.submit();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
187
			}
188
		},
189
190
		clearToken: function() {
191
			$( '.stripe_token' ).remove();
192
		}
193
	};
194
195
	wc_stripe_form.init();
196
} );
197