@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if ( ! defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -32,10 +32,10 @@ discard block |
||
| 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,8 +51,8 @@ discard block |
||
| 51 | 51 | * Set Stripe customer ID. |
| 52 | 52 | * @param [type] $id [description] |
| 53 | 53 | */ |
| 54 | - public function set_id( $id ) { |
|
| 55 | - $this->id = wc_clean( $id ); |
|
| 54 | + public function set_id($id) { |
|
| 55 | + $this->id = wc_clean($id); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
@@ -60,15 +60,15 @@ discard block |
||
| 60 | 60 | * @return int |
| 61 | 61 | */ |
| 62 | 62 | public function get_user_id() { |
| 63 | - return absint( $this->user_id ); |
|
| 63 | + return absint($this->user_id); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * Set User ID used by WordPress. |
| 68 | 68 | * @param int $user_id |
| 69 | 69 | */ |
| 70 | - public function set_user_id( $user_id ) { |
|
| 71 | - $this->user_id = absint( $user_id ); |
|
| 70 | + public function set_user_id($user_id) { |
|
| 71 | + $this->user_id = absint($user_id); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -76,13 +76,13 @@ discard block |
||
| 76 | 76 | * @return WP_User |
| 77 | 77 | */ |
| 78 | 78 | protected function get_user() { |
| 79 | - return $this->get_user_id() ? get_user_by( 'id', $this->get_user_id() ) : false; |
|
| 79 | + return $this->get_user_id() ? get_user_by('id', $this->get_user_id()) : false; |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * Store data from the Stripe API about this customer |
| 84 | 84 | */ |
| 85 | - public function set_customer_data( $data ) { |
|
| 85 | + public function set_customer_data($data) { |
|
| 86 | 86 | $this->customer_data = $data; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -90,14 +90,14 @@ discard block |
||
| 90 | 90 | * Get data from the Stripe API about this customer |
| 91 | 91 | */ |
| 92 | 92 | public function get_customer_data() { |
| 93 | - $this->customer_data = get_transient( 'stripe_customer_' . $this->get_id() ); |
|
| 93 | + $this->customer_data = get_transient('stripe_customer_' . $this->get_id()); |
|
| 94 | 94 | |
| 95 | - if ( empty( $this->customer_data ) && $this->get_id() && false === $this->customer_data ) { |
|
| 96 | - $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() ); |
|
| 95 | + if (empty($this->customer_data) && $this->get_id() && false === $this->customer_data) { |
|
| 96 | + $response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id()); |
|
| 97 | 97 | |
| 98 | - if ( empty( $response->error ) ) { |
|
| 99 | - $this->set_customer_data( $response ); |
|
| 100 | - set_transient( 'stripe_customer_' . $this->get_id(), $response, HOUR_IN_SECONDS * 48 ); |
|
| 98 | + if (empty($response->error)) { |
|
| 99 | + $this->set_customer_data($response); |
|
| 100 | + set_transient('stripe_customer_' . $this->get_id(), $response, HOUR_IN_SECONDS * 48); |
|
| 101 | 101 | } |
| 102 | 102 | } |
| 103 | 103 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | $data = $this->get_customer_data(); |
| 113 | 113 | $source = ''; |
| 114 | 114 | |
| 115 | - if ( $data ) { |
|
| 115 | + if ($data) { |
|
| 116 | 116 | $source = $data->default_source; |
| 117 | 117 | } |
| 118 | 118 | |
@@ -124,13 +124,13 @@ discard block |
||
| 124 | 124 | * @param array $args |
| 125 | 125 | * @return WP_Error|int |
| 126 | 126 | */ |
| 127 | - public function create_customer( $args = array() ) { |
|
| 128 | - $billing_email = isset( $_POST['billing_email'] ) ? filter_var( $_POST['billing_email'], FILTER_SANITIZE_EMAIL ) : ''; |
|
| 127 | + public function create_customer($args = array()) { |
|
| 128 | + $billing_email = isset($_POST['billing_email']) ? filter_var($_POST['billing_email'], FILTER_SANITIZE_EMAIL) : ''; |
|
| 129 | 129 | $user = $this->get_user(); |
| 130 | 130 | |
| 131 | - if ( $user ) { |
|
| 132 | - $billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true ); |
|
| 133 | - $billing_last_name = get_user_meta( $user->ID, 'billing_last_name', true ); |
|
| 131 | + if ($user) { |
|
| 132 | + $billing_first_name = get_user_meta($user->ID, 'billing_first_name', true); |
|
| 133 | + $billing_last_name = get_user_meta($user->ID, 'billing_last_name', true); |
|
| 134 | 134 | |
| 135 | 135 | $defaults = array( |
| 136 | 136 | 'email' => $user->user_email, |
@@ -145,24 +145,24 @@ discard block |
||
| 145 | 145 | |
| 146 | 146 | $metadata = array(); |
| 147 | 147 | |
| 148 | - $defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user ); |
|
| 148 | + $defaults['metadata'] = apply_filters('wc_stripe_customer_metadata', $metadata, $user); |
|
| 149 | 149 | |
| 150 | - $args = wp_parse_args( $args, $defaults ); |
|
| 151 | - $response = WC_Stripe_API::request( apply_filters( 'wc_stripe_create_customer_args', $args ), 'customers' ); |
|
| 150 | + $args = wp_parse_args($args, $defaults); |
|
| 151 | + $response = WC_Stripe_API::request(apply_filters('wc_stripe_create_customer_args', $args), 'customers'); |
|
| 152 | 152 | |
| 153 | - if ( ! empty( $response->error ) ) { |
|
| 154 | - throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
|
| 153 | + if ( ! empty($response->error)) { |
|
| 154 | + throw new WC_Stripe_Exception(print_r($response, true), $response->error->message); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - $this->set_id( $response->id ); |
|
| 157 | + $this->set_id($response->id); |
|
| 158 | 158 | $this->clear_cache(); |
| 159 | - $this->set_customer_data( $response ); |
|
| 159 | + $this->set_customer_data($response); |
|
| 160 | 160 | |
| 161 | - if ( $this->get_user_id() ) { |
|
| 162 | - update_user_meta( $this->get_user_id(), '_stripe_customer_id', $response->id ); |
|
| 161 | + if ($this->get_user_id()) { |
|
| 162 | + update_user_meta($this->get_user_id(), '_stripe_customer_id', $response->id); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - do_action( 'woocommerce_stripe_add_customer', $args, $response ); |
|
| 165 | + do_action('woocommerce_stripe_add_customer', $args, $response); |
|
| 166 | 166 | |
| 167 | 167 | return $response->id; |
| 168 | 168 | } |
@@ -173,72 +173,72 @@ discard block |
||
| 173 | 173 | * @param bool $retry |
| 174 | 174 | * @return WP_Error|int |
| 175 | 175 | */ |
| 176 | - public function add_source( $source_id, $retry = true ) { |
|
| 177 | - if ( ! $this->get_id() ) { |
|
| 178 | - $this->set_id( $this->create_customer() ); |
|
| 176 | + public function add_source($source_id, $retry = true) { |
|
| 177 | + if ( ! $this->get_id()) { |
|
| 178 | + $this->set_id($this->create_customer()); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - $response = WC_Stripe_API::request( array( |
|
| 181 | + $response = WC_Stripe_API::request(array( |
|
| 182 | 182 | 'source' => $source_id, |
| 183 | - ), 'customers/' . $this->get_id() . '/sources' ); |
|
| 183 | + ), 'customers/' . $this->get_id() . '/sources'); |
|
| 184 | 184 | |
| 185 | - if ( ! empty( $response->error ) ) { |
|
| 185 | + if ( ! empty($response->error)) { |
|
| 186 | 186 | // It is possible the WC user once was linked to a customer on Stripe |
| 187 | 187 | // but no longer exists. Instead of failing, lets try to create a |
| 188 | 188 | // new customer. |
| 189 | - if ( preg_match( '/No such customer/i', $response->error->message ) ) { |
|
| 190 | - delete_user_meta( $this->get_user_id(), '_stripe_customer_id' ); |
|
| 189 | + if (preg_match('/No such customer/i', $response->error->message)) { |
|
| 190 | + delete_user_meta($this->get_user_id(), '_stripe_customer_id'); |
|
| 191 | 191 | $this->create_customer(); |
| 192 | - return $this->add_source( $source_id, false ); |
|
| 192 | + return $this->add_source($source_id, false); |
|
| 193 | 193 | } else { |
| 194 | 194 | return $response; |
| 195 | 195 | } |
| 196 | - } elseif ( empty( $response->id ) ) { |
|
| 197 | - return new WP_Error( 'error', __( 'Unable to add payment source.', 'woocommerce-gateway-stripe' ) ); |
|
| 196 | + } elseif (empty($response->id)) { |
|
| 197 | + return new WP_Error('error', __('Unable to add payment source.', 'woocommerce-gateway-stripe')); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | // Add token to WooCommerce. |
| 201 | - if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) { |
|
| 202 | - if ( ! empty( $response->type ) ) { |
|
| 203 | - switch ( $response->type ) { |
|
| 201 | + if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) { |
|
| 202 | + if ( ! empty($response->type)) { |
|
| 203 | + switch ($response->type) { |
|
| 204 | 204 | case 'alipay': |
| 205 | 205 | break; |
| 206 | 206 | case 'sepa_debit': |
| 207 | 207 | $wc_token = new WC_Payment_Token_SEPA(); |
| 208 | - $wc_token->set_token( $response->id ); |
|
| 209 | - $wc_token->set_gateway_id( 'stripe_sepa' ); |
|
| 210 | - $wc_token->set_last4( $response->sepa_debit->last4 ); |
|
| 208 | + $wc_token->set_token($response->id); |
|
| 209 | + $wc_token->set_gateway_id('stripe_sepa'); |
|
| 210 | + $wc_token->set_last4($response->sepa_debit->last4); |
|
| 211 | 211 | break; |
| 212 | 212 | default: |
| 213 | - if ( 'source' === $response->object && 'card' === $response->type ) { |
|
| 213 | + if ('source' === $response->object && 'card' === $response->type) { |
|
| 214 | 214 | $wc_token = new WC_Payment_Token_CC(); |
| 215 | - $wc_token->set_token( $response->id ); |
|
| 216 | - $wc_token->set_gateway_id( 'stripe' ); |
|
| 217 | - $wc_token->set_card_type( strtolower( $response->card->brand ) ); |
|
| 218 | - $wc_token->set_last4( $response->card->last4 ); |
|
| 219 | - $wc_token->set_expiry_month( $response->card->exp_month ); |
|
| 220 | - $wc_token->set_expiry_year( $response->card->exp_year ); |
|
| 215 | + $wc_token->set_token($response->id); |
|
| 216 | + $wc_token->set_gateway_id('stripe'); |
|
| 217 | + $wc_token->set_card_type(strtolower($response->card->brand)); |
|
| 218 | + $wc_token->set_last4($response->card->last4); |
|
| 219 | + $wc_token->set_expiry_month($response->card->exp_month); |
|
| 220 | + $wc_token->set_expiry_year($response->card->exp_year); |
|
| 221 | 221 | } |
| 222 | 222 | break; |
| 223 | 223 | } |
| 224 | 224 | } else { |
| 225 | 225 | // Legacy. |
| 226 | 226 | $wc_token = new WC_Payment_Token_CC(); |
| 227 | - $wc_token->set_token( $response->id ); |
|
| 228 | - $wc_token->set_gateway_id( 'stripe' ); |
|
| 229 | - $wc_token->set_card_type( strtolower( $response->brand ) ); |
|
| 230 | - $wc_token->set_last4( $response->last4 ); |
|
| 231 | - $wc_token->set_expiry_month( $response->exp_month ); |
|
| 232 | - $wc_token->set_expiry_year( $response->exp_year ); |
|
| 227 | + $wc_token->set_token($response->id); |
|
| 228 | + $wc_token->set_gateway_id('stripe'); |
|
| 229 | + $wc_token->set_card_type(strtolower($response->brand)); |
|
| 230 | + $wc_token->set_last4($response->last4); |
|
| 231 | + $wc_token->set_expiry_month($response->exp_month); |
|
| 232 | + $wc_token->set_expiry_year($response->exp_year); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | - $wc_token->set_user_id( $this->get_user_id() ); |
|
| 235 | + $wc_token->set_user_id($this->get_user_id()); |
|
| 236 | 236 | $wc_token->save(); |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | $this->clear_cache(); |
| 240 | 240 | |
| 241 | - do_action( 'woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id ); |
|
| 241 | + do_action('woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id); |
|
| 242 | 242 | |
| 243 | 243 | return $response->id; |
| 244 | 244 | } |
@@ -250,42 +250,42 @@ discard block |
||
| 250 | 250 | * @return array |
| 251 | 251 | */ |
| 252 | 252 | public function get_sources() { |
| 253 | - if ( ! $this->get_id() ) { |
|
| 253 | + if ( ! $this->get_id()) { |
|
| 254 | 254 | return array(); |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - $sources = get_transient( 'stripe_sources_' . $this->get_id() ); |
|
| 257 | + $sources = get_transient('stripe_sources_' . $this->get_id()); |
|
| 258 | 258 | |
| 259 | - $response = WC_Stripe_API::request( array( |
|
| 259 | + $response = WC_Stripe_API::request(array( |
|
| 260 | 260 | 'limit' => 100, |
| 261 | - ), 'customers/' . $this->get_id() . '/sources', 'GET' ); |
|
| 261 | + ), 'customers/' . $this->get_id() . '/sources', 'GET'); |
|
| 262 | 262 | |
| 263 | - if ( ! empty( $response->error ) ) { |
|
| 263 | + if ( ! empty($response->error)) { |
|
| 264 | 264 | return array(); |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - if ( is_array( $response->data ) ) { |
|
| 267 | + if (is_array($response->data)) { |
|
| 268 | 268 | $sources = $response->data; |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - return empty( $sources ) ? array() : $sources; |
|
| 271 | + return empty($sources) ? array() : $sources; |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
| 275 | 275 | * Delete a source from stripe. |
| 276 | 276 | * @param string $source_id |
| 277 | 277 | */ |
| 278 | - public function delete_source( $source_id ) { |
|
| 279 | - if ( ! $this->get_id() ) { |
|
| 278 | + public function delete_source($source_id) { |
|
| 279 | + if ( ! $this->get_id()) { |
|
| 280 | 280 | return false; |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $source_id ), 'DELETE' ); |
|
| 283 | + $response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field($source_id), 'DELETE'); |
|
| 284 | 284 | |
| 285 | 285 | $this->clear_cache(); |
| 286 | 286 | |
| 287 | - if ( empty( $response->error ) ) { |
|
| 288 | - do_action( 'wc_stripe_delete_source', $this->get_id(), $response ); |
|
| 287 | + if (empty($response->error)) { |
|
| 288 | + do_action('wc_stripe_delete_source', $this->get_id(), $response); |
|
| 289 | 289 | |
| 290 | 290 | return true; |
| 291 | 291 | } |
@@ -297,15 +297,15 @@ discard block |
||
| 297 | 297 | * Set default source in Stripe |
| 298 | 298 | * @param string $source_id |
| 299 | 299 | */ |
| 300 | - public function set_default_source( $source_id ) { |
|
| 301 | - $response = WC_Stripe_API::request( array( |
|
| 302 | - 'default_source' => sanitize_text_field( $source_id ), |
|
| 303 | - ), 'customers/' . $this->get_id(), 'POST' ); |
|
| 300 | + public function set_default_source($source_id) { |
|
| 301 | + $response = WC_Stripe_API::request(array( |
|
| 302 | + 'default_source' => sanitize_text_field($source_id), |
|
| 303 | + ), 'customers/' . $this->get_id(), 'POST'); |
|
| 304 | 304 | |
| 305 | 305 | $this->clear_cache(); |
| 306 | 306 | |
| 307 | - if ( empty( $response->error ) ) { |
|
| 308 | - do_action( 'wc_stripe_set_default_source', $this->get_id(), $response ); |
|
| 307 | + if (empty($response->error)) { |
|
| 308 | + do_action('wc_stripe_set_default_source', $this->get_id(), $response); |
|
| 309 | 309 | |
| 310 | 310 | return true; |
| 311 | 311 | } |
@@ -317,8 +317,8 @@ discard block |
||
| 317 | 317 | * Deletes caches for this users cards. |
| 318 | 318 | */ |
| 319 | 319 | public function clear_cache() { |
| 320 | - delete_transient( 'stripe_sources_' . $this->get_id() ); |
|
| 321 | - delete_transient( 'stripe_customer_' . $this->get_id() ); |
|
| 320 | + delete_transient('stripe_sources_' . $this->get_id()); |
|
| 321 | + delete_transient('stripe_customer_' . $this->get_id()); |
|
| 322 | 322 | $this->customer_data = array(); |
| 323 | 323 | } |
| 324 | 324 | } |