PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / trunk
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder vtrunk
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Payments / PaymentMethods / Stripe / StripeSettings.php

StripeSettings.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder trunk, at app/Modules/Payments/PaymentMethods/Stripe/StripeSettings.php

313 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Payments\PaymentMethods\Stripe;
4
5 use FluentForm\App\Models\Subscription;
6 use FluentForm\Framework\Helpers\ArrayHelper;
7 use FluentForm\App\Modules\Payments\PaymentHelper;
8
9 if (!defined('ABSPATH')) {
10 exit; // Exit if accessed directly.
11 }
12
13 class StripeSettings
14 {
15 public static function getSettings($decrypted = true)
16 {
17 $defaults = [
18 'test_publishable_key' => '',
19 'test_secret_key' => '',
20 'live_publishable_key' => '',
21 'live_secret_key' => '',
22 'payment_mode' => 'test',
23 'is_active' => 'no',
24 'provider' => 'api_keys',
25 'test_account_id' => '',
26 'live_account_id' => '',
27 'is_encrypted' => 'no'
28 ];
29
30 $settings = get_option('fluentform_payment_settings_stripe', []);
31
32 if (!$settings) {
33 $defaults['provider'] = 'connect';
34 }
35
36 $settings = wp_parse_args($settings, $defaults);
37
38 if ($settings['provider'] == 'connect' && apply_filters('fluentform/disable_stripe_connect', false)) {
39 $settings['provider'] = 'api_keys';
40 }
41
42 if ($decrypted) {
43 $settings = self::maybeDecryptKeys($settings);
44 }
45
46 return $settings;
47 }
48
49 public static function updateSettings($data)
50 {
51 $settings = self::getSettings();
52 $settings = wp_parse_args($data, $settings);
53
54 $settings = self::encryptKeys($settings);
55 $settings = self::preserveUnreadableKeys($settings);
56 update_option('fluentform_payment_settings_stripe', $settings);
57 return self::getSettings();
58 }
59
60 /**
61 * A decrypt-failed stored blob is the only recoverable copy of that key
62 * (restoring the old salts revives it), and every writer works from the
63 * decrypted settings where a failed key reads as ''. Keep the stored blob
64 * unless the mode is explicitly torn down (account/publishable cleared too),
65 * so the failure flag and admin notice persist until the key really works.
66 *
67 * @param array $settings encrypted settings about to be persisted
68 * @return array
69 */
70 public static function preserveUnreadableKeys($settings)
71 {
72 if (get_option('fluentform_stripe_key_decrypt_failed') !== 'yes') {
73 return $settings;
74 }
75
76 $stored = get_option('fluentform_payment_settings_stripe', []);
77
78 foreach (['test', 'live'] as $mode) {
79 $modeConfigured = !empty($settings[$mode . '_publishable_key']) || !empty($settings[$mode . '_account_id']);
80 if (empty($settings[$mode . '_secret_key']) && $modeConfigured && !empty($stored[$mode . '_secret_key'])) {
81 $settings[$mode . '_secret_key'] = $stored[$mode . '_secret_key'];
82 }
83 }
84
85 return $settings;
86 }
87
88 public static function encryptKeys($settings)
89 {
90 $settings['live_secret_key'] = PaymentHelper::encryptKey($settings['live_secret_key']);
91 $settings['test_secret_key'] = PaymentHelper::encryptKey($settings['test_secret_key']);
92 $settings['is_encrypted'] = 'yes';
93
94 return $settings;
95 }
96
97 public static function maybeDecryptKeys($settings)
98 {
99 if (ArrayHelper::get($settings, 'is_encrypted') == 'yes') {
100 $storedLive = ArrayHelper::get($settings, 'live_secret_key');
101 $storedTest = ArrayHelper::get($settings, 'test_secret_key');
102 $failed = false;
103 $legacyFound = false;
104
105 if (!empty($storedLive)) {
106 $decrypted = PaymentHelper::decryptKey($storedLive);
107 if ($decrypted === false) {
108 $failed = true;
109 $settings['live_secret_key'] = '';
110 } else {
111 $settings['live_secret_key'] = $decrypted;
112 $legacyFound = $legacyFound || strpos($storedLive, 'v2:') !== 0;
113 }
114 }
115
116 if (!empty($storedTest)) {
117 $decrypted = PaymentHelper::decryptKey($storedTest);
118 if ($decrypted === false) {
119 $failed = true;
120 $settings['test_secret_key'] = '';
121 } else {
122 $settings['test_secret_key'] = $decrypted;
123 $legacyFound = $legacyFound || strpos($storedTest, 'v2:') !== 0;
124 }
125 }
126
127 if ($failed) {
128 update_option('fluentform_stripe_key_decrypt_failed', 'yes');
129 } else {
130 if (get_option('fluentform_stripe_key_decrypt_failed')) {
131 delete_option('fluentform_stripe_key_decrypt_failed');
132 }
133 if ($legacyFound) {
134 $stored = get_option('fluentform_payment_settings_stripe', []);
135 $stored['live_secret_key'] = PaymentHelper::encryptKey($settings['live_secret_key']);
136 $stored['test_secret_key'] = PaymentHelper::encryptKey($settings['test_secret_key']);
137 $stored['is_encrypted'] = 'yes';
138 update_option('fluentform_payment_settings_stripe', $stored);
139 }
140 }
141 } else {
142 $encrypted = self::encryptKeys($settings);
143 update_option('fluentform_payment_settings_stripe', $encrypted);
144 }
145
146 $settings['is_encrypted'] = 'yes';
147
148 return $settings;
149 }
150
151 public static function getSecretKey($formId = false)
152 {
153 if ($formId) {
154 $formPaymentSettings = PaymentHelper::getFormSettings($formId, 'admin');
155 if (ArrayHelper::get($formPaymentSettings, 'stripe_account_type') == 'custom') {
156 return ArrayHelper::get($formPaymentSettings, 'stripe_custom_config.secret_key');
157 }
158 }
159
160 $settings = self::getSettings();
161
162 if ($settings['payment_mode'] == 'live') {
163 return $settings['live_secret_key'];
164 }
165
166 return $settings['test_secret_key'];
167 }
168
169 public static function getPublishableKey($formId = false)
170 {
171 if ($formId) {
172 $formPaymentSettings = PaymentHelper::getFormSettings($formId, 'admin');
173 if (ArrayHelper::get($formPaymentSettings, 'stripe_account_type') == 'custom') {
174 return ArrayHelper::get($formPaymentSettings, 'stripe_custom_config.publishable_key');
175 }
176 }
177
178 $settings = self::getSettings();
179 if ($settings['payment_mode'] == 'live') {
180 return $settings['live_publishable_key'];
181 }
182
183 return $settings['test_publishable_key'];
184 }
185
186 public static function isLive($formId = false)
187 {
188 if ($formId) {
189 $formPaymentSettings = PaymentHelper::getFormSettings($formId, 'admin');
190 if (ArrayHelper::get($formPaymentSettings, 'stripe_account_type') == 'custom') {
191 return ArrayHelper::get($formPaymentSettings, 'stripe_custom_config.payment_mode') == 'live';
192 }
193 }
194
195 $settings = self::getSettings();
196 return $settings['payment_mode'] == 'live';
197 }
198
199 public static function getMode($formId = false)
200 {
201 return static::isLive($formId) ? 'live' : 'test';
202 }
203
204 public static function supportedShippingCountries()
205 {
206 $countries = [
207 'AC', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AT', 'AU', 'AW', 'AX', 'AZ',
208 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS',
209 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO',
210 'CR', 'CV', 'CW', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER',
211 'ES', 'ET', 'FI', 'FJ', 'FK', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL',
212 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID',
213 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI',
214 'KM', 'KN', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV',
215 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MK', 'ML', 'MM', 'MN', 'MO', 'MQ', 'MR', 'MS', 'MT',
216 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU',
217 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PY', 'QA',
218 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM',
219 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SZ', 'TA', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK',
220 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'UZ', 'VA', 'VC',
221 'VE', 'VG', 'VN', 'VU', 'WF', 'WS', 'XK', 'YE', 'YT', 'ZA', 'ZM', 'ZW', 'ZZ'
222 ];
223
224 return apply_filters('fluentform/stripe_supported_shipping_countries', $countries);
225 }
226
227 public static function getClientId($paymentMode = 'live')
228 {
229 if ($paymentMode == 'live') {
230 return 'ca_GwAPNUlKAW6EgeTqERQieU3DXLAX3k7N';
231 } else {
232 return 'ca_GwAPhxHwszjhsFRdC4j5DqKHud7JLQ8C';
233 }
234 }
235
236 public static function guessFormIdFromEvent($event)
237 {
238 $eventType = $event->type;
239
240 $metaDataEvents = [
241 'checkout.session.completed',
242 'checkout.session.async_payment_succeeded',
243 'checkout.session.async_payment_failed',
244 'charge.refunded',
245 'charge.succeeded',
246 'charge.failed',
247 ];
248
249 if (in_array($eventType, $metaDataEvents)) {
250 $data = $event->data->object;
251 $metaData = (array)$data->metadata;
252 return ArrayHelper::get($metaData, 'form_id');
253 }
254
255 // A delayed subscription's first invoice settles the entry, so it needs the form's own key too. Once the entry is
256 // linked, renewals keep the global key: a form moved to its own account still bills older subscriptions on it.
257 $invoice = $event->data->object;
258 if ('invoice.payment_succeeded' === $eventType && isset($invoice->subscription, $invoice->subscription_details->metadata->form_id)
259 && !Subscription::byVendorSubscriptionId($invoice->subscription)->first()) {
260 return $invoice->subscription_details->metadata->form_id;
261 }
262
263 return false;
264 }
265
266 public static function getPaymentDescriptor($form)
267 {
268 $title = $form->title;
269 $paymentSettings = PaymentHelper::getFormSettings($form->id, 'admin');
270 if ($providedDescriptor = ArrayHelper::get($paymentSettings, 'stripe_descriptor')) {
271 $title = $providedDescriptor;
272 } else {
273 $globalSettings = PaymentHelper::getPaymentSettings();
274 if (!empty($globalSettings['business_name'])) {
275 $title = $globalSettings['business_name'];
276 }
277 }
278
279 $illegal = array('<', '>', '"', "'");
280 // Remove slashes
281 $descriptor = stripslashes($title);
282 // Remove illegal characters
283 $descriptor = str_replace($illegal, '', $descriptor);
284 // Trim to 22 characters max
285 $descriptor = substr($descriptor, 0, 22);
286
287 if (!$descriptor || strlen($descriptor) < 5) {
288 $descriptor = 'FluentForm';
289 }
290
291 return $descriptor;
292
293 }
294
295 public static function isConnectedAndPlatformCountrySame()
296 {
297 $settings = static::getSettings();
298 return ArrayHelper::get($settings, 'connected_account_country') === ArrayHelper::get($settings, 'platform_account_country');
299 }
300
301 public static function getAccountId()
302 {
303 $settings = self::getSettings();
304 $isLive = self::isLive();
305
306 if ($isLive) {
307 return ArrayHelper::get($settings, 'live_account_id');
308 }
309 return ArrayHelper::get($settings, 'test_account_id');
310 }
311
312 }
313