@@ -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,25 +98,25 @@ 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 | // If billing first name does not exists try the user first name. |
110 | - if ( empty( $billing_first_name ) ) { |
|
111 | - $billing_first_name = get_user_meta( $user->ID, 'first_name', true ); |
|
110 | + if (empty($billing_first_name)) { |
|
111 | + $billing_first_name = get_user_meta($user->ID, 'first_name', true); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | // If billing last name does not exists try the user last name. |
115 | - if ( empty( $billing_last_name ) ) { |
|
116 | - $billing_last_name = get_user_meta( $user->ID, 'last_name', true ); |
|
115 | + if (empty($billing_last_name)) { |
|
116 | + $billing_last_name = get_user_meta($user->ID, 'last_name', true); |
|
117 | 117 | } |
118 | 118 | |
119 | - $description = __( 'Name', 'woocommerce-gateway-stripe' ) . ': ' . $billing_first_name . ' ' . $billing_last_name . ' ' . __( 'Username', 'woocommerce-gateway-stripe' ) . ': ' . $user->user_login; |
|
119 | + $description = __('Name', 'woocommerce-gateway-stripe') . ': ' . $billing_first_name . ' ' . $billing_last_name . ' ' . __('Username', 'woocommerce-gateway-stripe') . ': ' . $user->user_login; |
|
120 | 120 | |
121 | 121 | $defaults = array( |
122 | 122 | 'email' => $user->user_email, |
@@ -131,24 +131,24 @@ discard block |
||
131 | 131 | |
132 | 132 | $metadata = array(); |
133 | 133 | |
134 | - $defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user ); |
|
134 | + $defaults['metadata'] = apply_filters('wc_stripe_customer_metadata', $metadata, $user); |
|
135 | 135 | |
136 | - $args = wp_parse_args( $args, $defaults ); |
|
137 | - $response = WC_Stripe_API::request( apply_filters( 'wc_stripe_create_customer_args', $args ), 'customers' ); |
|
136 | + $args = wp_parse_args($args, $defaults); |
|
137 | + $response = WC_Stripe_API::request(apply_filters('wc_stripe_create_customer_args', $args), 'customers'); |
|
138 | 138 | |
139 | - if ( ! empty( $response->error ) ) { |
|
140 | - throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
|
139 | + if ( ! empty($response->error)) { |
|
140 | + throw new WC_Stripe_Exception(print_r($response, true), $response->error->message); |
|
141 | 141 | } |
142 | 142 | |
143 | - $this->set_id( $response->id ); |
|
143 | + $this->set_id($response->id); |
|
144 | 144 | $this->clear_cache(); |
145 | - $this->set_customer_data( $response ); |
|
145 | + $this->set_customer_data($response); |
|
146 | 146 | |
147 | - if ( $this->get_user_id() ) { |
|
148 | - update_user_meta( $this->get_user_id(), '_stripe_customer_id', $response->id ); |
|
147 | + if ($this->get_user_id()) { |
|
148 | + update_user_meta($this->get_user_id(), '_stripe_customer_id', $response->id); |
|
149 | 149 | } |
150 | 150 | |
151 | - do_action( 'woocommerce_stripe_add_customer', $args, $response ); |
|
151 | + do_action('woocommerce_stripe_add_customer', $args, $response); |
|
152 | 152 | |
153 | 153 | return $response->id; |
154 | 154 | } |
@@ -160,11 +160,11 @@ discard block |
||
160 | 160 | * @since 4.1.2 |
161 | 161 | * @param array $error |
162 | 162 | */ |
163 | - public function is_no_such_customer_error( $error ) { |
|
163 | + public function is_no_such_customer_error($error) { |
|
164 | 164 | return ( |
165 | 165 | $error && |
166 | 166 | 'invalid_request_error' === $error->type && |
167 | - preg_match( '/No such customer/i', $error->message ) |
|
167 | + preg_match('/No such customer/i', $error->message) |
|
168 | 168 | ); |
169 | 169 | } |
170 | 170 | |
@@ -174,74 +174,74 @@ discard block |
||
174 | 174 | * @param bool $retry |
175 | 175 | * @return WP_Error|int |
176 | 176 | */ |
177 | - public function add_source( $source_id, $retry = true ) { |
|
178 | - if ( ! $this->get_id() ) { |
|
179 | - $this->set_id( $this->create_customer() ); |
|
177 | + public function add_source($source_id, $retry = true) { |
|
178 | + if ( ! $this->get_id()) { |
|
179 | + $this->set_id($this->create_customer()); |
|
180 | 180 | } |
181 | 181 | |
182 | - $response = WC_Stripe_API::request( array( |
|
182 | + $response = WC_Stripe_API::request(array( |
|
183 | 183 | 'source' => $source_id, |
184 | - ), 'customers/' . $this->get_id() . '/sources' ); |
|
184 | + ), 'customers/' . $this->get_id() . '/sources'); |
|
185 | 185 | |
186 | 186 | $wc_token = false; |
187 | 187 | |
188 | - if ( ! empty( $response->error ) ) { |
|
188 | + if ( ! empty($response->error)) { |
|
189 | 189 | // It is possible the WC user once was linked to a customer on Stripe |
190 | 190 | // but no longer exists. Instead of failing, lets try to create a |
191 | 191 | // new customer. |
192 | - if ( $this->is_no_such_customer_error( $response->error ) ) { |
|
193 | - delete_user_meta( $this->get_user_id(), '_stripe_customer_id' ); |
|
192 | + if ($this->is_no_such_customer_error($response->error)) { |
|
193 | + delete_user_meta($this->get_user_id(), '_stripe_customer_id'); |
|
194 | 194 | $this->create_customer(); |
195 | - return $this->add_source( $source_id, false ); |
|
195 | + return $this->add_source($source_id, false); |
|
196 | 196 | } else { |
197 | 197 | return $response; |
198 | 198 | } |
199 | - } elseif ( empty( $response->id ) ) { |
|
200 | - return new WP_Error( 'error', __( 'Unable to add payment source.', 'woocommerce-gateway-stripe' ) ); |
|
199 | + } elseif (empty($response->id)) { |
|
200 | + return new WP_Error('error', __('Unable to add payment source.', 'woocommerce-gateway-stripe')); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | // Add token to WooCommerce. |
204 | - if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) { |
|
205 | - if ( ! empty( $response->type ) ) { |
|
206 | - switch ( $response->type ) { |
|
204 | + if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) { |
|
205 | + if ( ! empty($response->type)) { |
|
206 | + switch ($response->type) { |
|
207 | 207 | case 'alipay': |
208 | 208 | break; |
209 | 209 | case 'sepa_debit': |
210 | 210 | $wc_token = new WC_Payment_Token_SEPA(); |
211 | - $wc_token->set_token( $response->id ); |
|
212 | - $wc_token->set_gateway_id( 'stripe_sepa' ); |
|
213 | - $wc_token->set_last4( $response->sepa_debit->last4 ); |
|
211 | + $wc_token->set_token($response->id); |
|
212 | + $wc_token->set_gateway_id('stripe_sepa'); |
|
213 | + $wc_token->set_last4($response->sepa_debit->last4); |
|
214 | 214 | break; |
215 | 215 | default: |
216 | - if ( 'source' === $response->object && 'card' === $response->type ) { |
|
216 | + if ('source' === $response->object && 'card' === $response->type) { |
|
217 | 217 | $wc_token = new WC_Payment_Token_CC(); |
218 | - $wc_token->set_token( $response->id ); |
|
219 | - $wc_token->set_gateway_id( 'stripe' ); |
|
220 | - $wc_token->set_card_type( strtolower( $response->card->brand ) ); |
|
221 | - $wc_token->set_last4( $response->card->last4 ); |
|
222 | - $wc_token->set_expiry_month( $response->card->exp_month ); |
|
223 | - $wc_token->set_expiry_year( $response->card->exp_year ); |
|
218 | + $wc_token->set_token($response->id); |
|
219 | + $wc_token->set_gateway_id('stripe'); |
|
220 | + $wc_token->set_card_type(strtolower($response->card->brand)); |
|
221 | + $wc_token->set_last4($response->card->last4); |
|
222 | + $wc_token->set_expiry_month($response->card->exp_month); |
|
223 | + $wc_token->set_expiry_year($response->card->exp_year); |
|
224 | 224 | } |
225 | 225 | break; |
226 | 226 | } |
227 | 227 | } else { |
228 | 228 | // Legacy. |
229 | 229 | $wc_token = new WC_Payment_Token_CC(); |
230 | - $wc_token->set_token( $response->id ); |
|
231 | - $wc_token->set_gateway_id( 'stripe' ); |
|
232 | - $wc_token->set_card_type( strtolower( $response->brand ) ); |
|
233 | - $wc_token->set_last4( $response->last4 ); |
|
234 | - $wc_token->set_expiry_month( $response->exp_month ); |
|
235 | - $wc_token->set_expiry_year( $response->exp_year ); |
|
230 | + $wc_token->set_token($response->id); |
|
231 | + $wc_token->set_gateway_id('stripe'); |
|
232 | + $wc_token->set_card_type(strtolower($response->brand)); |
|
233 | + $wc_token->set_last4($response->last4); |
|
234 | + $wc_token->set_expiry_month($response->exp_month); |
|
235 | + $wc_token->set_expiry_year($response->exp_year); |
|
236 | 236 | } |
237 | 237 | |
238 | - $wc_token->set_user_id( $this->get_user_id() ); |
|
238 | + $wc_token->set_user_id($this->get_user_id()); |
|
239 | 239 | $wc_token->save(); |
240 | 240 | } |
241 | 241 | |
242 | 242 | $this->clear_cache(); |
243 | 243 | |
244 | - do_action( 'woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id ); |
|
244 | + do_action('woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id); |
|
245 | 245 | |
246 | 246 | return $response->id; |
247 | 247 | } |
@@ -253,42 +253,42 @@ discard block |
||
253 | 253 | * @return array |
254 | 254 | */ |
255 | 255 | public function get_sources() { |
256 | - if ( ! $this->get_id() ) { |
|
256 | + if ( ! $this->get_id()) { |
|
257 | 257 | return array(); |
258 | 258 | } |
259 | 259 | |
260 | - $sources = get_transient( 'stripe_sources_' . $this->get_id() ); |
|
260 | + $sources = get_transient('stripe_sources_' . $this->get_id()); |
|
261 | 261 | |
262 | - $response = WC_Stripe_API::request( array( |
|
262 | + $response = WC_Stripe_API::request(array( |
|
263 | 263 | 'limit' => 100, |
264 | - ), 'customers/' . $this->get_id() . '/sources', 'GET' ); |
|
264 | + ), 'customers/' . $this->get_id() . '/sources', 'GET'); |
|
265 | 265 | |
266 | - if ( ! empty( $response->error ) ) { |
|
266 | + if ( ! empty($response->error)) { |
|
267 | 267 | return array(); |
268 | 268 | } |
269 | 269 | |
270 | - if ( is_array( $response->data ) ) { |
|
270 | + if (is_array($response->data)) { |
|
271 | 271 | $sources = $response->data; |
272 | 272 | } |
273 | 273 | |
274 | - return empty( $sources ) ? array() : $sources; |
|
274 | + return empty($sources) ? array() : $sources; |
|
275 | 275 | } |
276 | 276 | |
277 | 277 | /** |
278 | 278 | * Delete a source from stripe. |
279 | 279 | * @param string $source_id |
280 | 280 | */ |
281 | - public function delete_source( $source_id ) { |
|
282 | - if ( ! $this->get_id() ) { |
|
281 | + public function delete_source($source_id) { |
|
282 | + if ( ! $this->get_id()) { |
|
283 | 283 | return false; |
284 | 284 | } |
285 | 285 | |
286 | - $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $source_id ), 'DELETE' ); |
|
286 | + $response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field($source_id), 'DELETE'); |
|
287 | 287 | |
288 | 288 | $this->clear_cache(); |
289 | 289 | |
290 | - if ( empty( $response->error ) ) { |
|
291 | - do_action( 'wc_stripe_delete_source', $this->get_id(), $response ); |
|
290 | + if (empty($response->error)) { |
|
291 | + do_action('wc_stripe_delete_source', $this->get_id(), $response); |
|
292 | 292 | |
293 | 293 | return true; |
294 | 294 | } |
@@ -300,15 +300,15 @@ discard block |
||
300 | 300 | * Set default source in Stripe |
301 | 301 | * @param string $source_id |
302 | 302 | */ |
303 | - public function set_default_source( $source_id ) { |
|
304 | - $response = WC_Stripe_API::request( array( |
|
305 | - 'default_source' => sanitize_text_field( $source_id ), |
|
306 | - ), 'customers/' . $this->get_id(), 'POST' ); |
|
303 | + public function set_default_source($source_id) { |
|
304 | + $response = WC_Stripe_API::request(array( |
|
305 | + 'default_source' => sanitize_text_field($source_id), |
|
306 | + ), 'customers/' . $this->get_id(), 'POST'); |
|
307 | 307 | |
308 | 308 | $this->clear_cache(); |
309 | 309 | |
310 | - if ( empty( $response->error ) ) { |
|
311 | - do_action( 'wc_stripe_set_default_source', $this->get_id(), $response ); |
|
310 | + if (empty($response->error)) { |
|
311 | + do_action('wc_stripe_set_default_source', $this->get_id(), $response); |
|
312 | 312 | |
313 | 313 | return true; |
314 | 314 | } |
@@ -320,8 +320,8 @@ discard block |
||
320 | 320 | * Deletes caches for this users cards. |
321 | 321 | */ |
322 | 322 | public function clear_cache() { |
323 | - delete_transient( 'stripe_sources_' . $this->get_id() ); |
|
324 | - delete_transient( 'stripe_customer_' . $this->get_id() ); |
|
323 | + delete_transient('stripe_sources_' . $this->get_id()); |
|
324 | + delete_transient('stripe_customer_' . $this->get_id()); |
|
325 | 325 | $this->customer_data = array(); |
326 | 326 | } |
327 | 327 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( ! defined( 'ABSPATH' ) ) { |
|
3 | +if ( ! defined('ABSPATH')) { |
|
4 | 4 | exit; // Exit if accessed directly |
5 | 5 | } |
6 | 6 | |
@@ -35,10 +35,10 @@ discard block |
||
35 | 35 | * @param string $deprecated Deprecated since WooCommerce 3.0 |
36 | 36 | * @return string |
37 | 37 | */ |
38 | - public function get_display_name( $deprecated = '' ) { |
|
38 | + public function get_display_name($deprecated = '') { |
|
39 | 39 | $display = sprintf( |
40 | 40 | /* translators: last 4 digits of IBAN account */ |
41 | - __( 'SEPA IBAN ending in %s', 'woocommerce-gateway-stripe' ), |
|
41 | + __('SEPA IBAN ending in %s', 'woocommerce-gateway-stripe'), |
|
42 | 42 | $this->get_last4() |
43 | 43 | ); |
44 | 44 | |
@@ -66,11 +66,11 @@ discard block |
||
66 | 66 | * @return boolean True if the passed data is valid |
67 | 67 | */ |
68 | 68 | public function validate() { |
69 | - if ( false === parent::validate() ) { |
|
69 | + if (false === parent::validate()) { |
|
70 | 70 | return false; |
71 | 71 | } |
72 | 72 | |
73 | - if ( ! $this->get_last4( 'edit' ) ) { |
|
73 | + if ( ! $this->get_last4('edit')) { |
|
74 | 74 | return false; |
75 | 75 | } |
76 | 76 | |
@@ -85,8 +85,8 @@ discard block |
||
85 | 85 | * @param string $context |
86 | 86 | * @return string Last 4 digits |
87 | 87 | */ |
88 | - public function get_last4( $context = 'view' ) { |
|
89 | - return WC_Stripe_Helper::is_pre_30() ? $this->get_meta( 'last4' ) : $this->get_prop( 'last4', $context ); |
|
88 | + public function get_last4($context = 'view') { |
|
89 | + return WC_Stripe_Helper::is_pre_30() ? $this->get_meta('last4') : $this->get_prop('last4', $context); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | /** |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @version 4.0.0 |
96 | 96 | * @param string $last4 |
97 | 97 | */ |
98 | - public function set_last4( $last4 ) { |
|
99 | - WC_Stripe_Helper::is_pre_30() ? $this->add_meta_data( 'last4', $last4, true ) : $this->set_prop( 'last4', $last4 ); |
|
98 | + public function set_last4($last4) { |
|
99 | + WC_Stripe_Helper::is_pre_30() ? $this->add_meta_data('last4', $last4, true) : $this->set_prop('last4', $last4); |
|
100 | 100 | } |
101 | 101 | } |