| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Payplug\PayplugWoocommerce\Service; |
| 4 | 4 | |
| 5 | 5 | use Payplug\Core\HttpClient; |
| 6 | +use Payplug\Exception\HttpException; | |
| 6 | 7 | use Payplug\Payplug; |
| 7 | 8 | use Payplug\PayplugWoocommerce\Traits\ServiceGetter; |
| 8 | 9 | |
| 9 | 10 | class Api |
| @@ -45,16 +46,36 @@ | ||
| 45 | 46 | * @return array |
| 46 | 47 | */ |
| 47 | 48 | public function get_keys_by_login($email = '', $password = '') |
| 48 | 49 | { |
| 49 | - $keys = $this->do_request_with_fallback('\Payplug\Authentication::getKeysByLogin', [$email, $password]); | |
| 50 | + try { | |
| 51 | + $response = $this->do_request('\Payplug\Authentication::getKeysByLogin', [$email, $password]); | |
| 52 | + $http_response = isset($response['httpResponse']) && !empty($response['httpResponse']) | |
| 53 | + ? $response['httpResponse'] | |
| 54 | + : null; | |
| 50 | 55 | |
| 51 | - return [ | |
| 52 | - 'result' => $keys['result'], | |
| 53 | - 'response' => isset($keys['response']['httpResponse']) && !empty($keys['response']['httpResponse']) | |
| 54 | - ? $keys['response']['httpResponse'] | |
| 55 | - : null, | |
| 56 | - ]; | |
| 56 | + if (is_null($http_response)) { | |
| 57 | + return ['result' => false, 'response' => null]; | |
| 58 | + } | |
| 59 | + | |
| 60 | + return [ | |
| 61 | + 'result' => true, | |
| 62 | + 'response' => $http_response, | |
| 63 | + ]; | |
| 64 | + } catch (HttpException $e) { | |
| 65 | + $error = $e->getErrorObject(); | |
| 66 | + | |
| 67 | + return [ | |
| 68 | + 'result' => false, | |
| 69 | + 'response' => null, | |
| 70 | + 'multiple_users' => isset($error['message']) && $error['message'] === 'Multiple users found.', | |
| 71 | + ]; | |
| 72 | + } catch (\Exception $e) { | |
| 73 | + return [ | |
| 74 | + 'result' => false, | |
| 75 | + 'response' => null, | |
| 76 | + ]; | |
| 77 | + } | |
| 57 | 78 | } |
| 58 | 79 | |
| 59 | 80 | /** |
| 60 | 81 | * @description Initialize the api with current bearer token |
| @@ -64,9 +85,9 @@ | ||
| 64 | 85 | * @throws \Payplug\Exception\ConfigurationException |
| 65 | 86 | * |
| 66 | 87 | * @return void |
| 67 | 88 | */ |
| 68 | - protected function initialize($mode = '') | |
| 89 | + protected function initialize($mode = ''): void | |
| 69 | 90 | { |
| 70 | 91 | $bearer_token = $this->get_bearer_token($mode); |
| 71 | 92 | $this->api_payplug = new Payplug($bearer_token, '2019-08-06'); |
| 72 | 93 | HttpClient::setDefaultUserAgentProduct( |
| @@ -304,12 +325,12 @@ | ||
| 304 | 325 | : []; |
| 305 | 326 | } |
| 306 | 327 | |
| 307 | 328 | // todo: this method bellow should be implemented, do no removed it for now |
| 308 | - public function get_permissions() | |
| 329 | + public function get_permissions(): void | |
| 309 | 330 | { |
| 310 | 331 | } |
| 311 | 332 | |
| 312 | - public function get_register_url() | |
| 333 | + public function get_register_url(): void | |
| 313 | 334 | { |
| 314 | 335 | } |
| 315 | 336 | } |