Completed
Pull Request — master (#236)
by Roy
02:25
created
includes/class-wc-gateway-stripe-addons.php 1 patch
Spacing   +130 added lines, -130 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
 
@@ -16,22 +16,22 @@  discard block
 block discarded – undo
16 16
 	public function __construct() {
17 17
 		parent::__construct();
18 18
 
19
-		if ( class_exists( 'WC_Subscriptions_Order' ) ) {
20
-			add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'scheduled_subscription_payment' ), 10, 2 );
21
-			add_action( 'wcs_resubscribe_order_created', array( $this, 'delete_resubscribe_meta' ), 10 );
22
-			add_action( 'wcs_renewal_order_created', array( $this, 'delete_renewal_meta' ), 10 );
23
-			add_action( 'woocommerce_subscription_failing_payment_method_updated_stripe', array( $this, 'update_failing_payment_method' ), 10, 2 );
19
+		if (class_exists('WC_Subscriptions_Order')) {
20
+			add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'scheduled_subscription_payment'), 10, 2);
21
+			add_action('wcs_resubscribe_order_created', array($this, 'delete_resubscribe_meta'), 10);
22
+			add_action('wcs_renewal_order_created', array($this, 'delete_renewal_meta'), 10);
23
+			add_action('woocommerce_subscription_failing_payment_method_updated_stripe', array($this, 'update_failing_payment_method'), 10, 2);
24 24
 
25 25
 			// display the credit card used for a subscription in the "My Subscriptions" table
26
-			add_filter( 'woocommerce_my_subscriptions_payment_method', array( $this, 'maybe_render_subscription_payment_method' ), 10, 2 );
26
+			add_filter('woocommerce_my_subscriptions_payment_method', array($this, 'maybe_render_subscription_payment_method'), 10, 2);
27 27
 
28 28
 			// allow store managers to manually set Stripe as the payment method on a subscription
29
-			add_filter( 'woocommerce_subscription_payment_meta', array( $this, 'add_subscription_payment_meta' ), 10, 2 );
30
-			add_filter( 'woocommerce_subscription_validate_payment_meta', array( $this, 'validate_subscription_payment_meta' ), 10, 2 );
29
+			add_filter('woocommerce_subscription_payment_meta', array($this, 'add_subscription_payment_meta'), 10, 2);
30
+			add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 2);
31 31
 		}
32 32
 
33
-		if ( class_exists( 'WC_Pre_Orders_Order' ) ) {
34
-			add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array( $this, 'process_pre_order_release_payment' ) );
33
+		if (class_exists('WC_Pre_Orders_Order')) {
34
+			add_action('wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array($this, 'process_pre_order_release_payment'));
35 35
 		}
36 36
 	}
37 37
 
@@ -40,8 +40,8 @@  discard block
 block discarded – undo
40 40
 	 * @param  int  $order_id
41 41
 	 * @return boolean
42 42
 	 */
43
-	protected function is_subscription( $order_id ) {
44
-		return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) );
43
+	protected function is_subscription($order_id) {
44
+		return (function_exists('wcs_order_contains_subscription') && (wcs_order_contains_subscription($order_id) || wcs_is_subscription($order_id) || wcs_order_contains_renewal($order_id)));
45 45
 	}
46 46
 
47 47
 	/**
@@ -49,8 +49,8 @@  discard block
 block discarded – undo
49 49
 	 * @param  int  $order_id
50 50
 	 * @return boolean
51 51
 	 */
52
-	protected function is_pre_order( $order_id ) {
53
-		return ( class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order_id ) );
52
+	protected function is_pre_order($order_id) {
53
+		return (class_exists('WC_Pre_Orders_Order') && WC_Pre_Orders_Order::order_contains_pre_order($order_id));
54 54
 	}
55 55
 
56 56
 	/**
@@ -58,41 +58,41 @@  discard block
 block discarded – undo
58 58
 	 * @param  int $order_id
59 59
 	 * @return array
60 60
 	 */
