diff --git a/lib/Payplug/Authentication.php b/lib/Payplug/Authentication.php index a4650e3..94b2b6b 100644 --- a/lib/Payplug/Authentication.php +++ b/lib/Payplug/Authentication.php @@ -121,4 +121,27 @@ public static function getPublishableKeys(Payplug $payplug = null) return false; } } + + /** + * Generate a token JWT. + * + * @param Payplug $payplug the client configuration + * + * @return array the token JWT + * + * @throws Exception + */ + public static function generateJWT(Payplug $payplug = null) + { + if ($payplug === null) { + $payplug = Payplug::getDefaultConfiguration(); + } + + $httpClient = new Core\HttpClient($payplug); + try { + return $httpClient->post(Core\APIRoutes::getRoute(Core\APIRoutes::$HYDRA_RESOURCE)); + } catch (Exception $e) { + return array(); + } + } } diff --git a/lib/Payplug/Core/APIRoutes.php b/lib/Payplug/Core/APIRoutes.php index dc39efc..c882a78 100644 --- a/lib/Payplug/Core/APIRoutes.php +++ b/lib/Payplug/Core/APIRoutes.php @@ -18,6 +18,11 @@ class APIRoutes */ public static $MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE; + /** + * @var string the root URL of the Hydra microService + */ + public static $HYDRA_RESOURCE; + const API_VERSION = 1; // Resources routes @@ -77,6 +82,15 @@ public static function setMerchantPluginsDataCollectorService($microServiceBaseU self::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = $microServiceBaseUrl; } + /** + * @description set $HYDRA_RESOURCE from plugin + * @param $microServiceBaseUrl + */ + public static function setHydraResource($microServiceBaseUrl) + { + self::$HYDRA_RESOURCE = $microServiceBaseUrl; + } + /** * Gets a route that allows to check whether the remote API is up. * @@ -90,4 +104,5 @@ public static function getTestRoute() APIRoutes::$API_BASE_URL = 'https://api.payplug.com'; APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = 'Microservice Url'; +APIRoutes::$HYDRA_RESOURCE = 'Microservice Url'; diff --git a/tests/unit_tests/AuthenticationTest.php b/tests/unit_tests/AuthenticationTest.php index d659728..4b67162 100644 --- a/tests/unit_tests/AuthenticationTest.php +++ b/tests/unit_tests/AuthenticationTest.php @@ -1,7 +1,6 @@ assertEquals(200, $publishable_keys['httpStatus']); $this->assertEquals('pk_test_everythingIsUnderControl', $publishable_keys['httpResponse']['publishable_key']); } + + public function testWhenErrorResponse() + { + $response = array( + 'access_token' => 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImE2NmQxZWU3LTQzMWMtNDNiYS04NzA4LWQ1MzNkNTVmZjhlZCIsInR5cCI6IkpXVCJ9.eyJhdWQiOltdLCJjbGllbnRfaWQiOiIyNzdlNDk4MS0yYTBjLTQ4NGEtYTE3Ni1hZWNhOWRjNDhkNTQiLCJleHAiOjE3Mjc5NjU3OTQsImV4dCI6eyJjb21wYW55X2lkIjoiZDE0NzQ1ZmQtMTc5Yy00N2IxLTlkZDgtMTk3OTVmMzQ1MjJiIiwicmVhbG1faWQiOiIxNTM0N2NjNy0xZThmLTQzYzMtYjJjZi1iZDMxY2M5ZWU4YTEiLCJyZXNvdXJjZV9hY2Nlc3MiOnsiYXBpIjp7InJvbGVzIjpbIk1FUkNIQU5UX0FQUFMiXX19fSwiaWF0IjoxNzI3OTYyMTk0LCJpc3MiOiJodHRwczovLzEyNy4wLjAuMTo0NDQ0IiwianRpIjoiMGNhMzUyOWItOGQ5Zi00NDQxLWEyNDAtMGZhY2YyNDNmN2JiIiwibmJmIjoxNzI3OTYyMTk0LCJzY3AiOltdLCJzdWIiOiIyNzdlNDk4MS0yYTBjLTQ4NGEtYTE3Ni1hZWNhOWRjNDhkNTQifQ.K1pavlVMz4D4cAJtL8IklLXIos7ZjiC9YSofb343uLkjXdhbgel23k0GyEE_JkQ2xSSB46XLYQp0j-M1AaJIoNjCfVR-O1yWNpLYnLM07ECETO3kQc63vcvzOm5trn5oBq_T3FE78EmAIA5B3oaSu_m5_qUBci_C7oM0ItMMIpFnKYqk2ta8y2eUFFPu7detxJRLlBK4I7hW0xAt07GNhfyRl8eN7twC3aYFFkUejZmuEB_FmZkj7OqZDsNblDR21Ci_cahuZmt9WOmIqTW58l7wxXOB9vq5APtBi2LpQtE52ARofUNCWWOK1KHz_vSQlGiGDM6_56K85Whfkkj-LYvLRRZUuIXE2m528JTtnCarZr7Md5P9zHQOZyIbcWrjiUdM_daI1vELPT4UaCBIfpy-vY0wywiPtoosokNsFQNrwxo8f9affUkiuEwZedK9sreDfVL_tmrz5Bh6XzxMZB5ZiVQckUUPF7LKrBB8qDwotYvwdLIN-Wy3l2IeeTzF_NOFmO6mrNift2RhSQZP0s7Xfn2dVK1eiyO4gERNrsPvasb9nB15PIQ57wwWKzN8ue6z9utAX6YThTgc2fadrOWYMeo2W4c7KmuKr9hhLK2ThIWRM7H5rQq3H7Ke5AzlKyCQdgFlQzSl0O1gjzA0T4AnfuNW1zNedSfUsqMJCfM', + 'expires_in' => 3599, + 'scope' => '', + 'token_type' => 'bearer' + ); + + $this->_requestMock + ->expects($this->once()) + ->method('exec') + ->will($this->returnValue(json_encode($response))); + + $this->_requestMock + ->expects($this->any()) + ->method('getinfo') + ->will($this->returnCallback(function($option) { + switch($option) { + case CURLINFO_HTTP_CODE: + return 401; + } + return null; + })); + + $jwt = Authentication::generateJWT($this->_configuration); + + $this->assertEquals(array(), $jwt); + } + + public function testWhenSuccessResponse() + { + $response = array( + 'access_token' => 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImE2NmQxZWU3LTQzMWMtNDNiYS04NzA4LWQ1MzNkNTVmZjhlZCIsInR5cCI6IkpXVCJ9.eyJhdWQiOltdLCJjbGllbnRfaWQiOiIyNzdlNDk4MS0yYTBjLTQ4NGEtYTE3Ni1hZWNhOWRjNDhkNTQiLCJleHAiOjE3Mjc5NjU3OTQsImV4dCI6eyJjb21wYW55X2lkIjoiZDE0NzQ1ZmQtMTc5Yy00N2IxLTlkZDgtMTk3OTVmMzQ1MjJiIiwicmVhbG1faWQiOiIxNTM0N2NjNy0xZThmLTQzYzMtYjJjZi1iZDMxY2M5ZWU4YTEiLCJyZXNvdXJjZV9hY2Nlc3MiOnsiYXBpIjp7InJvbGVzIjpbIk1FUkNIQU5UX0FQUFMiXX19fSwiaWF0IjoxNzI3OTYyMTk0LCJpc3MiOiJodHRwczovLzEyNy4wLjAuMTo0NDQ0IiwianRpIjoiMGNhMzUyOWItOGQ5Zi00NDQxLWEyNDAtMGZhY2YyNDNmN2JiIiwibmJmIjoxNzI3OTYyMTk0LCJzY3AiOltdLCJzdWIiOiIyNzdlNDk4MS0yYTBjLTQ4NGEtYTE3Ni1hZWNhOWRjNDhkNTQifQ.K1pavlVMz4D4cAJtL8IklLXIos7ZjiC9YSofb343uLkjXdhbgel23k0GyEE_JkQ2xSSB46XLYQp0j-M1AaJIoNjCfVR-O1yWNpLYnLM07ECETO3kQc63vcvzOm5trn5oBq_T3FE78EmAIA5B3oaSu_m5_qUBci_C7oM0ItMMIpFnKYqk2ta8y2eUFFPu7detxJRLlBK4I7hW0xAt07GNhfyRl8eN7twC3aYFFkUejZmuEB_FmZkj7OqZDsNblDR21Ci_cahuZmt9WOmIqTW58l7wxXOB9vq5APtBi2LpQtE52ARofUNCWWOK1KHz_vSQlGiGDM6_56K85Whfkkj-LYvLRRZUuIXE2m528JTtnCarZr7Md5P9zHQOZyIbcWrjiUdM_daI1vELPT4UaCBIfpy-vY0wywiPtoosokNsFQNrwxo8f9affUkiuEwZedK9sreDfVL_tmrz5Bh6XzxMZB5ZiVQckUUPF7LKrBB8qDwotYvwdLIN-Wy3l2IeeTzF_NOFmO6mrNift2RhSQZP0s7Xfn2dVK1eiyO4gERNrsPvasb9nB15PIQ57wwWKzN8ue6z9utAX6YThTgc2fadrOWYMeo2W4c7KmuKr9hhLK2ThIWRM7H5rQq3H7Ke5AzlKyCQdgFlQzSl0O1gjzA0T4AnfuNW1zNedSfUsqMJCfM', + 'expires_in' => 3599, + 'scope' => '', + 'token_type' => 'bearer' + ); + + $this->_requestMock + ->expects($this->once()) + ->method('exec') + ->will($this->returnValue(json_encode($response))); + + $this->_requestMock + ->expects($this->any()) + ->method('getinfo') + ->will($this->returnCallback(function($option) { + switch($option) { + case CURLINFO_HTTP_CODE: + return 200; + } + return null; + })); + + $jwt = Authentication::generateJWT($this->_configuration); + + $this->assertEquals(200, $jwt['httpStatus']); + $this->assertEquals($response, $jwt['httpResponse']); + } }