Completed
Push — master ( c7ef51...e8218a )
by Akeda
8s
created
includes/class-wc-gateway-stripe-addons.php 1 patch
Spacing   +129 added lines, -129 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,39 +103,39 @@  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 133
 		);
134
-		$response            = WC_Stripe_API::request( $request );
134
+		$response            = WC_Stripe_API::request($request);
135 135
 
136 136
 		// Process valid response
137
-		if ( ! is_wp_error( $response ) ) {
138
-			$this->process_response( $response, $order );
137
+		if ( ! is_wp_error($response)) {
138
+			$this->process_response($response, $order);
139 139
 		}
140 140
 
141 141
 		return $response;
@@ -146,42 +146,42 @@  discard block
 block discarded – undo
146 146
 	 * @param int $order_id
147 147
 	 * @return array
148 148
 	 */
149
-	public function process_pre_order( $order_id, $retry, $force_customer ) {
150
-		if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) {
149
+	public function process_pre_order($order_id, $retry, $force_customer) {
150
+		if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
151 151
 			try {
152
-				$order = wc_get_order( $order_id );
152
+				$order = wc_get_order($order_id);
153 153
 
154
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
155
-					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 ) ) );
154
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
155
+					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)));
156 156
 				}
157 157
 
158
-				$source = $this->get_source( get_current_user_id(), true );
158
+				$source = $this->get_source(get_current_user_id(), true);
159 159
 
160 160
 				// We need a source on file to continue.
161
-				if ( empty( $source->customer ) || empty( $source->source ) ) {
162
-					throw new Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) );
161
+				if (empty($source->customer) || empty($source->source)) {
162
+					throw new Exception(__('Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe'));
163 163
 				}
164 164
 
165 165
 				// Store source to order meta
166
-				$this->save_source( $order, $source );
166
+				$this->save_source($order, $source);
167 167
 
168 168
 				// Remove cart
169 169
 				WC()->cart->empty_cart();
170 170
 
171 171
 				// Is pre ordered!
172
-				WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order );
172
+				WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
173 173
 
174 174
 				// Return thank you page redirect
175 175
 				return array(
176 176
 					'result'   => 'success',
177
-					'redirect' => $this->get_return_url( $order ),
177
+					'redirect' => $this->get_return_url($order),
178 178
 				);
179
-			} catch ( Exception $e ) {
180
-				wc_add_notice( $e->getMessage(), 'error' );
179
+			} catch (Exception $e) {
180
+				wc_add_notice($e->getMessage(), 'error');
181 181
 				return;
182 182
 			}
183 183
 		} else {
184
-			return parent::process_payment( $order_id, $retry, $force_customer );
184
+			return parent::process_payment($order_id, $retry, $force_customer);
185 185
 		}
186 186
 	}
187 187
 
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
 	 * @param WC_Order $order
191 191
 	 * @return void
192 192
 	 */
193
-	public function process_pre_order_release_payment( $order ) {
193
+	public function process_pre_order_release_payment($order) {
194 194
 		try {
195 195
 			// Define some callbacks if the first attempt fails.
196 196
 			$retry_callbacks = array(
@@ -198,32 +198,32 @@  discard block
 block discarded – undo
198 198
 				'remove_order_customer_before_retry',
199 199
 			);
200 200
 
201
-			while ( 1 ) {
202
-				$source   = $this->get_order_source( $order );
203
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) );
201
+			while (1) {
202
+				$source   = $this->get_order_source($order);
203
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $source));
204 204
 
205
-				if ( is_wp_error( $response ) ) {
206
-					if ( 0 === sizeof( $retry_callbacks ) ) {
207
-						throw new Exception( $response->get_error_message() );
205
+				if (is_wp_error($response)) {
206
+					if (0 === sizeof($retry_callbacks)) {
207
+						throw new Exception($response->get_error_message());
208 208
 					} else {
209
-						$retry_callback = array_shift( $retry_callbacks );
210
-						call_user_func( array( $this, $retry_callback ), $order );
209
+						$retry_callback = array_shift($retry_callbacks);
210
+						call_user_func(array($this, $retry_callback), $order);
211 211
 					}
212 212
 				} else {
213 213
 					// Successful
214
-					$this->process_response( $response, $order );
214
+					$this->process_response($response, $order);
215 215
 					break;
216 216
 				}
217 217
 			}
218
-		} catch ( Exception $e ) {
219
-			$order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $e->getMessage() );
218
+		} catch (Exception $e) {
219
+			$order_note = sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $e->getMessage());
220 220
 
