| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\PaymentMethods\PayPalGateway; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\App\Modules\PaymentMethods\Core\BaseGatewaySettings; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCart\Api\StoreSettings; |
| 9 |
|
| 10 |
class PayPalSettingsBase extends BaseGatewaySettings |
| 11 |
{ |
| 12 |
public $methodHandler = 'fluent_cart_payment_settings_paypal'; |
| 13 |
|
| 14 |
public $settings; |
| 15 |
public $storeSettings = null; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
parent::__construct(); |
| 20 |
|
| 21 |
$settings = $this->getCachedSettings(); |
| 22 |
$defaults = static::getDefaults(); |
| 23 |
|
| 24 |
if (!$settings || !is_array($settings) || empty($settings)) { |
| 25 |
$settings = $defaults; |
| 26 |
} else { |
| 27 |
$settings = wp_parse_args($settings, $defaults); |
| 28 |
} |
| 29 |
|
| 30 |
// if key defined |
| 31 |
$isTestDefined = $this->hasManualTesKeys(); |
| 32 |
$isLiveDefined = $this->hasManualLiveKeys(); |
| 33 |
|
| 34 |
if ($isTestDefined || $isLiveDefined) { |
| 35 |
$settings['define_test_keys'] = $isTestDefined; |
| 36 |
$settings['define_live_keys'] = $isLiveDefined; |
| 37 |
$settings['provider'] = 'api_keys'; |
| 38 |
} |
| 39 |
|
| 40 |
$this->settings = $settings; |
| 41 |
|
| 42 |
if (!$this->storeSettings) { |
| 43 |
$this->storeSettings = new StoreSettings(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
private function hasManualTesKeys(): bool |
| 48 |
{ |
| 49 |
return defined('FCT_PAYPAL_TEST_PUBLIC_KEY') && defined('FCT_PAYPAL_TEST_SECRET_KEY'); |
| 50 |
} |
| 51 |
|
| 52 |
private function hasManualLiveKeys(): bool |
| 53 |
{ |
| 54 |
return defined('FCT_PAYPAL_LIVE_PUBLIC_KEY') && defined('FCT_PAYPAL_LIVE_SECRET_KEY'); |
| 55 |
} |
| 56 |
|
| 57 |
public static function getDefaults(): array |
| 58 |
{ |
| 59 |
|
| 60 |
return [ |
| 61 |
'is_active' => 'no', |
| 62 |
'provider' => 'connect', |
| 63 |
//define keys |
| 64 |
'define_test_keys' => false, |
| 65 |
'define_live_keys' => false, |
| 66 |
'payment_mode' => 'live', |
| 67 |
'live_email_address' => '', |
| 68 |
'test_email_address' => '', |
| 69 |
'checkout_mode' => 'paypal_pro', |
| 70 |
'test_client_id' => '', |
| 71 |
'live_client_id' => '', |
| 72 |
'test_client_secret' => '', |
| 73 |
'live_client_secret' => '', |
| 74 |
'notify_method' => 'webhook', |
| 75 |
'test_webhook_id' => '', |
| 76 |
'live_webhook_id' => '', |
| 77 |
'test_webhook_events' => [], |
| 78 |
'live_webhook_events' => [], |
| 79 |
|
| 80 |
'disable_ipn_verification' => 'no', |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
public function getProviderType() |
| 85 |
{ |
| 86 |
return Arr::get( $this->settings, 'provider'); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
public function isActive(): bool |
| 91 |
{ |
| 92 |
$settings = $this->get(); |
| 93 |
|
| 94 |
if ($settings['is_active'] === 'yes' && Arr::get($settings, 'checkout_mode') === 'paypal_pro') { |
| 95 |
|
| 96 |
if ( $this->getProviderType() === 'api_keys') { |
| 97 |
return Arr::get($settings, 'define_test_keys', true) || Arr::get($settings, 'define_live_keys', true); |
| 98 |
} |
| 99 |
|
| 100 |
$mode = $this->storeSettings->get('order_mode'); |
| 101 |
$id = $mode . '_client_id'; |
| 102 |
|
| 103 |
return !empty($settings[$id]); |
| 104 |
} |
| 105 |
|
| 106 |
return $settings['is_active'] === 'yes' && !!$this->getVendorEmail(); |
| 107 |
} |
| 108 |
|
| 109 |
public function get($key = '') |
| 110 |
{ |
| 111 |
$settings = $this->settings; |
| 112 |
if ($key) { |
| 113 |
return $this->settings[$key] ?? null; |
| 114 |
} |
| 115 |
return $settings; |
| 116 |
} |
| 117 |
|
| 118 |
public function getMode() |
| 119 |
{ |
| 120 |
if (!$this->storeSettings) { |
| 121 |
$this->storeSettings = new StoreSettings(); |
| 122 |
} |
| 123 |
return $this->storeSettings->get('order_mode'); |
| 124 |
} |
| 125 |
|
| 126 |
public function getVendorEmail() |
| 127 |
{ |
| 128 |
if ($this->getMode() === 'test') { |
| 129 |
return $this->settings['test_email_address']; |
| 130 |
} |
| 131 |
return $this->settings['live_email_address']; |
| 132 |
} |
| 133 |
|
| 134 |
public function getApiKey($mode = '') |
| 135 |
{ |
| 136 |
// if no custom mode is provided, get the mode from the store settings |
| 137 |
if (!$mode) { |
| 138 |
$mode = $this->getMode(); |
| 139 |
} |
| 140 |
|
| 141 |
if ($mode === 'test') { |
| 142 |
return defined('FCT_PAYPAL_TEST_SECRET_KEY') ? FCT_PAYPAL_TEST_SECRET_KEY : Helper::decryptKey($this->get()['test_client_secret']) . ''; |
| 143 |
} |
| 144 |
|
| 145 |
return defined('FCT_PAYPAL_LIVE_SECRET_KEY') ? FCT_PAYPAL_LIVE_SECRET_KEY : Helper::decryptKey($this->get()['live_client_secret']) . ''; |
| 146 |
} |
| 147 |
|
| 148 |
public function getPublicKey($mode = '') |
| 149 |
{ |
| 150 |
if (!$mode) { |
| 151 |
$mode = $this->getMode(); |
| 152 |
} |
| 153 |
|
| 154 |
if ($mode === 'test') { |
| 155 |
return defined('FCT_PAYPAL_TEST_PUBLIC_KEY') ? FCT_PAYPAL_TEST_PUBLIC_KEY : $this->get()['test_client_id']; |
| 156 |
} |
| 157 |
|
| 158 |
return defined('FCT_PAYPAL_LIVE_PUBLIC_KEY') ? FCT_PAYPAL_LIVE_PUBLIC_KEY : $this->get()['live_client_id']; |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
public function getMerchantId() |
| 163 |
{ |
| 164 |
return $this->get($this->getMode() . '_account_id'); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
public function updateNonSensitiveData($data) |
| 169 |
{ |
| 170 |
$data = Arr::except($data, ['live_client_secret', 'test_client_secret']); |
| 171 |
$settings = wp_parse_args($data, $this->settings); |
| 172 |
fluent_cart_update_option('fluent_cart_payment_settings_paypal', $settings); |
| 173 |
} |
| 174 |
} |
| 175 |
|