-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathclass-wc-stripe-helper.php
1681 lines (1468 loc) · 55.8 KB
/
class-wc-stripe-helper.php
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Provides static methods as helpers.
*
* @since 4.0.0
*/
class WC_Stripe_Helper {
const SETTINGS_OPTION = 'woocommerce_stripe_settings';
const LEGACY_META_NAME_FEE = 'Stripe Fee';
const LEGACY_META_NAME_NET = 'Net Revenue From Stripe';
const META_NAME_FEE = '_stripe_fee';
const META_NAME_NET = '_stripe_net';
const META_NAME_STRIPE_CURRENCY = '_stripe_currency';
const PAYMENT_AWAITING_ACTION_META = '_stripe_payment_awaiting_action';
/**
* List of legacy Stripe gateways.
*
* @var array
*/
public static $stripe_legacy_gateways = [];
/**
* Get the main Stripe settings option.
*
* @param string $method (Optional) The payment method to get the settings from.
* @return array $settings The Stripe settings.
*/
public static function get_stripe_settings( $method = null ) {
$settings = null === $method ? get_option( self::SETTINGS_OPTION, [] ) : get_option( 'woocommerce_stripe_' . $method . '_settings', [] );
if ( ! is_array( $settings ) ) {
$settings = [];
}
return $settings;
}
/**
* Update the main Stripe settings option.
*
* @param $options array The Stripe settings.
* @return void
*/
public static function update_main_stripe_settings( $options ) {
update_option( self::SETTINGS_OPTION, $options );
}
/**
* Delete the main Stripe settings option.
*
* @return void
*/
public static function delete_main_stripe_settings() {
delete_option( self::SETTINGS_OPTION );
}
/**
* Gets the Stripe currency for order.
*
* @since 4.1.0
* @param object $order
* @return string $currency
*/
public static function get_stripe_currency( $order = null ) {
if ( is_null( $order ) ) {
return false;
}
return $order->get_meta( self::META_NAME_STRIPE_CURRENCY, true );
}
/**
* Updates the Stripe currency for order.
*
* @since 4.1.0
* @param object $order
* @param string $currency
*/
public static function update_stripe_currency( $order, $currency ) {
if ( is_null( $order ) ) {
return false;
}
$order->update_meta_data( self::META_NAME_STRIPE_CURRENCY, $currency );
}
/**
* Gets the Stripe fee for order. With legacy check.
*
* @since 4.1.0
* @param object $order
* @return string $amount
*/
public static function get_stripe_fee( $order = null ) {
if ( is_null( $order ) ) {
return false;
}
$amount = $order->get_meta( self::META_NAME_FEE, true );
// If not found let's check for legacy name.
if ( empty( $amount ) ) {
$amount = $order->get_meta( self::LEGACY_META_NAME_FEE, true );
// If found update to new name.
if ( $amount ) {
self::update_stripe_fee( $order, $amount );
}
}
return $amount;
}
/**
* Updates the Stripe fee for order.
*
* @since 4.1.0
* @param object $order
* @param float $amount
*/
public static function update_stripe_fee( $order = null, $amount = 0.0 ) {
if ( is_null( $order ) ) {
return false;
}
$order->update_meta_data( self::META_NAME_FEE, $amount );
}
/**
* Deletes the Stripe fee for order.
*
* @since 4.1.0
* @param object $order
*/
public static function delete_stripe_fee( $order = null ) {
if ( is_null( $order ) ) {
return false;
}
$order->delete_meta_data( self::META_NAME_FEE );
$order->delete_meta_data( self::LEGACY_META_NAME_FEE );
}
/**
* Gets the Stripe net for order. With legacy check.
*
* @since 4.1.0
* @param object $order
* @return string $amount
*/
public static function get_stripe_net( $order = null ) {
if ( is_null( $order ) ) {
return false;
}
$amount = $order->get_meta( self::META_NAME_NET, true );
// If not found let's check for legacy name.
if ( empty( $amount ) ) {
$amount = $order->get_meta( self::LEGACY_META_NAME_NET, true );
// If found update to new name.
if ( $amount ) {
self::update_stripe_net( $order, $amount );
}
}
return $amount;
}
/**
* Updates the Stripe net for order.
*
* @since 4.1.0
* @param object $order
* @param float $amount
*/
public static function update_stripe_net( $order = null, $amount = 0.0 ) {
if ( is_null( $order ) ) {
return false;
}
$order->update_meta_data( self::META_NAME_NET, $amount );
}
/**
* Deletes the Stripe net for order.
*
* @since 4.1.0
* @param object $order
*/
public static function delete_stripe_net( $order = null ) {
if ( is_null( $order ) ) {
return false;
}
$order->delete_meta_data( self::META_NAME_NET );
$order->delete_meta_data( self::LEGACY_META_NAME_NET );
}
/**
* Get Stripe amount to pay
*
* @param float $total Amount due.
* @param string $currency Accepted currency.
*
* @return float|int
*/
public static function get_stripe_amount( $total, $currency = '' ) {
if ( ! $currency ) {
$currency = get_woocommerce_currency();
}
$currency = strtolower( $currency );
if ( in_array( $currency, self::no_decimal_currencies(), true ) ) {
return absint( $total );
} elseif ( in_array( $currency, self::three_decimal_currencies(), true ) ) {
$price_decimals = wc_get_price_decimals();
$amount = absint( wc_format_decimal( ( (float) $total * 1000 ), $price_decimals ) ); // For tree decimal currencies.
return $amount - ( $amount % 10 ); // Round the last digit down. See https://docs.stripe.com/currencies?presentment-currency=AE#three-decimal
} else {
return absint( wc_format_decimal( ( (float) $total * 100 ), wc_get_price_decimals() ) ); // In cents.
}
}
/**
* Localize Stripe messages based on code
*
* @since 3.0.6
* @version 3.0.6
* @return array
*/
public static function get_localized_messages() {
return apply_filters(
'wc_stripe_localized_messages',
[
'invalid_number' => __( 'The card number is not a valid credit card number.', 'woocommerce-gateway-stripe' ),
'invalid_expiry_month' => __( 'The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe' ),
'invalid_expiry_year' => __( 'The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe' ),
'invalid_cvc' => __( 'The card\'s security code is invalid.', 'woocommerce-gateway-stripe' ),
'incorrect_number' => __( 'The card number is incorrect.', 'woocommerce-gateway-stripe' ),
'incomplete_number' => __( 'The card number is incomplete.', 'woocommerce-gateway-stripe' ),
'incomplete_cvc' => __( 'The card\'s security code is incomplete.', 'woocommerce-gateway-stripe' ),
'incomplete_expiry' => __( 'The card\'s expiration date is incomplete.', 'woocommerce-gateway-stripe' ),
'expired_card' => __( 'The card has expired.', 'woocommerce-gateway-stripe' ),
'incorrect_cvc' => __( 'The card\'s security code is incorrect.', 'woocommerce-gateway-stripe' ),
'incorrect_zip' => __( 'The card\'s zip code failed validation.', 'woocommerce-gateway-stripe' ),
'postal_code_invalid' => __( 'Invalid zip code, please correct and try again', 'woocommerce-gateway-stripe' ),
'invalid_expiry_year_past' => __( 'The card\'s expiration year is in the past', 'woocommerce-gateway-stripe' ),
'card_declined' => __( 'The card was declined.', 'woocommerce-gateway-stripe' ),
'missing' => __( 'There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe' ),
'processing_error' => __( 'An error occurred while processing the card.', 'woocommerce-gateway-stripe' ),
'invalid_sofort_country' => __( 'The billing country is not accepted by Sofort. Please try another country.', 'woocommerce-gateway-stripe' ),
'email_invalid' => __( 'Invalid email address, please correct and try again.', 'woocommerce-gateway-stripe' ),
'invalid_request_error' => is_add_payment_method_page()
? __( 'Unable to save this payment method, please try again or use alternative method.', 'woocommerce-gateway-stripe' )
: __( 'Unable to process this payment, please try again or use alternative method.', 'woocommerce-gateway-stripe' ),
'amount_too_large' => __( 'The order total is too high for this payment method', 'woocommerce-gateway-stripe' ),
'amount_too_small' => __( 'The order total is too low for this payment method', 'woocommerce-gateway-stripe' ),
'country_code_invalid' => __( 'Invalid country code, please try again with a valid country code', 'woocommerce-gateway-stripe' ),
'tax_id_invalid' => __( 'Invalid Tax Id, please try again with a valid tax id', 'woocommerce-gateway-stripe' ),
'invalid_wallet_type' => __( 'Invalid wallet payment type, please try again or use an alternative method.', 'woocommerce-gateway-stripe' ),
'payment_intent_authentication_failure' => __( 'We are unable to authenticate your payment method. Please choose a different payment method and try again.', 'woocommerce-gateway-stripe' ),
'insufficient_funds' => __( 'Your card has insufficient funds.', 'woocommerce-gateway-stripe' ),
]
);
}
/**
* List of currencies supported by Stripe that has no decimals
* https://docs.stripe.com/currencies#zero-decimal from https://docs.stripe.com/currencies#presentment-currencies
* ugx is an exception and not in this list for being a special cases in Stripe https://docs.stripe.com/currencies#special-cases
*
* @return array $currencies
*/
public static function no_decimal_currencies() {
return [
'bif', // Burundian Franc
'clp', // Chilean Peso
'djf', // Djiboutian Franc
'gnf', // Guinean Franc
'jpy', // Japanese Yen
'kmf', // Comorian Franc
'krw', // South Korean Won
'mga', // Malagasy Ariary
'pyg', // Paraguayan Guaraní
'rwf', // Rwandan Franc
'vnd', // Vietnamese Đồng
'vuv', // Vanuatu Vatu
'xaf', // Central African Cfa Franc
'xof', // West African Cfa Franc
'xpf', // Cfp Franc
];
}
/**
* List of currencies supported by Stripe that has three decimals
* https://docs.stripe.com/currencies?presentment-currency=AE#three-decimal
*
* @return array $currencies
*/
private static function three_decimal_currencies() {
return [
'bhd', // Bahraini Dinar
'jod', // Jordanian Dinar
'kwd', // Kuwaiti Dinar
'omr', // Omani Rial
'tnd', // Tunisian Dinar
];
}
/**
* Stripe uses smallest denomination in currencies such as cents.
* We need to format the returned currency from Stripe into human readable form.
* The amount is not used in any calculations so returning string is sufficient.
*
* @param object $balance_transaction
* @param string $type Type of number to format
* @return string
*/
public static function format_balance_fee( $balance_transaction, $type = 'fee' ) {
if ( ! is_object( $balance_transaction ) ) {
return;
}
if ( in_array( strtolower( $balance_transaction->currency ), self::no_decimal_currencies() ) ) {
if ( 'fee' === $type ) {
return $balance_transaction->fee;
}
return $balance_transaction->net;
}
if ( 'fee' === $type ) {
return number_format( $balance_transaction->fee / 100, 2, '.', '' );
}
return number_format( $balance_transaction->net / 100, 2, '.', '' );
}
/**
* Checks Stripe minimum order value authorized per currency
*/
public static function get_minimum_amount() {
// Check order amount
switch ( get_woocommerce_currency() ) {
case WC_Stripe_Currency_Code::UNITED_STATES_DOLLAR:
case WC_Stripe_Currency_Code::CANADIAN_DOLLAR:
case WC_Stripe_Currency_Code::EURO:
case WC_Stripe_Currency_Code::SWISS_FRANC:
case WC_Stripe_Currency_Code::AUSTRALIAN_DOLLAR:
case WC_Stripe_Currency_Code::SINGAPORE_DOLLAR:
$minimum_amount = 50;
break;
case WC_Stripe_Currency_Code::POUND_STERLING:
$minimum_amount = 30;
break;
case WC_Stripe_Currency_Code::DANISH_KRONE:
$minimum_amount = 250;
break;
case WC_Stripe_Currency_Code::NORWEGIAN_KRONE:
case WC_Stripe_Currency_Code::SWEDISH_KRONA:
$minimum_amount = 300;
break;
case WC_Stripe_Currency_Code::JAPANESE_YEN:
$minimum_amount = 5000;
break;
case WC_Stripe_Currency_Code::MEXICAN_PESO:
$minimum_amount = 1000;
break;
case WC_Stripe_Currency_Code::HONG_KONG_DOLLAR:
$minimum_amount = 400;
break;
default:
$minimum_amount = 50;
break;
}
return $minimum_amount;
}
/**
* Gets all the saved setting options from a specific method.
* If specific setting is passed, only return that.
*
* @since 4.0.0
* @version 4.0.0
* @param string $method The payment method to get the settings from.
* @param string $setting The name of the setting to get.
*/
public static function get_settings( $method = null, $setting = null ) {
$all_settings = self::get_stripe_settings( $method );
if ( null === $setting ) {
return $all_settings;
}
return $all_settings[ $setting ] ?? '';
}
/**
* List of legacy payment method classes.
*
* @return array
*/
public static function get_legacy_payment_method_classes() {
$payment_method_classes = [
WC_Gateway_Stripe_Alipay::class,
WC_Gateway_Stripe_Bancontact::class,
WC_Gateway_Stripe_Boleto::class,
WC_Gateway_Stripe_Eps::class,
WC_Gateway_Stripe_Giropay::class,
WC_Gateway_Stripe_Ideal::class,
WC_Gateway_Stripe_Multibanco::class,
WC_Gateway_Stripe_Oxxo::class,
WC_Gateway_Stripe_P24::class,
WC_Gateway_Stripe_Sepa::class,
];
/** Show Sofort if it's already enabled. Hide from the new merchants and keep it for the old ones who are already using this gateway, until we remove it completely.
* Stripe is deprecating Sofort https://support.stripe.com/questions/sofort-is-being-deprecated-as-a-standalone-payment-method.
*/
$sofort_settings = get_option( 'woocommerce_stripe_sofort_settings', [] );
if ( isset( $sofort_settings['enabled'] ) && 'yes' === $sofort_settings['enabled'] ) {
$payment_method_classes[] = WC_Gateway_Stripe_Sofort::class;
}
return $payment_method_classes;
}
/**
* List of legacy payment methods.
*
* @return array
*/
public static function get_legacy_payment_methods() {
if ( ! empty( self::$stripe_legacy_gateways ) ) {
return self::$stripe_legacy_gateways;
}
$payment_gateways = WC()->payment_gateways()->payment_gateways();
$payment_gateway_classes = array_map( 'get_class', $payment_gateways );
foreach ( self::get_legacy_payment_method_classes() as $payment_method_class ) {
// If the payment method is already registered, use it, otherwise create a new instance.
if ( in_array( $payment_method_class, $payment_gateway_classes, true ) ) {
$gateway_id = array_search( $payment_method_class, $payment_gateway_classes, true );
$payment_method = $payment_gateways[ $gateway_id ];
} else {
$payment_method = new $payment_method_class();
}
self::$stripe_legacy_gateways[ $payment_method->id ] = $payment_method;
}
return self::$stripe_legacy_gateways;
}
/**
* Get legacy payment method by id.
*
* @return object|null
*/
public static function get_legacy_payment_method( $id ) {
$payment_methods = self::get_legacy_payment_methods();
if ( ! isset( $payment_methods[ $id ] ) ) {
return null;
}
return $payment_methods[ $id ];
}
/**
* List of available legacy payment method ids.
* It returns the order saved in the `stripe_legacy_method_order` option in Stripe settings.
* If the `stripe_legacy_method_order` option is not set, it returns the default order.
*
* The ids are mapped to the corresponding equivalent UPE method ids for rendeing on the frontend.
*
* @return array
*/
public static function get_legacy_available_payment_method_ids() {
$stripe_settings = self::get_stripe_settings();
$payment_method_classes = self::get_legacy_payment_method_classes();
$ordered_payment_method_ids = isset( $stripe_settings['stripe_legacy_method_order'] ) ? $stripe_settings['stripe_legacy_method_order'] : [];
// If the legacy method order is not set, return the default order.
if ( ! empty( $ordered_payment_method_ids ) ) {
$payment_method_ids = array_map(
function ( $payment_method_id ) {
if ( 'stripe' === $payment_method_id ) {
return WC_Stripe_Payment_Methods::CARD;
} else {
return str_replace( 'stripe_', '', $payment_method_id );
}
},
$ordered_payment_method_ids
);
// Cover the edge case when new Stripe payment methods are added to the plugin which do not exist in
// the `stripe_legacy_method_order` option.
if ( count( $payment_method_ids ) - 1 !== count( $payment_method_classes ) ) {
foreach ( $payment_method_classes as $payment_method_class ) {
$id = str_replace( 'stripe_', '', $payment_method_class::ID );
if ( ! in_array( $id, $payment_method_ids, true ) ) {
$payment_method_ids[] = $id;
}
}
// Update the `stripe_legacy_method_order` option with the new order including missing payment methods from the option.
$stripe_settings['stripe_legacy_method_order'] = $payment_method_ids;
self::update_main_stripe_settings( $stripe_settings );
}
} else {
$payment_method_ids = array_map(
function ( $payment_method_class ) {
return str_replace( 'stripe_', '', $payment_method_class::ID );
},
$payment_method_classes
);
$payment_method_ids = array_merge( [ WC_Stripe_Payment_Methods::CARD ], $payment_method_ids );
}
return $payment_method_ids;
}
/**
* List of enabled legacy payment methods.
*
* @return array
*/
public static function get_legacy_enabled_payment_methods() {
$payment_methods = self::get_legacy_payment_methods();
$enabled_payment_methods = [];
foreach ( $payment_methods as $payment_method ) {
if ( ! $payment_method->is_enabled() ) {
continue;
}
$enabled_payment_methods[ $payment_method->id ] = $payment_method;
}
return $enabled_payment_methods;
}
/**
* List of enabled legacy payment method ids.
*
* @return array
*/
public static function get_legacy_enabled_payment_method_ids() {
$is_stripe_enabled = self::get_settings( null, 'enabled' );
// In legacy mode (when UPE is disabled), Stripe refers to card as payment method.
$enabled_payment_method_ids = 'yes' === $is_stripe_enabled ? [ WC_Stripe_Payment_Methods::CARD ] : [];
$payment_methods = self::get_legacy_payment_methods();
$mapped_enabled_payment_method_ids = [];
foreach ( $payment_methods as $payment_method ) {
if ( ! $payment_method->is_enabled() ) {
continue;
}
$payment_method_id = str_replace( 'stripe_', '', $payment_method->id );
$mapped_enabled_payment_method_ids[] = $payment_method_id;
}
return array_merge( $enabled_payment_method_ids, $mapped_enabled_payment_method_ids );
}
/**
* Get settings of individual legacy payment methods.
*
* @return array
*/
public static function get_legacy_individual_payment_method_settings() {
$stripe_settings = self::get_stripe_settings();
$payment_methods = self::get_legacy_payment_methods();
$payment_method_settings = [
WC_Stripe_Payment_Methods::CARD => [
'name' => isset( $stripe_settings['title'] ) ? $stripe_settings['title'] : '',
'description' => isset( $stripe_settings['description'] ) ? $stripe_settings['description'] : '',
],
];
foreach ( $payment_methods as $payment_method ) {
$settings = [
'name' => $payment_method->get_option( 'title' ),
'description' => $payment_method->get_option( 'description' ),
];
$unique_settings = $payment_method->get_unique_settings();
if ( isset( $unique_settings[ $payment_method->id . '_expiration' ] ) ) {
$settings['expiration'] = $unique_settings[ $payment_method->id . '_expiration' ];
}
$payment_method_id = str_replace( 'stripe_', '', $payment_method->id );
$payment_method_settings[ $payment_method_id ] = $settings;
}
return $payment_method_settings;
}
/**
* Get settings of individual upe payment methods.
*
* @param WC_Stripe_Payment_Gateway $gateway Stripe payment gateway.
* @return array
*/
public static function get_upe_individual_payment_method_settings( $gateway ) {
$payment_method_settings = [];
$available_gateways = $gateway->get_upe_available_payment_methods();
foreach ( $available_gateways as $gateway ) {
$individual_gateway_settings = get_option( 'woocommerce_stripe_' . $gateway . '_settings', [] );
$settings = [
'name' => isset( $individual_gateway_settings['title'] ) ? $individual_gateway_settings['title'] : '',
'description' => isset( $individual_gateway_settings['description'] ) ? $individual_gateway_settings['description'] : '',
];
if ( in_array( $gateway, [ WC_Stripe_Payment_Methods::BOLETO ], true ) ) {
$settings['expiration'] = isset( $individual_gateway_settings['expiration'] ) ? $individual_gateway_settings['expiration'] : '';
}
$payment_method_settings[ $gateway ] = $settings;
}
// If card settings are not set, get it from the default Stripe settings which might be set before enabling UPE.
if ( ! isset( $payment_method_settings['card']['title'] ) && ! isset( $payment_method_settings['card']['description'] ) ) {
$stripe_settings = self::get_stripe_settings();
$title = isset( $stripe_settings['title'] ) ? $stripe_settings['title'] : '';
$description = isset( $stripe_settings['description'] ) ? $stripe_settings['description'] : '';
$payment_method_settings['card'] = [
'name' => $title,
'description' => $description,
];
// Save the title and description to the card settings option.
update_option(
'woocommerce_stripe_card_settings',
[
'title' => $title,
'description' => $description,
]
);
}
return $payment_method_settings;
}
/**
* Returns the list of ordered payment methods for the settings page when UPE is enabled.
* It returns the order saved in the `stripe_upe_payment_method_order` option in Stripe settings.
* If the `stripe_upe_payment_method_order` option is not set, it returns the default order of available gateways.
*
* @param WC_Stripe_Payment_Gateway $gateway Stripe payment gateway.
* @return string[]
*/
public static function get_upe_ordered_payment_method_ids( $gateway ) {
$stripe_settings = self::get_stripe_settings();
$testmode = WC_Stripe_Mode::is_test();
$ordered_payment_method_ids = isset( $stripe_settings['stripe_upe_payment_method_order'] ) ? $stripe_settings['stripe_upe_payment_method_order'] : [];
// When switched to the new checkout experience, the UPE method order is not set. Copy the legacy order to the UPE order to persist previous settings.
if ( empty( $stripe_settings['stripe_upe_payment_method_order'] ) && ! empty( $stripe_settings['stripe_legacy_method_order'] ) ) {
$ordered_payment_method_ids = array_map(
function ( $payment_method_id ) {
if ( 'stripe' === $payment_method_id ) {
return WC_Stripe_Payment_Methods::CARD;
} elseif ( 'stripe_sepa' === $payment_method_id ) {
return WC_Stripe_Payment_Methods::SEPA_DEBIT;
}
return str_replace( 'stripe_', '', $payment_method_id );
},
$stripe_settings['stripe_legacy_method_order']
);
}
// The `stripe_upe_payment_method_order` option has the order of the UPE methods set by the user.
// This list is filtered on the basis of the capabilities set in the Stripe account data on the frontend before saving.
// If the list is empty or we have any new available payment methods, we need to update the list by including the available payment methods having capabilities.
$available_methods_with_capability = self::filter_payment_methods_with_capabilities( $gateway->get_upe_available_payment_methods(), $testmode );
$ordered_payment_method_ids_with_capability = array_filter(
$ordered_payment_method_ids,
function ( $payment_method_id ) use ( $available_methods_with_capability ) {
return in_array( $payment_method_id, $available_methods_with_capability, true );
}
);
if ( count( $ordered_payment_method_ids_with_capability ) === count( $available_methods_with_capability ) ) {
return $ordered_payment_method_ids_with_capability;
}
// Update the `stripe_upe_payment_method_order` option with the new order including rest of the available methods with capabilities.
$additional_methods = array_diff( $available_methods_with_capability, $ordered_payment_method_ids_with_capability );
$updated_order = array_merge( $ordered_payment_method_ids_with_capability, $additional_methods );
$stripe_settings['stripe_upe_payment_method_order'] = $updated_order;
self::update_main_stripe_settings( $stripe_settings );
return $updated_order;
}
/**
* Returns the list of payment methods that have capabilities set in the Stripe account data.
*
* @param string[] $payment_method_ids Payment method ids to filter by capabilities.
* @param bool $testmode Whether stripe is in test mode.
* @return string[]
*/
public static function filter_payment_methods_with_capabilities( $payment_method_ids, $testmode ) {
$account = WC_Stripe::get_instance()->account;
$data = $account->get_cached_account_data();
// return empty array if capabilities are not set.
if ( empty( $data ) || ! isset( $data['capabilities'] ) ) {
return [];
}
// Return all payment methods if in test mode.
if ( $testmode ) {
return $payment_method_ids;
}
$payment_method_ids_with_capability = [];
foreach ( $payment_method_ids as $payment_method_id ) {
$key = self::get_payment_method_capability_id( $payment_method_id );
// Check if the payment method has capabilities set in the account data.
// Generally the key is the payment method id appended with '_payments' (i.e. 'card_payments', 'sepa_debit_payments', 'klarna_payments').
// In some cases, the Stripe account might have the legacy key set. For example, for Klarna, the legacy key is 'klarna'.
// For card, the legacy key is 'legacy_payments'.
$has_capability = isset( $data['capabilities'][ $key ] ) || isset( $data['capabilities'][ $payment_method_id ] ) || ( WC_Stripe_Payment_Methods::CARD === $payment_method_id && isset( $data['capabilities']['legacy_payments'] ) );
if ( $has_capability ) {
$payment_method_ids_with_capability[] = $payment_method_id;
}
}
return $payment_method_ids_with_capability;
}
/**
* Returns the list of enabled payment methods for the settings page when UPE is enabled.
*
* @param WC_Stripe_Payment_Gateway $gateway Stripe payment gateway.
* @return string[]
*/
public static function get_upe_settings_enabled_payment_method_ids( $gateway ) {
$enabled_gateways = $gateway->get_upe_enabled_payment_method_ids();
return $enabled_gateways;
}
/**
* Reorders the list of available payment gateways in 'woocommerce_gateway_order' option to include the Stripe methods
* in the order merchants have chosen in the settings.
*
* @param array $ordered_payment_method_ids Ordered Stripe payment method list.
*/
public static function add_stripe_methods_in_woocommerce_gateway_order( $ordered_payment_method_ids = [] ) {
// If the ordered payment method ids are not passed, get them from the relevant settings.
if ( empty( $ordered_payment_method_ids ) ) {
$is_upe_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled();
$stripe_settings = self::get_stripe_settings();
if ( $is_upe_enabled ) {
$ordered_payment_method_ids = $stripe_settings['stripe_upe_payment_method_order'] ?? [];
} else {
$ordered_payment_method_ids = $stripe_settings['stripe_legacy_method_order'] ?? [];
}
if ( empty( $ordered_payment_method_ids ) ) {
return;
}
}
$gateway_order = get_option( 'woocommerce_gateway_order', [] );
asort( $gateway_order );
$ordered_available_stripe_methods = [];
// Map the Stripe payment method list to the right format to save in the 'woocommerce_gateway_order' option.
foreach ( $ordered_payment_method_ids as $payment_method_id ) {
$gateway_id = 0 === strpos( $payment_method_id, 'stripe' ) ? $payment_method_id : 'stripe_' . $payment_method_id;
if ( WC_Stripe_Payment_Methods::CARD === $payment_method_id ) {
$gateway_id = 'stripe';
}
$ordered_available_stripe_methods[] = $gateway_id;
}
$updated_gateway_order = [];
$index = 0;
$stripe_gateways_added = false;
foreach ( array_keys( $gateway_order ) as $gateway ) {
if ( 0 === strpos( $gateway, 'stripe_' ) ) {
continue; // Skip the other stripe gateways. We'll add all Stripe methods back in the right order.
} elseif ( 'stripe' === $gateway ) {
// When the main Stripe gateway is found in the option, add all the Stripe methods in the right order starting from this index.
foreach ( $ordered_available_stripe_methods as $ordered_available_stripe_method ) {
$updated_gateway_order[ $ordered_available_stripe_method ] = (string) $index++;
}
$stripe_gateways_added = true;
} else {
// Add the rest of the gateways.
$updated_gateway_order[ $gateway ] = (string) $index++;
}
}
// Stripe may not initially be in the gateway order options even when enabled --
// we ensure it's added here.
if ( ! $stripe_gateways_added ) {
foreach ( $ordered_available_stripe_methods as $ordered_available_stripe_method ) {
$updated_gateway_order[ $ordered_available_stripe_method ] = (string) $index++;
}
}
update_option( 'woocommerce_gateway_order', $updated_gateway_order );
}
/**
* Checks if WC version is less than passed in version.
*
* @since 4.1.11
* @param string $version Version to check against.
* @return bool
*/
public static function is_wc_lt( $version ) {
return version_compare( WC_VERSION, $version, '<' );
}
/**
* Gets the webhook URL for Stripe triggers. Used mainly for
* asyncronous redirect payment methods in which statuses are
* not immediately chargeable.
*
* @since 4.0.0
* @version 4.0.0
* @return string
*/
public static function get_webhook_url() {
return wp_sanitize_redirect(
esc_url_raw(
add_query_arg( 'wc-api', 'wc_stripe', trailingslashit( get_home_url() ) )
)
);
}
/**
* Gets the order by Stripe source ID.
*
* @since 4.0.0
* @version 4.0.0
* @param string $source_id
*/
public static function get_order_by_source_id( $source_id ) {
global $wpdb;
if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) {
$orders = wc_get_orders(
[
'limit' => 1,
'meta_query' => [
[
'key' => '_stripe_source_id',
'value' => $source_id,
],
],
]
);
$order_id = current( $orders ) ? current( $orders )->get_id() : false;
} else {
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $source_id, '_stripe_source_id' ) );
}
if ( ! empty( $order_id ) ) {
return wc_get_order( $order_id );
}
return false;
}
/**
* Gets the order by Stripe charge ID.
*
* @since 4.0.0
* @since 4.1.16 Return false if charge_id is empty.
* @param string $charge_id
*/
public static function get_order_by_charge_id( $charge_id ) {
global $wpdb;
if ( empty( $charge_id ) ) {
return false;
}
if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) {
$orders = wc_get_orders(
[
'transaction_id' => $charge_id,
'limit' => 1,
]
);
$order_id = current( $orders ) ? current( $orders )->get_id() : false;
} else {
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $charge_id, '_transaction_id' ) );
}
if ( ! empty( $order_id ) ) {
return wc_get_order( $order_id );
}
return false;
}
/**
* Gets the order by Stripe refund ID.
*
* @since 7.5.0
* @param string $refund_id
*/
public static function get_order_by_refund_id( $refund_id ) {
global $wpdb;
if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) {
$orders = wc_get_orders(
[
'limit' => 1,
'meta_query' => [
[
'key' => '_stripe_refund_id',
'value' => $refund_id,
],
],
]
);
$order_id = current( $orders ) ? current( $orders )->get_id() : false;
} else {
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $refund_id, '_stripe_refund_id' ) );
}
if ( ! empty( $order_id ) ) {
return wc_get_order( $order_id );
}
return false;
}
/**
* Gets the order by Stripe PaymentIntent ID.
*
* @since 4.2
* @param string $intent_id The ID of the intent.
* @return WC_Order|bool Either an order or false when not found.
*/
public static function get_order_by_intent_id( $intent_id ) {
global $wpdb;
if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) {
$orders = wc_get_orders(
[
'limit' => 1,
'meta_query' => [
[
'key' => '_stripe_intent_id',
'value' => $intent_id,
],
],
]
);
$order_id = current( $orders ) ? current( $orders )->get_id() : false;
} else {
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_intent_id' ) );
}
if ( ! empty( $order_id ) ) {
$order = wc_get_order( $order_id );
}
if ( ! empty( $order ) && $order->get_status() !== 'trash' ) {
return $order;
}
return false;
}
/**
* Gets the order by Stripe SetupIntent ID.