PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.1
1.7.1 1.7.0 1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 All 51 releases
fluent-cart / app / Modules / PaymentMethods / PayPalGateway / PayPalSettingsBase.php

PayPalSettingsBase.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.7.1, at app/Modules/PaymentMethods/PayPalGateway/PayPalSettingsBase.php

185 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'brand_name' => '',
82 ];
83 }
84
85 public function getProviderType()
86 {
87 return Arr::get( $this->settings, 'provider');
88 }
89
90
91 public function isActive(): bool
92 {
93 $settings = $this->get();
94
95 if ($settings['is_active'] === 'yes' && Arr::get($settings, 'checkout_mode') === 'paypal_pro') {
96
97 if ( $this->getProviderType() === 'api_keys') {
98 return Arr::get($settings, 'define_test_keys', true) || Arr::get($settings, 'define_live_keys', true);
99 }
100
101 $mode = $this->storeSettings->get('order_mode');
102 $id = $mode . '_client_id';
103
104 return !empty($settings[$id]);
105 }
106
107 return $settings['is_active'] === 'yes' && !!$this->getVendorEmail();
108 }
109
110 public function get($key = '')
111 {
112 $settings = $this->settings;
113 if ($key) {
114 return $this->settings[$key] ?? null;
115 }
116 return $settings;
117 }
118
119 public function getMode()
120 {
121 if (!$this->storeSettings) {
122 $this->storeSettings = new StoreSettings();
123 }
124 return $this->storeSettings->get('order_mode');
125 }
126
127 public function getVendorEmail()
128 {
129 if ($this->getMode() === 'test') {
130 return $this->settings['test_email_address'];
131 }
132 return $this->settings['live_email_address'];
133 }
134
135 public function getApiKey($mode = '')
136 {
137 // if no custom mode is provided, get the mode from the store settings
138 if (!$mode) {
139 $mode = $this->getMode();
140 }
141
142 if ($mode === 'test') {
143 return defined('FCT_PAYPAL_TEST_SECRET_KEY') ? FCT_PAYPAL_TEST_SECRET_KEY : Helper::decryptKey($this->get()['test_client_secret']) . '';
144 }
145
146 return defined('FCT_PAYPAL_LIVE_SECRET_KEY') ? FCT_PAYPAL_LIVE_SECRET_KEY : Helper::decryptKey($this->get()['live_client_secret']) . '';
147 }
148
149 public function getPublicKey($mode = '')
150 {
151 if (!$mode) {
152 $mode = $this->getMode();
153 }
154
155 if ($mode === 'test') {
156 return defined('FCT_PAYPAL_TEST_PUBLIC_KEY') ? FCT_PAYPAL_TEST_PUBLIC_KEY : $this->get()['test_client_id'];
157 }
158
159 return defined('FCT_PAYPAL_LIVE_PUBLIC_KEY') ? FCT_PAYPAL_LIVE_PUBLIC_KEY : $this->get()['live_client_id'];
160
161 }
162
163 public function getBrandName(array $context = []): string
164 {
165 $brandName = trim((string) $this->get('brand_name'));
166
167 $brandName = (string) apply_filters('fluent_cart/paypal/brand_name', $brandName, $context);
168
169 return mb_substr(trim(wp_strip_all_tags($brandName)), 0, 127);
170 }
171
172 public function getMerchantId()
173 {
174 return $this->get($this->getMode() . '_account_id');
175 }
176
177
178 public function updateNonSensitiveData($data)
179 {
180 $data = Arr::except($data, ['live_client_secret', 'test_client_secret']);
181 $settings = wp_parse_args($data, $this->settings);
182 fluent_cart_update_option('fluent_cart_payment_settings_paypal', $settings);
183 }
184 }
185