Completed
Push — master ( 013109...33910d )
by Roy
02:10
created

wc_stripe_apple_pay.init   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 116

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 8.2857
c 0
b 0
f 0
cc 1
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global wc_stripe_apple_pay_params, Stripe */
2
Stripe.setPublishableKey( wc_stripe_apple_pay_params.key );
3
4
jQuery( function( $ ) {
5
	'use strict';
6
7
	/**
8
	 * Object to handle Stripe payment forms.
9
	 */
10
	var wc_stripe_apple_pay = {
11
		/**
12
		 * Get WC AJAX endpoint URL.
13
		 *
14
		 * @param  {String} endpoint Endpoint.
15
		 * @return {String}
16
		 */
17
		getAjaxURL: function( endpoint ) {
18
			return wc_stripe_apple_pay_params.ajaxurl
19
				.toString()
20
				.replace( '%%endpoint%%', 'wc_stripe_' + endpoint );
21
		},
22
23
		/**
24
		 * Initialize event handlers and UI state.
25
		 */
26
		init: function() {
27
			var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
28
29
			Stripe.applePay.checkAvailability( function( available ) {
30
				if ( available ) {
31
					$( '.apple-pay-button' ).show();
32
33
					wc_stripe_apple_pay.generate_cart();
34
				}
35
			});
36
37
			$( document.body ).on( 'click', '.apple-pay-button', function( e ) {
38
				e.preventDefault();
39
40
				var paymentRequest = {
41
						countryCode: wc_stripe_apple_pay_params.country_code,
42
						currencyCode: wc_stripe_apple_pay_params.currency_code,
43
						total: {
44
							label: wc_stripe_apple_pay_params.label,
45
							amount: wc_stripe_apple_pay_params.total
46
						},
47
						lineItems: wc_stripe_apple_pay_params.line_items,
48
						requiredBillingContactFields: ['postalAddress'],
49
						requiredShippingContactFields: 'yes' === wc_stripe_apple_pay_params.needs_shipping ? ['postalAddress', 'phone', 'email', 'name'] : ['phone', 'email', 'name']
50
					};
51
52
				var applePaySession = Stripe.applePay.buildSession( paymentRequest, function( result, completion ) {
53
					var data = {
54
						'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_nonce,
55
						'result': result
56
					};
57
58
					$.ajax({
59
						type:    'POST',
60
						data:    data,
61
						url:     wc_stripe_apple_pay.getAjaxURL( 'apple_pay' ),
62
						success: function( response ) {
63
							if ( 'true' === response.success ) {
64
								completion( ApplePaySession.STATUS_SUCCESS );
0 ignored issues
show
Bug introduced by
The variable ApplePaySession seems to be never declared. If this is a global, consider adding a /** global: ApplePaySession */ 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...
65
								window.location.href = response.redirect;
66
							}
67
68
							if ( 'false' === response.success ) {
69
								completion( ApplePaySession.STATUS_FAILURE );
70
71
								$( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + response.msg + '</p>' );
72
73
								// Scroll to error so user can see it.
74
								$( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 );
75
							}
76
						}
77
					});
78
				});
79
80
				// If shipping is needed -- get shipping methods.
81
				if ( 'yes' === wc_stripe_apple_pay_params.needs_shipping ) {
82
					// After the shipping contact/address has been selected
83
					applePaySession.onshippingcontactselected = function( shipping ) {
84
						var data = {
85
							'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_get_shipping_methods_nonce,
86
							'address': shipping.shippingContact
87
						};
88
89
						$.ajax({
90
							type:    'POST',
91
							data:    data,
92
							url:     wc_stripe_apple_pay.getAjaxURL( 'apple_pay_get_shipping_methods' ),
93
							success: function( response ) {
94
								var total = { 
95
									'label': wc_stripe_apple_pay_params.label,
96
									'amount': response.total
97
								};
98
99
								if ( 'true' === response.success ) {
100
									applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_SUCCESS, response.shipping_methods, total, response.line_items );
0 ignored issues
show
Bug introduced by
The variable ApplePaySession seems to be never declared. If this is a global, consider adding a /** global: ApplePaySession */ 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...
101
								}
102
103
								if ( 'false' === response.success ) {
104
									applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, response.shipping_methods, total, response.line_items );
105
								}
106
							}
107
						});
108
					};
109
110
					// After the shipping method has been selected
111
					applePaySession.onshippingmethodselected = function( event ) {
112
						var data = {
113
							'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_update_shipping_method_nonce,
114
							'selected_shipping_method': event.shippingMethod
115
						};
116
117
						$.ajax({
118
							type:    'POST',
119
							data:    data,
120
							url:     wc_stripe_apple_pay.getAjaxURL( 'apple_pay_update_shipping_method' ),
121
							success: function( response ) {
122
								var newTotal = {
123
									'label': wc_stripe_apple_pay_params.label,
124
									'amount': parseFloat( response.total ).toFixed(2)
125
								};
126
127
								if ( 'true' === response.success ) {
128
									applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_SUCCESS, newTotal, response.line_items );
0 ignored issues
show
Bug introduced by
The variable ApplePaySession seems to be never declared. If this is a global, consider adding a /** global: ApplePaySession */ 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...
129
								}
130
131
								if ( 'false' === response.success ) {
132
									applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, newTotal, response.line_items );
133
								}
134
							}
135
						});
136
					};
137
				}
138
139
				applePaySession.begin();
140
			});
141
		},
142
143
		generate_cart: function() {
144
			var self = this,
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
145
				data = {
146
					'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_cart_nonce
147
				};
148
149
			$.ajax({
150
				type:    'POST',
151
				data:    data,
152
				url:     wc_stripe_apple_pay.getAjaxURL( 'generate_apple_pay_cart' ),
153
				success: function( response ) {
154
					wc_stripe_apple_pay_params.total      = response.total;
155
					wc_stripe_apple_pay_params.line_items = response.line_items;
156
				}
157
			});
158
		}
159
	};
160
161
	wc_stripe_apple_pay.init();
162
163
	// We need to refresh Apple Pay data when total is updated.
164
	$( document.body ).on( 'updated_cart_totals', function() {
165
		wc_stripe_apple_pay.init();
166
	});
167
168
	// We need to refresh Apple Pay data when total is updated.
169
	$( document.body ).on( 'updated_checkout', function() {
170
		wc_stripe_apple_pay.init();
171
	});
172
});
173