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