10
10
use WC_Payments_Features ;
11
11
use WC_Payments_Subscriptions_Utilities ;
12
12
use Platform_Checkout_Extension ;
13
+ use WCPay \Logger ;
14
+ use WC_Geolocation ;
15
+ use WC_Payments ;
13
16
14
17
/**
15
18
* Platform_Checkout
16
19
*/
17
20
class Platform_Checkout_Utilities {
18
21
use WC_Payments_Subscriptions_Utilities;
19
22
23
+ const AVAILABLE_COUNTRIES_KEY = 'woocommerce_woocommerce_payments_woopay_available_countries ' ;
24
+ const AVAILABLE_COUNTRIES_LAST_CHECK_KEY = 'woocommerce_woocommerce_payments_woopay_available_countries_last_check ' ;
25
+
20
26
/**
21
27
* Check various conditions to determine if we should enable platform checkout.
22
28
*
@@ -36,7 +42,7 @@ public function should_enable_platform_checkout( $gateway ) {
36
42
* @return boolean
37
43
*/
38
44
public function is_woopay_express_checkout_enabled () {
39
- return WC_Payments_Features::is_woopay_express_checkout_enabled (); // Feature flag.
45
+ return WC_Payments_Features::is_woopay_express_checkout_enabled () && $ this -> is_country_available ( WC_Payments:: get_gateway () ) ; // Feature flag.
40
46
}
41
47
42
48
/**
@@ -62,6 +68,73 @@ public function should_save_platform_customer() {
62
68
return ( isset ( $ _POST ['save_user_in_platform_checkout ' ] ) && filter_var ( wp_unslash ( $ _POST ['save_user_in_platform_checkout ' ] ), FILTER_VALIDATE_BOOLEAN ) ) || ( isset ( $ session_data ['save_user_in_platform_checkout ' ] ) && filter_var ( $ session_data ['save_user_in_platform_checkout ' ], FILTER_VALIDATE_BOOLEAN ) ); // phpcs:ignore WordPress.Security.NonceVerification
63
69
}
64
70
71
+ /**
72
+ * Get the list of WooPay available countries and cache it for 24 hours.
73
+ *
74
+ * @return array
75
+ */
76
+ public function get_woopay_available_countries () {
77
+ $ last_check = get_option ( self ::AVAILABLE_COUNTRIES_LAST_CHECK_KEY );
78
+
79
+ if ( $ last_check && gmdate ( 'Y-m-d ' ) === $ last_check ) {
80
+ $ available_countries = get_option ( self ::AVAILABLE_COUNTRIES_KEY , '["US"] ' );
81
+
82
+ return json_decode ( $ available_countries , true );
83
+ }
84
+
85
+ $ platform_checkout_host = defined ( 'PLATFORM_CHECKOUT_HOST ' ) ? PLATFORM_CHECKOUT_HOST : 'https://pay.woo.com ' ;
86
+ $ url = $ platform_checkout_host . '/wp-json/platform-checkout/v1/user/available-countries ' ;
87
+
88
+ $ args = [
89
+ 'url ' => $ url ,
90
+ 'method ' => 'GET ' ,
91
+ 'timeout ' => 30 ,
92
+ 'headers ' => [
93
+ 'Content-Type ' => 'application/json ' ,
94
+ ],
95
+ ];
96
+
97
+ /**
98
+ * Suppress psalm error from Jetpack Connection namespacing WP_Error.
99
+ *
100
+ * @psalm-suppress UndefinedDocblockClass
101
+ */
102
+ $ response = \Automattic \Jetpack \Connection \Client::remote_request ( $ args );
103
+
104
+ if ( is_wp_error ( $ response ) || ! is_array ( $ response ) || ! empty ( $ response ['code ' ] ) ) {
105
+ Logger::error ( 'HTTP_REQUEST_ERROR ' . var_export ( $ response , true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
106
+
107
+ // If there's an error, return current data and check again on the next day.
108
+ $ available_countries = get_option ( self ::AVAILABLE_COUNTRIES_KEY , '["US"] ' );
109
+ } else {
110
+ $ available_countries = wp_remote_retrieve_body ( $ response );
111
+
112
+ update_option ( self ::AVAILABLE_COUNTRIES_KEY , $ available_countries );
113
+ }
114
+
115
+ update_option ( self ::AVAILABLE_COUNTRIES_LAST_CHECK_KEY , gmdate ( 'Y-m-d ' ) );
116
+
117
+ return json_decode ( $ available_countries , true );
118
+ }
119
+
120
+ /**
121
+ * Get if WooPay is available on the user country.
122
+ *
123
+ * @param \WC_Payment_Gateway_WCPay $gateway Gateway instance.
124
+ * @return boolean
125
+ */
126
+ public function is_country_available ( $ gateway ) {
127
+ if ( $ gateway ->is_in_test_mode () ) {
128
+ return true ;
129
+ }
130
+
131
+ $ location_data = WC_Geolocation::geolocate_ip ();
132
+
133
+ $ available_countries = $ this ->get_woopay_available_countries ();
134
+
135
+ return in_array ( $ location_data ['country ' ], $ available_countries , true );
136
+ }
137
+
65
138
/**
66
139
* Get phone number for creating platform checkout customer.
67
140
*
0 commit comments