Skip to content

Commit

Permalink
Merge pull request #89 from payplug/release-3.6.0
Browse files Browse the repository at this point in the history
Release 3.6.0
  • Loading branch information
ilajili authored Dec 11, 2024
2 parents 8c16f35 + c28ce9f commit c85ed7e
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Git Workflow Steps

git checkout master
git pull origin master
git tag -a v<version-number> -m "Release <version-number>"
git tag -a <version-number> -m "Release <version-number>"
git push origin master --tags

Usage
Expand Down
128 changes: 126 additions & 2 deletions lib/Payplug/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,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 Down Expand Up @@ -130,6 +130,36 @@ public static function getPublishableKeys(Payplug $payplug = null)
}
}

/**
* Generate a token JWT.
*
* @param Payplug $payplug the client configuration
*
* @return array the token JWT
*
* @throws Exception
*/
public static function generateJWT($client_id = '', Payplug $payplug = null)
{
if ($client_id == '') {
return array();
}

if ($payplug === null) {
$payplug = Payplug::getDefaultConfiguration();
}

$httpClient = new Core\HttpClient($payplug);
try {
return $httpClient->post(
Core\APIRoutes::getRoute(Core\APIRoutes::$HYDRA_RESOURCE),
array('client_id' => $client_id, 'grant_type' => 'client_credentials')
);
} catch (Exception $e) {
return array();
}
}

/**
* Validates the Payplug token
*
Expand All @@ -144,4 +174,98 @@ private static function validateToken(Payplug $payplug)
throw new ConfigurationException('The Payplug configuration requires a valid token.');
}
}

/**
* Retrieve client datas from the user manager resource.
*
* @param Payplug $payplug the client configuration
*
* @return array the client id and client_secret_mask
*
* @throws Exception
*/
public static function getClientData($session = null, Payplug $payplug = null)
{
if ($payplug === null) {
$payplug = Payplug::getDefaultConfiguration();
}
$kratosSession = self::setKratosSession($session);

$httpClient = new Core\HttpClient($payplug);
$response = $httpClient->get(Core\APIRoutes::$USER_MANAGER_RESOURCE, null, $kratosSession);
$result = array();
foreach ($response['httpResponse'] as $client) {
$result[] = array(
'client_id' => $client['client_id'],
'client_secret_mask' => $client['client_secret_mask'],
'client_name' => $client['client_name'],
'client_type' => $client['client_type'],
'mode' => $client['mode'],

);
}

return $result;
}

/**
* Create a client ID and secret for a given mode
*
* @param $company_id
* @param $client_name
* @param $mode
* @param $session
* @param Payplug|null $payplug
* @return array
* @throws ConfigurationException
* @throws Exception\ConfigurationNotSetException
* @throws Exception\ConnectionException
* @throws Exception\HttpException
* @throws Exception\UnexpectedAPIResponseException
*/
public static function createClientIdAndSecret($company_id='', $client_name='', $mode='', $session = null, Payplug $payplug = null)
{

if ($payplug === null) {
$payplug = Payplug::getDefaultConfiguration();
}
$kratosSession = self::setKratosSession($session);

$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);
foreach ($response['httpResponse'] as $client) {
$result[] = array(
'client_id' => $client['client_id'],
'client_secret' => $client['client_secret'],
);
}

return $result;
}



/**
* Set the Kratos session cookie.
*
* @param string $session The session value to be set in the cookie.
*
* @return string The formatted Kratos session cookie string.
* @throws ConfigurationException
*/
public static function setKratosSession($session)
{
if (empty($session)) {
throw new ConfigurationException('The session value must be set.');
}
return 'ory_kratos_session=' . $session;
}

}
33 changes: 31 additions & 2 deletions lib/Payplug/Core/APIRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class APIRoutes
*/
public static $MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE;

/**
* @var string the root URL of the Hydra microService
*/
public static $HYDRA_RESOURCE;

/**
* @var string the root URL of the User Manager microService
*/
public static $USER_MANAGER_RESOURCE;

const API_VERSION = 1;

// Resources routes
Expand Down Expand Up @@ -77,6 +87,24 @@ 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;
}

/**
* @description set $USER_MANAGER_RESOURCE from plugin
* @param $microServiceBaseUrl
*/
public static function setUserManagerResource($microServiceBaseUrl)
{
self::$USER_MANAGER_RESOURCE = $microServiceBaseUrl;
}

/**
* Gets a route that allows to check whether the remote API is up.
*
Expand All @@ -89,5 +117,6 @@ public static function getTestRoute()
}

APIRoutes::$API_BASE_URL = 'https://api.payplug.com';
APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = 'Microservice Url';

APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = 'https://retail.service.payplug.com/merchant-plugin-data-collectors/api/v1/plugin_telemetry';
APIRoutes::$USER_MANAGER_RESOURCE ='User manager resource';
APIRoutes::$HYDRA_RESOURCE = 'Microservice Url';
11 changes: 8 additions & 3 deletions lib/Payplug/Core/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,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)
public function get($resource, $data = null, $cookie=null)
{
return $this->request('GET', $resource, $data);
return $this->request('GET', $resource, $data, true, $cookie);
}

/**
Expand Down Expand Up @@ -226,7 +226,7 @@ public static function getUserAgent()
* @throws Payplug\Exception\HttpException When status code is not 2xx.
* @throws Payplug\Exception\ConnectionException When an error was encountered while connecting to the resource.
*/
private function request($httpVerb, $resource, array $data = null, $authenticated = true)
private function request($httpVerb, $resource, array $data = null, $authenticated = true, $cookie = null)
{
if (self::$REQUEST_HANDLER === null) {
$request = new CurlRequest();
Expand All @@ -246,6 +246,10 @@ private function request($httpVerb, $resource, array $data = null, $authenticate
$headers[] = 'PayPlug-Version: ' . $this->_configuration->getApiVersion();
}

if (!empty($cookie)) {
$headers[] = 'Cookie:' . $cookie;
}

$request->setopt(CURLOPT_FAILONERROR, false);
$request->setopt(CURLOPT_RETURNTRANSFER, true);
$request->setopt(CURLOPT_CUSTOMREQUEST, $httpVerb);
Expand All @@ -254,6 +258,7 @@ private function request($httpVerb, $resource, array $data = null, $authenticate
$request->setopt(CURLOPT_SSL_VERIFYPEER, true);
$request->setopt(CURLOPT_SSL_VERIFYHOST, 2);
$request->setopt(CURLOPT_CAINFO, self::$CACERT_PATH);
$request->setopt(CURLOPT_FOLLOWLOCATION, true);
if (!empty($data)) {
$request->setopt(CURLOPT_POSTFIELDS, json_encode($data));
}
Expand Down
9 changes: 6 additions & 3 deletions lib/Payplug/PluginTelemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ class PluginTelemetry
public static function send(string $data, Payplug\Payplug $payplug = null) {

$data = json_decode($data,true);
$httpClient = new Payplug\Core\HttpClient();
if ($payplug === null) {
$payplug = Payplug\Payplug::getDefaultConfiguration();
}

$httpClient = new Payplug\Core\HttpClient($payplug);

return $response = $httpClient->post(
Payplug\Core\APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE,
$data,
false
$data
);

}
Expand Down
Loading

0 comments on commit c85ed7e

Please sign in to comment.