61
-	public function process_payment( $order_id, $retry = true, $force_customer = false ) {
62
-		if ( $this->is_subscription( $order_id ) ) {
61
+	public function process_payment($order_id, $retry = true, $force_customer = false) {
62
+		if ($this->is_subscription($order_id)) {
63 63
 			// Regular payment with force customer enabled
64
-			return parent::process_payment( $order_id, true, true );
64
+			return parent::process_payment($order_id, true, true);
65 65
 
66
-		} elseif ( $this->is_pre_order( $order_id ) ) {
67
-			return $this->process_pre_order( $order_id, $retry, $force_customer );
66
+		} elseif ($this->is_pre_order($order_id)) {
67
+			return $this->process_pre_order($order_id, $retry, $force_customer);
68 68
 
69 69
 		} else {
70
-			return parent::process_payment( $order_id, $retry, $force_customer );
70
+			return parent::process_payment($order_id, $retry, $force_customer);
71 71
 		}
72 72
 	}
73 73
 
74 74
 	/**
75 75
 	 * Updates other subscription sources.
76 76
 	 */
77
-	protected function save_source( $order, $source ) {
78
-		parent::save_source( $order, $source );
77
+	protected function save_source($order, $source) {
78
+		parent::save_source($order, $source);
79 79
 
80
-		$wc_pre_30 = version_compare( WC_VERSION, '3.0.0', '<' );
80
+		$wc_pre_30 = version_compare(WC_VERSION, '3.0.0', '<');
81 81
 		$order_id  = $wc_pre_30 ? $order->id : $order->get_id();
82 82
 
83 83
 		// Also store it on the subscriptions being purchased or paid for in the order
84
-		if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order_id ) ) {
85
-			$subscriptions = wcs_get_subscriptions_for_order( $order_id );
86
-		} elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order_id ) ) {
87
-			$subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id );
84
+		if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order_id)) {
85
+			$subscriptions = wcs_get_subscriptions_for_order($order_id);
86
+		} elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($order_id)) {
87
+			$subscriptions = wcs_get_subscriptions_for_renewal_order($order_id);
88 88
 		} else {
89 89
 			$subscriptions = array();
90 90
 		}
91 91
 
92
-		foreach ( $subscriptions as $subscription ) {
92
+		foreach ($subscriptions as $subscription) {
93 93
 			$subscription_id = $wc_pre_30 ? $subscription->id : $subscription->get_id();
94
-			update_post_meta( $subscription_id, '_stripe_customer_id', $source->customer );
95
-			update_post_meta( $subscription_id, '_stripe_card_id', $source->source );
94
+			update_post_meta($subscription_id, '_stripe_customer_id', $source->customer);
95
+			update_post_meta($subscription_id, '_stripe_card_id', $source->source);
96 96
 		}
97 97
 	}
98 98
 
@@ -103,40 +103,40 @@  discard block
 block discarded – undo
103 103
 	 * @param string $stripe_token (default: '')
104 104
 	 * @param  bool initial_payment
105 105
 	 */
106
-	public function process_subscription_payment( $order = '', $amount = 0 ) {
107
-		if ( $amount * 100 < WC_Stripe::get_minimum_amount() ) {
108
-			return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) );
106
+	public function process_subscription_payment($order = '', $amount = 0) {
107
+		if ($amount * 100 < WC_Stripe::get_minimum_amount()) {
108
+			return new WP_Error('stripe_error', sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100)));
109 109
 		}
110 110
 
111 111
 		// Get source from order
112
-		$source = $this->get_order_source( $order );
112
+		$source = $this->get_order_source($order);
113 113
 
114 114
 		// If no order source was defined, use user source instead.
115
-		if ( ! $source->customer ) {
116
-			$source = $this->get_source( $order->customer_user );
115
+		if ( ! $source->customer) {
116
+			$source = $this->get_source($order->customer_user);
117 117
 		}
118 118
 
119 119
 		// Or fail :(
120
-		if ( ! $source->customer ) {
121
-			return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) );
120
+		if ( ! $source->customer) {
121
+			return new WP_Error('stripe_error', __('Customer not found', 'woocommerce-gateway-stripe'));
122 122
 		}
123 123
 
124
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
125
-		$this->log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" );
124
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
125
+		$this->log("Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}");
126 126
 
127 127
 		// Make the request