221 221
 			// Mark order as failed if not already set,
222 222
 			// otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times
223
-			if ( ! $order->has_status( 'failed' ) ) {
224
-				$order->update_status( 'failed', $order_note );
223
+			if ( ! $order->has_status('failed')) {
224
+				$order->update_status('failed', $order_note);
225 225
 			} else {
226
-				$order->add_order_note( $order_note );
226
+				$order->add_order_note($order_note);
227 227
 			}
228 228
 		}
229 229
 	}
@@ -232,20 +232,20 @@  discard block
 block discarded – undo
232 232
 	 * Don't transfer Stripe customer/token meta to resubscribe orders.
233 233
 	 * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription
234 234
 	 */
235
-	public function delete_resubscribe_meta( $resubscribe_order ) {
236
-		delete_post_meta( $resubscribe_order->id, '_stripe_customer_id' );
237
-		delete_post_meta( $resubscribe_order->id, '_stripe_card_id' );
238
-		$this->delete_renewal_meta( $resubscribe_order );
235
+	public function delete_resubscribe_meta($resubscribe_order) {
236
+		delete_post_meta($resubscribe_order->id, '_stripe_customer_id');
237
+		delete_post_meta($resubscribe_order->id, '_stripe_card_id');
238
+		$this->delete_renewal_meta($resubscribe_order);
239 239
 	}
240 240
 
241 241
 	/**
242 242
 	 * Don't transfer Stripe fee/ID meta to renewal orders.
243 243
 	 * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription
244 244
 	 */
245
-	public function delete_renewal_meta( $renewal_order ) {
246
-		delete_post_meta( $renewal_order->id, 'Stripe Fee' );
247
-		delete_post_meta( $renewal_order->id, 'Net Revenue From Stripe' );
248
-		delete_post_meta( $renewal_order->id, 'Stripe Payment ID' );
245
+	public function delete_renewal_meta($renewal_order) {
246
+		delete_post_meta($renewal_order->id, 'Stripe Fee');
247
+		delete_post_meta($renewal_order->id, 'Net Revenue From Stripe');
248
+		delete_post_meta($renewal_order->id, 'Stripe Payment ID');
249 249
 		return $renewal_order;
250 250
 	}
251 251
 
@@ -255,11 +255,11 @@  discard block
 block discarded – undo
255 255
 	 * @param $amount_to_charge float The amount to charge.
256 256
 	 * @param $renewal_order WC_Order A WC_Order object created to record the renewal payment.
257 257
 	 */
258
-	public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) {
259
-		$response = $this->process_subscription_payment( $renewal_order, $amount_to_charge );
258
+	public function scheduled_subscription_payment($amount_to_charge, $renewal_order) {
259
+		$response = $this->process_subscription_payment($renewal_order, $amount_to_charge);
260 260
 
261
-		if ( is_wp_error( $response ) ) {
262
-			$renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) );
261
+		if (is_wp_error($response)) {
262
+			$renewal_order->update_status('failed', sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $response->get_error_message()));
263 263
 		}
264 264
 	}
265 265
 
@@ -267,18 +267,18 @@  discard block
 block discarded – undo
267 267
 	 * Remove order meta
268 268
 	 * @param  object $order
269 269
 	 */
