8
8
* Class WC_Stripe_Payment_Method_Configurations
9
9
*/
10
10
class WC_Stripe_Payment_Method_Configurations {
11
+
11
12
/**
12
13
* The primary configuration.
13
14
*
@@ -30,40 +31,106 @@ class WC_Stripe_Payment_Method_Configurations {
30
31
const LIVE_MODE_CONFIGURATION_PARENT_ID = 'pmc_1LEKjAGX8lmJQndTk2ziRchV ' ;
31
32
32
33
/**
33
- * Reset the primary configuration.
34
+ * The test mode payment method configuration transient key (for cache purposes).
35
+ *
36
+ * @var string
34
37
*/
35
- public static function reset_primary_configuration () {
36
- self ::$ primary_configuration = null ;
37
- }
38
+ const TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY = 'wcstripe_test_payment_method_configuration_cache ' ;
39
+
40
+ /**
41
+ * The live mode payment method configuration transient key (for cache purposes).
42
+ *
43
+ * @var string
44
+ */
45
+ const LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY = 'wcstripe_live_payment_method_configuration_cache ' ;
46
+
47
+ /**
48
+ * The payment method configuration transient expiration (for cache purposes).
49
+ *
50
+ * @var int
51
+ */
52
+ const CONFIGURATION_CACHE_TRANSIENT_EXPIRATION = 10 * MINUTE_IN_SECONDS ;
38
53
39
54
/**
40
55
* Get the merchant payment method configuration in Stripe.
41
56
*
57
+ * @param bool $force_refresh Whether to force a refresh of the payment method configuration from Stripe.
58
+ * @return object|null
59
+ */
60
+ private static function get_primary_configuration ( $ force_refresh = false ) {
61
+ if ( ! $ force_refresh ) {
62
+ $ cached_primary_configuration = self ::get_payment_method_configuration_from_cache ();
63
+ if ( $ cached_primary_configuration ) {
64
+ return $ cached_primary_configuration ;
65
+ }
66
+ }
67
+
68
+ return self ::get_payment_method_configuration_from_stripe ();
69
+ }
70
+
71
+ /**
72
+ * Get the payment method configuration from cache.
73
+ *
42
74
* @return object|null
43
75
*/
44
- private static function get_primary_configuration () {
76
+ private static function get_payment_method_configuration_from_cache () {
45
77
if ( null !== self ::$ primary_configuration ) {
46
78
return self ::$ primary_configuration ;
47
79
}
48
80
49
- $ result = WC_Stripe_API::get_instance ()->get_payment_method_configurations ();
50
- $ payment_method_configurations = $ result ->data ?? null ;
51
-
52
- if ( ! $ payment_method_configurations ) {
81
+ $ cache_key = WC_Stripe_Mode::is_test () ? self ::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self ::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY ;
82
+ $ cached_primary_configuration = get_transient ( $ cache_key );
83
+ if ( false === $ cached_primary_configuration || null === $ cached_primary_configuration ) {
53
84
return null ;
54
85
}
55
86
56
- foreach ( $ payment_method_configurations as $ payment_method_configuration ) {
57
- if ( ! $ payment_method_configuration ->livemode && $ payment_method_configuration ->parent && self ::TEST_MODE_CONFIGURATION_PARENT_ID === $ payment_method_configuration ->parent ) {
58
- self ::$ primary_configuration = $ payment_method_configuration ;
59
- return $ payment_method_configuration ;
60
- }
87
+ self ::$ primary_configuration = $ cached_primary_configuration ;
88
+ return self ::$ primary_configuration ;
89
+ }
90
+
91
+ /**
92
+ * Clear the payment method configuration from cache.
93
+ */
94
+ public static function clear_payment_method_configuration_cache () {
95
+ self ::$ primary_configuration = null ;
96
+ $ cache_key = WC_Stripe_Mode::is_test () ? self ::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self ::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY ;
97
+ delete_transient ( $ cache_key );
98
+ }
99
+
100
+ /**
101
+ * Cache the payment method configuration.
102
+ *
103
+ * @param object|array $configuration The payment method configuration to set in cache.
104
+ */
105
+ private static function set_payment_method_configuration_cache ( $ configuration ) {
106
+ self ::$ primary_configuration = $ configuration ;
107
+ $ cache_key = WC_Stripe_Mode::is_test () ? self ::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self ::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY ;
108
+ set_transient ( $ cache_key , $ configuration , self ::CONFIGURATION_CACHE_TRANSIENT_EXPIRATION );
109
+ }
110
+
111
+ /**
112
+ * Get the payment method configuration from Stripe.
113
+ *
114
+ * @return object|null
115
+ */
116
+ private static function get_payment_method_configuration_from_stripe () {
117
+ $ result = WC_Stripe_API::get_instance ()->get_payment_method_configurations ();
118
+ $ configurations = $ result ->data ?? null ;
119
+
120
+ if ( ! $ configurations ) {
121
+ return null ;
122
+ }
61
123
62
- if ( $ payment_method_configuration ->livemode && $ payment_method_configuration ->parent && self ::LIVE_MODE_CONFIGURATION_PARENT_ID === $ payment_method_configuration ->parent ) {
63
- self ::$ primary_configuration = $ payment_method_configuration ;
64
- return $ payment_method_configuration ;
124
+ // When connecting to the WooCommerce Platform account a new payment method configuration is created for the merchant.
125
+ // This new payment method configuration has the WooCommerce Platform payment method configuration as parent, and inherits it's default payment methods.
126
+ foreach ( $ configurations as $ configuration ) {
127
+ // The API returns data for the corresponding mode of the api keys used, so we'll get either test or live PMCs, but never both.
128
+ if ( $ configuration ->parent && ( self ::LIVE_MODE_CONFIGURATION_PARENT_ID === $ configuration ->parent || self ::TEST_MODE_CONFIGURATION_PARENT_ID === $ configuration ->parent ) ) {
129
+ self ::set_payment_method_configuration_cache ( $ configuration );
130
+ return $ configuration ;
65
131
}
66
132
}
133
+
67
134
return null ;
68
135
}
69
136
@@ -79,9 +146,10 @@ public static function get_parent_configuration_id() {
79
146
/**
80
147
* Get the UPE enabled payment method IDs.
81
148
*
149
+ * @param bool $force_refresh Whether to force a refresh of the payment method configuration from Stripe.
82
150
* @return array
83
151
*/
84
- public static function get_upe_enabled_payment_method_ids () {
152
+ public static function get_upe_enabled_payment_method_ids ( $ force_refresh = false ) {
85
153
// If the payment method configurations API is not enabled, we fallback to the enabled payment methods stored in the DB.
86
154
if ( ! self ::is_enabled () ) {
87
155
$ stripe_settings = WC_Stripe_Helper::get_stripe_settings ();
@@ -94,7 +162,7 @@ public static function get_upe_enabled_payment_method_ids() {
94
162
self ::maybe_migrate_payment_methods_from_db_to_pmc ();
95
163
96
164
$ enabled_payment_method_ids = [];
97
- $ merchant_payment_method_configuration = self ::get_primary_configuration ();
165
+ $ merchant_payment_method_configuration = self ::get_primary_configuration ( $ force_refresh );
98
166
99
167
if ( $ merchant_payment_method_configuration ) {
100
168
foreach ( $ merchant_payment_method_configuration as $ payment_method_id => $ payment_method ) {
@@ -119,6 +187,11 @@ public static function update_payment_method_configuration( $enabled_payment_met
119
187
$ newly_enabled_methods = [];
120
188
$ newly_disabled_methods = [];
121
189
190
+ if ( ! $ payment_method_configuration ) {
191
+ WC_Stripe_Logger::log ( 'No primary payment method configuration found while updating payment method configuration ' );
192
+ return ;
193
+ }
194
+
122
195
foreach ( $ available_payment_method_ids as $ stripe_id ) {
123
196
$ will_enable = in_array ( $ stripe_id , $ enabled_payment_method_ids , true );
124
197
@@ -137,15 +210,15 @@ public static function update_payment_method_configuration( $enabled_payment_met
137
210
];
138
211
}
139
212
140
- if ( ! $ payment_method_configuration ) {
141
- WC_Stripe_Logger::log ( 'No primary payment method configuration found while updating payment method configuration ' );
142
- return ;
143
- }
144
-
145
- WC_Stripe_API::get_instance ()->update_payment_method_configurations (
213
+ $ response = WC_Stripe_API::get_instance ()->update_payment_method_configurations (
146
214
$ payment_method_configuration ->id ,
147
215
$ updated_payment_method_configuration
148
216
);
217
+ if ( ! empty ( $ response ->error ) ) {
218
+ WC_Stripe_Logger::log ( 'Error: ' . $ response ->error ->message . ': ' . $ response ->error ->request_log_url );
219
+ }
220
+
221
+ self ::clear_payment_method_configuration_cache ();
149
222
150
223
self ::record_payment_method_settings_event ( $ newly_enabled_methods , $ newly_disabled_methods );
151
224
}
0 commit comments