Skip to content

Commit c85ed7e

Browse files
authored
Merge pull request #89 from payplug/release-3.6.0
Release 3.6.0
2 parents 8c16f35 + c28ce9f commit c85ed7e

File tree

6 files changed

+337
-11
lines changed

6 files changed

+337
-11
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Git Workflow Steps
128128

129129
git checkout master
130130
git pull origin master
131-
git tag -a v<version-number> -m "Release <version-number>"
131+
git tag -a <version-number> -m "Release <version-number>"
132132
git push origin master --tags
133133

134134
Usage

lib/Payplug/Authentication.php

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Authentication
1414
* This function is for user-friendly interface purpose only.
1515
* You should probably not use this more than once, login/password MUST NOT be stored and API Keys are enough to interact with API.
1616
*
17-
* @param string $email the user email
18-
* @param string $password the user password
17+
* @param string $email the user email
18+
* @param string $password the user password
1919
*
2020
* @return null|array the API keys
2121
*
@@ -130,6 +130,36 @@ public static function getPublishableKeys(Payplug $payplug = null)
130130
}
131131
}
132132

133+
/**
134+
* Generate a token JWT.
135+
*
136+
* @param Payplug $payplug the client configuration
137+
*
138+
* @return array the token JWT
139+
*
140+
* @throws Exception
141+
*/
142+
public static function generateJWT($client_id = '', Payplug $payplug = null)
143+
{
144+
if ($client_id == '') {
145+
return array();
146+
}
147+
148+
if ($payplug === null) {
149+
$payplug = Payplug::getDefaultConfiguration();
150+
}
151+
152+
$httpClient = new Core\HttpClient($payplug);
153+
try {
154+
return $httpClient->post(
155+
Core\APIRoutes::getRoute(Core\APIRoutes::$HYDRA_RESOURCE),
156+
array('client_id' => $client_id, 'grant_type' => 'client_credentials')
157+
);
158+
} catch (Exception $e) {
159+
return array();
160+
}
161+
}
162+
133163
/**
134164
* Validates the Payplug token
135165
*
@@ -144,4 +174,98 @@ private static function validateToken(Payplug $payplug)
144174
throw new ConfigurationException('The Payplug configuration requires a valid token.');
145175
}
146176
}
177+
178+
/**
179+
* Retrieve client datas from the user manager resource.
180+
*
181+
* @param Payplug $payplug the client configuration
182+
*
183+
* @return array the client id and client_secret_mask
184+
*
185+
* @throws Exception
186+
*/
187+
public static function getClientData($session = null, Payplug $payplug = null)
188+
{
189+
if ($payplug === null) {
190+
$payplug = Payplug::getDefaultConfiguration();
191+
}
192+
$kratosSession = self::setKratosSession($session);
193+
194+
$httpClient = new Core\HttpClient($payplug);
195+
$response = $httpClient->get(Core\APIRoutes::$USER_MANAGER_RESOURCE, null, $kratosSession);
196+
$result = array();
197+
foreach ($response['httpResponse'] as $client) {
198+
$result[] = array(
199+
'client_id' => $client['client_id'],
200+
'client_secret_mask' => $client['client_secret_mask'],
201+
'client_name' => $client['client_name'],
202+
'client_type' => $client['client_type'],
203+
'mode' => $client['mode'],
204+
205+
);
206+
}
207+
208+
return $result;
209+
}
210+
211+
/**
212+
* Create a client ID and secret for a given mode
213+
*
214+
* @param $company_id
215+
* @param $client_name
216+
* @param $mode
217+
* @param $session
218+
* @param Payplug|null $payplug
219+
* @return array
220+
* @throws ConfigurationException
221+
* @throws Exception\ConfigurationNotSetException
222+
* @throws Exception\ConnectionException
223+
* @throws Exception\HttpException
224+
* @throws Exception\UnexpectedAPIResponseException
225+
*/
226+
public static function createClientIdAndSecret($company_id='', $client_name='', $mode='', $session = null, Payplug $payplug = null)
227+
{
228+
229+
if ($payplug === null) {
230+
$payplug = Payplug::getDefaultConfiguration();
231+
}
232+
$kratosSession = self::setKratosSession($session);
233+
234+
$httpClient = new Core\HttpClient($payplug);
235+
$result = array();
236+
237+
$response = $httpClient->post(Core\APIRoutes::$USER_MANAGER_RESOURCE, array(
238+
'company_id' => $company_id,
239+
'client_name' => $client_name,
240+
'client_type' =>'oauth2',
241+
'mode' => $mode,
242+
), $kratosSession);
243+
foreach ($response['httpResponse'] as $client) {
244+
$result[] = array(
245+
'client_id' => $client['client_id'],
246+
'client_secret' => $client['client_secret'],
247+
);
248+
}
249+
250+
return $result;
251+
}
252+
253+
254+
255+
/**
256+
* Set the Kratos session cookie.
257+
*
258+
* @param string $session The session value to be set in the cookie.
259+
*
260+
* @return string The formatted Kratos session cookie string.
261+
* @throws ConfigurationException
262+
*/
263+
public static function setKratosSession($session)
264+
{
265+
if (empty($session)) {
266+
throw new ConfigurationException('The session value must be set.');
267+
}
268+
return 'ory_kratos_session=' . $session;
269+
}
270+
147271
}

