-
Notifications
You must be signed in to change notification settings - Fork 69
/
class-wc-payments-account.php
2496 lines (2198 loc) · 91.4 KB
/
class-wc-payments-account.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
/**
* Class WC_Payments_Account
*
* @package WooCommerce\Payments
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
use WCPay\Constants\Country_Code;
use WCPay\Constants\Currency_Code;
use WCPay\Core\Server\Request\Get_Account;
use WCPay\Core\Server\Request;
use WCPay\Core\Server\Request\Update_Account;
use WCPay\Exceptions\API_Exception;
use WCPay\Logger;
use WCPay\Database_Cache;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyAccountInterface;
/**
* Class handling any account connection functionality
*/
class WC_Payments_Account implements MultiCurrencyAccountInterface {
// ACCOUNT_OPTION is only used in the supporting dev tools plugin, it can be removed once everyone has upgraded.
const ACCOUNT_OPTION = 'wcpay_account_data';
const ONBOARDING_DISABLED_TRANSIENT = 'wcpay_on_boarding_disabled';
const ONBOARDING_STARTED_TRANSIENT = 'wcpay_on_boarding_started';
const ONBOARDING_STATE_TRANSIENT = 'wcpay_stripe_onboarding_state';
const EMBEDDED_KYC_IN_PROGRESS_OPTION = 'wcpay_onboarding_embedded_kyc_in_progress';
const ERROR_MESSAGE_TRANSIENT = 'wcpay_error_message';
const INSTANT_DEPOSITS_REMINDER_ACTION = 'wcpay_instant_deposit_reminder';
const TRACKS_EVENT_ACCOUNT_CONNECT_START = 'wcpay_account_connect_start';
const TRACKS_EVENT_ACCOUNT_CONNECT_WPCOM_CONNECTION_START = 'wcpay_account_connect_wpcom_connection_start';
const TRACKS_EVENT_ACCOUNT_CONNECT_WPCOM_CONNECTION_SUCCESS = 'wcpay_account_connect_wpcom_connection_success';
const TRACKS_EVENT_ACCOUNT_CONNECT_WPCOM_CONNECTION_FAILURE = 'wcpay_account_connect_wpcom_connection_failure';
const TRACKS_EVENT_ACCOUNT_CONNECT_FINISHED = 'wcpay_account_connect_finished';
const TRACKS_EVENT_KYC_REMINDER_MERCHANT_RETURNED = 'wcpay_kyc_reminder_merchant_returned';
/**
* Client for making requests to the WooCommerce Payments API
*
* @var WC_Payments_API_Client
*/
private $payments_api_client;
/**
* Cache util for managing the account data
*
* @var Database_Cache
*/
private $database_cache;
/**
* Action scheduler service
*
* @var WC_Payments_Action_Scheduler_Service
*/
private $action_scheduler_service;
/**
* WC_Payments_Onboarding_Service instance for working with onboarding business logic
*
* @var WC_Payments_Onboarding_Service
*/
private $onboarding_service;
/**
* WC_Payments_Redirect_Service instance for handling redirects business logic
*
* @var WC_Payments_Redirect_Service
*/
private $redirect_service;
/**
* Class constructor
*
* @param WC_Payments_API_Client $payments_api_client Payments API client.
* @param Database_Cache $database_cache Database cache util.
* @param WC_Payments_Action_Scheduler_Service $action_scheduler_service Action scheduler service.
* @param WC_Payments_Onboarding_Service $onboarding_service Onboarding service.
* @param WC_Payments_Redirect_Service $redirect_service Redirect service.
*/
public function __construct(
WC_Payments_API_Client $payments_api_client,
Database_Cache $database_cache,
WC_Payments_Action_Scheduler_Service $action_scheduler_service,
WC_Payments_Onboarding_Service $onboarding_service,
WC_Payments_Redirect_Service $redirect_service
) {
$this->payments_api_client = $payments_api_client;
$this->database_cache = $database_cache;
$this->action_scheduler_service = $action_scheduler_service;
$this->onboarding_service = $onboarding_service;
$this->redirect_service = $redirect_service;
}
/**
* Initialise class hooks.
*
* @return void
*/
public function init_hooks() {
// Add admin init hooks.
// Our onboarding handling comes first.
add_action( 'admin_init', [ $this, 'maybe_handle_onboarding' ] );
add_action( 'admin_init', [ $this, 'maybe_activate_woopay' ] );
// Second, handle redirections based on context.
add_action( 'admin_init', [ $this, 'maybe_redirect_after_plugin_activation' ], 11 ); // Run this after the WC setup wizard and onboarding redirection logic.
add_action( 'admin_init', [ $this, 'maybe_redirect_by_get_param' ], 12 ); // Run this after the redirect to onboarding logic.
// Third, handle page redirections.
add_action( 'admin_init', [ $this, 'maybe_redirect_from_settings_page' ], 15 );
add_action( 'admin_init', [ $this, 'maybe_redirect_from_onboarding_wizard_page' ], 15 );
add_action( 'admin_init', [ $this, 'maybe_redirect_from_connect_page' ], 15 );
add_action( 'admin_init', [ $this, 'maybe_redirect_from_overview_page' ], 15 );
// Add handlers for inbox notes and reminders.
add_action( 'woocommerce_payments_account_refreshed', [ $this, 'handle_instant_deposits_inbox_note' ] );
add_action( 'woocommerce_payments_account_refreshed', [ $this, 'handle_loan_approved_inbox_note' ] );
add_action( self::INSTANT_DEPOSITS_REMINDER_ACTION, [ $this, 'handle_instant_deposits_inbox_reminder' ] );
// Add all other hooks.
add_filter( 'allowed_redirect_hosts', [ $this, 'allowed_redirect_hosts' ] );
add_action( 'jetpack_site_registered', [ $this, 'clear_cache' ] );
add_action( 'updated_option', [ $this, 'possibly_update_wcpay_account_locale' ], 10, 3 );
add_action( 'woocommerce_woocommerce_payments_updated', [ $this, 'clear_cache' ] );
}
/**
* Wipes the account data option, forcing to re-fetch the account status from WP.com.
*/
public function clear_cache() {
$this->database_cache->delete( Database_Cache::ACCOUNT_KEY );
}
/**
* Return connected account ID
*
* @return string|null Account ID if connected, null if not connected or on error
*/
public function get_stripe_account_id() {
$account = $this->get_cached_account_data();
if ( empty( $account ) ) {
return null;
}
return $account['account_id'];
}
/**
* Gets public key for the connected account
*
* @param bool $is_test true to get the test key, false otherwise.
*
* @return string|null public key if connected, null if not connected.
*/
public function get_publishable_key( $is_test ) {
$account = $this->get_cached_account_data();
if ( empty( $account ) ) {
return null;
}
if ( $is_test ) {
return $account['test_publishable_key'];
}
return $account['live_publishable_key'];
}
/**
* Checks if the account is connected to the payment provider.
* Note: This method is a proxy for `is_stripe_connected` for the MultiCurrencyAccountInterface.
*
* @param bool $on_error Value to return on server error, defaults to false.
*
* @return bool True if the account is connected, false otherwise, $on_error on error.
*/
public function is_provider_connected( bool $on_error = false ): bool {
return $this->is_stripe_connected( $on_error );
}
/**
* Determine if the store has a working Jetpack connection.
*
* @return bool Whether the Jetpack connection is established and working or not.
*/
public function has_working_jetpack_connection(): bool {
return $this->payments_api_client->is_server_connected() && $this->payments_api_client->has_server_connection_owner();
}
/**
* Check if there is meaningful data in the WooPayments account cache.
*
* It bypasses WPCOM/Jetpack connection check, the cache expiry check and only checks if the account_id is present.
*
* @return boolean Whether there is account data.
*/
public function has_account_data(): bool {
$account_data = $this->database_cache->get( Database_Cache::ACCOUNT_KEY );
if ( ! empty( $account_data['account_id'] ) ) {
return true;
}
return false;
}
/**
* Checks if the account is connected, assumes the value of $on_error on server error.
*
* @param bool $on_error Value to return on server error, defaults to false.
*
* @return bool True if the account is connected, false otherwise, $on_error on error.
*/
public function is_stripe_connected( bool $on_error = false ): bool {
try {
return $this->try_is_stripe_connected();
} catch ( Exception $e ) {
return $on_error;
}
}
/**
* Checks if the account is connected, throws on server error.
*
* @return bool True if the account is connected, false otherwise.
* @throws Exception Throws exception when unable to detect connection status.
*/
public function try_is_stripe_connected(): bool {
$account = $this->get_cached_account_data();
if ( false === $account ) {
throw new Exception( esc_html__( 'Failed to detect connection status', 'woocommerce-payments' ) );
}
// The empty array indicates that account is not connected yet.
return [] !== $account;
}
/**
* Checks if the account is valid.
*
* This means:
* - it's connected (i.e. we have account data)
* - has submitted details (i.e. is not partially onboarded)
* - has valid card_payments capability status (requested, pending_verification, active and other valid ones).
*
* Card_payments capability is crucial for account to function properly. If it is unrequested, we shouldn't show
* any other options for the merchants since it'll lead to various errors.
*
* @see https://github.com/Automattic/woocommerce-payments/issues/5275
*
* @return bool True if the account is a valid Stripe account, false otherwise.
*/
public function is_stripe_account_valid(): bool {
$account = $this->get_cached_account_data();
// The account is disconnected or we failed to get the account data.
if ( empty( $account ) ) {
return false;
}
// The account is partially onboarded.
if ( empty( $account['details_submitted'] ) ) {
return false;
}
// The account doesn't have the minimum required capabilities.
if ( ! isset( $account['capabilities']['card_payments'] )
|| 'unrequested' === $account['capabilities']['card_payments'] ) {
return false;
}
// The account is valid.
return true;
}
/**
* Checks if the account has been rejected, assumes the value of false on any account retrieval error.
* Returns false if the account is not connected.
*
* @return bool True if the account is connected and rejected, false otherwise or on error.
*/
public function is_account_rejected(): bool {
if ( ! $this->is_stripe_connected() ) {
return false;
}
$account = $this->get_cached_account_data();
return strpos( $account['status'] ?? '', 'rejected' ) === 0;
}
/**
* Checks if the account is under review, assumes the value of false on any account retrieval error.
* Returns false if the account is not connected.
*
* @return bool
*/
public function is_account_under_review(): bool {
if ( ! $this->is_stripe_connected() ) {
return false;
}
$account = $this->get_cached_account_data();
return 'under_review' === $account['status'];
}
/**
* Checks if the account "details_submitted" flag is true.
* This is a proxy for telling if an account has completed onboarding.
* If the "details_submitted" flag is false, it means that the account has not
* yet finished the initial KYC.
*
* @return boolean True if the account is connected and details are not submitted, false otherwise.
*/
public function is_details_submitted(): bool {
$account = $this->get_cached_account_data();
$details_submitted = $account['details_submitted'] ?? false;
return true === $details_submitted;
}
/**
* Gets the account status data for rendering on the settings page.
*
* @return array An array containing the status data, or [ 'error' => true ] on error or no connected account.
*/
public function get_account_status_data(): array {
$account = $this->get_cached_account_data();
if ( empty( $account ) ) {
// empty means no account. This data should not be used when the account is not connected.
return [
'error' => true,
];
}
if ( ! isset( $account['status'], $account['payments_enabled'] ) ) {
// return an error if any of the account data is missing.
return [
'error' => true,
];
}
return [
'email' => $account['email'] ?? '',
'country' => $account['country'] ?? Country_Code::UNITED_STATES,
'status' => $account['status'],
'created' => $account['created'] ?? '',
'testDrive' => $account['is_test_drive'] ?? false,
'paymentsEnabled' => $account['payments_enabled'],
'detailsSubmitted' => $account['details_submitted'] ?? true,
'deposits' => $account['deposits'] ?? [],
'currentDeadline' => $account['current_deadline'] ?? false,
'pastDue' => $account['has_overdue_requirements'] ?? false,
// Test-drive accounts don't have access to the Stripe dashboard.
'accountLink' => empty( $account['is_test_drive'] ) ? $this->get_login_url() : false,
'hasSubmittedVatData' => $account['has_submitted_vat_data'] ?? false,
'requirements' => [
'errors' => $account['requirements']['errors'] ?? [],
],
'progressiveOnboarding' => [
'isEnabled' => $account['progressive_onboarding']['is_enabled'] ?? false,
'isComplete' => $account['progressive_onboarding']['is_complete'] ?? false,
'tpv' => (int) ( $account['progressive_onboarding']['tpv'] ?? 0 ),
'firstTransactionDate' => $account['progressive_onboarding']['first_transaction_date'] ?? null,
],
'fraudProtection' => [
'declineOnAVSFailure' => $account['fraud_mitigation_settings']['avs_check_enabled'] ?? null,
'declineOnCVCFailure' => $account['fraud_mitigation_settings']['cvc_check_enabled'] ?? null,
],
];
}
/**
* Gets the account statement descriptor for rendering on the settings page.
*
* @return string Account statement descriptor.
*/
public function get_statement_descriptor(): string {
$account = $this->get_cached_account_data();
return ! empty( $account ) && isset( $account['statement_descriptor'] ) ? $account['statement_descriptor'] : '';
}
/**
* Gets the account statement descriptor for rendering on the settings page.
*
* @return string Account statement descriptor.
*/
public function get_statement_descriptor_kanji(): string {
$account = $this->get_cached_account_data();
return ! empty( $account ) && isset( $account['statement_descriptor_kanji'] ) ? $account['statement_descriptor_kanji'] : '';
}
/**
* Gets the account statement descriptor for rendering on the settings page.
*
* @return string Account statement descriptor.
*/
public function get_statement_descriptor_kana(): string {
$account = $this->get_cached_account_data();
return ! empty( $account ) && isset( $account['statement_descriptor_kana'] ) ? $account['statement_descriptor_kana'] : '';
}
/**
* Gets the business name.
*
* @return string Business profile name.
*/
public function get_business_name(): string {
$account = $this->get_cached_account_data();
return isset( $account['business_profile']['name'] ) ? $account['business_profile']['name'] : '';
}
/**
* Gets the business url.
*
* @return string Business profile url.
*/
public function get_business_url(): string {
$account = $this->get_cached_account_data();
return isset( $account['business_profile']['url'] ) ? $account['business_profile']['url'] : '';
}
/**
* Gets the business support address.
*
* @return array Business profile support address.
*/
public function get_business_support_address(): array {
$account = $this->get_cached_account_data();
return isset( $account['business_profile']['support_address'] ) ? $account['business_profile']['support_address'] : [];
}
/**
* Gets the business support email.
*
* @return string Business profile support email.
*/
public function get_business_support_email(): string {
$account = $this->get_cached_account_data();
return isset( $account['business_profile']['support_email'] ) ? $account['business_profile']['support_email'] : '';
}
/**
* Gets the business support phone.
*
* @return string Business profile support phone.
*/
public function get_business_support_phone(): string {
$account = $this->get_cached_account_data();
return isset( $account['business_profile']['support_phone'] ) ? $account['business_profile']['support_phone'] : '';
}
/**
* Gets the branding logo.
*
* @return string branding logo.
*/
public function get_branding_logo(): string {
$account = $this->get_cached_account_data();
return isset( $account['branding']['logo'] ) ? $account['branding']['logo'] : '';
}
/**
* Gets the branding icon.
*
* @return string branding icon.
*/
public function get_branding_icon(): string {
$account = $this->get_cached_account_data();
return isset( $account['branding']['icon'] ) ? $account['branding']['icon'] : '';
}
/**
* Gets the branding primary color.
*
* @return string branding primary color.
*/
public function get_branding_primary_color(): string {
$account = $this->get_cached_account_data();
return isset( $account['branding']['primary_color'] ) ? $account['branding']['primary_color'] : '';
}
/**
* Gets the branding secondary color.
*
* @return string branding secondary color.
*/
public function get_branding_secondary_color(): string {
$account = $this->get_cached_account_data();
return isset( $account['branding']['secondary_color'] ) ? $account['branding']['secondary_color'] : '';
}
/**
* Gets the deposit schedule interval.
*
* @return string interval e.g. weekly, monthly.
*/
public function get_deposit_schedule_interval(): string {
$account = $this->get_cached_account_data();
return $account['deposits']['interval'] ?? '';
}
/**
* Gets the deposit schedule weekly anchor.
*
* @return string weekly anchor e.g. monday, tuesday.
*/
public function get_deposit_schedule_weekly_anchor(): string {
$account = $this->get_cached_account_data();
return $account['deposits']['weekly_anchor'] ?? '';
}
/**
* Gets the deposit schedule monthly anchor.
*
* @return int|null monthly anchor e.g. 1, 2.
*/
public function get_deposit_schedule_monthly_anchor() {
$account = $this->get_cached_account_data();
return ! empty( $account['deposits']['monthly_anchor'] ) ? $account['deposits']['monthly_anchor'] : null;
}
/**
* Gets the number of days payments are delayed for.
*
* @return int|null e.g. 2, 7.
*/
public function get_deposit_delay_days() {
$account = $this->get_cached_account_data();
return $account['deposits']['delay_days'] ?? null;
}
/**
* Gets the deposit status
*
* @return string e.g. disabled, blocked, enabled.
*/
public function get_deposit_status(): string {
$account = $this->get_cached_account_data();
return $account['deposits']['status'] ?? '';
}
/**
* Gets the deposit restrictions
*
* @return string e.g. not_blocked, blocked, schedule locked.
*/
public function get_deposit_restrictions(): string {
$account = $this->get_cached_account_data();
return $account['deposits']['restrictions'] ?? '';
}
/**
* Gets whether the account has completed the deposit waiting period.
*
* @return bool
*/
public function get_deposit_completed_waiting_period(): bool {
$account = $this->get_cached_account_data();
return $account['deposits']['completed_waiting_period'] ?? false;
}
/**
* Get card present eligible flag account
*
* @return bool
*/
public function is_card_present_eligible(): bool {
$account = $this->get_cached_account_data();
return $account['card_present_eligible'] ?? false;
}
/**
* Get has account connected readers flag
*
* @return bool
*/
public function has_card_readers_available(): bool {
$account = $this->get_cached_account_data();
return $account['has_card_readers_available'] ?? false;
}
/**
* Gets the current account fees for rendering on the settings page.
*
* @return array Fees.
*/
public function get_fees(): array {
$account = $this->get_cached_account_data();
return ! empty( $account ) && isset( $account['fees'] ) ? $account['fees'] : [];
}
/**
* Get the progressive onboarding details needed on the frontend.
*
* @return array Progressive Onboarding details.
*/
public function get_progressive_onboarding_details(): array {
$account = $this->get_cached_account_data();
return [
'isEnabled' => $account['progressive_onboarding']['is_enabled'] ?? false,
'isComplete' => $account['progressive_onboarding']['is_complete'] ?? false,
'isNewFlowEnabled' => WC_Payments_Utils::should_use_new_onboarding_flow(),
'isEligibilityModalDismissed' => get_option( WC_Payments_Onboarding_Service::ONBOARDING_ELIGIBILITY_MODAL_OPTION, false ),
];
}
/**
* Determine whether Progressive Onboarding is in progress for this account.
*
* @return boolean
*/
public function is_progressive_onboarding_in_progress(): bool {
$account = $this->get_cached_account_data();
return ( $account['progressive_onboarding']['is_enabled'] ?? false )
&& ! ( $account['progressive_onboarding']['is_complete'] ?? false );
}
/**
* Gets the current account loan data for rendering on the settings pages.
*
* @return array loan data.
*/
public function get_capital() {
$account = $this->get_cached_account_data();
return ! empty( $account ) && isset( $account['capital'] ) && ! empty( $account['capital'] ) ? $account['capital'] : [
'loans' => [],
'has_active_loan' => false,
'has_previous_loans' => false,
];
}
/**
* Gets the current account email for rendering on the settings page.
*
* @return string Email.
*/
public function get_account_email(): string {
$account = $this->get_cached_account_data();
return $account['email'] ?? '';
}
/**
* Gets the customer currencies supported by Stripe available for the account.
*
* @return array Currencies.
*/
public function get_account_customer_supported_currencies(): array {
$account = $this->get_cached_account_data();
return ! empty( $account ) && isset( $account['customer_currencies']['supported'] ) ? $account['customer_currencies']['supported'] : [];
}
/**
* List of countries enabled for Stripe platform account. See also this URL:
* https://woocommerce.com/document/woopayments/compatibility/countries/#supported-countries
*
* @return array
*/
public function get_supported_countries(): array {
// This is a wrapper function because of the MultiCurrencyAccountInterface.
return WC_Payments_Utils::supported_countries();
}
/**
* Gets the account live mode value.
*
* @return bool|null Account is_live value.
*/
public function get_is_live() {
$account = $this->get_cached_account_data();
return ! empty( $account ) && isset( $account['is_live'] ) ? $account['is_live'] : null;
}
/**
* Checks if the request contains specific get param to redirect further, and redirects to the relevant link if so.
*
* Only admins are be able to perform this action. The redirect doesn't happen if the request is an AJAX request.
*/
public function maybe_redirect_by_get_param() {
// Safety check to prevent non-admin users to be redirected to the view offer page.
if ( wp_doing_ajax() || ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
// This is an automatic redirection page, used to authenticate users that come from the KYC reminder email. For this reason
// we're not using a nonce. The GET parameter accessed here is just to indicate that we should process the redirection.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['wcpay-connect-redirect'] ) ) {
$params = [
'page' => 'wc-admin',
'path' => '/payments/connect',
];
// We're not in the connect page, don't redirect.
if ( count( $params ) !== count( array_intersect_assoc( $_GET, $params ) ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
return;
}
$redirect_param = sanitize_text_field( wp_unslash( $_GET['wcpay-connect-redirect'] ) );
// Let's record in Tracks merchants returning via the KYC reminder email.
if ( 'initial' === $redirect_param ) {
$offset = 1;
$description = 'initial';
} elseif ( 'second' === $redirect_param ) {
$offset = 3;
$description = 'second';
} else {
$follow_number = in_array( $redirect_param, [ '1', '2', '3', '4' ], true ) ? $redirect_param : '0';
// offset is recorded in days, $follow_number maps to the week number.
$offset = (int) $follow_number * 7;
$description = 'weekly-' . $follow_number;
}
$track_props = [
'offset' => $offset,
'description' => $description,
];
$this->tracks_event( self::TRACKS_EVENT_KYC_REMINDER_MERCHANT_RETURNED, $track_props );
$this->redirect_service->redirect_to_wcpay_connect( 'WCPAY_KYC_REMINDER' );
}
// This is an automatic redirection page, used to authenticate users that come from the capitcal offer email. For this reason
// we're not using a nonce. The GET parameter accessed here is just to indicate that we should process the redirection.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['wcpay-loan-offer'] ) ) {
$this->redirect_service->redirect_to_capital_view_offer_page();
}
// This is an automatic redirection page, used to authenticate users that come from an email link. For this reason
// we're not using a nonce. The GET parameter accessed here is just to indicate that we should process the redirection.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['wcpay-link-handler'] ) ) {
// Get all request arguments to be forwarded and remove the link handler identifier.
$args = $_GET;
unset( $args['wcpay-link-handler'] );
$this->redirect_service->redirect_to_account_link( $args );
}
}
/**
* Proxy method that's called in other classes that have access to account (not redirect_service)
* to immediately redirect to the main "Welcome to WooPayments" onboarding page.
* Note that this function immediately ends the execution.
*
* @param string|null $error_message Optional error message to show in a notice.
*/
public function redirect_to_onboarding_welcome_page( $error_message = null ) {
$this->redirect_service->redirect_to_connect_page( $error_message );
}
/**
* Checks if everything is in working order and redirects to the connect page if not.
*
* @return bool True if the redirection happened.
*/
public function maybe_redirect_after_plugin_activation(): bool {
if ( wp_doing_ajax() || ! current_user_can( 'manage_woocommerce' ) ) {
return false;
}
$is_on_settings_page = WC_Payments_Admin_Settings::is_current_page_settings();
$should_redirect_to_onboarding = (bool) get_option( 'wcpay_should_redirect_to_onboarding', false );
if (
// If not loading the settings page...
! $is_on_settings_page
// ...and we have redirected before.
&& ! $should_redirect_to_onboarding
) {
// Do not attempt to redirect again.
return false;
}
if ( $should_redirect_to_onboarding ) {
// Update the option. We try to redirect once and will not attempt to redirect again.
update_option( 'wcpay_should_redirect_to_onboarding', false );
}
// If everything is in working order, don't redirect.
if ( $this->has_working_jetpack_connection() && $this->is_stripe_account_valid() ) {
return false;
}
// Redirect to Connect page.
$this->redirect_service->redirect_to_connect_page(
null,
WC_Payments_Onboarding_Service::FROM_PLUGIN_ACTIVATION,
[ 'source' => WC_Payments_Onboarding_Service::get_source() ]
);
return true;
}
/**
* Redirects WooPayments settings to the connect page when there is no account or an invalid account.
*
* Every WooPayments page except connect are already hidden, but merchants can still access
* it through WooCommerce settings.
*
* @return bool True if a redirection happened, false otherwise.
*/
public function maybe_redirect_from_settings_page(): bool {
if ( wp_doing_ajax() || ! current_user_can( 'manage_woocommerce' ) ) {
return false;
}
$params = [
'page' => 'wc-settings',
'tab' => 'checkout',
'section' => 'woocommerce_payments',
];
// We're not in the WooPayments settings page, don't redirect.
if ( count( $params ) !== count( array_intersect_assoc( $_GET, $params ) ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
return false;
}
// If everything is NOT in good working condition, redirect to Payments Connect page.
if ( ! $this->has_working_jetpack_connection() || ! $this->is_stripe_account_valid() ) {
$this->redirect_service->redirect_to_connect_page(
sprintf(
/* translators: 1: WooPayments. */
__( 'Please <b>complete your %1$s setup</b> to process transactions.', 'woocommerce-payments' ),
'WooPayments'
),
WC_Payments_Onboarding_Service::FROM_WCADMIN_PAYMENTS_SETTINGS,
[ 'source' => WC_Payments_Onboarding_Service::SOURCE_WCADMIN_SETTINGS_PAGE ]
);
return true;
}
// Everything is OK, don't redirect.
return false;
}
/**
* Redirects onboarding wizard page (payments/onboarding) to the overview page for accounts that have a valid Stripe account.
*
* Payments onboarding wizard page is already hidden for those who have a Stripe account connected,
* but merchants can still access it by clicking back in the browser tab.
*
* @return bool True if the redirection happened, false otherwise.
*/
public function maybe_redirect_from_onboarding_wizard_page(): bool {
if ( wp_doing_ajax() || ! current_user_can( 'manage_woocommerce' ) ) {
return false;
}
$params = [
'page' => 'wc-admin',
'path' => '/payments/onboarding',
];
// We're not in the onboarding wizard page, don't redirect.
if ( count( $params ) !== count( array_intersect_assoc( $_GET, $params ) ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
return false;
}
// Determine the original source from where the merchant entered the onboarding flow.
$onboarding_source = WC_Payments_Onboarding_Service::get_source();
// Prevent access to onboarding wizard if we don't have a working WPCOM/Jetpack connection.
// Redirect back to the connect page with an error message.
if ( ! $this->has_working_jetpack_connection() ) {
$referer = sanitize_text_field( wp_get_raw_referer() );
// Track unsuccessful Jetpack connection.
if ( strpos( $referer, 'wordpress.com' ) ) {
$this->tracks_event(
self::TRACKS_EVENT_ACCOUNT_CONNECT_WPCOM_CONNECTION_FAILURE,
[
'mode' => WC_Payments_Onboarding_Service::is_test_mode_enabled() ? 'test' : 'live',
// Capture the user source of the connection attempt originating page.
// This is the same source that is used to track the onboarding flow origin.
'source' => $onboarding_source,
]
);
}
$this->redirect_service->redirect_to_connect_page(
sprintf(
/* translators: %s: WooPayments */
__( 'Please connect to WordPress.com to start using %s.', 'woocommerce-payments' ),
'WooPayments'
),
WC_Payments_Onboarding_Service::FROM_ONBOARDING_WIZARD,
[ 'source' => $onboarding_source ]
);
return true;
}
// We check it here after refreshing the cache, because merchant might have clicked back in browser (after Stripe KYC).
// That will mean that no redirect from Stripe happened and user might be able to go through onboarding again if no webhook processed yet.
// That might cause issues if user selects sandbox onboarding after live one.
// Shouldn't be called with force disconnected option enabled, otherwise we'll get current account data.
if ( ! WC_Payments_Utils::force_disconnected_enabled() ) {
$this->refresh_account_data();
}
// Don't redirect merchants that have no Stripe account connected.
if ( ! $this->is_stripe_connected() ) {
return false;
}
// Merchants with an invalid Stripe account, need to go to the Stripe KYC, not our onboarding wizard.
if ( ! $this->is_stripe_account_valid() ) {
$this->redirect_service->redirect_to_connect_page(
null,
WC_Payments_Onboarding_Service::FROM_ONBOARDING_WIZARD,
[ 'source' => $onboarding_source ]
);
return true;
}
$this->redirect_service->redirect_to_overview_page( WC_Payments_Onboarding_Service::FROM_ONBOARDING_WIZARD );
return true;
}
/**
* Maybe redirects the connect page (payments/connect)
*
* We redirect to the overview page for stores that have a working Jetpack connection and a valid Stripe account.
*
* Note: Connect _page_ links are not the same as connect links.
* Connect links are used to start/re-start/continue the onboarding flow and they are independent of
* the WP dashboard page (based solely on request params).
*
* IMPORTANT: The logic should be kept in sync with the one in maybe_redirect_from_overview_page to avoid loops.
*
* @see self::maybe_redirect_from_overview_page() for the opposite redirection.
* @see self::maybe_handle_onboarding() for connect links handling.
*
* @return bool True if the redirection happened, false otherwise.
*/
public function maybe_redirect_from_connect_page(): bool {
if ( wp_doing_ajax() || ! current_user_can( 'manage_woocommerce' ) ) {
return false;
}
$params = [
'page' => 'wc-admin',
'path' => '/payments/connect',
];
// We're not on the Connect page, don't redirect.
if ( count( $params ) !== count( array_intersect_assoc( $_GET, $params ) ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
return false;
}
// There are certain cases where it is best to refresh the account data
// to be sure we are dealing with the current account state on the Connect page:
// - When the merchant is coming from the onboarding wizard it is best to refresh the account data because
// the merchant might have started the embedded Stripe KYC.
// - When the merchant is coming from the embedded KYC, definitely refresh the account data.
// The account data shouldn't be refreshed with force disconnected option enabled.
if ( ! WC_Payments_Utils::force_disconnected_enabled()
&& in_array(
WC_Payments_Onboarding_Service::get_from(),
[
WC_Payments_Onboarding_Service::FROM_ONBOARDING_WIZARD,
WC_Payments_Onboarding_Service::FROM_ONBOARDING_KYC,
],
true
) ) {
$this->refresh_account_data();
}
// If everything is in good working condition, redirect to Payments Overview page.
if ( $this->has_working_jetpack_connection() && $this->is_stripe_account_valid() ) {
$this->redirect_service->redirect_to_overview_page( WC_Payments_Onboarding_Service::FROM_CONNECT_PAGE );
return true;
}
// Determine from where the merchant was directed to the Connect page.
$from = WC_Payments_Onboarding_Service::get_from();
// If the user came from the core Payments task list item,
// we run an experiment to skip the Connect page
// and go directly to the Jetpack connection flow and/or onboarding wizard.
if ( WC_Payments_Onboarding_Service::FROM_WCADMIN_PAYMENTS_TASK === $from
&& WC_Payments_Utils::is_in_core_payments_task_onboarding_flow_treatment_mode() ) {
// We use a connect link to allow our logic to determine what comes next:
// the Jetpack connection setup and/or onboarding wizard (MOX).
$this->redirect_service->redirect_to_wcpay_connect(
// The next step should treat the merchant as coming from the Payments task list item,
// not the Connect page.
WC_Payments_Onboarding_Service::FROM_WCADMIN_PAYMENTS_TASK,
[
'source' => WC_Payments_Onboarding_Service::get_source(),
]
);
return true;