PluginProbe
PayPlug for WooCommerce (Official) / 2.18.0
PayPlug for WooCommerce (Official) v2.18.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Gateway / AccountGateway.php

AccountGateway.php in PayPlug for WooCommerce (Official) 2.18.0, at src/Gateway/AccountGateway.php

424 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Gateway;
4
5 use Payplug\PayplugWoocommerce\Traits\Configuration;
6 use Payplug\PayplugWoocommerce\Traits\ServiceGetter;
7
8 class AccountGateway
9 {
10 use ServiceGetter;
11
12 public function _construct()
13 {
14 $this->configuration = $this->get_service('configuration');
15 }
16
17 /**
18 * @param $email
19 * @param $password
20 *
21 * @return array
22 */
23 public function register($email = '', $password = '')
24 {
25 // Clean previous configuration to avoid any conflict with
26 $this->get_configuration()->clean_option();
27
28 // Initialize default option value
29 $this->get_configuration()->initialize_option();
30
31 // get admin keys
32 $keys = $this->get_api()->get_keys_by_login($email, $password);
33 if (!$keys['result']) {
34 return [];
35 }
36
37 // if keys got, register keys in database
38 $secret_keys = $keys['response']['secret_keys'];
39 $options_to_update = [
40 'version' => PAYPLUG_GATEWAY_VERSION,
41 'enabled' => true,
42 'mode' => isset($secret_keys['live']) && !empty($secret_keys['live']) ? true : false,
43 'email' => $email,
44 'api_key' => json_encode([
45 'live' => isset($secret_keys['live']) ? $secret_keys['live'] : '',
46 'test' => isset($secret_keys['test']) ? $secret_keys['test'] : '',
47 ]),
48 ];
49
50 $this->get_configuration()->update_options($options_to_update);
51
52 $this->initialize_permisssion();
53
54 return $this->get_configuration()->get_options();
55 }
56
57 /**
58 * @param $client_id
59 * @param $company_id
60 *
61 * @return void
62 */
63 public function initialize_jwt($client_id = '', $company_id = '')
64 {
65 // Clean previous configuration to avoid any conflict with
66 $this->get_configuration()->clean_option();
67
68 // Initialize default option value
69 $this->get_configuration()->initialize_option();
70
71 //
72 $client_id = sanitize_text_field($client_id);
73 $company_id = sanitize_text_field($company_id);
74 $this->get_configuration()->update_option('oauth_client_id', $client_id);
75 $this->get_configuration()->update_option('oauth_company_id', $company_id);
76
77 // Generate a code verifier
78 $code_verifier = bin2hex(openssl_random_pseudo_bytes(50));
79 $this->get_configuration()->update_option('oauth_code_verifier', $code_verifier);
80
81 // Then get callbackUri
82 $oauth_callback_uri = admin_url('admin.php?page=wc-settings&tab=checkout&section=payplug');
83 $this->get_configuration()->update_option('oauth_callback_uri', $oauth_callback_uri);
84
85 // Initiate OAuth flow using the PayPlug library
86 $this->get_api()->initiate_oauth($client_id, $oauth_callback_uri, $code_verifier);
87
88 // This function will perform the redirect and exit
89 }
90
91 /**
92 * @param $authorization_code
93 *
94 * @return bool
95 */
96 public function register_jwt($authorization_code = '')
97 {
98 if (!is_string($authorization_code) || empty($authorization_code)) {
99 PayplugGateway::log('Missing authorization code', 'error');
100
101 return false;
102 }
103
104 $temporary_jwt = $this->generate_temporary_jwt((string) $authorization_code);
105 if (empty($temporary_jwt)) {
106 PayplugGateway::log('Failed to obtain access token from authorization code', 'error');
107
108 return false;
109 }
110
111 // Get Email from the token
112 $id_token = $temporary_jwt['id_token'];
113 $id_token_split = explode('.', $id_token);
114 $payload = base64_decode($id_token_split[1]);
115 $payload_decode = json_decode($payload, true);
116 $email = isset($payload_decode['email']) ? $payload_decode['email'] : '';
117
118 // Then get the client id and secret
119 $access_token = $temporary_jwt['access_token'];
120 $oauth_client_data = $this->get_oauth_merchant_data($access_token);
121 if (empty($oauth_client_data)) {
122 PayplugGateway::log('Failed to get oauth merchant data', 'error');
123
124 return false;
125 }
126
127 // Then update the options
128 $options_to_update = [
129 'version' => PAYPLUG_GATEWAY_VERSION,
130 'enabled' => true,
131 'mode' => isset($oauth_client_data['live']) && !empty($oauth_client_data['live']) ? true : false,
132 'email' => $email,
133 'oauth_client_data' => json_encode($oauth_client_data),
134 ];
135 $this->get_configuration()->update_options($options_to_update);
136
137 // Then generate the JWT
138 $jwt = $this->generate_jwt();
139 if (empty($jwt)) {
140 PayplugGateway::log('Failed to get jwt', 'error');
141
142 return false;
143 }
144 $this->get_configuration()->update_option('jwt', json_encode($jwt));
145
146 // Initialize the merchant permission
147 $this->initialize_permisssion();
148
149 // Clean up transients
150 $this->get_configuration()->update_option('oauth_client_id', '');
151 $this->get_configuration()->update_option('oauth_company_id', '');
152 $this->get_configuration()->update_option('oauth_code_verifier', '');
153 $this->get_configuration()->update_option('oauth_callback_uri', '');
154
155 return true;
156 }
157
158 /**
159 * @return bool
160 */
161 public function is_logged()
162 {
163 $jwt = json_decode($this->get_configuration()->get_option('jwt'), true);
164
165 if (!empty($jwt)) {
166 $is_logged = isset($jwt['test']) && isset($jwt['test']['access_token']) && !empty($jwt['test']['access_token']);
167 } else {
168 $api_key = json_decode($this->get_configuration()->get_option('api_key'), true);
169 $is_logged = isset($api_key['test']) && !empty($api_key['test']);
170 }
171
172 return $is_logged;
173 }
174
175 /**
176 * @return void
177 */
178 public function logout()
179 {
180 }
181
182 /**
183 * @param $mode
184 *
185 * @return array
186 */
187 public function get_permissions($mode = '')
188 {
189 $account_permissions = $this->get_api()->get_account($mode);
190
191 return $this->format_account_permissions($account_permissions['response']);
192 }
193
194 /**
195 * @return string
196 */
197 public function get_merchant_id()
198 {
199 $account_permissions = $this->get_api()->get_account('test');
200
201 return $account_permissions['response']['id'];
202 }
203
204 /**
205 * @return void
206 */
207 public function initialize_permisssion()
208 {
209 // get the permissions
210 $permissions = $this->get_permissions();
211
212 // then get test merchant id
213 $company_id = $this->get_merchant_id();
214 $permissions['global']['company_id'] = $company_id;
215
216 // and update options
217 $this->get_configuration()->update_options($permissions['global']);
218 $this->get_configuration()->update_payment_permissions($permissions['payment_methods']);
219 }
220
221 /**
222 * @param $access_token
223 *
224 * @return array
225 */
226 public function get_oauth_merchant_data($access_token)
227 {
228 if (!is_string($access_token) || empty($access_token)) {
229 return [];
230 }
231 $company_id = $this->get_configuration()->get_option('oauth_company_id');
232 $auth_live = $this->get_api()->create_client_id_and_secret($access_token, $company_id, 'live');
233 $auth_test = $this->get_api()->create_client_id_and_secret($access_token, $company_id, 'test');
234 $oauth_client_data = [
235 'live' => !empty($auth_live) ? [
236 'client_secret' => $auth_live['client_secret'],
237 'client_id' => $auth_live['client_id'],
238 ] : $auth_live,
239 'test' => !empty($auth_test) ? [
240 'client_secret' => $auth_test['client_secret'],
241 'client_id' => $auth_test['client_id'],
242 ] : $auth_test,
243 ];
244
245 return (empty($auth_live) && empty($auth_test)) ? [] : $oauth_client_data;
246 }
247
248 /**
249 * @return array
250 */
251 public function generate_jwt()
252 {
253 $oauth_client_data = json_decode($this->get_configuration()->get_option('oauth_client_data'), true);
254 if (empty($oauth_client_data)) {
255 return [];
256 }
257
258 foreach ($oauth_client_data as $key => $data) {
259 if (empty($data) || !in_array($key, ['live', 'test'])) {
260 $jwt[$key] = [];
261 continue;
262 }
263
264 if (isset($data['client_id']) && isset($data['client_secret'])) {
265 $generated_jwt = $this->get_api()->generate_jwt($data['client_id'], $data['client_secret']);
266 if (empty($generated_jwt)) {
267 return false;
268 }
269 $jwt[$key] = $generated_jwt;
270 }
271 }
272
273 return $jwt;
274 }
275
276 /**
277 * @param $authorization_code
278 *
279 * @return array
280 */
281 private function generate_temporary_jwt($authorization_code = '')
282 {
283 if (!is_string($authorization_code) || empty($authorization_code)) {
284 return [];
285 }
286
287 $code_verifier = $this->get_configuration()->get_option('oauth_code_verifier');
288 $callback_uri = $this->get_configuration()->get_option('oauth_callback_uri');
289 $client_id = $this->get_configuration()->get_option('oauth_client_id');
290 if (empty($code_verifier) || empty($callback_uri) || empty($client_id)) {
291 PayplugGateway::log('Missing OAuth parameters for token exchange', 'error');
292
293 return [];
294 }
295
296 $temporary_jwt = $this->get_api()->generate_jwt_one_shot($authorization_code, $callback_uri, $client_id, $code_verifier);
297 if (empty($temporary_jwt)) {
298 PayplugGateway::log('Failed to obtain access token from authorization code', 'error');
299
300 return [];
301 }
302
303 return $temporary_jwt;
304 }
305
306 /**
307 * @param $account_permissions
308 *
309 * @return array
310 */
311 protected function format_account_permissions($account_permissions = [])
312 {
313 if (!is_array($account_permissions) || empty($account_permissions)) {
314 return [];
315 }
316
317 $expected_fields = $this->get_configuration()->get_expected_fields();
318 $options = $this->get_configuration()->get_options();
319
320 $formated_account_permissions = [];
321 $expected_payment_permissions = $expected_fields['payment_methods']['permissions'];
322
323 // format global configuration
324 foreach ($expected_fields as $key => $field) {
325 switch (true) {
326 case 'company_id' == $key:
327 $value = (string) $account_permissions['id'];
328 break;
329 case 'company_iso' == $key:
330 $value = (string) $account_permissions['country'];
331 break;
332 case 'currencies' == $key:
333 $value = json_encode($account_permissions['configuration']['currencies']);
334 break;
335 default:
336 $value = null;
337 break;
338 }
339 if (null !== $value) {
340 $formated_account_permissions['global'][$key] = $value;
341 }
342 }
343
344 // format payment configuration
345 foreach ($expected_payment_permissions as $key => $value) {
346 $payment_permissions = [];
347 switch (true) {
348 default:
349 $account_payment = isset($account_permissions['payment_methods'][$key])
350 && !empty($account_permissions['payment_methods'][$key])
351 ? $account_permissions['payment_methods'][$key]
352 : [];
353 if (!empty($account_payment)) {
354 $account_payment = $account_permissions['payment_methods'][$key];
355 $countries = isset($account_payment['allowed_countries']) && !empty($account_payment['allowed_countries'])
356 ? $account_payment['allowed_countries']
357 : ['ALL'];
358 $min_amounts = isset($account_payment['min_amounts']) && !empty($account_payment['min_amounts'])
359 ? $account_payment['min_amounts']
360 : $account_permissions['configuration']['min_amounts'];
361 $max_amounts = isset($account_payment['max_amounts']) && !empty($account_payment['max_amounts'])
362 ? $account_payment['max_amounts']
363 : $account_permissions['configuration']['max_amounts'];
364
365 $payment_permissions['enabled'] = $account_payment['enabled'];
366 $payment_permissions['countries'] = json_encode($countries);
367 $payment_permissions['amounts'] = json_encode([
368 'min' => $min_amounts,
369 'max' => $max_amounts,
370 ]);
371
372 if ('apple_pay' == $key) {
373 $payment_permissions['allowed_domains'] = json_encode($account_permissions['payment_methods'][$key]['allowed_domain_names']);
374 }
375 }
376 break;
377 case 'payplug' == $key:
378 $payment_permissions = [
379 'enabled' => true,
380 'countries' => json_encode(['ALL']),
381 'amounts' => json_encode([
382 'min' => $account_permissions['configuration']['min_amounts'],
383 'max' => $account_permissions['configuration']['max_amounts'],
384 ]),
385 ];
386 break;
387 case 'installment' == $key:
388 $payment_permissions = [
389 'enabled' => $account_permissions['permissions']['can_create_installment_plan'],
390 'countries' => json_encode(['ALL']),
391 'amounts' => json_encode([
392 'min' => $account_permissions['configuration']['min_amounts'],
393 'max' => $account_permissions['configuration']['max_amounts'],
394 ]),
395 ];
396 break;
397 case 'deferred' == $key:
398 $payment_permissions = [
399 'enabled' => $account_permissions['permissions']['can_create_deferred_payment'],
400 'countries' => json_encode(['ALL']),
401 'amounts' => json_encode([
402 'min' => $account_permissions['configuration']['min_amounts'],
403 'max' => $account_permissions['configuration']['max_amounts'],
404 ]),
405 ];
406 break;
407 case 'one_click' == $key:
408 $payment_permissions = [
409 'enabled' => $account_permissions['permissions']['can_save_cards'],
410 'countries' => json_encode(['ALL']),
411 'amounts' => json_encode([
412 'min' => $account_permissions['configuration']['min_amounts'],
413 'max' => $account_permissions['configuration']['max_amounts'],
414 ]),
415 ];
416 break;
417 }
418 $formated_account_permissions['payment_methods'][$key] = $payment_permissions;
419 }
420
421 return $formated_account_permissions;
422 }
423 }
424