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