blob: e9a97aca3fac785d27a05eff5ab5a1ac3a92a4b2 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4}
5
6/**
7 * WC_Stripe_Account class.
8 *
9 * Communicates with Stripe API.
10 */
11class WC_Stripe_Account {
12
13 const LIVE_ACCOUNT_OPTION = 'wcstripe_account_data_live';
14 const TEST_ACCOUNT_OPTION = 'wcstripe_account_data_test';
15
16 const STATUS_COMPLETE = 'complete';
17 const STATUS_NO_ACCOUNT = 'NOACCOUNT';
18 const STATUS_RESTRICTED_SOON = 'restricted_soon';
19 const STATUS_RESTRICTED = 'restricted';
20
21 /**
22 * The Stripe connect instance.
23 *
24 * @var WC_Stripe_Connect
25 */
26 private $connect;
27
28 /**
29 * The Stripe API class to access the static method.
30 *
31 * @var WC_Stripe_API
32 */
33 private $stripe_api;
34
35 /**
36 * Constructor
37 *
38 * @param WC_Stripe_Connect $connect Stripe connect
39 * @param $stripe_api Stripe API class
40 */
41 public function __construct( WC_Stripe_Connect $connect, $stripe_api ) {
42 $this->connect = $connect;
43 $this->stripe_api = $stripe_api;
44 }
45
46 /**
47 * Gets and caches the data for the account connected to this site.
48 *
49 * @return array Account data or empty if failed to retrieve account data.
50 */
51 public function get_cached_account_data() {
52 if ( ! $this->connect->is_connected() ) {
53 return [];
54 }
55
56 $account = $this->read_account_from_cache();
57
58 if ( ! empty( $account ) ) {
59 return $account;
60 }
61
62 return $this->cache_account();
63 }
64
65 /**
66 * Read the account from the WP option we cache it in.
67 *
68 * @return array empty when no data found in transient, otherwise returns cached data
69 */
70 private function read_account_from_cache() {
71 $account_cache = json_decode( wp_json_encode( get_transient( $this->get_transient_key() ) ), true );
72
73 return false === $account_cache ? [] : $account_cache;
74 }
75
76 /**
77 * Caches account data for a period of time.
78 */
79 private function cache_account() {
80 $expiration = 2 * HOUR_IN_SECONDS;
81
82 // need call_user_func() as ( $this->stripe_api )::retrieve this syntax is not supported in php < 5.2
83 $account = call_user_func( [ $this->stripe_api, 'retrieve' ], 'account' );
84
85 if ( is_wp_error( $account ) || isset( $account->error->message ) ) {
86 return [];
87 }
88
89 // Add the account data and mode to the array we're caching.
90 $account_cache = $account;
91
92 // Create or update the account option cache.
93 set_transient( $this->get_transient_key(), $account_cache, $expiration );
94
95 return json_decode( wp_json_encode( $account ), true );
96 }
97
98 /**
99 * Checks Stripe connection mode if it is test mode or live mode
100 *
101 * @return string Transient key of test mode when testmode is enabled, otherwise returns the key of live mode.
102 */
103 private function get_transient_key() {
104 $settings_options = get_option( 'woocommerce_stripe_settings', [] );
105 $key = isset( $settings_options['testmode'] ) && 'yes' === $settings_options['testmode'] ? self::TEST_ACCOUNT_OPTION : self::LIVE_ACCOUNT_OPTION;
106
107 return $key;
108 }
109
110 /**
111 * Wipes the account data option.
112 */
113 public function clear_cache() {
114 delete_transient( self::LIVE_ACCOUNT_OPTION );
115 delete_transient( self::TEST_ACCOUNT_OPTION );
116 }
117
118 /**
119 * Indicates whether the account has any pending requirements that could cause the account to be restricted.
120 *
121 * @return bool True if account has pending restrictions, false otherwise.
122 */
123 public function has_pending_requirements() {
124 $requirements = $this->get_cached_account_data()['requirements'] ?? [];
125
126 if ( empty( $requirements ) ) {
127 return false;
128 }
129
130 $currently_due = $requirements['currently_due'] ?? [];
131 $past_due = $requirements['past_due'] ?? [];
132 $eventually_due = $requirements['eventually_due'] ?? [];
133
134 return (
135 ! empty( $currently_due ) ||
136 ! empty( $past_due ) ||
137 ! empty( $eventually_due )
138 );
139 }
140
141 /**
142 * Indicates whether the account has any overdue requirements that could cause the account to be restricted.
143 *
144 * @return bool True if account has overdue restrictions, false otherwise.
145 */
146 public function has_overdue_requirements() {
147 $requirements = $this->get_cached_account_data()['requirements'] ?? [];
148 return ! empty( $requirements['past_due'] );
149 }
150
151 /**
152 * Returns the account's Stripe status (completed, restricted_soon, restricted).
153 *
154 * @return string The account's status.
155 */
156 public function get_account_status() {
157 $account = $this->get_cached_account_data();
158 if ( empty( $account ) ) {
159 return self::STATUS_NO_ACCOUNT;
160 }
161
162 $requirements = $account['requirements'] ?? [];
163 if ( empty( $requirements ) ) {
164 return self::STATUS_COMPLETE;
165 }
166
167 if ( isset( $requirements['disabled_reason'] ) && is_string( $requirements['disabled_reason'] ) ) {
168 // If an account has been rejected, then disabled_reason will have a value like "rejected.<reason>"
169 if ( strpos( $requirements['disabled_reason'], 'rejected' ) === 0 ) {
170 return $requirements['disabled_reason'];
171 }
172 // If disabled_reason is not empty, then the account has been restricted.
173 if ( ! empty( $requirements['disabled_reason'] ) ) {
174 return self::STATUS_RESTRICTED;
175 }
176 }
177 // Should be covered by the non-empty disabled_reason, but past due requirements also restrict the account.
178 if ( isset( $requirements['past_due'] ) && ! empty( $requirements['past_due'] ) ) {
179 return self::STATUS_RESTRICTED;
180 }
181 // Any other pending requirments indicate restricted soon.
182 if ( $this->has_pending_requirements() ) {
183 return self::STATUS_RESTRICTED_SOON;
184 }
185
186 return self::STATUS_COMPLETE;
187 }
188
189 /**
190 * Returns the Stripe's account supported currencies.
191 *
192 * @return string[] Supported store currencies.
193 */
194 public function get_supported_store_currencies(): array {
195 $account = $this->get_cached_account_data();
196 if ( ! isset( $account['external_accounts']['data'] ) ) {
197 return [ $account['default_currency'] ?? get_woocommerce_currency() ];
198 }
199
200 $currencies = array_filter( array_column( $account['external_accounts']['data'], 'currency' ) );
201 return array_values( array_unique( $currencies ) );
202 }
203}