Completed
Pull Request — master (#127)
by Caleb
02:04
created
woocommerce-gateway-stripe.php 1 patch
Spacing   +125 added lines, -125 removed lines patch added patch discarded remove patch
@@ -25,20 +25,20 @@  discard block
 block discarded – undo
25 25
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 26
 */
27 27
 
28
-if ( ! defined( 'ABSPATH' ) ) {
28
+if ( ! defined('ABSPATH')) {
29 29
 	exit;
30 30
 }
31 31
 
32 32
 /**
33 33
  * Required minimums and constants
34 34
  */
35
-define( 'WC_STRIPE_VERSION', '3.0.6' );
36
-define( 'WC_STRIPE_MIN_PHP_VER', '5.3.0' );
37
-define( 'WC_STRIPE_MIN_WC_VER', '2.5.0' );
38
-define( 'WC_STRIPE_MAIN_FILE', __FILE__ );
39
-define( 'WC_STRIPE_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
35
+define('WC_STRIPE_VERSION', '3.0.6');
36
+define('WC_STRIPE_MIN_PHP_VER', '5.3.0');
37
+define('WC_STRIPE_MIN_WC_VER', '2.5.0');
38
+define('WC_STRIPE_MAIN_FILE', __FILE__);
39
+define('WC_STRIPE_PLUGIN_URL', untrailingslashit(plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__))));
40 40
 
41
-if ( ! class_exists( 'WC_Stripe' ) ) :
41
+if ( ! class_exists('WC_Stripe')) :
42 42
 
43 43
 class WC_Stripe {
44 44
 
@@ -58,7 +58,7 @@  discard block
 block discarded – undo
58 58
 	 * @return Singleton The *Singleton* instance.
59 59
 	 */
60 60
 	public static function get_instance() {
61
-		if ( null === self::$instance ) {
61
+		if (null === self::$instance) {
62 62
 			self::$instance = new self();
63 63
 		}
64 64
 		return self::$instance;
@@ -107,9 +107,9 @@  discard block
 block discarded – undo
107 107
 	 * *Singleton* via the `new` operator from outside of this class.
108 108
 	 */
109 109
 	protected function __construct() {
110
-		add_action( 'admin_init', array( $this, 'check_environment' ) );
111
-		add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
112
-		add_action( 'plugins_loaded', array( $this, 'init' ) );
110
+		add_action('admin_init', array($this, 'check_environment'));
111
+		add_action('admin_notices', array($this, 'admin_notices'), 15);
112
+		add_action('plugins_loaded', array($this, 'init'));
113 113
 	}
114 114
 
115 115
 	/**
@@ -117,33 +117,33 @@  discard block
 block discarded – undo
117 117
 	 */
118 118
 	public function init() {
119 119
 		// Don't hook anything else in the plugin if we're in an incompatible environment
120
-		if ( self::get_environment_warning() ) {
120
+		if (self::get_environment_warning()) {
121 121
 			return;
122 122
 		}
123 123
 
124
-		include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-api.php' );
125
-		include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-customer.php' );
124
+		include_once(dirname(__FILE__) . '/includes/class-wc-stripe-api.php');
125
+		include_once(dirname(__FILE__) . '/includes/class-wc-stripe-customer.php');
126 126
 
127 127
 		// Init the gateway itself
128 128
 		$this->init_gateways();
129 129
 
130
-		add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
131
-		add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'capture_payment' ) );
132
-		add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'capture_payment' ) );
133
-		add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'cancel_payment' ) );
134
-		add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'cancel_payment' ) );
135
-		add_filter( 'woocommerce_get_customer_payment_tokens', array( $this, 'woocommerce_get_customer_payment_tokens' ), 10, 3 );
136
-		add_action( 'woocommerce_payment_token_deleted', array( $this, 'woocommerce_payment_token_deleted' ), 10, 2 );
137
-		add_action( 'woocommerce_payment_token_set_default', array( $this, 'woocommerce_payment_token_set_default' ) );
130
+		add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links'));
131
+		add_action('woocommerce_order_status_on-hold_to_processing', array($this, 'capture_payment'));
132
+		add_action('woocommerce_order_status_on-hold_to_completed', array($this, 'capture_payment'));
133
+		add_action('woocommerce_order_status_on-hold_to_cancelled', array($this, 'cancel_payment'));
134
+		add_action('woocommerce_order_status_on-hold_to_refunded', array($this, 'cancel_payment'));
135
+		add_filter('woocommerce_get_customer_payment_tokens', array($this, 'woocommerce_get_customer_payment_tokens'), 10, 3);
136
+		add_action('woocommerce_payment_token_deleted', array($this, 'woocommerce_payment_token_deleted'), 10, 2);
137
+		add_action('woocommerce_payment_token_set_default', array($this, 'woocommerce_payment_token_set_default'));
138 138
 
