@@ -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 integer $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,12 +90,12 @@ discard block |
||
90 | 90 | * Get data from the Stripe API about this customer |
91 | 91 | */ |
92 | 92 | public function get_customer_data() { |
93 | - if ( empty( $this->customer_data ) && $this->get_id() && false === ( $this->customer_data = get_transient( 'stripe_customer_' . $this->get_id() ) ) ) { |
|
94 | - $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() ); |
|
93 | + if (empty($this->customer_data) && $this->get_id() && false === ($this->customer_data = get_transient('stripe_customer_' . $this->get_id()))) { |
|
94 | + $response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id()); |
|
95 | 95 | |
96 | - if ( ! is_wp_error( $response ) ) { |
|
97 | - $this->set_customer_data( $response ); |
|
98 | - set_transient( 'stripe_customer_' . $this->get_id(), $response, HOUR_IN_SECONDS * 48 ); |
|
96 | + if ( ! is_wp_error($response)) { |
|
97 | + $this->set_customer_data($response); |
|
98 | + set_transient('stripe_customer_' . $this->get_id(), $response, HOUR_IN_SECONDS * 48); |
|
99 | 99 | } |
100 | 100 | } |
101 | 101 | return $this->customer_data; |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | $data = $this->get_customer_data(); |
110 | 110 | $source = ''; |
111 | 111 | |
112 | - if ( $data ) { |
|
112 | + if ($data) { |
|
113 | 113 | $source = $data->default_source; |
114 | 114 | } |
115 | 115 | |
@@ -121,10 +121,10 @@ discard block |
||
121 | 121 | * @param array $args |
122 | 122 | * @return WP_Error|int |
123 | 123 | */ |
124 | - public function create_customer( $args = array() ) { |
|
125 | - if ( $user = $this->get_user() ) { |
|
126 | - $billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true ); |
|
127 | - $billing_last_name = get_user_meta( $user->ID, 'billing_last_name', true ); |
|
124 | + public function create_customer($args = array()) { |
|
125 | + if ($user = $this->get_user()) { |
|
126 | + $billing_first_name = get_user_meta($user->ID, 'billing_first_name', true); |
|
127 | + $billing_last_name = get_user_meta($user->ID, 'billing_last_name', true); |
|
128 | 128 | |
129 | 129 | $defaults = array( |
130 | 130 | 'email' => $user->user_email, |
@@ -139,26 +139,26 @@ discard block |
||
139 | 139 | |
140 | 140 | $metadata = array(); |
141 | 141 | |
142 | - $defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user ); |
|
142 | + $defaults['metadata'] = apply_filters('wc_stripe_customer_metadata', $metadata, $user); |
|
143 | 143 | |
144 | - $args = wp_parse_args( $args, $defaults ); |
|
145 | - $response = WC_Stripe_API::request( $args, 'customers' ); |
|
144 | + $args = wp_parse_args($args, $defaults); |
|
145 | + $response = WC_Stripe_API::request($args, 'customers'); |
|
146 | 146 | |
147 | - if ( is_wp_error( $response ) ) { |
|
147 | + if (is_wp_error($response)) { |
|
148 | 148 | return $response; |
149 | - } elseif ( empty( $response->id ) ) { |
|
150 | - return new WP_Error( 'stripe_error', __( 'Could not create Stripe customer.', 'woocommerce-gateway-stripe' ) ); |
|
149 | + } elseif (empty($response->id)) { |
|
150 | + return new WP_Error('stripe_error', __('Could not create Stripe customer.', 'woocommerce-gateway-stripe')); |
|
151 | 151 | } |
152 | 152 | |
153 | - $this->set_id( $response->id ); |
|
153 | + $this->set_id($response->id); |
|
154 | 154 | $this->clear_cache(); |
155 | - $this->set_customer_data( $response ); |
|
155 | + $this->set_customer_data($response); |
|
156 | 156 | |
157 | - if ( $this->get_user_id() ) { |
|
158 | - update_user_meta( $this->get_user_id(), '_stripe_customer_id', $response->id ); |
|
157 | + if ($this->get_user_id()) { |
|
158 | + update_user_meta($this->get_user_id(), '_stripe_customer_id', $response->id); |
|
159 | 159 | } |
160 | 160 | |
161 | - do_action( 'woocommerce_stripe_add_customer', $args, $response ); |
|
161 | + do_action('woocommerce_stripe_add_customer', $args, $response); |
|
162 | 162 | |
163 | 163 | return $response->id; |
164 | 164 | } |
@@ -169,51 +169,51 @@ discard block |
||
169 | 169 | * @param bool $retry |
170 | 170 | * @return WP_Error|int |
171 | 171 | */ |
172 | - public function add_card( $token, $retry = true ) { |
|
173 | - if ( ! $this->get_id() ) { |
|
174 | - if ( ( $response = $this->create_customer() ) && is_wp_error( $response ) ) { |
|
172 | + public function add_card($token, $retry = true) { |
|
173 | + if ( ! $this->get_id()) { |
|
174 | + if (($response = $this->create_customer()) && is_wp_error($response)) { |
|
175 | 175 | return $response; |
176 | 176 | } |
177 | 177 | } |
178 | 178 | |
179 | - $response = WC_Stripe_API::request( array( |
|
179 | + $response = WC_Stripe_API::request(array( |
|
180 | 180 | 'source' => $token, |
181 | - ), 'customers/' . $this->get_id() . '/sources' ); |
|
181 | + ), 'customers/' . $this->get_id() . '/sources'); |
|
182 | 182 | |
183 | - if ( is_wp_error( $response ) ) { |
|
183 | + if (is_wp_error($response)) { |
|
184 | 184 | // It is possible the WC user once was linked to a customer on Stripe |
185 | 185 | // but no longer exists. Instead of failing, lets try to create a |
186 | 186 | // new customer. |
187 | - if ( preg_match( '/No such customer:/', $response->get_error_message() ) ) { |
|
188 | - delete_user_meta( $this->get_user_id(), '_stripe_customer_id' ); |
|
187 | + if (preg_match('/No such customer:/', $response->get_error_message())) { |
|
188 | + delete_user_meta($this->get_user_id(), '_stripe_customer_id'); |
|
189 | 189 | $this->create_customer(); |
190 | - return $this->add_card( $token, false ); |
|
191 | - } elseif ( 'customer' === $response->get_error_code() && $retry ) { |
|
190 | + return $this->add_card($token, false); |
|
191 | + } elseif ('customer' === $response->get_error_code() && $retry) { |
|
192 | 192 | $this->create_customer(); |
193 | - return $this->add_card( $token, false ); |
|
193 | + return $this->add_card($token, false); |
|
194 | 194 | } else { |
195 | 195 | return $response; |
196 | 196 | } |
197 | - } elseif ( empty( $response->id ) ) { |
|
198 | - return new WP_Error( 'error', __( 'Unable to add card', 'woocommerce-gateway-stripe' ) ); |
|
197 | + } elseif (empty($response->id)) { |
|
198 | + return new WP_Error('error', __('Unable to add card', 'woocommerce-gateway-stripe')); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | // Add token to WooCommerce |
202 | - if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) { |
|
202 | + if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) { |
|
203 | 203 | $token = new WC_Payment_Token_CC(); |
204 | - $token->set_token( $response->id ); |
|
205 | - $token->set_gateway_id( 'stripe' ); |
|
206 | - $token->set_card_type( strtolower( $response->brand ) ); |
|
207 | - $token->set_last4( $response->last4 ); |
|
208 | - $token->set_expiry_month( $response->exp_month ); |
|
209 | - $token->set_expiry_year( $response->exp_year ); |
|
210 | - $token->set_user_id( $this->get_user_id() ); |
|
204 | + $token->set_token($response->id); |
|
205 | + $token->set_gateway_id('stripe'); |
|
206 | + $token->set_card_type(strtolower($response->brand)); |
|
207 | + $token->set_last4($response->last4); |
|
208 | + $token->set_expiry_month($response->exp_month); |
|
209 | + $token->set_expiry_year($response->exp_year); |
|
210 | + $token->set_user_id($this->get_user_id()); |
|
211 | 211 | $token->save(); |
212 | 212 | } |
213 | 213 | |
214 | 214 | $this->clear_cache(); |
215 | 215 | |
216 | - do_action( 'woocommerce_stripe_add_card', $this->get_id(), $token, $response ); |
|
216 | + do_action('woocommerce_stripe_add_card', $this->get_id(), $token, $response); |
|
217 | 217 | |
218 | 218 | return $response->id; |
219 | 219 | } |
@@ -227,20 +227,20 @@ discard block |
||
227 | 227 | public function get_cards() { |
228 | 228 | $cards = array(); |
229 | 229 | |
230 | - if ( $this->get_id() && false === ( $cards = get_transient( 'stripe_cards_' . $this->get_id() ) ) ) { |
|
231 | - $response = WC_Stripe_API::request( array( |
|
230 | + if ($this->get_id() && false === ($cards = get_transient('stripe_cards_' . $this->get_id()))) { |
|
231 | + $response = WC_Stripe_API::request(array( |
|
232 | 232 | 'limit' => 100, |
233 | - ), 'customers/' . $this->get_id() . '/sources', 'GET' ); |
|
233 | + ), 'customers/' . $this->get_id() . '/sources', 'GET'); |
|
234 | 234 | |
235 | - if ( is_wp_error( $response ) ) { |
|
235 | + if (is_wp_error($response)) { |
|
236 | 236 | return array(); |
237 | 237 | } |
238 | 238 | |
239 | - if ( is_array( $response->data ) ) { |
|
239 | + if (is_array($response->data)) { |
|
240 | 240 | $cards = $response->data; |
241 | 241 | } |
242 | 242 | |
243 | - set_transient( 'stripe_cards_' . $this->get_id(), $cards, HOUR_IN_SECONDS * 48 ); |
|
243 | + set_transient('stripe_cards_' . $this->get_id(), $cards, HOUR_IN_SECONDS * 48); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | return $cards; |
@@ -250,13 +250,13 @@ discard block |
||
250 | 250 | * Delete a card from stripe. |
251 | 251 | * @param string $card_id |
252 | 252 | */ |
253 | - public function delete_card( $card_id ) { |
|
254 | - $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $card_id ), 'DELETE' ); |
|
253 | + public function delete_card($card_id) { |
|
254 | + $response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field($card_id), 'DELETE'); |
|
255 | 255 | |
256 | 256 | $this->clear_cache(); |
257 | 257 | |
258 | - if ( ! is_wp_error( $response ) ) { |
|
259 | - do_action( 'wc_stripe_delete_card', $this->get_id(), $response ); |
|
258 | + if ( ! is_wp_error($response)) { |
|
259 | + do_action('wc_stripe_delete_card', $this->get_id(), $response); |
|
260 | 260 | |
261 | 261 | return true; |
262 | 262 | } |
@@ -268,15 +268,15 @@ discard block |
||
268 | 268 | * Set default card in Stripe |
269 | 269 | * @param string $card_id |
270 | 270 | */ |
271 | - public function set_default_card( $card_id ) { |
|
272 | - $response = WC_Stripe_API::request( array( |
|
273 | - 'default_source' => sanitize_text_field( $card_id ), |
|
274 | - ), 'customers/' . $this->get_id(), 'POST' ); |
|
271 | + public function set_default_card($card_id) { |
|
272 | + $response = WC_Stripe_API::request(array( |
|
273 | + 'default_source' => sanitize_text_field($card_id), |
|
274 | + ), 'customers/' . $this->get_id(), 'POST'); |
|
275 | 275 | |
276 | 276 | $this->clear_cache(); |
277 | 277 | |
278 | - if ( ! is_wp_error( $response ) ) { |
|
279 | - do_action( 'wc_stripe_set_default_card', $this->get_id(), $response ); |
|
278 | + if ( ! is_wp_error($response)) { |
|
279 | + do_action('wc_stripe_set_default_card', $this->get_id(), $response); |
|
280 | 280 | |
281 | 281 | return true; |
282 | 282 | } |
@@ -288,8 +288,8 @@ discard block |
||
288 | 288 | * Deletes caches for this users cards. |
289 | 289 | */ |
290 | 290 | public function clear_cache() { |
291 | - delete_transient( 'stripe_cards_' . $this->get_id() ); |
|
292 | - delete_transient( 'stripe_customer_' . $this->get_id() ); |
|
291 | + delete_transient('stripe_cards_' . $this->get_id()); |
|
292 | + delete_transient('stripe_customer_' . $this->get_id()); |
|
293 | 293 | $this->customer_data = array(); |
294 | 294 | } |
295 | 295 | } |