Completed
Push — master ( c7ef51...e8218a )
by Akeda
8s
created
includes/class-wc-stripe-payment-request.php 1 patch
Spacing   +84 added lines, -84 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
  * @version 3.1.0
8 8
  */
9 9
 
10
-if ( ! defined( 'ABSPATH' ) ) {
10
+if ( ! defined('ABSPATH')) {
11 11
 	exit;
12 12
 }
13 13
 
@@ -20,12 +20,12 @@  discard block
 block discarded – undo
20 20
 	 * Initialize class actions.
21 21
 	 */
22 22
 	public function __construct() {
23
-		add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
23
+		add_action('wp_enqueue_scripts', array($this, 'scripts'));
24 24
 
25
-		add_action( 'wc_ajax_wc_stripe_get_cart_details', array( $this, 'ajax_get_cart_details' ) );
26
-		add_action( 'wc_ajax_wc_stripe_get_shipping_options', array( $this, 'ajax_get_shipping_options' ) );
27
-		add_action( 'wc_ajax_wc_stripe_update_shipping_method', array( $this, 'ajax_update_shipping_method' ) );
28
-		add_action( 'wc_ajax_wc_stripe_create_order', array( $this, 'ajax_create_order' ) );
25
+		add_action('wc_ajax_wc_stripe_get_cart_details', array($this, 'ajax_get_cart_details'));
26
+		add_action('wc_ajax_wc_stripe_get_shipping_options', array($this, 'ajax_get_shipping_options'));
27
+		add_action('wc_ajax_wc_stripe_update_shipping_method', array($this, 'ajax_update_shipping_method'));
28
+		add_action('wc_ajax_wc_stripe_create_order', array($this, 'ajax_create_order'));
29 29
 	}
30 30
 
31 31
 	/**
@@ -34,10 +34,10 @@  discard block
 block discarded – undo
34 34
 	 * @return bool
35 35
 	 */
36 36
 	protected function is_activated() {
37
-		$options             = get_option( 'woocommerce_stripe_settings', array() );
38
-		$enabled             = isset( $options['enabled'] ) && 'yes' === $options['enabled'];
39
-		$stripe_checkout     = isset( $options['stripe_checkout'] ) && 'yes' !== $options['stripe_checkout'];
40
-		$request_payment_api = isset( $options['request_payment_api'] ) && 'yes' === $options['request_payment_api'];
37
+		$options             = get_option('woocommerce_stripe_settings', array());
38
+		$enabled             = isset($options['enabled']) && 'yes' === $options['enabled'];
39
+		$stripe_checkout     = isset($options['stripe_checkout']) && 'yes' !== $options['stripe_checkout'];
40
+		$request_payment_api = isset($options['request_payment_api']) && 'yes' === $options['request_payment_api'];
41 41
 
42 42
 		return $enabled && $stripe_checkout && $request_payment_api && is_ssl();
43 43
 	}
@@ -48,9 +48,9 @@  discard block
 block discarded – undo
48 48
 	 * @return string
49 49
 	 */
50 50
 	protected function get_publishable_key() {
51
-		$options = get_option( 'woocommerce_stripe_settings', array() );
51
+		$options = get_option('woocommerce_stripe_settings', array());
52 52
 
53
-		if ( empty( $options ) ) {
53
+		if (empty($options)) {
54 54
 			return '';
55 55
 		}
56 56
 
@@ -62,39 +62,39 @@  discard block
 block discarded – undo
62 62
 	 */
63 63
 	public function scripts() {
64 64
 		// Load PaymentRequest only on cart for now.
65
-		if ( ! is_cart() ) {
65
+		if ( ! is_cart()) {
66 66
 			return;
67 67
 		}
68 68
 
69
-		if ( ! $this->is_activated() ) {
69
+		if ( ! $this->is_activated()) {
70 70
 			return;
71 71
 		}
72 72
 
73
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
73
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
74 74
 
75
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
76
-		wp_enqueue_script( 'google-payment-request-shim', 'https://storage.googleapis.com/prshim/v1/payment-shim.js', '', '1.0', false );
77
-		wp_enqueue_script( 'wc-stripe-payment-request', plugins_url( 'assets/js/payment-request' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'jquery', 'stripe' ), WC_STRIPE_VERSION, true );
75
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
76
+		wp_enqueue_script('google-payment-request-shim', 'https://storage.googleapis.com/prshim/v1/payment-shim.js', '', '1.0', false);
77
+		wp_enqueue_script('wc-stripe-payment-request', plugins_url('assets/js/payment-request' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('jquery', 'stripe'), WC_STRIPE_VERSION, true);
78 78
 
79 79
 		wp_localize_script(
80 80
 			'wc-stripe-payment-request',
81 81
 			'wcStripePaymentRequestParams',
82 82
 			array(
83
-				'ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
83
+				'ajax_url' => WC_AJAX::get_endpoint('%%endpoint%%'),
84 84
 				'stripe'   => array(
85 85
 					'key'                => $this->get_publishable_key(),
86
-					'allow_prepaid_card' => apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no',
86
+					'allow_prepaid_card' => apply_filters('wc_stripe_allow_prepaid_card', true) ? 'yes' : 'no',
87 87
 				),
88 88
 				'nonce'    => array(
89
-					'payment'         => wp_create_nonce( 'wc-stripe-payment-request' ),
90
-					'shipping'        => wp_create_nonce( 'wc-stripe-payment-request-shipping' ),
91
-					'update_shipping' => wp_create_nonce( 'wc-stripe-update-shipping-method' ),
92
-					'checkout'        => wp_create_nonce( 'woocommerce-process_checkout' ),
89
+					'payment'         => wp_create_nonce('wc-stripe-payment-request'),
90
+					'shipping'        => wp_create_nonce('wc-stripe-payment-request-shipping'),
91
+					'update_shipping' => wp_create_nonce('wc-stripe-update-shipping-method'),
92
+					'checkout'        => wp_create_nonce('woocommerce-process_checkout'),
93 93
 				),
94 94
 				'i18n'     => array(
95
-					'no_prepaid_card'  => __( 'Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe' ),
95
+					'no_prepaid_card'  => __('Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe'),
96 96
 					/* translators: Do not translate the [option] placeholder */
97
-					'unknown_shipping' => __( 'Unknown shipping option "[option]".', 'woocommerce-gateway-stripe' ),
97
+					'unknown_shipping' => __('Unknown shipping option "[option]".', 'woocommerce-gateway-stripe'),
98 98
 				),
99 99
 			)
100 100
 		);
@@ -104,10 +104,10 @@  discard block
 block discarded – undo
104 104
 	 * Get cart details.
105 105
 	 */
106 106
 	public function ajax_get_cart_details() {
107
-		check_ajax_referer( 'wc-stripe-payment-request', 'security' );
107
+		check_ajax_referer('wc-stripe-payment-request', 'security');
108 108
 
109
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
110
-			define( 'WOOCOMMERCE_CART', true );
109
+		if ( ! defined('WOOCOMMERCE_CART')) {
110
+			define('WOOCOMMERCE_CART', true);
111 111
 		}
112 112
 
113 113
 		WC()->cart->calculate_totals();
@@ -119,18 +119,18 @@  discard block
 block discarded – undo
119 119
 			'shipping_required' => WC()->cart->needs_shipping(),
120 120
 			'order_data'        => array(
121 121
 				'total' => array(
122
-					'label'  => __( 'Total', 'woocommerce-gateway-stripe' ),
122
+					'label'  => __('Total', 'woocommerce-gateway-stripe'),
123 123
 					'amount' => array(
124
-						'value'    => max( 0, apply_filters( 'woocommerce_calculated_total', round( WC()->cart->cart_contents_total + WC()->cart->fee_total + WC()->cart->tax_total, WC()->cart->dp ), WC()->cart ) ),
124
+						'value'    => max(0, apply_filters('woocommerce_calculated_total', round(WC()->cart->cart_contents_total + WC()->cart->fee_total + WC()->cart->tax_total, WC()->cart->dp), WC()->cart)),
125 125
 						'currency' => $currency,
126 126
 					),
127 127
 				),
128 128
 				// Include line items such as subtotal, fees and taxes. No shipping option is provided here because it is not chosen yet.
129
-				'displayItems' => $this->compute_display_items( null ),
129
+				'displayItems' => $this->compute_display_items(null),
130 130
 			),
131 131
 		);
132 132
 
133
-		wp_send_json( $data );
133
+		wp_send_json($data);
134 134
 	}
135 135
 
136 136
 	/**
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 	 * @version 3.1.0
141 141
 	 * @param array $address
142 142
 	 */
143
-	public function calculate_shipping( $address = array() ) {
143
+	public function calculate_shipping($address = array()) {
144 144
 		$country   = $address['country'];
145 145
 		$state     = $address['state'];
146 146
 		$postcode  = $address['postcode'];
@@ -150,19 +150,19 @@  discard block
 block discarded – undo
150 150
 
151 151
 		WC()->shipping->reset_shipping();
152 152
 
153
-		if ( $postcode && WC_Validation::is_postcode( $postcode, $country ) ) {
154
-			$postcode = wc_format_postcode( $postcode, $country );
153
+		if ($postcode && WC_Validation::is_postcode($postcode, $country)) {
154
+			$postcode = wc_format_postcode($postcode, $country);
155 155
 		}
156 156
 
157
-		if ( $country ) {
158
-			WC()->customer->set_location( $country, $state, $postcode, $city );
159
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
157
+		if ($country) {
158
+			WC()->customer->set_location($country, $state, $postcode, $city);
159
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
160 160
 		} else {
161 161
 			WC()->customer->set_to_base();
162 162
 			WC()->customer->set_shipping_to_base();
163 163
 		}
164 164
 
165
-		version_compare( WC_VERSION, '3.0', '<' ) ? WC()->customer->calculated_shipping( true ) : WC()->customer->set_calculated_shipping( true );
165
+		version_compare(WC_VERSION, '3.0', '<') ? WC()->customer->calculated_shipping(true) : WC()->customer->set_calculated_shipping(true);
166 166
 
167 167
 		WC()->customer->save();
168 168
 
@@ -179,17 +179,17 @@  discard block
 block discarded – undo
179 179
 		$packages[0]['destination']['address']   = $address_1;
180 180
 		$packages[0]['destination']['address_2'] = $address_2;
181 181
 
182
-		foreach ( WC()->cart->get_cart() as $item ) {
183
-			if ( $item['data']->needs_shipping() ) {
184
-				if ( isset( $item['line_total'] ) ) {
182
+		foreach (WC()->cart->get_cart() as $item) {
183
+			if ($item['data']->needs_shipping()) {
184
+				if (isset($item['line_total'])) {
185 185
 					$packages[0]['contents_cost'] += $item['line_total'];
186 186
 				}
187 187
 			}
188 188
 		}
189 189
 
190
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
190
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
191 191
 
192
-		WC()->shipping->calculate_shipping( $packages );
192
+		WC()->shipping->calculate_shipping($packages);
193 193
 	}
194 194
 
195 195
 	/**
@@ -200,19 +200,19 @@  discard block
 block discarded – undo
200 200
 	 * @see WC_Shipping::get_packages().
201 201
 	 */
202 202
 	public function ajax_get_shipping_options() {
203
-		check_ajax_referer( 'wc-stripe-payment-request-shipping', 'security' );
203
+		check_ajax_referer('wc-stripe-payment-request-shipping', 'security');
204 204
 
205 205
 		// Set the shipping package.
206
-		$posted   = filter_input_array( INPUT_POST, array(
206
+		$posted = filter_input_array(INPUT_POST, array(
207 207
 			'country'   => FILTER_SANITIZE_STRING,
208 208
 			'state'     => FILTER_SANITIZE_STRING,
209 209
 			'postcode'  => FILTER_SANITIZE_STRING,
210 210
 			'city'      => FILTER_SANITIZE_STRING,
211 211
 			'address'   => FILTER_SANITIZE_STRING,
212 212
 			'address_2' => FILTER_SANITIZE_STRING,
213
-		) );
213
+		));
214 214
 
215
-		$this->calculate_shipping( $posted );
215
+		$this->calculate_shipping($posted);
216 216
 
217 217
 		// Set the shipping options.
218 218
 		$currency = get_woocommerce_currency();
@@ -220,13 +220,13 @@  discard block
 block discarded – undo
220 220
 
221 221
 		$packages = WC()->shipping->get_packages();
222 222
 
223
-		if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
224
-			foreach ( $packages as $package_key => $package ) {
225
-				if ( empty( $package['rates'] ) ) {
223
+		if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
224
+			foreach ($packages as $package_key => $package) {
225
+				if (empty($package['rates'])) {
226 226
 					break;
227 227
 				}
228 228
 
229
-				foreach ( $package['rates'] as $key => $rate ) {
229
+				foreach ($package['rates'] as $key => $rate) {
230 230
 					$data[] = array(
231 231
 						'id'       => $rate->id,
232 232
 						'label'    => $rate->label,
@@ -241,60 +241,60 @@  discard block
 block discarded – undo
241 241
 		}
242 242
 
243 243
 		// Auto select when have only one shipping method available.
244
-		if ( 1 === count( $data ) ) {
244
+		if (1 === count($data)) {
245 245
 			$data[0]['selected'] = true;
246 246
 		}
247 247
 
248
-		wp_send_json( $data );
248
+		wp_send_json($data);
249 249
 	}
250 250
 
251 251
 	/**
252 252
 	 * Update shipping method.
253 253
 	 */
254 254
 	public function ajax_update_shipping_method() {
255
-		check_ajax_referer( 'wc-stripe-update-shipping-method', 'security' );
255
+		check_ajax_referer('wc-stripe-update-shipping-method', 'security');
256 256
 
257
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
258
-			define( 'WOOCOMMERCE_CART', true );
257
+		if ( ! defined('WOOCOMMERCE_CART')) {
258
+			define('WOOCOMMERCE_CART', true);
259 259
 		}
260 260
 
261
-		$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
262
-		$shipping_method         = filter_input( INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
261
+		$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
262
+		$shipping_method         = filter_input(INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
263 263
 
264
-		if ( is_array( $shipping_method ) ) {
265
-			foreach ( $shipping_method as $i => $value ) {
266
-				$chosen_shipping_methods[ $i ] = wc_clean( $value );
264
+		if (is_array($shipping_method)) {
265
+			foreach ($shipping_method as $i => $value) {
266
+				$chosen_shipping_methods[$i] = wc_clean($value);
267 267
 			}
268 268
 		}
269 269
 
270
-		WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
270
+		WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods);
271 271
 
272 272
 		WC()->cart->calculate_totals();
273 273
 
274 274
 		// Send back the new cart total and line items to be displayed, such as subtotal, shipping rate(s), fees and taxes.
275
-		$data      = array(
275
+		$data = array(
276 276
 			'total' => WC()->cart->total,
277
-			'items' => $this->compute_display_items( $shipping_method[0] ),
277
+			'items' => $this->compute_display_items($shipping_method[0]),
278 278
 		);
279 279
 
280
-		wp_send_json( $data );
280
+		wp_send_json($data);
281 281
 	}
282 282
 
283 283
 	/**
284 284
 	 * Create order.
285 285
 	 */
286 286
 	public function ajax_create_order() {
287
-		if ( WC()->cart->is_empty() ) {
288
-			wp_send_json_error( __( 'Empty cart', 'woocommerce-gateway-stripe' ) );
287
+		if (WC()->cart->is_empty()) {
288
+			wp_send_json_error(__('Empty cart', 'woocommerce-gateway-stripe'));
289 289
 		}
290 290
 
291
-		if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
292
-			define( 'WOOCOMMERCE_CHECKOUT', true );
291
+		if ( ! defined('WOOCOMMERCE_CHECKOUT')) {
292
+			define('WOOCOMMERCE_CHECKOUT', true);
293 293
 		}
294 294
 
295 295
 		WC()->checkout()->process_checkout();
296 296
 
297
-		die( 0 );
297
+		die(0);
298 298
 	}
299 299
 
300 300
 	/**
@@ -302,26 +302,26 @@  discard block
 block discarded – undo
302 302
 	 *
303 303
 	 * @param string shipping_method_id If shipping method ID is provided, will include display items about shipping.
304 304
 	 */
305
-	protected function compute_display_items( $shipping_method_id ) {
305
+	protected function compute_display_items($shipping_method_id) {
306 306
 		$currency = get_woocommerce_currency();
307 307
 		$items = array(
308 308
 			// Subtotal excluding tax, because taxes is a separate item, below.
309 309
 			array(
310
-				'label' => __( 'Subtotal', 'woocommerce-gateway-stripe' ),
310
+				'label' => __('Subtotal', 'woocommerce-gateway-stripe'),
311 311
 				'amount' => array(
312
-					'value'    => max( 0, round( WC()->cart->subtotal_ex_tax, WC()->cart->dp ) ),
312
+					'value'    => max(0, round(WC()->cart->subtotal_ex_tax, WC()->cart->dp)),
313 313
 					'currency' => $currency,
314 314
 				),
315 315
 			),
316 316
 		);
317 317
 		// If a chosen shipping option was provided, add line item(s) for it and include the shipping tax.
318
-		$tax_total = max( 0, round( WC()->cart->tax_total, WC()->cart->dp ) );
319
-		if ( $shipping_method_id ) {
320
-			$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
318
+		$tax_total = max(0, round(WC()->cart->tax_total, WC()->cart->dp));
319
+		if ($shipping_method_id) {
320
+			$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
321 321
 			// Look through the package rates for $shipping_method_id, and when found, add a line item.
322
-			foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
323
-				foreach ( $package['rates'] as $key => $rate ) {
324
-					if ( $rate->id  == $shipping_method_id ) {
322
+			foreach (WC()->shipping->get_packages() as $package_key => $package) {
323
+				foreach ($package['rates'] as $key => $rate) {
324
+					if ($rate->id == $shipping_method_id) {
325 325
 						$items[] = array(
326 326
 							'label' => $rate->label,
327 327
 							'amount' => array(
@@ -335,7 +335,7 @@  discard block
 block discarded – undo
335 335
 			}
336 336
 		}
337 337
 		// Include fees and taxes as display items.
338
-		foreach ( WC()->cart->fees as $key => $fee ) {
338
+		foreach (WC()->cart->fees as $key => $fee) {
339 339
 			$items[] = array(
340 340
 				'label'  => $fee->name,
341 341
 				'amount' => array(
@@ -345,9 +345,9 @@  discard block
 block discarded – undo
345 345
 			);
346 346
 		}
347 347
 		// The tax total may include the shipping taxes if a shipping option is provided.
348
-		if ( 0 < $tax_total ) {
348
+		if (0 < $tax_total) {
349 349
 			$items[] = array(
350
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
350
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
351 351
 				'amount' => array(
352 352
 					'currency' => $currency,
353 353
 					'value'    => $tax_total,
Please login to merge, or discard this patch.