class-wc-stripe-payment-tokens.php
13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles and process WC payment tokens API.
* Seen in checkout page and my account->add payment method page.
*
* @since 4.0.0
*/
class WC_Stripe_Payment_Tokens {
private static $_this;
/**
* Constructor.
*
* @since 4.0.0
* @version 4.0.0
*/
public function __construct() {
self::$_this = $this;
add_filter( 'woocommerce_get_customer_payment_tokens', [ $this, 'woocommerce_get_customer_payment_tokens' ], 10, 3 );
add_filter( 'woocommerce_payment_methods_list_item', [ $this, 'get_account_saved_payment_methods_list_item_sepa' ], 10, 2 );
add_filter( 'woocommerce_get_credit_card_type_label', [ $this, 'normalize_sepa_label' ] );
add_action( 'woocommerce_payment_token_deleted', [ $this, 'woocommerce_payment_token_deleted' ], 10, 2 );
add_action( 'woocommerce_payment_token_set_default', [ $this, 'woocommerce_payment_token_set_default' ] );
}
/**
* Public access to instance object.
*
* @since 4.0.0
* @version 4.0.0
*/
public static function get_instance() {
return self::$_this;
}
/**
* Normalizes the SEPA IBAN label on My Account page.
*
* @since 4.0.0
* @version 4.0.0
* @param string $label
* @return string $label
*/
public function normalize_sepa_label( $label ) {
if ( 'sepa iban' === strtolower( $label ) ) {
return 'SEPA IBAN';
}
return $label;
}
/**
* Extract the payment token from the provided request.
*
* TODO: Once php requirement is bumped to >= 7.1.0 set return type to ?\WC_Payment_Token
* since the return type is nullable, as per
* https://www.php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
*
* @param array $request Associative array containing payment request information.
*
* @return \WC_Payment_Token|NULL
*/
public static function get_token_from_request( array $request ) {
$payment_method = ! is_null( $request['payment_method'] ) ? $request['payment_method'] : null;
$token_request_key = 'wc-' . $payment_method . '-payment-token';
if (
! isset( $request[ $token_request_key ] ) ||
'new' === $request[ $token_request_key ]
) {
return null;
}
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
$token = \WC_Payment_Tokens::get( wc_clean( $request[ $token_request_key ] ) );
// If the token doesn't belong to this gateway or the current user it's invalid.
if ( ! $token || $payment_method !== $token->get_gateway_id() || $token->get_user_id() !== get_current_user_id() ) {
return null;
}
return $token;
}
/**
* Checks if customer has saved payment methods.
*
* @since 4.1.0
* @param int $customer_id
* @return bool
*/
public static function customer_has_saved_methods( $customer_id ) {
$gateways = [ 'stripe', 'stripe_sepa' ];
if ( empty( $customer_id ) ) {
return false;
}
$has_token = false;
foreach ( $gateways as $gateway ) {
$tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, $gateway );
if ( ! empty( $tokens ) ) {
$has_token = true;
break;
}
}
return $has_token;
}
/**
* Gets saved tokens from Stripe, if they don't already exist in WooCommerce.
*
* @param array $tokens Array of tokens
* @param string $user_id WC User ID
* @param string $gateway_id WC Gateway ID
*
* @return array
*/
public function woocommerce_get_customer_payment_tokens( $tokens, $user_id, $gateway_id ) {
if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
return $this->woocommerce_get_customer_upe_payment_tokens( $tokens, $user_id, $gateway_id );
} else {
return $this->woocommerce_get_customer_payment_tokens_legacy( $tokens, $user_id, $gateway_id );
}
}
/**
* Gets saved tokens from Sources API if they don't already exist in WooCommerce.
*
* @since 3.1.0
* @version 4.0.0
* @param array $tokens
* @return array
*/
public function woocommerce_get_customer_payment_tokens_legacy( $tokens, $customer_id, $gateway_id ) {
if ( is_user_logged_in() && class_exists( 'WC_Payment_Token_CC' ) ) {
$stored_tokens = [];
foreach ( $tokens as $token ) {
$stored_tokens[ $token->get_token() ] = $token;
}
if ( 'stripe' === $gateway_id ) {
$stripe_customer = new WC_Stripe_Customer( $customer_id );
$stripe_sources = $stripe_customer->get_sources();
foreach ( $stripe_sources as $source ) {
if ( isset( $source->type ) && 'card' === $source->type ) {
if ( ! isset( $stored_tokens[ $source->id ] ) ) {
$token = new WC_Payment_Token_CC();
$token->set_token( $source->id );
$token->set_gateway_id( 'stripe' );
if ( 'source' === $source->object && 'card' === $source->type ) {
$token->set_card_type( strtolower( $source->card->brand ) );
$token->set_last4( $source->card->last4 );
$token->set_expiry_month( $source->card->exp_month );
$token->set_expiry_year( $source->card->exp_year );
}
$token->set_user_id( $customer_id );
$token->save();
$tokens[ $token->get_id() ] = $token;
} else {
unset( $stored_tokens[ $source->id ] );
}
} else {
if ( ! isset( $stored_tokens[ $source->id ] ) && 'card' === $source->object ) {
$token = new WC_Payment_Token_CC();
$token->set_token( $source->id );
$token->set_gateway_id( 'stripe' );
$token->set_card_type( strtolower( $source->brand ) );
$token->set_last4( $source->last4 );
$token->set_expiry_month( $source->exp_month );
$token->set_expiry_year( $source->exp_year );
$token->set_user_id( $customer_id );
$token->save();
$tokens[ $token->get_id() ] = $token;
} else {
unset( $stored_tokens[ $source->id ] );
}
}
}
}
if ( 'stripe_sepa' === $gateway_id ) {
$stripe_customer = new WC_Stripe_Customer( $customer_id );
$stripe_sources = $stripe_customer->get_sources();
foreach ( $stripe_sources as $source ) {
if ( isset( $source->type ) && 'sepa_debit' === $source->type ) {
if ( ! isset( $stored_tokens[ $source->id ] ) ) {
$token = new WC_Payment_Token_SEPA();
$token->set_token( $source->id );
$token->set_gateway_id( 'stripe_sepa' );
$token->set_last4( $source->sepa_debit->last4 );
$token->set_user_id( $customer_id );
$token->save();
$tokens[ $token->get_id() ] = $token;
} else {
unset( $stored_tokens[ $source->id ] );
}
}
}
}
}
return $tokens;
}
/**
* Gets saved tokens from Intentions API if they don't already exist in WooCommerce.
*
* @param array $tokens Array of tokens
* @param string $user_id WC User ID
* @param string $gateway_id WC Gateway ID
*
* @return array
*/
public function woocommerce_get_customer_upe_payment_tokens( $tokens, $user_id, $gateway_id ) {
if ( ( ! empty( $gateway_id ) && WC_Stripe_UPE_Payment_Gateway::ID !== $gateway_id ) || ! is_user_logged_in() ) {
return $tokens;
}
if ( count( $tokens ) >= get_option( 'posts_per_page' ) ) {
// The tokens data store is not paginated and only the first "post_per_page" (defaults to 10) tokens are retrieved.
// Having 10 saved credit cards is considered an unsupported edge case, new ones that have been stored in Stripe won't be added.
return $tokens;
}
$gateway = new WC_Stripe_UPE_Payment_Gateway();
$reusable_payment_methods = array_filter( $gateway->get_upe_enabled_payment_method_ids(), [ $gateway, 'is_enabled_for_saved_payments' ] );
$customer = new WC_Stripe_Customer( $user_id );
$remaining_tokens = [];
foreach ( $tokens as $token ) {
if ( WC_Stripe_UPE_Payment_Gateway::ID === $token->get_gateway_id() ) {
$payment_method_type = $this->get_payment_method_type_from_token( $token );
if ( ! in_array( $payment_method_type, $reusable_payment_methods, true ) ) {
// Remove saved token from list, if payment method is not enabled.
unset( $tokens[ $token->get_id() ] );
} else {
// Store relevant existing tokens here.
// We will use this list to check whether these methods still exist on Stripe's side.
$remaining_tokens[ $token->get_token() ] = $token;
}
}
}
$retrievable_payment_method_types = [];
foreach ( $reusable_payment_methods as $payment_method_id ) {
$upe_payment_method = $gateway->payment_methods[ $payment_method_id ];
if ( ! in_array( $upe_payment_method->get_retrievable_type(), $retrievable_payment_method_types, true ) ) {
$retrievable_payment_method_types[] = $upe_payment_method->get_retrievable_type();
}
}
foreach ( $retrievable_payment_method_types as $payment_method_id ) {
$payment_methods = $customer->get_payment_methods( $payment_method_id );
// Prevent unnecessary recursion, WC_Payment_Token::save() ends up calling 'woocommerce_get_customer_payment_tokens' in some cases.
remove_action( 'woocommerce_get_customer_payment_tokens', [ $this, 'woocommerce_get_customer_payment_tokens' ], 10, 3 );
foreach ( $payment_methods as $payment_method ) {
if ( ! isset( $remaining_tokens[ $payment_method->id ] ) ) {
$payment_method_type = $this->get_original_payment_method_type( $payment_method );
if ( ! in_array( $payment_method_type, $reusable_payment_methods, true ) ) {
continue;
}
// Create new token for new payment method and add to list.
$upe_payment_method = $gateway->payment_methods[ $payment_method_type ];
$token = $upe_payment_method->create_payment_token_for_user( $user_id, $payment_method );
$tokens[ $token->get_id() ] = $token;
} else {
// Count that existing token for payment method is still present on Stripe.
// Remaining IDs in $remaining_tokens no longer exist with Stripe and will be eliminated.
unset( $remaining_tokens[ $payment_method->id ] );
}
}
add_action( 'woocommerce_get_customer_payment_tokens', [ $this, 'woocommerce_get_customer_payment_tokens' ], 10, 3 );
}
// Eliminate remaining payment methods no longer known by Stripe.
// Prevent unnecessary recursion, when deleting tokens.
remove_action( 'woocommerce_payment_token_deleted', [ $this, 'woocommerce_payment_token_deleted' ], 10, 2 );
foreach ( $remaining_tokens as $token ) {
unset( $tokens[ $token->get_id() ] );
$token->delete();
}
add_action( 'woocommerce_payment_token_deleted', [ $this, 'woocommerce_payment_token_deleted' ], 10, 2 );
return $tokens;
}
/**
* Returns original type of payment method from Stripe payment method response,
* after checking whether payment method is SEPA method generated from another type.
*
* @param object $payment_method Stripe payment method JSON object.
*
* @return string Payment method type/ID
*/
private function get_original_payment_method_type( $payment_method ) {
if ( WC_Stripe_UPE_Payment_Method_Sepa::STRIPE_ID === $payment_method->type ) {
if ( ! is_null( $payment_method->sepa_debit->generated_from->charge ) ) {
return $payment_method->sepa_debit->generated_from->charge->payment_method_details->type;
}
if ( ! is_null( $payment_method->sepa_debit->generated_from->setup_attempt ) ) {
return $payment_method->sepa_debit->generated_from->setup_attempt->payment_method_details->type;
}
}
return $payment_method->type;
}
/**
* Returns original Stripe payment method type from payment token
*
* @param WC_Payment_Token $payment_token WC Payment Token (CC or SEPA)
*
* @return string
*/
private function get_payment_method_type_from_token( $payment_token ) {
$type = $payment_token->get_type();
if ( 'CC' === $type ) {
return 'card';
} elseif ( 'sepa' === $type ) {
return $payment_token->get_payment_method_type();
} else {
return $type;
}
}
/**
* Controls the output for SEPA on the my account page.
*
* @since 4.0.0
* @version 4.0.0
* @param array $item Individual list item from woocommerce_saved_payment_methods_list
* @param WC_Payment_Token $payment_token The payment token associated with this method entry
* @return array Filtered item
*/
public function get_account_saved_payment_methods_list_item_sepa( $item, $payment_token ) {
if ( 'sepa' === strtolower( $payment_token->get_type() ) ) {
$item['method']['last4'] = $payment_token->get_last4();
$item['method']['brand'] = esc_html__( 'SEPA IBAN', 'woocommerce-gateway-stripe' );
}
return $item;
}
/**
* Delete token from Stripe.
*
* @since 3.1.0
* @version 4.0.0
*/
public function woocommerce_payment_token_deleted( $token_id, $token ) {
$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
if ( WC_Stripe_UPE_Payment_Gateway::ID === $token->get_gateway_id() ) {
$stripe_customer->detach_payment_method( $token->get_token() );
}
} else {
if ( 'stripe' === $token->get_gateway_id() || 'stripe_sepa' === $token->get_gateway_id() ) {
$stripe_customer->delete_source( $token->get_token() );
}
}
}
/**
* Set as default in Stripe.
*
* @since 3.1.0
* @version 4.0.0
*/
public function woocommerce_payment_token_set_default( $token_id ) {
$token = WC_Payment_Tokens::get( $token_id );
$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
if ( WC_Stripe_UPE_Payment_Gateway::ID === $token->get_gateway_id() ) {
$stripe_customer->set_default_payment_method( $token->get_token() );
}
} else {
if ( 'stripe' === $token->get_gateway_id() || 'stripe_sepa' === $token->get_gateway_id() ) {
$stripe_customer->set_default_source( $token->get_token() );
}
}
}
}
new WC_Stripe_Payment_Tokens();