Completed
Push — master ( 8328bb...628080 )
by Roy
03:52
created
includes/class-wc-stripe-customer.php 1 patch
Spacing   +83 added lines, -83 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( get_user_meta( $user_id, '_stripe_customer_id', true ) );
35
+	public function __construct($user_id = 0) {
36
+		if ($user_id) {
37
+			$this->set_user_id($user_id);
38
+			$this->set_id(get_user_meta($user_id, '_stripe_customer_id', true));
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
-			update_user_meta( $this->get_user_id(), '_stripe_customer_id', $id );
59
+			update_user_meta($this->get_user_id(), '_stripe_customer_id', $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
 
@@ -98,13 +98,13 @@  discard block
 block discarded – undo
98 98
 	 * @param array $args
99 99
 	 * @return WP_Error|int
100 100
 	 */
101
-	public function create_customer( $args = array() ) {
102
-		$billing_email = isset( $_POST['billing_email'] ) ? filter_var( $_POST['billing_email'], FILTER_SANITIZE_EMAIL ) : '';
101
+	public function create_customer($args = array()) {
102
+		$billing_email = isset($_POST['billing_email']) ? filter_var($_POST['billing_email'], FILTER_SANITIZE_EMAIL) : '';
103 103
 		$user = $this->get_user();
104 104
 
105
-		if ( $user ) {
106
-			$billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true );
107
-			$billing_last_name  = get_user_meta( $user->ID, 'billing_last_name', true );
105
+		if ($user) {
106
+			$billing_first_name = get_user_meta($user->ID, 'billing_first_name', true);
107
+			$billing_last_name  = get_user_meta($user->ID, 'billing_last_name', true);
108 108
 
109 109
 			$defaults = array(
110 110
 				'email'       => $user->user_email,
@@ -119,24 +119,24 @@  discard block
 block discarded – undo
119 119
 
120 120
 		$metadata = array();
121 121
 
122
-		$defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user );
122
+		$defaults['metadata'] = apply_filters('wc_stripe_customer_metadata', $metadata, $user);
123 123
 
124
-		$args     = wp_parse_args( $args, $defaults );
125
-		$response = WC_Stripe_API::request( apply_filters( 'wc_stripe_create_customer_args', $args ), 'customers' );
124
+		$args     = wp_parse_args($args, $defaults);
125
+		$response = WC_Stripe_API::request(apply_filters('wc_stripe_create_customer_args', $args), 'customers');
126 126
 
127
-		if ( ! empty( $response->error ) ) {
128
-			throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
127
+		if ( ! empty($response->error)) {
128
+			throw new WC_Stripe_Exception(print_r($response, true), $response->error->message);
129 129
 		}
130 130
 
131
-		$this->set_id( $response->id );
131
+		$this->set_id($response->id);
132 132
 		$this->clear_cache();
133
-		$this->set_customer_data( $response );
133
+		$this->set_customer_data($response);
134 134
 
135
-		if ( $this->get_user_id() ) {
136
-			update_user_meta( $this->get_user_id(), '_stripe_customer_id', $response->id );
135
+		if ($this->get_user_id()) {
136
+			update_user_meta($this->get_user_id(), '_stripe_customer_id', $response->id);
137 137
 		}
138 138
 
139
-		do_action( 'woocommerce_stripe_add_customer', $args, $response );
139
+		do_action('woocommerce_stripe_add_customer', $args, $response);
140 140
 
141 141
 		return $response->id;
142 142
 	}
@@ -148,11 +148,11 @@  discard block
 block discarded – undo
148 148
 	 * @since 4.1.2
149 149
 	 * @param array $error
150 150
 	 */
151
-	public function is_no_such_customer_error( $error ) {
151
+	public function is_no_such_customer_error($error) {
152 152
 		return (
153 153
 			$error &&
154 154
 			'invalid_request_error' === $error->type &&
155
-			preg_match( '/No such customer/i', $error->message )
155
+			preg_match('/No such customer/i', $error->message)
156 156
 		);
157 157
 	}
158 158
 
@@ -162,74 +162,74 @@  discard block
 block discarded – undo
162 162
 	 * @param bool $retry
163 163
 	 * @return WP_Error|int
164 164
 	 */
165
-	public function add_source( $source_id, $retry = true ) {
166
-		if ( ! $this->get_id() ) {
167
-			$this->set_id( $this->create_customer() );
165
+	public function add_source($source_id, $retry = true) {
166
+		if ( ! $this->get_id()) {
167
+			$this->set_id($this->create_customer());
168 168
 		}
169 169
 
170
-		$response = WC_Stripe_API::request( array(
170
+		$response = WC_Stripe_API::request(array(
171 171
 			'source' => $source_id,
172
-		), 'customers/' . $this->get_id() . '/sources' );
172
+		), 'customers/' . $this->get_id() . '/sources');
173 173
 
174 174
 		$wc_token = false;
175 175
 
176
-		if ( ! empty( $response->error ) ) {
176
+		if ( ! empty($response->error)) {
177 177
 			// It is possible the WC user once was linked to a customer on Stripe
178 178
 			// but no longer exists. Instead of failing, lets try to create a
179 179
 			// new customer.
180
-			if ( $this->is_no_such_customer_error( $response->error ) ) {
181
-				delete_user_meta( $this->get_user_id(), '_stripe_customer_id' );
180
+			if ($this->is_no_such_customer_error($response->error)) {
181
+				delete_user_meta($this->get_user_id(), '_stripe_customer_id');
182 182
 				$this->create_customer();
183
-				return $this->add_source( $source_id, false );
183
+				return $this->add_source($source_id, false);
184 184
 			} else {
185 185
 				return $response;
186 186
 			}
187
-		} elseif ( empty( $response->id ) ) {
188
-			return new WP_Error( 'error', __( 'Unable to add payment source.', 'woocommerce-gateway-stripe' ) );
187
+		} elseif (empty($response->id)) {
188
+			return new WP_Error('error', __('Unable to add payment source.', 'woocommerce-gateway-stripe'));
189 189
 		}
190 190
 
191 191
 		// Add token to WooCommerce.
192
-		if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) {
193
-			if ( ! empty( $response->type ) ) {
194
-				switch ( $response->type ) {
192
+		if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) {
193
+			if ( ! empty($response->type)) {
194
+				switch ($response->type) {
195 195
 					case 'alipay':
196 196
 						break;
197 197
 					case 'sepa_debit':
198 198
 						$wc_token = new WC_Payment_Token_SEPA();
199
-						$wc_token->set_token( $response->id );
200
-						$wc_token->set_gateway_id( 'stripe_sepa' );
201
-						$wc_token->set_last4( $response->sepa_debit->last4 );
199
+						$wc_token->set_token($response->id);
200
+						$wc_token->set_gateway_id('stripe_sepa');
201
+						$wc_token->set_last4($response->sepa_debit->last4);
202 202
 						break;
203 203
 					default:
204
-						if ( 'source' === $response->object && 'card' === $response->type ) {
204
+						if ('source' === $response->object && 'card' === $response->type) {
205 205
 							$wc_token = new WC_Payment_Token_CC();
206
-							$wc_token->set_token( $response->id );
207
-							$wc_token->set_gateway_id( 'stripe' );
208
-							$wc_token->set_card_type( strtolower( $response->card->brand ) );
209
-							$wc_token->set_last4( $response->card->last4 );
210
-							$wc_token->set_expiry_month( $response->card->exp_month );
211
-							$wc_token->set_expiry_year( $response->card->exp_year );
206
+							$wc_token->set_token($response->id);
207
+							$wc_token->set_gateway_id('stripe');
208
+							$wc_token->set_card_type(strtolower($response->card->brand));
209
+							$wc_token->set_last4($response->card->last4);
210
+							$wc_token->set_expiry_month($response->card->exp_month);
211
+							$wc_token->set_expiry_year($response->card->exp_year);
212 212
 						}
213 213
 						break;
214 214
 				}
215 215
 			} else {
216 216
 				// Legacy.
217 217
 				$wc_token = new WC_Payment_Token_CC();
218
-				$wc_token->set_token( $response->id );
219
-				$wc_token->set_gateway_id( 'stripe' );
220
-				$wc_token->set_card_type( strtolower( $response->brand ) );
221
-				$wc_token->set_last4( $response->last4 );
222
-				$wc_token->set_expiry_month( $response->exp_month );
223
-				$wc_token->set_expiry_year( $response->exp_year );
218
+				$wc_token->set_token($response->id);
219
+				$wc_token->set_gateway_id('stripe');
220
+				$wc_token->set_card_type(strtolower($response->brand));
221
+				$wc_token->set_last4($response->last4);
222
+				$wc_token->set_expiry_month($response->exp_month);
223
+				$wc_token->set_expiry_year($response->exp_year);
224 224
 			}
225 225
 
226
-			$wc_token->set_user_id( $this->get_user_id() );
226
+			$wc_token->set_user_id($this->get_user_id());
227 227
 			$wc_token->save();
228 228
 		}
229 229
 
230 230
 		$this->clear_cache();
231 231
 
232
-		do_action( 'woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id );
232
+		do_action('woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id);
233 233
 
234 234
 		return $response->id;
235 235
 	}
@@ -241,42 +241,42 @@  discard block
 block discarded – undo
241 241
 	 * @return array
242 242
 	 */
243 243
 	public function get_sources() {
244
-		if ( ! $this->get_id() ) {
244
+		if ( ! $this->get_id()) {
245 245
 			return array();
246 246
 		}
247 247
 
248
-		$sources = get_transient( 'stripe_sources_' . $this->get_id() );
248
+		$sources = get_transient('stripe_sources_' . $this->get_id());
249 249
 
250
-		$response = WC_Stripe_API::request( array(
250
+		$response = WC_Stripe_API::request(array(
251 251
 			'limit'       => 100,
252
-		), 'customers/' . $this->get_id() . '/sources', 'GET' );
252
+		), 'customers/' . $this->get_id() . '/sources', 'GET');
253 253
 
254
-		if ( ! empty( $response->error ) ) {
254
+		if ( ! empty($response->error)) {
255 255
 			return array();
256 256
 		}
257 257
 
258
-		if ( is_array( $response->data ) ) {
258
+		if (is_array($response->data)) {
259 259
 			$sources = $response->data;
260 260
 		}
261 261
 
262
-		return empty( $sources ) ? array() : $sources;
262
+		return empty($sources) ? array() : $sources;
263 263
 	}
264 264
 
265 265
 	/**
266 266
 	 * Delete a source from stripe.
267 267
 	 * @param string $source_id
268 268
 	 */
269
-	public function delete_source( $source_id ) {
270
-		if ( ! $this->get_id() ) {
269
+	public function delete_source($source_id) {
270
+		if ( ! $this->get_id()) {
271 271
 			return false;
272 272
 		}
273 273
 
274
-		$response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $source_id ), 'DELETE' );
274
+		$response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field($source_id), 'DELETE');
275 275
 
276 276
 		$this->clear_cache();
277 277
 
278
-		if ( empty( $response->error ) ) {
279
-			do_action( 'wc_stripe_delete_source', $this->get_id(), $response );
278
+		if (empty($response->error)) {
279
+			do_action('wc_stripe_delete_source', $this->get_id(), $response);
280 280
 
281 281
 			return true;
282 282
 		}
@@ -288,15 +288,15 @@  discard block
 block discarded – undo
288 288
 	 * Set default source in Stripe
289 289
 	 * @param string $source_id
290 290
 	 */
291
-	public function set_default_source( $source_id ) {
292
-		$response = WC_Stripe_API::request( array(
293
-			'default_source' => sanitize_text_field( $source_id ),
294
-		), 'customers/' . $this->get_id(), 'POST' );
291
+	public function set_default_source($source_id) {
292
+		$response = WC_Stripe_API::request(array(
293
+			'default_source' => sanitize_text_field($source_id),
294
+		), 'customers/' . $this->get_id(), 'POST');
295 295
 
296 296
 		$this->clear_cache();
297 297
 
298
-		if ( empty( $response->error ) ) {
299
-			do_action( 'wc_stripe_set_default_source', $this->get_id(), $response );
298
+		if (empty($response->error)) {
299
+			do_action('wc_stripe_set_default_source', $this->get_id(), $response);
300 300
 
301 301
 			return true;
302 302
 		}
@@ -308,8 +308,8 @@  discard block
 block discarded – undo
308 308
 	 * Deletes caches for this users cards.
309 309
 	 */
310 310
 	public function clear_cache() {
311
-		delete_transient( 'stripe_sources_' . $this->get_id() );
312
-		delete_transient( 'stripe_customer_' . $this->get_id() );
311
+		delete_transient('stripe_sources_' . $this->get_id());
312
+		delete_transient('stripe_customer_' . $this->get_id());
313 313
 		$this->customer_data = array();
314 314
 	}
315 315
 }
Please login to merge, or discard this patch.