128
-		$request             = $this->generate_payment_request( $order, $source );
128
+		$request             = $this->generate_payment_request($order, $source);
129 129
 		$request['capture']  = 'true';
130
-		$request['amount']   = $this->get_stripe_amount( $amount, $request['currency'] );
130
+		$request['amount']   = $this->get_stripe_amount($amount, $request['currency']);
131 131
 		$request['metadata'] = array(
132 132
 			'payment_type'   => 'recurring',
133
-			'site_url'       => esc_url( get_site_url() ),
133
+			'site_url'       => esc_url(get_site_url()),
134 134
 		);
135
-		$response            = WC_Stripe_API::request( $request );
135
+		$response = WC_Stripe_API::request($request);
136 136
 
137 137
 		// Process valid response
138
-		if ( ! is_wp_error( $response ) ) {
139
-			$this->process_response( $response, $order );
138
+		if ( ! is_wp_error($response)) {
139
+			$this->process_response($response, $order);
140 140
 		}
141 141
 
142 142
 		return $response;
@@ -147,42 +147,42 @@  discard block
 block discarded – undo
147 147
 	 * @param int $order_id
148 148
 	 * @return array
149 149
 	 */
150
-	public function process_pre_order( $order_id, $retry, $force_customer ) {
151
-		if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) {
150
+	public function process_pre_order($order_id, $retry, $force_customer) {
151
+		if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
152 152
 			try {
153
-				$order = wc_get_order( $order_id );
153
+				$order = wc_get_order($order_id);
154 154
 
155
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
156
-					throw new Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) );
155
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
156
+					throw new Exception(sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100)));
157 157
 				}
158 158
 
159
-				$source = $this->get_source( get_current_user_id(), true );
159
+				$source = $this->get_source(get_current_user_id(), true);
160 160
 
161 161
 				// We need a source on file to continue.
162
-				if ( empty( $source->customer ) || empty( $source->source ) ) {
163
-					throw new Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) );
162
+				if (empty($source->customer) || empty($source->source)) {
163
+					throw new Exception(__('Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe'));
164 164
 				}
165 165
 
166 166
 				// Store source to order meta
167
-				$this->save_source( $order, $source );
167
+				$this->save_source($order, $source);
168 168
 
169 169
 				// Remove cart
170 170
 				WC()->cart->empty_cart();
171 171
 
172 172
 				// Is pre ordered!
173
-				WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order );
173
+				WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
174 174
 
175 175
 				// Return thank you page redirect
176 176
 				return array(
177 177
 					'result'   => 'success',
178
-					'redirect' => $this->get_return_url( $order ),
178
+					'redirect' => $this->get_return_url($order),
179 179
 				);
180
-			} catch ( Exception $e ) {
181
-				wc_add_notice( $e->getMessage(), 'error' );
180
+			} catch (Exception $e) {
181
+				wc_add_notice($e->getMessage(), 'error');
182 182
 				return;
183 183
 			}
184 184
 		} else {
185
-			return parent::process_payment( $order_id, $retry, $force_customer );
185
+			return parent::process_payment($order_id, $retry, $force_customer);
186 186
 		}
187 187
 	}
188 188
 
@@ -191,7 +191,7 @@  discard block
 block discarded – undo
191 191
 	 * @param WC_Order $order
192 192
 	 * @return void
193 193
 	 */
194
-	public function process_pre_order_release_payment( $order ) {
194
+	public function process_pre_order_release_payment($order) {
195 195
 		try {
196 196
 			// Define some callbacks if the first attempt fails.
197 197
 			$retry_callbacks = array(
@@ -199,32 +199,32 @@  discard block
 block discarded – undo
199 199
 				'remove_order_customer_before_retry',
200 200
 			);
201 201
 
202
-			while ( 1 ) {
203
-				$source   = $this->get_order_source( $order );
204
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) );
202
+			while (1) {
203
+				$source   = $this->get_order_source($order);
204
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $source));
205 205
 
206
-				if ( is_wp_error( $response ) ) {
207
-					if ( 0 === sizeof( $retry_callbacks ) ) {
208
-						throw new Exception( $response->get_error_message() );
206
+				if (is_wp_error($response)) {
207
+					if (0 === sizeof($retry_callbacks)) {
208
+						throw new Exception($response->get_error_message());
209 209
 					} else {
210
-						$retry_callback = array_shift( $retry_callbacks );
211
-						call_user_func( array( $this, $retry_callback ), $order );
210
+						$retry_callback = array_shift($retry_callbacks);
211
+						call_user_func(array($this, $retry_callback), $order);
212 212
 					}
213 213
 				} else {
214 214
 					// Successful
215
-					$this->process_response( $response, $order );
215
+					$this->process_response($response, $order);
216 216
 					break;
217 217
 				}
218 218
 			}
219
-		} catch ( Exception $e ) {
220
-			$order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $e->getMessage() );
219
+		} catch (Exception $e) {
220
+			$order_note = sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $e->getMessage());
221 221
 