lib/Payplug/Core/APIRoutes.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ class APIRoutes
1818
*/
1919
public static $MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE;
2020

21+
/**
22+
* @var string the root URL of the Hydra microService
23+
*/
24+
public static $HYDRA_RESOURCE;
25+
26+
/**
27+
* @var string the root URL of the User Manager microService
28+
*/
29+
public static $USER_MANAGER_RESOURCE;
30+
2131
const API_VERSION = 1;
2232

2333
// Resources routes
@@ -77,6 +87,24 @@ public static function setMerchantPluginsDataCollectorService($microServiceBaseU
7787
self::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = $microServiceBaseUrl;
7888
}
7989

90+
/**
91+
* @description set $HYDRA_RESOURCE from plugin
92+
* @param $microServiceBaseUrl
93+
*/
94+
public static function setHydraResource($microServiceBaseUrl)
95+
{
96+
self::$HYDRA_RESOURCE = $microServiceBaseUrl;
97+
}
98+
99+
/**
100+
* @description set $USER_MANAGER_RESOURCE from plugin
101+
* @param $microServiceBaseUrl
102+
*/
103+
public static function setUserManagerResource($microServiceBaseUrl)
104+
{
105+
self::$USER_MANAGER_RESOURCE = $microServiceBaseUrl;
106+
}
107+
80108
/**
81109
* Gets a route that allows to check whether the remote API is up.
82110
*
@@ -89,5 +117,6 @@ public static function getTestRoute()
89117
}
90118

91119
APIRoutes::$API_BASE_URL = 'https://api.payplug.com';
92-
APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = 'Microservice Url';
93-
120+
APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = 'https://retail.service.payplug.com/merchant-plugin-data-collectors/api/v1/plugin_telemetry';
121+
APIRoutes::$USER_MANAGER_RESOURCE ='User manager resource';
122+
APIRoutes::$HYDRA_RESOURCE = 'Microservice Url';

lib/Payplug/Core/HttpClient.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ public function delete($resource, $data = null)
122122
* @throws Payplug\Exception\HttpException When status code is not 2xx.
123123
* @throws Payplug\Exception\ConnectionException When an error was encountered while connecting to the resource.
124124
*/
125-
public function get($resource, $data = null)
125+
public function get($resource, $data = null, $cookie=null)
126126
{
127-
return $this->request('GET', $resource, $data);
127+
return $this->request('GET', $resource, $data, true, $cookie);
128128
}
129129

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

249+
if (!empty($cookie)) {
250+
$headers[] = 'Cookie:' . $cookie;
251+
}
252+
249253
$request->setopt(CURLOPT_FAILONERROR, false);
250254
$request->setopt(CURLOPT_RETURNTRANSFER, true);
251255
$request->setopt(CURLOPT_CUSTOMREQUEST, $httpVerb);
@@ -254,6 +258,7 @@ private function request($httpVerb, $resource, array $data = null, $authenticate
254258
$request->setopt(CURLOPT_SSL_VERIFYPEER, true);
255259
$request->setopt(CURLOPT_SSL_VERIFYHOST, 2);
256260
$request->setopt(CURLOPT_CAINFO, self::$CACERT_PATH);
261+
$request->setopt(CURLOPT_FOLLOWLOCATION, true);
257262
if (!empty($data)) {
258263
$request->setopt(CURLOPT_POSTFIELDS, json_encode($data));
259264
}

lib/Payplug/PluginTelemetry.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ class PluginTelemetry
2727
public static function send(string $data, Payplug\Payplug $payplug = null) {
2828

2929
$data = json_decode($data,true);
30-
$httpClient = new Payplug\Core\HttpClient();
30+
if ($payplug === null) {
31+
$payplug = Payplug\Payplug::getDefaultConfiguration();
32+
}
33+
34+
$httpClient = new Payplug\Core\HttpClient($payplug);
3135

3236
return $response = $httpClient->post(
3337
Payplug\Core\APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE,
34-
$data,
35-
false
38+
$data
3639
);
3740

3841
}

0 commit comments

Comments
 (0)