Completed
Pull Request — master (#1349)
by
unknown
02:00
created
includes/class-wc-stripe-customer.php 1 patch
Spacing   +109 added lines, -109 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined( 'ABSPATH' ) ) {
2
+if ( ! defined('ABSPATH')) {
3 3
 	exit;
4 4
 }
5 5
 
@@ -32,10 +32,10 @@  discard block
 block discarded – undo
32 32
 	 * Constructor
33 33
 	 * @param int $user_id The WP user ID
34 34
 	 */
35
-	public function __construct( $user_id = 0 ) {
36
-		if ( $user_id ) {
37
-			$this->set_user_id( $user_id );
38
-			$this->set_id( $this->get_id_from_meta( $user_id ) );
35
+	public function __construct($user_id = 0) {
36
+		if ($user_id) {
37
+			$this->set_user_id($user_id);
38
+			$this->set_id($this->get_id_from_meta($user_id));
39 39
 		}
40 40
 	}
41 41
 
@@ -51,15 +51,15 @@  discard block
 block discarded – undo
51 51
 	 * Set Stripe customer ID.
52 52
 	 * @param [type] $id [description]
53 53
 	 */
54
-	public function set_id( $id ) {
54
+	public function set_id($id) {
55 55
 		// Backwards compat for customer ID stored in array format. (Pre 3.0)
56
-		if ( is_array( $id ) && isset( $id['customer_id'] ) ) {
56
+		if (is_array($id) && isset($id['customer_id'])) {
57 57
 			$id = $id['customer_id'];
58 58
 
59
-			$this->update_id_in_meta( $id );
59
+			$this->update_id_in_meta($id);
60 60
 		}
61 61
 
62
-		$this->id = wc_clean( $id );
62
+		$this->id = wc_clean($id);
63 63
 	}
64 64
 
65 65
 	/**
@@ -67,15 +67,15 @@  discard block
 block discarded – undo
67 67
 	 * @return int
68 68
 	 */
69 69
 	public function get_user_id() {
70
-		return absint( $this->user_id );
70
+		return absint($this->user_id);
71 71
 	}
72 72
 
73 73
 	/**
74 74
 	 * Set User ID used by WordPress.
75 75
 	 * @param int $user_id
76 76
 	 */
77
-	public function set_user_id( $user_id ) {
78
-		$this->user_id = absint( $user_id );
77
+	public function set_user_id($user_id) {
78
+		$this->user_id = absint($user_id);
79 79
 	}
80 80
 
81 81
 	/**
@@ -83,13 +83,13 @@  discard block
 block discarded – undo
83 83
 	 * @return WP_User
84 84
 	 */
85 85
 	protected function get_user() {
86
-		return $this->get_user_id() ? get_user_by( 'id', $this->get_user_id() ) : false;
86
+		return $this->get_user_id() ? get_user_by('id', $this->get_user_id()) : false;
87 87
 	}
88 88
 
89 89
 	/**
90 90
 	 * Store data from the Stripe API about this customer
91 91
 	 */
92
-	public function set_customer_data( $data ) {
92
+	public function set_customer_data($data) {
93 93
 		$this->customer_data = $data;
94 94
 	}
95 95
 
@@ -99,58 +99,58 @@  discard block
 block discarded – undo
99 99
 	 * @param  array $args Additional arguments (optional).
100 100
 	 * @return array
101 101
 	 */
102
-	protected function generate_customer_request( $args = array() ) {
103
-		$billing_email = isset( $_POST['billing_email'] ) ? filter_var( $_POST['billing_email'], FILTER_SANITIZE_EMAIL ) : '';
102
+	protected function generate_customer_request($args = array()) {
103
+		$billing_email = isset($_POST['billing_email']) ? filter_var($_POST['billing_email'], FILTER_SANITIZE_EMAIL) : '';
104 104
 		$user          = $this->get_user();
105 105
 
106
-		if ( $user ) {
107
-			$billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true );
108
-			$billing_last_name  = get_user_meta( $user->ID, 'billing_last_name', true );
106
+		if ($user) {
107
+			$billing_first_name = get_user_meta($user->ID, 'billing_first_name', true);
108
+			$billing_last_name  = get_user_meta($user->ID, 'billing_last_name', true);
109 109
 
110 110
 			// If billing first name does not exists try the user first name.
111
-			if ( empty( $billing_first_name ) ) {
112
-				$billing_first_name = get_user_meta( $user->ID, 'first_name', true );
111
+			if (empty($billing_first_name)) {
112
+				$billing_first_name = get_user_meta($user->ID, 'first_name', true);
113 113
 			}
114 114
 
115 115
 			// If billing last name does not exists try the user last name.
116
-			if ( empty( $billing_last_name ) ) {
117
-				$billing_last_name = get_user_meta( $user->ID, 'last_name', true );
116
+			if (empty($billing_last_name)) {
117
+				$billing_last_name = get_user_meta($user->ID, 'last_name', true);
118 118
 			}
119 119
 
120 120
 			// translators: %1$s First name, %2$s Second name, %3$s Username.
121
-			$description = sprintf( __( 'Name: %1$s %2$s, Username: %s', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name, $user->user_login );
121
+			$description = sprintf(__('Name: %1$s %2$s, Username: %s', 'woocommerce-gateway-stripe'), $billing_first_name, $billing_last_name, $user->user_login);
122 122
 
123 123
 			$defaults = array(
124 124
 				'email'       => $user->user_email,
125 125
 				'description' => $description,
126 126
 			);
127 127
 
128
-			$billing_full_name = trim( $billing_first_name . ' ' . $billing_last_name );
129
-			if ( ! empty( $billing_full_name ) ) {
128
+			$billing_full_name = trim($billing_first_name . ' ' . $billing_last_name);
129
+			if ( ! empty($billing_full_name)) {
130 130
 				$defaults['name'] = $billing_full_name;
131 131
 			}
132 132
 		} else {
133
-			$billing_first_name = isset( $_POST['billing_first_name'] ) ? filter_var( wp_unslash( $_POST['billing_first_name'] ), FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
134
-			$billing_last_name  = isset( $_POST['billing_last_name'] ) ? filter_var( wp_unslash( $_POST['billing_last_name'] ), FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
133
+			$billing_first_name = isset($_POST['billing_first_name']) ? filter_var(wp_unslash($_POST['billing_first_name']), FILTER_SANITIZE_STRING) : ''; // phpcs:ignore WordPress.Security.NonceVerification
134
+			$billing_last_name  = isset($_POST['billing_last_name']) ? filter_var(wp_unslash($_POST['billing_last_name']), FILTER_SANITIZE_STRING) : ''; // phpcs:ignore WordPress.Security.NonceVerification
135 135
 
136 136
 			// translators: %1$s First name, %2$s Second name.
137
-			$description = sprintf( __( 'Name: %1$s %2$s, Guest', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name );
137
+			$description = sprintf(__('Name: %1$s %2$s, Guest', 'woocommerce-gateway-stripe'), $billing_first_name, $billing_last_name);
138 138
 
139 139
 			$defaults = array(
140 140
 				'email'       => $billing_email,
141 141
 				'description' => $description,
142 142
 			);
143 143
 
144
-			$billing_full_name = trim( $billing_first_name . ' ' . $billing_last_name );
145
-			if ( ! empty( $billing_full_name ) ) {
144
+			$billing_full_name = trim($billing_first_name . ' ' . $billing_last_name);
145
+			if ( ! empty($billing_full_name)) {
146 146
 				$defaults['name'] = $billing_full_name;
147 147
 			}
148 148
 		}
149 149
 
150 150
 		$metadata             = array();
151
-		$defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user );
151
+		$defaults['metadata'] = apply_filters('wc_stripe_customer_metadata', $metadata, $user);
152 152
 
153
-		return wp_parse_args( $args, $defaults );
153
+		return wp_parse_args($args, $defaults);
154 154
 	}
155 155
 
156 156
 	/**
@@ -158,23 +158,23 @@  discard block
 block discarded – undo
158 158
 	 * @param array $args
159 159
 	 * @return WP_Error|int
160 160
 	 */
161
-	public function create_customer( $args = array() ) {
162
-		$args     = $this->generate_customer_request( $args );
163
-		$response = WC_Stripe_API::request( apply_filters( 'wc_stripe_create_customer_args', $args ), 'customers' );
161
+	public function create_customer($args = array()) {
162
+		$args     = $this->generate_customer_request($args);
163
+		$response = WC_Stripe_API::request(apply_filters('wc_stripe_create_customer_args', $args), 'customers');
164 164
 
165
-		if ( ! empty( $response->error ) ) {
166
-			throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
165
+		if ( ! empty($response->error)) {
166
+			throw new WC_Stripe_Exception(print_r($response, true), $response->error->message);
167 167
 		}
168 168
 
169
-		$this->set_id( $response->id );
169
+		$this->set_id($response->id);
170 170
 		$this->clear_cache();
171
-		$this->set_customer_data( $response );
171
+		$this->set_customer_data($response);
172 172
 
173
-		if ( $this->get_user_id() ) {
174
-			$this->update_id_in_meta( $response->id );
173
+		if ($this->get_user_id()) {
174
+			$this->update_id_in_meta($response->id);
175 175
 		}
176 176
 
177
-		do_action( 'woocommerce_stripe_add_customer', $args, $response );
177
+		do_action('woocommerce_stripe_add_customer', $args, $response);
178 178
 
179 179
 		return $response->id;
180 180
 	}
@@ -189,30 +189,30 @@  discard block
 block discarded – undo
189 189
 	 *
190 190
 	 * @throws WC_Stripe_Exception
191 191
 	 */
192
-	public function update_customer( $args = array(), $is_retry = false ) {
193
-		if ( empty( $this->get_id() ) ) {
194
-			throw new WC_Stripe_Exception( 'id_required_to_update_user', __( 'Attempting to update a Stripe customer without a customer ID.', 'woocommerce-gateway-stripe' ) );
192
+	public function update_customer($args = array(), $is_retry = false) {
193
+		if (empty($this->get_id())) {
194
+			throw new WC_Stripe_Exception('id_required_to_update_user', __('Attempting to update a Stripe customer without a customer ID.', 'woocommerce-gateway-stripe'));
195 195
 		}
196 196
 
197
-		$args     = $this->generate_customer_request( $args );
198
-		$args     = apply_filters( 'wc_stripe_update_customer_args', $args );
199
-		$response = WC_Stripe_API::request( $args, 'customers/' . $this->get_id() );
197
+		$args     = $this->generate_customer_request($args);
198
+		$args     = apply_filters('wc_stripe_update_customer_args', $args);
199
+		$response = WC_Stripe_API::request($args, 'customers/' . $this->get_id());
200 200
 
201
-		if ( ! empty( $response->error ) ) {
202
-			if ( $this->is_no_such_customer_error( $response->error ) && ! $is_retry ) {
201
+		if ( ! empty($response->error)) {
202
+			if ($this->is_no_such_customer_error($response->error) && ! $is_retry) {
203 203
 				// This can happen when switching the main Stripe account or importing users from another site.
204 204
 				// If not already retrying, recreate the customer and then try updating it again.
205 205
 				$this->recreate_customer();
206
-				return $this->update_customer( $args, true );
206
+				return $this->update_customer($args, true);
207 207
 			}
208 208
 
209
-			throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
209
+			throw new WC_Stripe_Exception(print_r($response, true), $response->error->message);
210 210
 		}
211 211
 
212 212
 		$this->clear_cache();
213
-		$this->set_customer_data( $response );
213
+		$this->set_customer_data($response);
214 214
 
215
-		do_action( 'woocommerce_stripe_update_customer', $args, $response );
215
+		do_action('woocommerce_stripe_update_customer', $args, $response);
216 216
 
217 217
 		return $this->get_id();
218 218
 	}
@@ -224,11 +224,11 @@  discard block
 block discarded – undo
224 224
 	 * @since 4.1.2
225 225
 	 * @param array $error
226 226
 	 */
227
-	public function is_no_such_customer_error( $error ) {
227
+	public function is_no_such_customer_error($error) {
228 228
 		return (
229 229
 			$error &&
230 230
 			'invalid_request_error' === $error->type &&
231
-			preg_match( '/No such customer/i', $error->message )
231
+			preg_match('/No such customer/i', $error->message)
232 232
 		);
233 233
 	}
234 234
 
@@ -237,9 +237,9 @@  discard block
 block discarded – undo
237 237
 	 * @param string $source_id
238 238
 	 * @return WP_Error|int
239 239
 	 */
240
-	public function add_source( $source_id ) {
241
-		if ( ! $this->get_id() ) {
242
-			$this->set_id( $this->create_customer() );
240
+	public function add_source($source_id) {
241
+		if ( ! $this->get_id()) {
242
+			$this->set_id($this->create_customer());
243 243
 		}
244 244
 
245 245
 		$response = WC_Stripe_API::request(
@@ -251,62 +251,62 @@  discard block
 block discarded – undo
251 251
 
252 252
 		$wc_token = false;
253 253
 
254
-		if ( ! empty( $response->error ) ) {
254
+		if ( ! empty($response->error)) {
255 255
 			// It is possible the WC user once was linked to a customer on Stripe
256 256
 			// but no longer exists. Instead of failing, lets try to create a
257 257
 			// new customer.
258
-			if ( $this->is_no_such_customer_error( $response->error ) ) {
258
+			if ($this->is_no_such_customer_error($response->error)) {
259 259
 				$this->recreate_customer();
260
-				return $this->add_source( $source_id );
260
+				return $this->add_source($source_id);
261 261
 			} else {
262 262
 				return $response;
263 263
 			}
264
-		} elseif ( empty( $response->id ) ) {
265
-			return new WP_Error( 'error', __( 'Unable to add payment source.', 'woocommerce-gateway-stripe' ) );
264
+		} elseif (empty($response->id)) {
265
+			return new WP_Error('error', __('Unable to add payment source.', 'woocommerce-gateway-stripe'));
266 266
 		}
267 267
 
268 268
 		// Add token to WooCommerce.
269
-		if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) {
270
-			if ( ! empty( $response->type ) ) {
271
-				switch ( $response->type ) {
269
+		if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) {
270
+			if ( ! empty($response->type)) {
271
+				switch ($response->type) {
272 272
 					case 'alipay':
273 273
 						break;
274 274
 					case 'sepa_debit':
275 275
 						$wc_token = new WC_Payment_Token_SEPA();
276
-						$wc_token->set_token( $response->id );
277
-						$wc_token->set_gateway_id( 'stripe_sepa' );
278
-						$wc_token->set_last4( $response->sepa_debit->last4 );
276
+						$wc_token->set_token($response->id);
277
+						$wc_token->set_gateway_id('stripe_sepa');
278
+						$wc_token->set_last4($response->sepa_debit->last4);
279 279
 						break;
280 280
 					default:
281
-						if ( 'source' === $response->object && 'card' === $response->type ) {
281
+						if ('source' === $response->object && 'card' === $response->type) {
282 282
 							$wc_token = new WC_Payment_Token_CC();
283
-							$wc_token->set_token( $response->id );
284
-							$wc_token->set_gateway_id( 'stripe' );
285
-							$wc_token->set_card_type( strtolower( $response->card->brand ) );
286
-							$wc_token->set_last4( $response->card->last4 );
287
-							$wc_token->set_expiry_month( $response->card->exp_month );
288
-							$wc_token->set_expiry_year( $response->card->exp_year );
283
+							$wc_token->set_token($response->id);
284
+							$wc_token->set_gateway_id('stripe');
285
+							$wc_token->set_card_type(strtolower($response->card->brand));
286
+							$wc_token->set_last4($response->card->last4);
287
+							$wc_token->set_expiry_month($response->card->exp_month);
288
+							$wc_token->set_expiry_year($response->card->exp_year);
289 289
 						}
290 290
 						break;
291 291
 				}
292 292
 			} else {
293 293
 				// Legacy.
294 294
 				$wc_token = new WC_Payment_Token_CC();
295
-				$wc_token->set_token( $response->id );
296
-				$wc_token->set_gateway_id( 'stripe' );
297
-				$wc_token->set_card_type( strtolower( $response->brand ) );
298
-				$wc_token->set_last4( $response->last4 );
299
-				$wc_token->set_expiry_month( $response->exp_month );
300
-				$wc_token->set_expiry_year( $response->exp_year );
295
+				$wc_token->set_token($response->id);
296
+				$wc_token->set_gateway_id('stripe');
297
+				$wc_token->set_card_type(strtolower($response->brand));
298
+				$wc_token->set_last4($response->last4);
299
+				$wc_token->set_expiry_month($response->exp_month);
300
+				$wc_token->set_expiry_year($response->exp_year);
301 301
 			}
302 302
 
303
-			$wc_token->set_user_id( $this->get_user_id() );
303
+			$wc_token->set_user_id($this->get_user_id());
304 304
 			$wc_token->save();
305 305
 		}
306 306
 
307 307
 		$this->clear_cache();
308 308
 
309
-		do_action( 'woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id );
309
+		do_action('woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id);
310 310
 
311 311
 		return $response->id;
312 312
 	}
@@ -318,13 +318,13 @@  discard block
 block discarded – undo
318 318
 	 * @return array
319 319
 	 */
320 320
 	public function get_sources() {
321
-		if ( ! $this->get_id() ) {
321
+		if ( ! $this->get_id()) {
322 322
 			return array();
323 323
 		}
324 324
 
325
-		$sources = get_transient( 'stripe_sources_' . $this->get_id() );
325
+		$sources = get_transient('stripe_sources_' . $this->get_id());
326 326
 
327
-		if ( false === $sources ) {
327
+		if (false === $sources) {
328 328
 			$response = WC_Stripe_API::request(
329 329
 				array(
330 330
 					'limit' => 100,
@@ -333,35 +333,35 @@  discard block
 block discarded – undo
333 333
 				'GET'
334 334
 			);
335 335
 
336
-			if ( ! empty( $response->error ) ) {
336
+			if ( ! empty($response->error)) {
337 337
 				return array();
338 338
 			}
339 339
 
340
-			if ( is_array( $response->data ) ) {
340
+			if (is_array($response->data)) {
341 341
 				$sources = $response->data;
342 342
 			}
343 343
 
344
-			set_transient( 'stripe_sources_' . $this->get_id(), $sources, DAY_IN_SECONDS );
344
+			set_transient('stripe_sources_' . $this->get_id(), $sources, DAY_IN_SECONDS);
345 345
 		}
346 346
 
347
-		return empty( $sources ) ? array() : $sources;
347
+		return empty($sources) ? array() : $sources;
348 348
 	}
349 349
 
350 350
 	/**
351 351
 	 * Delete a source from stripe.
352 352
 	 * @param string $source_id
353 353
 	 */
354
-	public function delete_source( $source_id ) {
355
-		if ( ! $this->get_id() ) {
354
+	public function delete_source($source_id) {
355
+		if ( ! $this->get_id()) {
356 356
 			return false;
357 357
 		}
358 358
 
359
-		$response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $source_id ), 'DELETE' );
359
+		$response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field($source_id), 'DELETE');
360 360
 
361 361
 		$this->clear_cache();
362 362
 
363
-		if ( empty( $response->error ) ) {
364
-			do_action( 'wc_stripe_delete_source', $this->get_id(), $response );
363
+		if (empty($response->error)) {
364
+			do_action('wc_stripe_delete_source', $this->get_id(), $response);
365 365
 
366 366
 			return true;
367 367
 		}
@@ -373,10 +373,10 @@  discard block
 block discarded – undo
373 373
 	 * Set default source in Stripe
374 374
 	 * @param string $source_id
375 375
 	 */
376
-	public function set_default_source( $source_id ) {
376
+	public function set_default_source($source_id) {
377 377
 		$response = WC_Stripe_API::request(
378 378
 			array(
379
-				'default_source' => sanitize_text_field( $source_id ),
379
+				'default_source' => sanitize_text_field($source_id),
380 380
 			),
381 381
 			'customers/' . $this->get_id(),
382 382
 			'POST'
@@ -384,8 +384,8 @@  discard block
 block discarded – undo
384 384
 
385 385
 		$this->clear_cache();
386 386
 
387
-		if ( empty( $response->error ) ) {
388
-			do_action( 'wc_stripe_set_default_source', $this->get_id(), $response );
387
+		if (empty($response->error)) {
388
+			do_action('wc_stripe_set_default_source', $this->get_id(), $response);
389 389
 
390 390
 			return true;
391 391
 		}
@@ -397,8 +397,8 @@  discard block
 block discarded – undo
397 397
 	 * Deletes caches for this users cards.
398 398
 	 */
399 399
 	public function clear_cache() {
400
-		delete_transient( 'stripe_sources_' . $this->get_id() );
401
-		delete_transient( 'stripe_customer_' . $this->get_id() );
400
+		delete_transient('stripe_sources_' . $this->get_id());
401
+		delete_transient('stripe_customer_' . $this->get_id());
402 402
 		$this->customer_data = array();
403 403
 	}
404 404
 
@@ -408,8 +408,8 @@  discard block
 block discarded – undo
408 408
 	 * @param  int $user_id The ID of the WordPress user.
409 409
 	 * @return string|bool  Either the Stripe ID or false.
410 410
 	 */
411
-	public function get_id_from_meta( $user_id ) {
412
-		return get_user_option( '_stripe_customer_id', $user_id );
411
+	public function get_id_from_meta($user_id) {
412
+		return get_user_option('_stripe_customer_id', $user_id);
413 413
 	}
414 414
 
415 415
 	/**
@@ -417,15 +417,15 @@  discard block
 block discarded – undo
417 417
 	 *
418 418
 	 * @param string $id The Stripe customer ID.
419 419
 	 */
420
-	public function update_id_in_meta( $id ) {
421
-		update_user_option( $this->get_user_id(), '_stripe_customer_id', $id, false );
420
+	public function update_id_in_meta($id) {
421
+		update_user_option($this->get_user_id(), '_stripe_customer_id', $id, false);
422 422
 	}
423 423
 
424 424
 	/**
425 425
 	 * Deletes the user ID from the meta table with the right key.
426 426
 	 */
427 427
 	public function delete_id_from_meta() {
428
-		delete_user_option( $this->get_user_id(), '_stripe_customer_id', false );
428
+		delete_user_option($this->get_user_id(), '_stripe_customer_id', false);
429 429
 	}
430 430
 
431 431
 	/**
Please login to merge, or discard this patch.