Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 53 additions & 21 deletions lib/Payplug/Authentication.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Payplug;

use Exception;
Expand All @@ -14,8 +15,8 @@ class Authentication
* This function is for user-friendly interface purpose only.
* You should probably not use this more than once, login/password MUST NOT be stored and API Keys are enough to interact with API.
*
* @param string $email the user email
* @param string $password the user password
* @param string $email the user email
* @param string $password the user password
*
* @return null|array the API keys
*
Expand All @@ -35,7 +36,7 @@ public static function getKeysByLogin($email, $password)
/**
* Retrieve account info.
*
* @param Payplug $payplug the client configuration
* @param Payplug $payplug the client configuration
*
* @return null|array the account settings
*
Expand All @@ -58,7 +59,7 @@ public static function getAccount(Payplug $payplug = null)
/**
* Retrieve the account permissions
*
* @param Payplug $payplug the client configuration
* @param Payplug $payplug the client configuration
*
* @return null|array the account permissions
*
Expand Down Expand Up @@ -110,7 +111,7 @@ public static function getPermissionsByLogin($email, $password)
/**
* Retrieve publisable keys
*
* @param Payplug $payplug the client configuration
* @param Payplug $payplug the client configuration
*
* @return array|false
*
Expand All @@ -133,7 +134,7 @@ public static function getPublishableKeys(Payplug $payplug = null)
/**
* Generate a token JWT.
*
* @param Payplug $payplug the client configuration
* @param Payplug $payplug the client configuration
*
* @return array the token JWT
*
Expand Down Expand Up @@ -178,7 +179,7 @@ private static function validateToken(Payplug $payplug)
/**
* Retrieve client datas from the user manager resource.
*
* @param Payplug $payplug the client configuration
* @param Payplug $payplug the client configuration
*
* @return array the client id and client_secret_mask
*
Expand All @@ -198,7 +199,7 @@ public static function getClientData($session = null, Payplug $payplug = null)
$result[] = array(
'client_id' => $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'],

Expand All @@ -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) {
Expand All @@ -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.
*
Expand All @@ -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
);
}
}
15 changes: 15 additions & 0 deletions lib/Payplug/Core/APIRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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';
6 changes: 4 additions & 2 deletions lib/Payplug/Core/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* <pre>
Expand All @@ -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);
}

/**
Expand Down
77 changes: 53 additions & 24 deletions tests/unit_tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

namespace Payplug;

use Payplug;
use Payplug\Core\HttpClient;
use Payplug\Exception\ConfigurationException;

/**
* @group unit
* @group ci
* @group recommended
*/
* @group unit
* @group ci
* @group recommended
*/
class AuthenticationTest extends \PHPUnit\Framework\TestCase
{
private $_configuration;
Expand All @@ -28,7 +30,7 @@ protected function setUpTest()

protected function setUpTwice()
{
$this->_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');
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -124,6 +126,7 @@ public function testGetAccountWithoutSecretKey()

Authentication::getAccount();
}

public function testGetPermissions()
{
$response = array(
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -184,7 +186,6 @@ public function testGetPermissionsWithoutSecretKey()
Authentication::getPermissions();
}


public function testPublishableKeys()
{
$response = array(
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -269,7 +270,7 @@ public function testGetClientData()
public function testCreateClientIdAndSecret()
{
$response = array(
array(
array(
'client_id' => 'test_client_id',
'client_secret' => 'test_client_secret',
),
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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']);
}
}
Loading