222 222
 			// Mark order as failed if not already set,
223 223
 			// otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times
224
-			if ( ! $order->has_status( 'failed' ) ) {
225
-				$order->update_status( 'failed', $order_note );
224
+			if ( ! $order->has_status('failed')) {
225
+				$order->update_status('failed', $order_note);
226 226
 			} else {
227
-				$order->add_order_note( $order_note );
227
+				$order->add_order_note($order_note);
228 228
 			}
229 229
 		}
230 230
 	}
@@ -233,20 +233,20 @@  discard block
 block discarded – undo
233 233
 	 * Don't transfer Stripe customer/token meta to resubscribe orders.
234 234
 	 * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription
235 235
 	 */
236
-	public function delete_resubscribe_meta( $resubscribe_order ) {
237
-		delete_post_meta( $resubscribe_order->id, '_stripe_customer_id' );
238
-		delete_post_meta( $resubscribe_order->id, '_stripe_card_id' );
239
-		$this->delete_renewal_meta( $resubscribe_order );
236
+	public function delete_resubscribe_meta($resubscribe_order) {
237
+		delete_post_meta($resubscribe_order->id, '_stripe_customer_id');
238
+		delete_post_meta($resubscribe_order->id, '_stripe_card_id');
239
+		$this->delete_renewal_meta($resubscribe_order);
240 240
 	}
241 241
 
242 242
 	/**
243 243
 	 * Don't transfer Stripe fee/ID meta to renewal orders.
244 244
 	 * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription
245 245
 	 */
246
-	public function delete_renewal_meta( $renewal_order ) {
247
-		delete_post_meta( $renewal_order->id, 'Stripe Fee' );
248
-		delete_post_meta( $renewal_order->id, 'Net Revenue From Stripe' );
249
-		delete_post_meta( $renewal_order->id, 'Stripe Payment ID' );
246
+	public function delete_renewal_meta($renewal_order) {
247
+		delete_post_meta($renewal_order->id, 'Stripe Fee');
248
+		delete_post_meta($renewal_order->id, 'Net Revenue From Stripe');
249
+		delete_post_meta($renewal_order->id, 'Stripe Payment ID');
250 250
 		return $renewal_order;
251 251
 	}
252 252
 
@@ -256,11 +256,11 @@  discard block
 block discarded – undo
256 256
 	 * @param $amount_to_charge float The amount to charge.
257 257
 	 * @param $renewal_order WC_Order A WC_Order object created to record the renewal payment.
258 258
 	 */
259
-	public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) {
260
-		$response = $this->process_subscription_payment( $renewal_order, $amount_to_charge );
259
+	public function scheduled_subscription_payment($amount_to_charge, $renewal_order) {
260
+		$response = $this->process_subscription_payment($renewal_order, $amount_to_charge);
261 261
 
262
-		if ( is_wp_error( $response ) ) {
263
-			$renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) );
262
+		if (is_wp_error($response)) {
263
+			$renewal_order->update_status('failed', sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $response->get_error_message()));
264 264
 		}
265 265
 	}
266 266
 
@@ -268,18 +268,18 @@  discard block
 block discarded – undo
268 268
 	 * Remove order meta
269 269
 	 * @param  object $order
270 270
 	 */
