diff --git a/lib/Payplug/Authentication.php b/lib/Payplug/Authentication.php index 6eddb78..1a9e705 100644 --- a/lib/Payplug/Authentication.php +++ b/lib/Payplug/Authentication.php @@ -1,4 +1,5 @@ $client['client_id'], 'client_secret_mask' => $client['client_secret_mask'], - 'client_name' => $client['client_name'], + 'client_name' => $client['client_name'], 'client_type' => $client['client_type'], 'mode' => $client['mode'], @@ -223,7 +224,7 @@ public static function getClientData($session = null, Payplug $payplug = null) * @throws Exception\HttpException * @throws Exception\UnexpectedAPIResponseException */ - public static function createClientIdAndSecret($company_id='', $client_name='', $mode='', $session = null, Payplug $payplug = null) + public static function createClientIdAndSecret($company_id = '', $client_name = '', $mode = '', $session = null, Payplug $payplug = null) { if ($payplug === null) { @@ -234,24 +235,22 @@ public static function createClientIdAndSecret($company_id='', $client_name='', $httpClient = new Core\HttpClient($payplug); $result = array(); - $response = $httpClient->post(Core\APIRoutes::$USER_MANAGER_RESOURCE, array( - 'company_id' => $company_id, - 'client_name' => $client_name, - 'client_type' =>'oauth2', - 'mode' => $mode, - ), $kratosSession); + $response = $httpClient->post(Core\APIRoutes::$USER_MANAGER_RESOURCE, array( + 'company_id' => $company_id, + 'client_name' => $client_name, + 'client_type' => 'oauth2', + 'mode' => $mode, + ), $kratosSession); foreach ($response['httpResponse'] as $client) { - $result[] = array( - 'client_id' => $client['client_id'], - 'client_secret' => $client['client_secret'], - ); + $result[] = array( + 'client_id' => $client['client_id'], + 'client_secret' => $client['client_secret'], + ); } return $result; } - - /** * Set the Kratos session cookie. * @@ -268,4 +267,37 @@ public static function setKratosSession($session) return 'ory_kratos_session=' . $session; } + /** + * Get the return url to register user through the portal + * + * @param string $setup_redirection_uri + * @param string $oauth_callback_uri + * + * @return array + * @throws Exception\ConnectionException + * @throws Exception\HttpException + * @throws Exception\UnexpectedAPIResponseException + */ + public static function getRegisterUrl($setup_redirection_uri = '', $oauth_callback_uri = '') + { + if (empty($setup_redirection_uri)) { + throw new Exception\ConfigurationException('Expected string values for setup redirection uri.'); + } + if (empty($oauth_callback_uri)) { + throw new Exception\ConfigurationException('Expected string values for oauth callback uri.'); + } + $httpClient = new Core\HttpClient(null); + + $url_datas = array( + 'setup_redirection_uri' => $setup_redirection_uri, + 'oauth_callback_uri' => $oauth_callback_uri, + ); + $register_uri = Core\APIRoutes::$PLUGIN_SETUP_URL . '?' . http_build_query($url_datas); + + return $httpClient->get( + $register_uri, + null, + false + ); + } } diff --git a/lib/Payplug/Core/APIRoutes.php b/lib/Payplug/Core/APIRoutes.php index c8055e0..1838467 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 Plugin setup route + */ + public static $PLUGIN_SETUP_URL; + /** * @var string the root URL of the Hydra microService */ @@ -87,6 +92,15 @@ public static function setMerchantPluginsDataCollectorService($microServiceBaseU self::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = $microServiceBaseUrl; } + /** + * @description set $PLUGIN_SETUP_URL from plugin + * @param $pluginSetupUrl + */ + public static function setPluginSetupUrl($pluginSetupUrl) + { + self::$PLUGIN_SETUP_URL = $pluginSetupUrl; + } + /** * @description set $HYDRA_RESOURCE from plugin * @param $microServiceBaseUrl @@ -118,5 +132,6 @@ public static function getTestRoute() APIRoutes::$API_BASE_URL = 'https://api.payplug.com'; APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = 'https://retail.service.payplug.com/merchant-plugin-data-collectors/api/v1/plugin_telemetry'; +APIRoutes::$PLUGIN_SETUP_URL = "https://retail.service.payplug.com/users/api/v1/plugin_setup"; APIRoutes::$USER_MANAGER_RESOURCE ='User manager resource'; APIRoutes::$HYDRA_RESOURCE = 'Microservice Url'; diff --git a/lib/Payplug/Core/HttpClient.php b/lib/Payplug/Core/HttpClient.php index 08ed9ca..695e64d 100644 --- a/lib/Payplug/Core/HttpClient.php +++ b/lib/Payplug/Core/HttpClient.php @@ -111,6 +111,8 @@ public function delete($resource, $data = null) * * @param string $resource the path to the remote resource * @param array $data Request data + * @param bool $authenticated + * @param bool $cookie * * @return array the response in a dictionary with following keys: *
@@ -122,9 +124,9 @@ public function delete($resource, $data = null)
* @throws Payplug\Exception\HttpException When status code is not 2xx.
* @throws Payplug\Exception\ConnectionException When an error was encountered while connecting to the resource.
*/
- public function get($resource, $data = null, $cookie=null)
+ public function get($resource, $data = null, $authenticated = true, $cookie=null)
{
- return $this->request('GET', $resource, $data, true, $cookie);
+ return $this->request('GET', $resource, $data, $authenticated, $cookie);
}
/**
diff --git a/tests/unit_tests/AuthenticationTest.php b/tests/unit_tests/AuthenticationTest.php
index 90c6249..bdf34e2 100644
--- a/tests/unit_tests/AuthenticationTest.php
+++ b/tests/unit_tests/AuthenticationTest.php
@@ -1,14 +1,16 @@
_configuration = new \Payplug\Payplug('abc','1970-01-01');
+ $this->_configuration = new \Payplug\Payplug('abc', '1970-01-01');
Payplug\Payplug::setDefaultConfiguration($this->_configuration);
$this->_requestMock = $this->createMock('\Payplug\Core\IHttpRequest');
@@ -53,8 +55,8 @@ public function testGetKeysByLogin()
$this->_requestMock
->expects($this->any())
->method('getinfo')
- ->will($this->returnCallback(function($option) {
- switch($option) {
+ ->will($this->returnCallback(function ($option) {
+ switch ($option) {
case CURLINFO_HTTP_CODE:
return 201;
}
@@ -92,8 +94,8 @@ public function testGetAccount()
$this->_requestMock
->expects($this->any())
->method('getinfo')
- ->will($this->returnCallback(function($option) {
- switch($option) {
+ ->will($this->returnCallback(function ($option) {
+ switch ($option) {
case CURLINFO_HTTP_CODE:
return 200;
}
@@ -124,6 +126,7 @@ public function testGetAccountWithoutSecretKey()
Authentication::getAccount();
}
+
public function testGetPermissions()
{
$response = array(
@@ -151,8 +154,8 @@ public function testGetPermissions()
$this->_requestMock
->expects($this->any())
->method('getinfo')
- ->will($this->returnCallback(function($option) {
- switch($option) {
+ ->will($this->returnCallback(function ($option) {
+ switch ($option) {
case CURLINFO_HTTP_CODE:
return 200;
}
@@ -168,7 +171,6 @@ public function testGetPermissions()
$this->assertEquals(false, $permissions['can_save_cards']);
}
-
/**
* Tests the getPermissions method when no secret key is provided.
* @return void
@@ -184,7 +186,6 @@ public function testGetPermissionsWithoutSecretKey()
Authentication::getPermissions();
}
-
public function testPublishableKeys()
{
$response = array(
@@ -199,8 +200,8 @@ public function testPublishableKeys()
$this->_requestMock
->expects($this->any())
->method('getinfo')
- ->will($this->returnCallback(function($option) {
- switch($option) {
+ ->will($this->returnCallback(function ($option) {
+ switch ($option) {
case CURLINFO_HTTP_CODE:
return 200;
}
@@ -241,8 +242,8 @@ public function testGetClientData()
$this->_requestMock
->expects($this->any())
->method('getinfo')
- ->will($this->returnCallback(function($option) {
- switch($option) {
+ ->will($this->returnCallback(function ($option) {
+ switch ($option) {
case CURLINFO_HTTP_CODE:
return 200;
}
@@ -269,7 +270,7 @@ public function testGetClientData()
public function testCreateClientIdAndSecret()
{
$response = array(
- array(
+ array(
'client_id' => 'test_client_id',
'client_secret' => 'test_client_secret',
),
@@ -283,8 +284,8 @@ public function testCreateClientIdAndSecret()
$this->_requestMock
->expects($this->any())
->method('getinfo')
- ->will($this->returnCallback(function($option) {
- switch($option) {
+ ->will($this->returnCallback(function ($option) {
+ switch ($option) {
case CURLINFO_HTTP_CODE:
return 200;
}
@@ -333,8 +334,8 @@ public function testGenerateJWTWhenErrorResponse()
$this->_requestMock
->expects($this->any())
->method('getinfo')
- ->will($this->returnCallback(function($option) {
- switch($option) {
+ ->will($this->returnCallback(function ($option) {
+ switch ($option) {
case CURLINFO_HTTP_CODE:
return 401;
}
@@ -363,8 +364,8 @@ public function testGenerateJWTWhenSuccessResponse()
$this->_requestMock
->expects($this->any())
->method('getinfo')
- ->will($this->returnCallback(function($option) {
- switch($option) {
+ ->will($this->returnCallback(function ($option) {
+ switch ($option) {
case CURLINFO_HTTP_CODE:
return 200;
}
@@ -376,4 +377,32 @@ public function testGenerateJWTWhenSuccessResponse()
$this->assertEquals(200, $jwt['httpStatus']);
$this->assertEquals($response, $jwt['httpResponse']);
}
+
+ public function testGetRegisterUrl()
+ {
+ $setup_redirection_uri = 'setup.redirection.uri.com';
+ $oauth_callback_uri = 'oauth.callback.uri.com';
+ $response = array(
+ 'redirect_to' => 'portal.uri.com'
+ );
+ $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;
+ }));
+
+ $authentication = Authentication::getRegisterUrl($setup_redirection_uri, $oauth_callback_uri);
+ $this->assertEquals(200, $authentication['httpStatus']);
+ $this->assertEquals($response, $authentication['httpResponse']);
+ }
}