270
-	public function remove_order_source_before_retry( $order ) {
271
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
272
-		delete_post_meta( $order_id, '_stripe_card_id' );
270
+	public function remove_order_source_before_retry($order) {
271
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
272
+		delete_post_meta($order_id, '_stripe_card_id');
273 273
 	}
274 274
 
275 275
 	/**
276 276
 	 * Remove order meta
277 277
 	 * @param  object $order
278 278
 	 */
279
-	public function remove_order_customer_before_retry( $order ) {
280
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
281
-		delete_post_meta( $order_id, '_stripe_customer_id' );
279
+	public function remove_order_customer_before_retry($order) {
280
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
281
+		delete_post_meta($order_id, '_stripe_customer_id');
282 282
 	}
283 283
 
284 284
 	/**
@@ -290,9 +290,9 @@  discard block
 block discarded – undo
290 290
 	 * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment).
291 291
 	 * @return void
292 292
 	 */
293
-	public function update_failing_payment_method( $subscription, $renewal_order ) {
294
-		update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id );
295
-		update_post_meta( $subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id );
293
+	public function update_failing_payment_method($subscription, $renewal_order) {
294
+		update_post_meta($subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id);
295
+		update_post_meta($subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id);
296 296
 	}
297 297
 
298 298
 	/**
@@ -304,15 +304,15 @@  discard block
 block discarded – undo
304 304
 	 * @param WC_Subscription $subscription An instance of a subscription object
305 305
 	 * @return array
306 306
 	 */
307
-	public function add_subscription_payment_meta( $payment_meta, $subscription ) {
308
-		$payment_meta[ $this->id ] = array(
307
+	public function add_subscription_payment_meta($payment_meta, $subscription) {
308
+		$payment_meta[$this->id] = array(
309 309
 			'post_meta' => array(
310 310
 				'_stripe_customer_id' => array(
311
-					'value' => get_post_meta( $subscription->id, '_stripe_customer_id', true ),
311
+					'value' => get_post_meta($subscription->id, '_stripe_customer_id', true),
312 312
 					'label' => 'Stripe Customer ID',
313 313
 				),
314 314
 				'_stripe_card_id' => array(
315
-					'value' => get_post_meta( $subscription->id, '_stripe_card_id', true ),
315
+					'value' => get_post_meta($subscription->id, '_stripe_card_id', true),
316 316
 					'label' => 'Stripe Card ID',
317 317
 				),
318 318
 			),
@@ -329,17 +329,17 @@  discard block
 block discarded – undo
329 329
 	 * @param array $payment_meta associative array of meta data required for automatic payments
330 330
 	 * @return array
331 331
 	 */
332
-	public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) {
333
-		if ( $this->id === $payment_method_id ) {
332
+	public function validate_subscription_payment_meta($payment_method_id, $payment_meta) {
333
+		if ($this->id === $payment_method_id) {
334 334
 
335
-			if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) {
336
-				throw new Exception( 'A "_stripe_customer_id" value is required.' );
337
-			} elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) {
338
-				throw new Exception( 'Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".' );
335
+			if ( ! isset($payment_meta['post_meta']['_stripe_customer_id']['value']) || empty($payment_meta['post_meta']['_stripe_customer_id']['value'])) {
336
+				throw new Exception('A "_stripe_customer_id" value is required.');
337
+			} elseif (0 !== strpos($payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_')) {
338
+				throw new Exception('Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".');
339 339
 			}
340 340
 
341
-			if ( ! empty( $payment_meta['post_meta']['_stripe_card_id']['value'] ) && 0 !== strpos( $payment_meta['post_meta']['_stripe_card_id']['value'], 'card_' ) ) {
342
-				throw new Exception( 'Invalid card ID. A valid "_stripe_card_id" must begin with "card_".' );
341
+			if ( ! empty($payment_meta['post_meta']['_stripe_card_id']['value']) && 0 !== strpos($payment_meta['post_meta']['_stripe_card_id']['value'], 'card_')) {
342
+				throw new Exception('Invalid card ID. A valid "_stripe_card_id" must begin with "card_".');
343 343
 			}
344 344
 		}
345 345
 	}
@@ -352,43 +352,43 @@  discard block
 block discarded – undo
352 352
 	 * @param WC_Subscription $subscription the subscription details
353 353
 	 * @return string the subscription payment method
354 354
 	 */
355
-	public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) {
355
+	public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) {
356 356
 		// bail for other payment methods
357
-		if ( $this->id !== $subscription->payment_method || ! $subscription->customer_user ) {
357
+		if ($this->id !== $subscription->payment_method || ! $subscription->customer_user) {
358 358
 			return $payment_method_to_display;
359 359
 		}
360 360
 
361 361
 		$stripe_customer    = new WC_Stripe_Customer();
362
-		$stripe_customer_id = get_post_meta( $subscription->id, '_stripe_customer_id', true );
363
-		$stripe_card_id     = get_post_meta( $subscription->id, '_stripe_card_id', true );
362
+		$stripe_customer_id = get_post_meta($subscription->id, '_stripe_customer_id', true);
363
+		$stripe_card_id     = get_post_meta($subscription->id, '_stripe_card_id', true);
364 364
 
365 365
 		// If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data.
366
-		if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) {
366
+		if ( ! $stripe_customer_id || ! is_string($stripe_customer_id)) {
367 367
 			$user_id            = $subscription->customer_user;
368
-			$stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true );
369
-			$stripe_card_id     = get_user_meta( $user_id, '_stripe_card_id', true );
368
+			$stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true);
369
+			$stripe_card_id     = get_user_meta($user_id, '_stripe_card_id', true);
370 370
 		}
371 371
 
372 372
 		// If we couldn't find a Stripe customer linked to the account, fallback to the order meta data.
373
-		if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) {
374
-			$stripe_customer_id = get_post_meta( $subscription->order->id, '_stripe_customer_id', true );
375
-			$stripe_card_id     = get_post_meta( $subscription->order->id, '_stripe_card_id', true );
373
+		if (( ! $stripe_customer_id || ! is_string($stripe_customer_id)) && false !== $subscription->order) {
374
+			$stripe_customer_id = get_post_meta($subscription->order->id, '_stripe_customer_id', true);
375
+			$stripe_card_id     = get_post_meta($subscription->order->id, '_stripe_card_id', true);
376 376
 		}
377 377
 
378
-		$stripe_customer->set_id( $stripe_customer_id );
378
+		$stripe_customer->set_id($stripe_customer_id);
379 379
 		$cards = $stripe_customer->get_cards();
380 380
 
381
-		if ( $cards ) {
381
+		if ($cards) {
382 382
 			$found_card = false;
383
-			foreach ( $cards as $card ) {
384
-				if ( $card->id === $stripe_card_id ) {
383
+			foreach ($cards as $card) {
384
+				if ($card->id === $stripe_card_id) {
385 385
 					$found_card                = true;
386
-					$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 );
386
+					$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 387
 					break;
388 388
 				}
389 389
 			}
390
-			if ( ! $found_card ) {
391
-				$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 );
390
+			if ( ! $found_card) {
391
+				$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);
392 392
 			}
393 393
 		}
394 394
 
@@ -403,11 +403,11 @@  discard block
 block discarded – undo
403 403
 	 *
404 404
 	 * @param string $message
405 405
 	 */
406
-	public function log( $message ) {
407
-		$options = get_option( 'woocommerce_stripe_settings' );
406
+	public function log($message) {
407
+		$options = get_option('woocommerce_stripe_settings');
408 408
 
409
-		if ( 'yes' === $options['logging'] ) {
410
-			WC_Stripe::log( $message );
409
+		if ('yes' === $options['logging']) {
410
+			WC_Stripe::log($message);
411 411
 		}
412 412
 	}
413 413
 }
Please login to merge, or discard this patch.