271
-	public function remove_order_source_before_retry( $order ) {
272
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
273
-		delete_post_meta( $order_id, '_stripe_card_id' );
271
+	public function remove_order_source_before_retry($order) {
272
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
273
+		delete_post_meta($order_id, '_stripe_card_id');
274 274
 	}
275 275
 
276 276
 	/**
277 277
 	 * Remove order meta
278 278
 	 * @param  object $order
279 279
 	 */
280
-	public function remove_order_customer_before_retry( $order ) {
281
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
282
-		delete_post_meta( $order_id, '_stripe_customer_id' );
280
+	public function remove_order_customer_before_retry($order) {
281
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
282
+		delete_post_meta($order_id, '_stripe_customer_id');
283 283
 	}
284 284
 
285 285
 	/**
@@ -291,9 +291,9 @@  discard block
 block discarded – undo
291 291
 	 * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment).
292 292
 	 * @return void
293 293
 	 */
294
-	public function update_failing_payment_method( $subscription, $renewal_order ) {
295
-		update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id );
296
-		update_post_meta( $subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id );
294
+	public function update_failing_payment_method($subscription, $renewal_order) {
295
+		update_post_meta($subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id);
296
+		update_post_meta($subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id);
297 297
 	}
298 298
 
299 299
 	/**
@@ -305,15 +305,15 @@  discard block
 block discarded – undo
305 305
 	 * @param WC_Subscription $subscription An instance of a subscription object
306 306
 	 * @return array
307 307
 	 */
308
-	public function add_subscription_payment_meta( $payment_meta, $subscription ) {
309
-		$payment_meta[ $this->id ] = array(
308
+	public function add_subscription_payment_meta($payment_meta, $subscription) {
309
+		$payment_meta[$this->id] = array(
310 310
 			'post_meta' => array(
311 311
 				'_stripe_customer_id' => array(
312
-					'value' => get_post_meta( $subscription->id, '_stripe_customer_id', true ),
312
+					'value' => get_post_meta($subscription->id, '_stripe_customer_id', true),
313 313
 					'label' => 'Stripe Customer ID',
314 314
 				),
315 315
 				'_stripe_card_id' => array(
316
-					'value' => get_post_meta( $subscription->id, '_stripe_card_id', true ),
316
+					'value' => get_post_meta($subscription->id, '_stripe_card_id', true),
317 317
 					'label' => 'Stripe Card ID',
318 318
 				),
319 319
 			),
@@ -330,17 +330,17 @@  discard block
 block discarded – undo
330 330
 	 * @param array $payment_meta associative array of meta data required for automatic payments
331 331
 	 * @return array
332 332
 	 */
333
-	public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) {
334
-		if ( $this->id === $payment_method_id ) {
333
+	public function validate_subscription_payment_meta($payment_method_id, $payment_meta) {
334
+		if ($this->id === $payment_method_id) {
335 335
 
336
-			if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) {
337
-				throw new Exception( 'A "_stripe_customer_id" value is required.' );
338
-			} elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) {
339
-				throw new Exception( 'Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".' );
336
+			if ( ! isset($payment_meta['post_meta']['_stripe_customer_id']['value']) || empty($payment_meta['post_meta']['_stripe_customer_id']['value'])) {
337
+				throw new Exception('A "_stripe_customer_id" value is required.');
338
+			} elseif (0 !== strpos($payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_')) {
339
+				throw new Exception('Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".');
340 340
 			}
341 341
 
342
-			if ( ! empty( $payment_meta['post_meta']['_stripe_card_id']['value'] ) && 0 !== strpos( $payment_meta['post_meta']['_stripe_card_id']['value'], 'card_' ) ) {
343
-				throw new Exception( 'Invalid card ID. A valid "_stripe_card_id" must begin with "card_".' );
342
+			if ( ! empty($payment_meta['post_meta']['_stripe_card_id']['value']) && 0 !== strpos($payment_meta['post_meta']['_stripe_card_id']['value'], 'card_')) {
343
+				throw new Exception('Invalid card ID. A valid "_stripe_card_id" must begin with "card_".');
344 344
 			}
345 345
 		}
346 346
 	}
@@ -353,43 +353,43 @@  discard block
 block discarded – undo
353 353
 	 * @param WC_Subscription $subscription the subscription details
354 354
 	 * @return string the subscription payment method
355 355
 	 */
356
-	public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) {
356
+	public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) {
357 357
 		// bail for other payment methods
358
-		if ( $this->id !== $subscription->payment_method || ! $subscription->customer_user ) {
358
+		if ($this->id !== $subscription->payment_method || ! $subscription->customer_user) {
359 359
 			return $payment_method_to_display;
360 360
 		}
361 361
 
362 362
 		$stripe_customer    = new WC_Stripe_Customer();
363
-		$stripe_customer_id = get_post_meta( $subscription->id, '_stripe_customer_id', true );
364
-		$stripe_card_id     = get_post_meta( $subscription->id, '_stripe_card_id', true );
363
+		$stripe_customer_id = get_post_meta($subscription->id, '_stripe_customer_id', true);
364
+		$stripe_card_id     = get_post_meta($subscription->id, '_stripe_card_id', true);
365 365
 
366 366
 		// If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data.
367
-		if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) {
367
+		if ( ! $stripe_customer_id || ! is_string($stripe_customer_id)) {
368 368
 			$user_id            = $subscription->customer_user;
369
-			$stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true );
370
-			$stripe_card_id     = get_user_meta( $user_id, '_stripe_card_id', true );
369
+			$stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true);
370
+			$stripe_card_id     = get_user_meta($user_id, '_stripe_card_id', true);
371 371
 		}
372 372
 
373 373
 		// If we couldn't find a Stripe customer linked to the account, fallback to the order meta data.
374
-		if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) {
375
-			$stripe_customer_id = get_post_meta( $subscription->order->id, '_stripe_customer_id', true );
376
-			$stripe_card_id     = get_post_meta( $subscription->order->id, '_stripe_card_id', true );
374
+		if (( ! $stripe_customer_id || ! is_string($stripe_customer_id)) && false !== $subscription->order) {
375
+			$stripe_customer_id = get_post_meta($subscription->order->id, '_stripe_customer_id', true);
376
+			$stripe_card_id     = get_post_meta($subscription->order->id, '_stripe_card_id', true);
377 377
 		}
378 378
 
379
-		$stripe_customer->set_id( $stripe_customer_id );
379
+		$stripe_customer->set_id($stripe_customer_id);
380 380
 		$cards = $stripe_customer->get_cards();
381 381
 
382
-		if ( $cards ) {
382
+		if ($cards) {
383 383
 			$found_card = false;
384
-			foreach ( $cards as $card ) {
385
-				if ( $card->id === $stripe_card_id ) {
384
+			foreach ($cards as $card) {
385
+				if ($card->id === $stripe_card_id) {
386 386
 					$found_card                = true;
387
-					$payment_method_to_display = sprintf( __( 'Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe' ), ( isset( $card->type ) ? $card->type : $card->brand ), $card->last4 );
387
+					$payment_method_to_display = sprintf(__('Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe'), (isset($card->type) ? $card->type : $card->brand), $card->last4);
388 388
 					break;
389 389
 				}
390 390
 			}
391
-			if ( ! $found_card ) {
392
-				$payment_method_to_display = sprintf( __( 'Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe' ), ( isset( $cards[0]->type ) ? $cards[0]->type : $cards[0]->brand ), $cards[0]->last4 );
391
+			if ( ! $found_card) {
392
+				$payment_method_to_display = sprintf(__('Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe'), (isset($cards[0]->type) ? $cards[0]->type : $cards[0]->brand), $cards[0]->last4);
393 393
 			}
394 394
 		}
395 395
 
@@ -404,11 +404,11 @@  discard block
 block discarded – undo
404 404
 	 *
405 405
 	 * @param string $message
406 406
 	 */
407
-	public function log( $message ) {
408
-		$options = get_option( 'woocommerce_stripe_settings' );
407
+	public function log($message) {
408
+		$options = get_option('woocommerce_stripe_settings');
409 409
 
410
-		if ( 'yes' === $options['logging'] ) {
411
-			WC_Stripe::log( $message );
410
+		if ('yes' === $options['logging']) {
411
+			WC_Stripe::log($message);
412 412
 		}
413 413
 	}
414 414
 }
Please login to merge, or discard this patch.