133 lines
3.3 KiB
PHP
133 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace KupShop\BalikonosBundle;
|
|
|
|
class BalikonosBase
|
|
{
|
|
public $test = false;
|
|
// App authentication
|
|
|
|
// benes@wpj.cz - Wpj987321
|
|
protected $app_auth = [
|
|
'id' => 'k7kvqh9xlr',
|
|
'password' => 'wpj987321',
|
|
'url' => 'https://rest.foxdeli.com/oauth/token/',
|
|
];
|
|
|
|
protected $app_auth_test = [
|
|
'id' => 'dmuv8yot7p',
|
|
'password' => 'prokop3210',
|
|
'url' => 'https://test.app.foxdeli.com/oauth/authorize/',
|
|
];
|
|
|
|
protected $redirect_url = 'https://www.kupshop.cz/balikonos/';
|
|
|
|
public function getClientId()
|
|
{
|
|
return $this->app_auth['id'];
|
|
}
|
|
|
|
/** Get new or reuse existing valid access token.
|
|
*
|
|
*/
|
|
public function get_token()
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
|
|
$token = getCache('balikonos_access_token');
|
|
|
|
if ($token) {
|
|
return $token;
|
|
}
|
|
|
|
$response = $this->request_token($dbcfg->balikonos['refresh_token']);
|
|
|
|
// Check return value
|
|
if (!empty($response->error)) {
|
|
exit('Chyba při získávání přístupu: '.$response->error_description);
|
|
}
|
|
|
|
if (empty($response->access_token)) {
|
|
exit('Chyba při získávání přístupu. Odpověď neobsahovala přístupové údaje.');
|
|
}
|
|
|
|
$token = $response->access_token;
|
|
setCache('balikonos_access_token', $token, $response->expires_in - 10);
|
|
|
|
return $token;
|
|
}
|
|
|
|
/**
|
|
* @param string $code access or refresh token
|
|
* @param bool $refresh is in $code refresh token?
|
|
*
|
|
* @return stdClass
|
|
*/
|
|
public function request_token($code, $refresh = true)
|
|
{
|
|
// Make token request parameters
|
|
$params = [
|
|
'scope' => 'deliveries collection-protocols',
|
|
'grant_type' => 'authorization_code',
|
|
];
|
|
|
|
$params['redirect_uri'] = $this->redirect_url;
|
|
|
|
if ($refresh) {
|
|
$params['refresh_token'] = $code;
|
|
$params['grant_type'] = 'refresh_token';
|
|
} else {
|
|
$params['code'] = $code;
|
|
}
|
|
|
|
return $this->make_request($params);
|
|
}
|
|
|
|
/** Make request to Balikonos.
|
|
* @param array $fields parameters
|
|
*
|
|
* @return stdClass
|
|
*/
|
|
public function make_request($fields)
|
|
{
|
|
$params = join('&', array_map(function ($key, $value) {
|
|
return join('=', [$key, urlencode($value)]);
|
|
}, array_keys($fields), $fields)
|
|
);
|
|
|
|
$url = $this->app_auth['url'];
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
$this->getAuth(),
|
|
'Content-type: application/x-www-form-urlencoded',
|
|
]);
|
|
|
|
// set the url, number of POST vars, POST data
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
// execute post
|
|
$result = curl_exec($ch);
|
|
|
|
// close connection
|
|
curl_close($ch);
|
|
|
|
return json_decode($result);
|
|
}
|
|
|
|
public function getAuth()
|
|
{
|
|
return 'Authorization: Basic '.base64_encode($this->app_auth['id'].':'.$this->app_auth['password']);
|
|
}
|
|
|
|
public function setTestMode()
|
|
{
|
|
$this->redirect_url .= '?test=1';
|
|
|
|
$this->app_auth = $this->app_auth_test;
|
|
}
|
|
}
|