| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\PaymentMethods\StripeGateway; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\Helpers\Helper; |
| 7 |
use FluentCart\App\Modules\PaymentMethods\Core\BaseGatewaySettings; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class StripeSettingsBase extends BaseGatewaySettings |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* Checkout Session submit_type values Stripe accepts. 'auto' is our stored |
| 15 |
* spelling for "send nothing" — Stripe then picks pay/subscribe from the mode. |
| 16 |
*/ |
| 17 |
const SUBMIT_TYPES = ['auto', 'pay', 'book', 'donate', 'subscribe']; |
| 18 |
|
| 19 |
public $settings; |
| 20 |
|
| 21 |
public $methodHandler = 'fluent_cart_payment_settings_stripe'; |
| 22 |
|
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
parent::__construct(); |
| 26 |
$settings = $this->getCachedSettings(); |
| 27 |
$defaults = static::getDefaults(); |
| 28 |
|
| 29 |
if (!$settings || !is_array($settings) || empty($settings)) { |
| 30 |
$settings = $defaults; |
| 31 |
} else { |
| 32 |
$settings = wp_parse_args($settings, $defaults); |
| 33 |
} |
| 34 |
|
| 35 |
//define key's handle |
| 36 |
$isTestDefined = defined('FCT_STRIPE_TEST_PUBLIC_KEY') && defined('FCT_STRIPE_TEST_SECRET_KEY'); |
| 37 |
$isLiveDefined = defined('FCT_STRIPE_LIVE_PUBLIC_KEY') && defined('FCT_STRIPE_LIVE_SECRET_KEY'); |
| 38 |
if ($isTestDefined || $isLiveDefined) { |
| 39 |
$settings['define_test_keys'] = $isTestDefined; |
| 40 |
$settings['define_live_keys'] = $isLiveDefined; |
| 41 |
$settings['provider'] = 'api_keys'; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
$this->settings = apply_filters('fluent_cart/stripe_settings', $settings); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return array with default fields value |
| 50 |
*/ |
| 51 |
public static function getDefaults() |
| 52 |
{ |
| 53 |
return [ |
| 54 |
'is_active' => 'no', |
| 55 |
'provider' => 'connect', |
| 56 |
//define keys |
| 57 |
'define_test_keys' => false, |
| 58 |
'define_live_keys' => false, |
| 59 |
// test keys |
| 60 |
'test_publishable_key' => '', |
| 61 |
'test_secret_key' => '', |
| 62 |
'test_webhook_secret' => '', |
| 63 |
// Live keys |
| 64 |
'live_publishable_key' => '', |
| 65 |
'live_secret_key' => '', |
| 66 |
'live_webhook_secret' => '', |
| 67 |
// Others |
| 68 |
'payment_mode' => 'live', |
| 69 |
'checkout_mode' => 'onsite', |
| 70 |
'submit_type' => 'auto', |
| 71 |
'live_is_encrypted' => 'no', |
| 72 |
'test_is_encrypted' => 'no', |
| 73 |
'secure' => 'yes' |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
public function isActive(): bool |
| 78 |
{ |
| 79 |
return $this->settings['is_active'] == 'yes'; |
| 80 |
} |
| 81 |
|
| 82 |
public function get($key = '') |
| 83 |
{ |
| 84 |
$settings = $this->settings; |
| 85 |
|
| 86 |
if ($key && isset($this->settings[$key])) { |
| 87 |
return $this->settings[$key]; |
| 88 |
} |
| 89 |
return $settings; |
| 90 |
} |
| 91 |
|
| 92 |
public function updateSettings($settings) |
| 93 |
{ |
| 94 |
$defaults = static::getDefaults(); |
| 95 |
$settings = wp_parse_args($settings, $defaults); |
| 96 |
|
| 97 |
// Goes straight onto the wire as a Checkout Session parameter, and the |
| 98 |
// controller hands this method the request body unfiltered. |
| 99 |
if (!in_array(Arr::get($settings, 'submit_type'), self::SUBMIT_TYPES, true)) { |
| 100 |
$settings['submit_type'] = 'auto'; |
| 101 |
} |
| 102 |
|
| 103 |
if (defined('FCT_STRIPE_LIVE_PUBLIC_KEY')) { |
| 104 |
$settings['live_publishable_key'] = ''; |
| 105 |
} |
| 106 |
|
| 107 |
if (defined('FCT_STRIPE_TEST_PUBLIC_KEY')) { |
| 108 |
$settings['test_publishable_key'] = ''; |
| 109 |
} |
| 110 |
|
| 111 |
if (defined('FCT_STRIPE_LIVE_SECRET_KEY')) { |
| 112 |
$settings['live_secret_key'] = ''; |
| 113 |
} |
| 114 |
|
| 115 |
if (defined('FCT_STRIPE_TEST_SECRET_KEY')) { |
| 116 |
$settings['test_secret_key'] = ''; |
| 117 |
} |
| 118 |
|
| 119 |
fluent_cart_update_option($this->methodHandler, $settings); |
| 120 |
|
| 121 |
return $settings; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* The Checkout Session submit_type to send, or null to let Stripe decide. |
| 126 |
* |
| 127 |
* Re-validated on read: `fluent_cart/stripe_settings` can rewrite the stored |
| 128 |
* value, and an unknown one is a 400 from Stripe on a live checkout. |
| 129 |
* |
| 130 |
* @return string|null |
| 131 |
*/ |
| 132 |
public function getSubmitType() |
| 133 |
{ |
| 134 |
$submitType = Arr::get($this->settings, 'submit_type', 'auto'); |
| 135 |
|
| 136 |
if ($submitType === 'auto' || !in_array($submitType, self::SUBMIT_TYPES, true)) { |
| 137 |
return null; |
| 138 |
} |
| 139 |
|
| 140 |
return $submitType; |
| 141 |
} |
| 142 |
|
| 143 |
public function getMode() |
| 144 |
{ |
| 145 |
// return store mode |
| 146 |
return (new StoreSettings)->get('order_mode'); |
| 147 |
} |
| 148 |
|
| 149 |
public function getPublicKey() |
| 150 |
{ |
| 151 |
if ($this->getMode() === 'test') { |
| 152 |
return defined('FCT_STRIPE_TEST_PUBLIC_KEY') ? FCT_STRIPE_TEST_PUBLIC_KEY : $this->get()['test_publishable_key'] . ''; |
| 153 |
} |
| 154 |
|
| 155 |
return defined('FCT_STRIPE_LIVE_PUBLIC_KEY') ? FCT_STRIPE_LIVE_PUBLIC_KEY : $this->get()['live_publishable_key'] . ''; |
| 156 |
} |
| 157 |
|
| 158 |
public function getApiKey($mode = 'current') |
| 159 |
{ |
| 160 |
if ($mode == 'current' || !$mode) { |
| 161 |
$mode = $this->getMode(); |
| 162 |
} |
| 163 |
|
| 164 |
if ($mode === 'test') { |
| 165 |
return defined('FCT_STRIPE_TEST_SECRET_KEY') ? FCT_STRIPE_TEST_SECRET_KEY : Helper::decryptKey($this->get()['test_secret_key']) . ''; |
| 166 |
} |
| 167 |
|
| 168 |
return defined('FCT_STRIPE_LIVE_SECRET_KEY') ? FCT_STRIPE_LIVE_SECRET_KEY : Helper::decryptKey($this->get()['live_secret_key']) . ''; |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|