139
-		include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-payment-request.php' );
139
+		include_once(dirname(__FILE__) . '/includes/class-wc-stripe-payment-request.php');
140 140
 	}
141 141
 
142 142
 	/**
143 143
 	 * Allow this class and other classes to add slug keyed notices (to avoid duplication)
144 144
 	 */
145
-	public function add_admin_notice( $slug, $class, $message ) {
146
-		$this->notices[ $slug ] = array(
145
+	public function add_admin_notice($slug, $class, $message) {
146
+		$this->notices[$slug] = array(
147 147
 			'class'   => $class,
148 148
 			'message' => $message
149 149
 		);
@@ -156,21 +156,21 @@  discard block
 block discarded – undo
156 156
 	public function check_environment() {
157 157
 		$environment_warning = self::get_environment_warning();
158 158
 
159
-		if ( $environment_warning && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
160
-			$this->add_admin_notice( 'bad_environment', 'error', $environment_warning );
159
+		if ($environment_warning && is_plugin_active(plugin_basename(__FILE__))) {
160
+			$this->add_admin_notice('bad_environment', 'error', $environment_warning);
161 161
 		}
162 162
 
163 163
 		// Check if secret key present. Otherwise prompt, via notice, to go to
164 164
 		// setting.
165
-		if ( ! class_exists( 'WC_Stripe_API' ) ) {
166
-			include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-api.php' );
165
+		if ( ! class_exists('WC_Stripe_API')) {
166
+			include_once(dirname(__FILE__) . '/includes/class-wc-stripe-api.php');
167 167
 		}
168 168
 
169 169
 		$secret = WC_Stripe_API::get_secret_key();
170 170
 
171
-		if ( empty( $secret ) && ! ( isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'stripe' === $_GET['section'] ) ) {
171
+		if (empty($secret) && ! (isset($_GET['page'], $_GET['section']) && 'wc-settings' === $_GET['page'] && 'stripe' === $_GET['section'])) {
172 172
 			$setting_link = $this->get_setting_link();
173
-			$this->add_admin_notice( 'prompt_connect', 'notice notice-warning', sprintf( __( 'Stripe is almost ready. To get started, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe' ), $setting_link ) );
173
+			$this->add_admin_notice('prompt_connect', 'notice notice-warning', sprintf(__('Stripe is almost ready. To get started, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe'), $setting_link));
174 174
 		}
175 175
 	}
176 176
 
@@ -179,24 +179,24 @@  discard block
 block discarded – undo
179 179
 	 * found or false if the environment has no problems.
180 180
 	 */
181 181
 	static function get_environment_warning() {
182
-		if ( version_compare( phpversion(), WC_STRIPE_MIN_PHP_VER, '<' ) ) {
183
-			$message = __( 'WooCommerce Stripe - The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe', 'woocommerce-gateway-stripe' );
182
+		if (version_compare(phpversion(), WC_STRIPE_MIN_PHP_VER, '<')) {
183
+			$message = __('WooCommerce Stripe - The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe', 'woocommerce-gateway-stripe');
184 184
 
185
-			return sprintf( $message, WC_STRIPE_MIN_PHP_VER, phpversion() );
185
+			return sprintf($message, WC_STRIPE_MIN_PHP_VER, phpversion());
186 186
 		}
187 187
 
188
-		if ( ! defined( 'WC_VERSION' ) ) {
189
-			return __( 'WooCommerce Stripe requires WooCommerce to be activated to work.', 'woocommerce-gateway-stripe' );
188
+		if ( ! defined('WC_VERSION')) {
189
+			return __('WooCommerce Stripe requires WooCommerce to be activated to work.', 'woocommerce-gateway-stripe');
190 190
 		}
191 191
 
192
-		if ( version_compare( WC_VERSION, WC_STRIPE_MIN_WC_VER, '<' ) ) {
193
-			$message = __( 'WooCommerce Stripe - The minimum WooCommerce version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe', 'woocommerce-gateway-stripe' );
192
+		if (version_compare(WC_VERSION, WC_STRIPE_MIN_WC_VER, '<')) {
193
+			$message = __('WooCommerce Stripe - The minimum WooCommerce version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe', 'woocommerce-gateway-stripe');
194 194
 
195
-			return sprintf( $message, WC_STRIPE_MIN_WC_VER, WC_VERSION );
195
+			return sprintf($message, WC_STRIPE_MIN_WC_VER, WC_VERSION);
196 196
 		}
197 197
 
198
-		if ( ! function_exists( 'curl_init' ) ) {
199
-			return __( 'WooCommerce Stripe - cURL is not installed.', 'woocommerce-gateway-stripe' );
198
+		if ( ! function_exists('curl_init')) {
199
+			return __('WooCommerce Stripe - cURL is not installed.', 'woocommerce-gateway-stripe');
200 200
 		}
201 201
 
202 202
 		return false;
@@ -207,15 +207,15 @@  discard block
 block discarded – undo
207 207
 	 *
208 208
 	 * @since 1.0.0
209 209
 	 */
210
-	public function plugin_action_links( $links ) {
210
+	public function plugin_action_links($links) {
211 211
 		$setting_link = $this->get_setting_link();
212 212
 
213 213
 		$plugin_links = array(
214
-			'<a href="' . $setting_link . '">' . __( 'Settings', 'woocommerce-gateway-stripe' ) . '</a>',
215
-			'<a href="https://docs.woothemes.com/document/stripe/">' . __( 'Docs', 'woocommerce-gateway-stripe' ) . '</a>',
216
-			'<a href="http://support.woothemes.com/">' . __( 'Support', 'woocommerce-gateway-stripe' ) . '</a>',
214
+			'<a href="' . $setting_link . '">' . __('Settings', 'woocommerce-gateway-stripe') . '</a>',
215
+			'<a href="https://docs.woothemes.com/document/stripe/">' . __('Docs', 'woocommerce-gateway-stripe') . '</a>',
216
+			'<a href="http://support.woothemes.com/">' . __('Support', 'woocommerce-gateway-stripe') . '</a>',
217 217
 		);
218
-		return array_merge( $plugin_links, $links );
218
+		return array_merge($plugin_links, $links);
219 219
 	}
220 220
 
221 221
 	/**
@@ -226,20 +226,20 @@  discard block
 block discarded – undo
226 226
 	 * @return string Setting link
227 227
 	 */
228 228
 	public function get_setting_link() {
229
-		$use_id_as_section = class_exists( 'WC' ) ? version_compare( WC()->version, '2.6', '>=' ) : false;
229
+		$use_id_as_section = class_exists('WC') ? version_compare(WC()->version, '2.6', '>=') : false;
230 230
 
231
-		$section_slug = $use_id_as_section ? 'stripe' : strtolower( 'WC_Gateway_Stripe' );
231
+		$section_slug = $use_id_as_section ? 'stripe' : strtolower('WC_Gateway_Stripe');
232 232
 
233
-		return admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . $section_slug );
233
+		return admin_url('admin.php?page=wc-settings&tab=checkout&section=' . $section_slug);
234 234
 	}
235 235
 
236 236
 	/**
237 237
 	 * Display any notices we've collected thus far (e.g. for connection, disconnection)
238 238
 	 */
239 239
 	public function admin_notices() {
240
-		foreach ( (array) $this->notices as $notice_key => $notice ) {
241
-			echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>";
242
-			echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) );
240
+		foreach ((array) $this->notices as $notice_key => $notice) {
241
+			echo "<div class='" . esc_attr($notice['class']) . "'><p>";
242
+			echo wp_kses($notice['message'], array('a' => array('href' => array())));
243 243
 			echo "</p></div>";
244 244
 		}
245 245
 	}
@@ -250,28 +250,28 @@  discard block
 block discarded – undo
250 250
 	 * @since 1.0.0
251 251
 	 */
252 252
 	public function init_gateways() {
253
-		if ( class_exists( 'WC_Subscriptions_Order' ) && function_exists( 'wcs_create_renewal_order' ) ) {
253
+		if (class_exists('WC_Subscriptions_Order') && function_exists('wcs_create_renewal_order')) {
254 254
 			$this->subscription_support_enabled = true;
255 255
 		}
256 256
 
257
-		if ( class_exists( 'WC_Pre_Orders_Order' ) ) {
257
+		if (class_exists('WC_Pre_Orders_Order')) {
258 258
 			$this->pre_order_enabled = true;
259 259
 		}
260 260
 
261
-		if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
261
+		if ( ! class_exists('WC_Payment_Gateway')) {
262 262
 			return;
263 263
 		}
264 264
 
265
-		if ( class_exists( 'WC_Payment_Gateway_CC' ) ) {
266
-			include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe.php' );
267
-			require_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-apple-pay.php' );
265
+		if (class_exists('WC_Payment_Gateway_CC')) {
266
+			include_once(dirname(__FILE__) . '/includes/class-wc-gateway-stripe.php');
267
+			require_once(dirname(__FILE__) . '/includes/class-wc-stripe-apple-pay.php');
268 268
 		} else {
269
-			include_once( dirname( __FILE__ ) . '/includes/legacy/class-wc-gateway-stripe.php' );
270
-			include_once( dirname( __FILE__ ) . '/includes/legacy/class-wc-gateway-stripe-saved-cards.php' );
269
+			include_once(dirname(__FILE__) . '/includes/legacy/class-wc-gateway-stripe.php');
270
+			include_once(dirname(__FILE__) . '/includes/legacy/class-wc-gateway-stripe-saved-cards.php');
271 271
 		}
272 272
 
273
-		load_plugin_textdomain( 'woocommerce-gateway-stripe', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
274
-		add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateways' ) );
273
+		load_plugin_textdomain('woocommerce-gateway-stripe', false, plugin_basename(dirname(__FILE__)) . '/languages');
274
+		add_filter('woocommerce_payment_gateways', array($this, 'add_gateways'));
275 275
 
276 276
 		$load_addons = (
277 277
 			$this->subscription_support_enabled
@@ -279,8 +279,8 @@  discard block
 block discarded – undo
279 279
 			$this->pre_order_enabled
280 280
 		);
281 281
 
282
-		if ( $load_addons ) {
283
-			require_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe-addons.php' );
282
+		if ($load_addons) {
283
+			require_once(dirname(__FILE__) . '/includes/class-wc-gateway-stripe-addons.php');
284 284
 		}
285 285
 	}
286 286
 
@@ -289,8 +289,8 @@  discard block
 block discarded – undo
289 289
 	 *
290 290
 	 * @since 1.0.0
291 291
 	 */
292
-	public function add_gateways( $methods ) {
293
-		if ( $this->subscription_support_enabled || $this->pre_order_enabled ) {
292
+	public function add_gateways($methods) {
293
+		if ($this->subscription_support_enabled || $this->pre_order_enabled) {
294 294
 			$methods[] = 'WC_Gateway_Stripe_Addons';
295 295
 		} else {
296 296
 			$methods[] = 'WC_Gateway_Stripe';
@@ -303,35 +303,35 @@  discard block
 block discarded – undo
303 303
 	 *
304 304
 	 * @param  int $order_id
305 305
 	 */
306
-	public function capture_payment( $order_id ) {
307
-		$order = wc_get_order( $order_id );
306
+	public function capture_payment($order_id) {
307
+		$order = wc_get_order($order_id);
308 308
 
309
-		if ( 'stripe' === $order->payment_method ) {
310
-			$charge   = get_post_meta( $order_id, '_stripe_charge_id', true );
311
-			$captured = get_post_meta( $order_id, '_stripe_charge_captured', true );
309
+		if ('stripe' === $order->payment_method) {
310
+			$charge   = get_post_meta($order_id, '_stripe_charge_id', true);
311
+			$captured = get_post_meta($order_id, '_stripe_charge_captured', true);
312 312
 
313
-			if ( $charge && 'no' === $captured ) {
314
-				$result = WC_Stripe_API::request( array(
313
+			if ($charge && 'no' === $captured) {
314
+				$result = WC_Stripe_API::request(array(
315 315
 					'amount'   => $order->get_total() * 100,
316 316
 					'expand[]' => 'balance_transaction'
317
-				), 'charges/' . $charge . '/capture' );
317
+				), 'charges/' . $charge . '/capture');
318 318
 
319
-				if ( is_wp_error( $result ) ) {
320
-					$order->add_order_note( __( 'Unable to capture charge!', 'woocommerce-gateway-stripe' ) . ' ' . $result->get_error_message() );
319
+				if (is_wp_error($result)) {
320
+					$order->add_order_note(__('Unable to capture charge!', 'woocommerce-gateway-stripe') . ' ' . $result->get_error_message());
321 321
 				} else {
322
-					$order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) );
323
-					update_post_meta( $order->id, '_stripe_charge_captured', 'yes' );
322
+					$order->add_order_note(sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id));
323
+					update_post_meta($order->id, '_stripe_charge_captured', 'yes');
324 324
 
325 325
 					// Store other data such as fees
326
-					update_post_meta( $order->id, 'Stripe Payment ID', $result->id );
326
+					update_post_meta($order->id, 'Stripe Payment ID', $result->id);
327 327
 
328
-					if ( isset( $result->balance_transaction ) && isset( $result->balance_transaction->fee ) ) {
328
+					if (isset($result->balance_transaction) && isset($result->balance_transaction->fee)) {
329 329
 						// Fees and Net needs to both come from Stripe to be accurate as the returned
330 330
 						// values are in the local currency of the Stripe account, not from WC.
331
-						$fee = ! empty( $result->balance_transaction->fee ) ? number_format( $result->balance_transaction->fee / 100, 2, '.', '' ) : 0;
332
-						$net = ! empty( $result->balance_transaction->net ) ? number_format( $result->balance_transaction->net / 100, 2, '.', '' ) : 0;
333
-						update_post_meta( $order->id, 'Stripe Fee', $fee );
334
-						update_post_meta( $order->id, 'Net Revenue From Stripe', $net );
331
+						$fee = ! empty($result->balance_transaction->fee) ? number_format($result->balance_transaction->fee / 100, 2, '.', '') : 0;
332
+						$net = ! empty($result->balance_transaction->net) ? number_format($result->balance_transaction->net / 100, 2, '.', '') : 0;
333
+						update_post_meta($order->id, 'Stripe Fee', $fee);
334
+						update_post_meta($order->id, 'Net Revenue From Stripe', $net);
335 335
 					}
336 336
 				}
337 337
 			}
@@ -343,23 +343,23 @@  discard block
 block discarded – undo
343 343
 	 *
344 344
 	 * @param  int $order_id
345 345
 	 */
346
-	public function cancel_payment( $order_id ) {
347
-		$order = wc_get_order( $order_id );
346
+	public function cancel_payment($order_id) {
347
+		$order = wc_get_order($order_id);
348 348
 
349
-		if ( 'stripe' === $order->payment_method ) {
350
-			$charge   = get_post_meta( $order_id, '_stripe_charge_id', true );
349
+		if ('stripe' === $order->payment_method) {
350
+			$charge = get_post_meta($order_id, '_stripe_charge_id', true);
351 351
 
352
-			if ( $charge ) {
353
-				$result = WC_Stripe_API::request( array(
352
+			if ($charge) {
353
+				$result = WC_Stripe_API::request(array(
354 354
 					'amount' => $order->get_total() * 100,
355
-				), 'charges/' . $charge . '/refund' );
355
+				), 'charges/' . $charge . '/refund');
356 356
 
357
-				if ( is_wp_error( $result ) ) {
358
-					$order->add_order_note( __( 'Unable to refund charge!', 'woocommerce-gateway-stripe' ) . ' ' . $result->get_error_message() );
357
+				if (is_wp_error($result)) {
358
+					$order->add_order_note(__('Unable to refund charge!', 'woocommerce-gateway-stripe') . ' ' . $result->get_error_message());
359 359
 				} else {
360
-					$order->add_order_note( sprintf( __( 'Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) );
361
-					delete_post_meta( $order->id, '_stripe_charge_captured' );
362
-					delete_post_meta( $order->id, '_stripe_charge_id' );
360
+					$order->add_order_note(sprintf(__('Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id));
361
+					delete_post_meta($order->id, '_stripe_charge_captured');
362
+					delete_post_meta($order->id, '_stripe_charge_id');
363 363
 				}
364 364
 			}
365 365
 		}
@@ -370,28 +370,28 @@  discard block
 block discarded – undo
370 370
 	 * @param array $tokens
371 371
 	 * @return array
372 372
 	 */
373
-	public function woocommerce_get_customer_payment_tokens( $tokens, $customer_id, $gateway_id ) {
374
-		if ( is_user_logged_in() && 'stripe' === $gateway_id && class_exists( 'WC_Payment_Token_CC' ) ) {
375
-			$stripe_customer = new WC_Stripe_Customer( $customer_id );
373
+	public function woocommerce_get_customer_payment_tokens($tokens, $customer_id, $gateway_id) {
374
+		if (is_user_logged_in() && 'stripe' === $gateway_id && class_exists('WC_Payment_Token_CC')) {
375
+			$stripe_customer = new WC_Stripe_Customer($customer_id);
376 376
 			$stripe_cards    = $stripe_customer->get_cards();
377 377
 			$stored_tokens   = array();
378 378
 
379
-			foreach ( $tokens as $token ) {
379
+			foreach ($tokens as $token) {
380 380
 				$stored_tokens[] = $token->get_token();
381 381
 			}
382 382
 
383
-			foreach ( $stripe_cards as $card ) {
384
-				if ( ! in_array( $card->id, $stored_tokens ) ) {
383
+			foreach ($stripe_cards as $card) {
384
+				if ( ! in_array($card->id, $stored_tokens)) {
385 385
 					$token = new WC_Payment_Token_CC();
386
-					$token->set_token( $card->id );
387
-					$token->set_gateway_id( 'stripe' );
388
-					$token->set_card_type( strtolower( $card->brand ) );
389
-					$token->set_last4( $card->last4 );
390
-					$token->set_expiry_month( $card->exp_month  );
391
-					$token->set_expiry_year( $card->exp_year );
392
-					$token->set_user_id( $customer_id );
386
+					$token->set_token($card->id);
387
+					$token->set_gateway_id('stripe');
388
+					$token->set_card_type(strtolower($card->brand));
389
+					$token->set_last4($card->last4);
390
+					$token->set_expiry_month($card->exp_month);
391
+					$token->set_expiry_year($card->exp_year);
392
+					$token->set_user_id($customer_id);
393 393
 					$token->save();
394
-					$tokens[ $token->get_id() ] = $token;
394
+					$tokens[$token->get_id()] = $token;
395 395
 				}
396 396
 			}
397 397
 		}
@@ -401,21 +401,21 @@  discard block
 block discarded – undo
401 401
 	/**
402 402
 	 * Delete token from Stripe
403 403
 	 */
404
-	public function woocommerce_payment_token_deleted( $token_id, $token ) {
405
-		if ( 'stripe' === $token->get_gateway_id() ) {
406
-			$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
407
-			$stripe_customer->delete_card( $token->get_token() );
404
+	public function woocommerce_payment_token_deleted($token_id, $token) {
405
+		if ('stripe' === $token->get_gateway_id()) {
406
+			$stripe_customer = new WC_Stripe_Customer(get_current_user_id());
407
+			$stripe_customer->delete_card($token->get_token());
408 408
 		}
409 409
 	}
410 410
 
411 411
 	/**
412 412
 	 * Set as default in Stripe
413 413
 	 */
414
-	public function woocommerce_payment_token_set_default( $token_id ) {
415
-		$token = WC_Payment_Tokens::get( $token_id );
416
-		if ( 'stripe' === $token->get_gateway_id() ) {
417
-			$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
418
-			$stripe_customer->set_default_card( $token->get_token() );
414
+	public function woocommerce_payment_token_set_default($token_id) {
415
+		$token = WC_Payment_Tokens::get($token_id);
416
+		if ('stripe' === $token->get_gateway_id()) {
417
+			$stripe_customer = new WC_Stripe_Customer(get_current_user_id());
418
+			$stripe_customer->set_default_card($token->get_token());
419 419
 		}
420 420
 	}
421 421
 	
@@ -425,7 +425,7 @@  discard block
 block discarded – undo
425 425
 	public static function get_minimum_amount() {
426 426
 	
427 427
 		// Check order amount
428
-		switch ( get_woocommerce_currency() ) {
428
+		switch (get_woocommerce_currency()) {
429 429
 			case 'USD':
430 430
 			case 'CAD':
431 431
 			case 'EUR':
@@ -470,15 +470,15 @@  discard block
 block discarded – undo
470 470
 	 * And fits on your back?
471 471
 	 * It's log, log, log
472 472
 	 */
473
-	public static function log( $message ) {
474
-		if ( empty( self::$log ) ) {
473
+	public static function log($message) {
474
+		if (empty(self::$log)) {
475 475
 			self::$log = new WC_Logger();
476 476
 		}
477 477
 
478
-		self::$log->add( 'woocommerce-gateway-stripe', $message );
478
+		self::$log->add('woocommerce-gateway-stripe', $message);
479 479
 
480
-		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
481
-			error_log( $message );
480
+		if (defined('WP_DEBUG') && WP_DEBUG) {
481
+			error_log($message);
482 482
 		}
483 483
 	}
484 484
 }
Please login to merge, or discard this patch.