@@ -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,15 +51,15 @@ discard block |
||
| 51 | 51 | * Set Stripe customer ID. |
| 52 | 52 | * @param [type] $id [description] |
| 53 | 53 | */ |
| 54 | - public function set_id( $id ) { |
|
| 54 | + public function set_id($id) { |
|
| 55 | 55 | // Backwards compat for customer ID stored in array format. (Pre 3.0) |
| 56 | - if ( is_array( $id ) && isset( $id['customer_id'] ) ) { |
|
| 56 | + if (is_array($id) && isset($id['customer_id'])) { |
|
| 57 | 57 | $id = $id['customer_id']; |
| 58 | 58 | |
| 59 | - update_user_meta( $this->get_user_id(), '_stripe_customer_id', $id ); |
|
| 59 | + update_user_meta($this->get_user_id(), '_stripe_customer_id', $id); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - $this->id = wc_clean( $id ); |
|
| 62 | + $this->id = wc_clean($id); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -67,15 +67,15 @@ discard block |
||
| 67 | 67 | * @return int |
| 68 | 68 | */ |
| 69 | 69 | public function get_user_id() { |
| 70 | - return absint( $this->user_id ); |
|
| 70 | + return absint($this->user_id); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | 74 | * Set User ID used by WordPress. |
| 75 | 75 | * @param int $user_id |
| 76 | 76 | */ |
| 77 | - public function set_user_id( $user_id ) { |
|
| 78 | - $this->user_id = absint( $user_id ); |
|
| 77 | + public function set_user_id($user_id) { |
|
| 78 | + $this->user_id = absint($user_id); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
@@ -83,13 +83,13 @@ discard block |
||
| 83 | 83 | * @return WP_User |
| 84 | 84 | */ |
| 85 | 85 | protected function get_user() { |
| 86 | - return $this->get_user_id() ? get_user_by( 'id', $this->get_user_id() ) : false; |
|
| 86 | + return $this->get_user_id() ? get_user_by('id', $this->get_user_id()) : false; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
| 90 | 90 | * Store data from the Stripe API about this customer |
| 91 | 91 | */ |
| 92 | - public function set_customer_data( $data ) { |
|
| 92 | + public function set_customer_data($data) { |
|
| 93 | 93 | $this->customer_data = $data; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -98,13 +98,13 @@ discard block |
||
| 98 | 98 | * @param array $args |
| 99 | 99 | * @return WP_Error|int |
| 100 | 100 | */ |
| 101 | - public function create_customer( $args = array() ) { |
|
| 102 | - $billing_email = isset( $_POST['billing_email'] ) ? filter_var( $_POST['billing_email'], FILTER_SANITIZE_EMAIL ) : ''; |
|
| 101 | + public function create_customer($args = array()) { |
|
| 102 | + $billing_email = isset($_POST['billing_email']) ? filter_var($_POST['billing_email'], FILTER_SANITIZE_EMAIL) : ''; |
|
| 103 | 103 | $user = $this->get_user(); |
| 104 | 104 | |
| 105 | - if ( $user ) { |
|
| 106 | - $billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true ); |
|
| 107 | - $billing_last_name = get_user_meta( $user->ID, 'billing_last_name', true ); |
|
| 105 | + if ($user) { |
|
| 106 | + $billing_first_name = get_user_meta($user->ID, 'billing_first_name', true); |
|
| 107 | + $billing_last_name = get_user_meta($user->ID, 'billing_last_name', true); |
|
| 108 | 108 | |
| 109 | 109 | $defaults = array( |
| 110 | 110 | 'email' => $user->user_email, |
@@ -119,24 +119,24 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | $metadata = array(); |
| 121 | 121 | |
| 122 | - $defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user ); |
|
| 122 | + $defaults['metadata'] = apply_filters('wc_stripe_customer_metadata', $metadata, $user); |
|
| 123 | 123 | |
| 124 | - $args = wp_parse_args( $args, $defaults ); |
|
| 125 | - $response = WC_Stripe_API::request( apply_filters( 'wc_stripe_create_customer_args', $args ), 'customers' ); |
|
| 124 | + $args = wp_parse_args($args, $defaults); |
|
| 125 | + $response = WC_Stripe_API::request(apply_filters('wc_stripe_create_customer_args', $args), 'customers'); |
|
| 126 | 126 | |
| 127 | - if ( ! empty( $response->error ) ) { |
|
| 128 | - throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
|
| 127 | + if ( ! empty($response->error)) { |
|
| 128 | + throw new WC_Stripe_Exception(print_r($response, true), $response->error->message); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - $this->set_id( $response->id ); |
|
| 131 | + $this->set_id($response->id); |
|
| 132 | 132 | $this->clear_cache(); |
| 133 | - $this->set_customer_data( $response ); |
|
| 133 | + $this->set_customer_data($response); |
|
| 134 | 134 | |
| 135 | - if ( $this->get_user_id() ) { |
|
| 136 | - update_user_meta( $this->get_user_id(), '_stripe_customer_id', $response->id ); |
|
| 135 | + if ($this->get_user_id()) { |
|
| 136 | + update_user_meta($this->get_user_id(), '_stripe_customer_id', $response->id); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - do_action( 'woocommerce_stripe_add_customer', $args, $response ); |
|
| 139 | + do_action('woocommerce_stripe_add_customer', $args, $response); |
|
| 140 | 140 | |
| 141 | 141 | return $response->id; |
| 142 | 142 | } |
@@ -147,74 +147,74 @@ discard block |
||
| 147 | 147 | * @param bool $retry |
| 148 | 148 | * @return WP_Error|int |
| 149 | 149 | */ |
| 150 | - public function add_source( $source_id, $retry = true ) { |
|
| 151 | - if ( ! $this->get_id() ) { |
|
| 152 | - $this->set_id( $this->create_customer() ); |
|
| 150 | + public function add_source($source_id, $retry = true) { |
|
| 151 | + if ( ! $this->get_id()) { |
|
| 152 | + $this->set_id($this->create_customer()); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - $response = WC_Stripe_API::request( array( |
|
| 155 | + $response = WC_Stripe_API::request(array( |
|
| 156 | 156 | 'source' => $source_id, |
| 157 | - ), 'customers/' . $this->get_id() . '/sources' ); |
|
| 157 | + ), 'customers/' . $this->get_id() . '/sources'); |
|
| 158 | 158 | |
| 159 | 159 | $wc_token = false; |
| 160 | 160 | |
| 161 | - if ( ! empty( $response->error ) ) { |
|
| 161 | + if ( ! empty($response->error)) { |
|
| 162 | 162 | // It is possible the WC user once was linked to a customer on Stripe |
| 163 | 163 | // but no longer exists. Instead of failing, lets try to create a |
| 164 | 164 | // new customer. |
| 165 | - if ( $this->is_no_such_customer_error( $response->error ) ) { |
|
| 166 | - delete_user_meta( $this->get_user_id(), '_stripe_customer_id' ); |
|
| 165 | + if ($this->is_no_such_customer_error($response->error)) { |
|
| 166 | + delete_user_meta($this->get_user_id(), '_stripe_customer_id'); |
|
| 167 | 167 | $this->create_customer(); |
| 168 | - return $this->add_source( $source_id, false ); |
|
| 168 | + return $this->add_source($source_id, false); |
|
| 169 | 169 | } else { |
| 170 | 170 | return $response; |
| 171 | 171 | } |
| 172 | - } elseif ( empty( $response->id ) ) { |
|
| 173 | - return new WP_Error( 'error', __( 'Unable to add payment source.', 'woocommerce-gateway-stripe' ) ); |
|
| 172 | + } elseif (empty($response->id)) { |
|
| 173 | + return new WP_Error('error', __('Unable to add payment source.', 'woocommerce-gateway-stripe')); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // Add token to WooCommerce. |
| 177 | - if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) { |
|
| 178 | - if ( ! empty( $response->type ) ) { |
|
| 179 | - switch ( $response->type ) { |
|
| 177 | + if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) { |
|
| 178 | + if ( ! empty($response->type)) { |
|
| 179 | + switch ($response->type) { |
|
| 180 | 180 | case 'alipay': |
| 181 | 181 | break; |
| 182 | 182 | case 'sepa_debit': |
| 183 | 183 | $wc_token = new WC_Payment_Token_SEPA(); |
| 184 | - $wc_token->set_token( $response->id ); |
|
| 185 | - $wc_token->set_gateway_id( 'stripe_sepa' ); |
|
| 186 | - $wc_token->set_last4( $response->sepa_debit->last4 ); |
|
| 184 | + $wc_token->set_token($response->id); |
|
| 185 | + $wc_token->set_gateway_id('stripe_sepa'); |
|
| 186 | + $wc_token->set_last4($response->sepa_debit->last4); |
|
| 187 | 187 | break; |
| 188 | 188 | default: |
| 189 | - if ( 'source' === $response->object && 'card' === $response->type ) { |
|
| 189 | + if ('source' === $response->object && 'card' === $response->type) { |
|
| 190 | 190 | $wc_token = new WC_Payment_Token_CC(); |
| 191 | - $wc_token->set_token( $response->id ); |
|
| 192 | - $wc_token->set_gateway_id( 'stripe' ); |
|
| 193 | - $wc_token->set_card_type( strtolower( $response->card->brand ) ); |
|
| 194 | - $wc_token->set_last4( $response->card->last4 ); |
|
| 195 | - $wc_token->set_expiry_month( $response->card->exp_month ); |
|
| 196 | - $wc_token->set_expiry_year( $response->card->exp_year ); |
|
| 191 | + $wc_token->set_token($response->id); |
|
| 192 | + $wc_token->set_gateway_id('stripe'); |
|
| 193 | + $wc_token->set_card_type(strtolower($response->card->brand)); |
|
| 194 | + $wc_token->set_last4($response->card->last4); |
|
| 195 | + $wc_token->set_expiry_month($response->card->exp_month); |
|
| 196 | + $wc_token->set_expiry_year($response->card->exp_year); |
|
| 197 | 197 | } |
| 198 | 198 | break; |
| 199 | 199 | } |
| 200 | 200 | } else { |
| 201 | 201 | // Legacy. |
| 202 | 202 | $wc_token = new WC_Payment_Token_CC(); |
| 203 | - $wc_token->set_token( $response->id ); |
|
| 204 | - $wc_token->set_gateway_id( 'stripe' ); |
|
| 205 | - $wc_token->set_card_type( strtolower( $response->brand ) ); |
|
| 206 | - $wc_token->set_last4( $response->last4 ); |
|
| 207 | - $wc_token->set_expiry_month( $response->exp_month ); |
|
| 208 | - $wc_token->set_expiry_year( $response->exp_year ); |
|
| 203 | + $wc_token->set_token($response->id); |
|
| 204 | + $wc_token->set_gateway_id('stripe'); |
|
| 205 | + $wc_token->set_card_type(strtolower($response->brand)); |
|
| 206 | + $wc_token->set_last4($response->last4); |
|
| 207 | + $wc_token->set_expiry_month($response->exp_month); |
|
| 208 | + $wc_token->set_expiry_year($response->exp_year); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - $wc_token->set_user_id( $this->get_user_id() ); |
|
| 211 | + $wc_token->set_user_id($this->get_user_id()); |
|
| 212 | 212 | $wc_token->save(); |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | $this->clear_cache(); |
| 216 | 216 | |
| 217 | - do_action( 'woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id ); |
|
| 217 | + do_action('woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id); |
|
| 218 | 218 | |
| 219 | 219 | return $response->id; |
| 220 | 220 | } |
@@ -226,42 +226,42 @@ discard block |
||
| 226 | 226 | * @return array |
| 227 | 227 | */ |
| 228 | 228 | public function get_sources() { |
| 229 | - if ( ! $this->get_id() ) { |
|
| 229 | + if ( ! $this->get_id()) { |
|
| 230 | 230 | return array(); |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | - $sources = get_transient( 'stripe_sources_' . $this->get_id() ); |
|
| 233 | + $sources = get_transient('stripe_sources_' . $this->get_id()); |
|
| 234 | 234 | |
| 235 | - $response = WC_Stripe_API::request( array( |
|
| 235 | + $response = WC_Stripe_API::request(array( |
|
| 236 | 236 | 'limit' => 100, |
| 237 | - ), 'customers/' . $this->get_id() . '/sources', 'GET' ); |
|
| 237 | + ), 'customers/' . $this->get_id() . '/sources', 'GET'); |
|
| 238 | 238 | |
| 239 | - if ( ! empty( $response->error ) ) { |
|
| 239 | + if ( ! empty($response->error)) { |
|
| 240 | 240 | return array(); |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | - if ( is_array( $response->data ) ) { |
|
| 243 | + if (is_array($response->data)) { |
|
| 244 | 244 | $sources = $response->data; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - return empty( $sources ) ? array() : $sources; |
|
| 247 | + return empty($sources) ? array() : $sources; |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
| 251 | 251 | * Delete a source from stripe. |
| 252 | 252 | * @param string $source_id |
| 253 | 253 | */ |
| 254 | - public function delete_source( $source_id ) { |
|
| 255 | - if ( ! $this->get_id() ) { |
|
| 254 | + public function delete_source($source_id) { |
|
| 255 | + if ( ! $this->get_id()) { |
|
| 256 | 256 | return false; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $source_id ), 'DELETE' ); |
|
| 259 | + $response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field($source_id), 'DELETE'); |
|
| 260 | 260 | |
| 261 | 261 | $this->clear_cache(); |
| 262 | 262 | |
| 263 | - if ( empty( $response->error ) ) { |
|
| 264 | - do_action( 'wc_stripe_delete_source', $this->get_id(), $response ); |
|
| 263 | + if (empty($response->error)) { |
|
| 264 | + do_action('wc_stripe_delete_source', $this->get_id(), $response); |
|
| 265 | 265 | |
| 266 | 266 | return true; |
| 267 | 267 | } |
@@ -273,15 +273,15 @@ discard block |
||
| 273 | 273 | * Set default source in Stripe |
| 274 | 274 | * @param string $source_id |
| 275 | 275 | */ |
| 276 | - public function set_default_source( $source_id ) { |
|
| 277 | - $response = WC_Stripe_API::request( array( |
|
| 278 | - 'default_source' => sanitize_text_field( $source_id ), |
|
| 279 | - ), 'customers/' . $this->get_id(), 'POST' ); |
|
| 276 | + public function set_default_source($source_id) { |
|
| 277 | + $response = WC_Stripe_API::request(array( |
|
| 278 | + 'default_source' => sanitize_text_field($source_id), |
|
| 279 | + ), 'customers/' . $this->get_id(), 'POST'); |
|
| 280 | 280 | |
| 281 | 281 | $this->clear_cache(); |
| 282 | 282 | |
| 283 | - if ( empty( $response->error ) ) { |
|
| 284 | - do_action( 'wc_stripe_set_default_source', $this->get_id(), $response ); |
|
| 283 | + if (empty($response->error)) { |
|
| 284 | + do_action('wc_stripe_set_default_source', $this->get_id(), $response); |
|
| 285 | 285 | |
| 286 | 286 | return true; |
| 287 | 287 | } |
@@ -293,8 +293,8 @@ discard block |
||
| 293 | 293 | * Deletes caches for this users cards. |
| 294 | 294 | */ |
| 295 | 295 | public function clear_cache() { |
| 296 | - delete_transient( 'stripe_sources_' . $this->get_id() ); |
|
| 297 | - delete_transient( 'stripe_customer_' . $this->get_id() ); |
|
| 296 | + delete_transient('stripe_sources_' . $this->get_id()); |
|
| 297 | + delete_transient('stripe_customer_' . $this->get_id()); |
|
| 298 | 298 | $this->customer_data = array(); |
| 299 | 299 | } |
| 300 | 300 | } |