Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
Update SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
regdos committed Aug 3, 2018
1 parent aad7129 commit d21cbd6
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 20 deletions.
155 changes: 153 additions & 2 deletions upload/system/library/sdk_v21/OpenPayU/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* OpenPayU Standard Library
*
* @copyright Copyright (c) 2011-2016 PayU
* @copyright Copyright (c) 2011-2017 PayU
* @license http://opensource.org/licenses/LGPL-3.0 Open Software License (LGPL 3.0)
* http://www.payu.com
* http://developers.payu.com
Expand All @@ -25,6 +25,10 @@ class OpenPayU_Configuration
*/
private static $signatureKey = '';

/**
* OAuth protocol - default type
*/
private static $oauthGrantType = OauthGrantType::CLIENT_CREDENTIAL;
/**
* OAuth protocol - client_id
*/
Expand All @@ -35,6 +39,16 @@ class OpenPayU_Configuration
*/
private static $oauthClientSecret = '';

/**
* OAuth protocol - email
*/
private static $oauthEmail = '';

/**
* OAuth protocol - extCustomerId
*/
private static $oauthExtCustomerId;

/**
* OAuth protocol - endpoint address
*/
Expand All @@ -45,14 +59,34 @@ class OpenPayU_Configuration
*/
private static $oauthTokenCache = null;

/**
* Proxy - host
*/
private static $proxyHost = null;

/**
* Proxy - port
*/
private static $proxyPort = null;

/**
* Proxy - user
*/
private static $proxyUser = null;

/**
* Proxy - password
*/
private static $proxyPassword = null;

private static $serviceUrl = '';
private static $hashAlgorithm = 'SHA-256';

private static $sender = 'Generic';

const API_VERSION = '2.1';
const COMPOSER_JSON = "/composer.json";
const DEFAULT_SDK_VERSION = 'PHP SDK 2.2.3';
const DEFAULT_SDK_VERSION = 'PHP SDK 2.2.10';
const OAUTH_CONTEXT = 'pl/standard/user/oauth/authorize';

/**
Expand Down Expand Up @@ -170,6 +204,27 @@ public static function getSignatureKey()
return self::$signatureKey;
}

/**
* @return string
*/
public static function getOauthGrantType()
{
return self::$oauthGrantType;
}

/**
* @param string $oauthGrantType
* @throws OpenPayU_Exception_Configuration
*/
public static function setOauthGrantType($oauthGrantType)
{
if ($oauthGrantType !== OauthGrantType::CLIENT_CREDENTIAL && $oauthGrantType !== OauthGrantType::TRUSTED_MERCHANT) {
throw new OpenPayU_Exception_Configuration('Oauth grand type "' . $oauthGrantType . '"" is not available');
}

self::$oauthGrantType = $oauthGrantType;
}

/**
* @return string
*/
Expand Down Expand Up @@ -202,6 +257,38 @@ public static function setOauthClientSecret($oauthClientSecret)
self::$oauthClientSecret = trim($oauthClientSecret);
}

/**
* @return mixed
*/
public static function getOauthEmail()
{
return self::$oauthEmail;
}

/**
* @param mixed $oauthEmail
*/
public static function setOauthEmail($oauthEmail)
{
self::$oauthEmail = $oauthEmail;
}

/**
* @return mixed
*/
public static function getOauthExtCustomerId()
{
return self::$oauthExtCustomerId;
}

/**
* @param mixed $oauthExtCustomerId
*/
public static function setOauthExtCustomerId($oauthExtCustomerId)
{
self::$oauthExtCustomerId = $oauthExtCustomerId;
}

/**
* @return null | OauthCacheInterface
*/
Expand All @@ -222,6 +309,70 @@ public static function setOauthTokenCache($oauthTokenCache)
self::$oauthTokenCache = $oauthTokenCache;
}

/**
* @return string | null
*/
public static function getProxyHost()
{
return self::$proxyHost;
}

/**
* @param string | null $proxyHost
*/
public static function setProxyHost($proxyHost)
{
self::$proxyHost = $proxyHost;
}

/**
* @return int | null
*/
public static function getProxyPort()
{
return self::$proxyPort;
}

/**
* @param int | null $proxyPort
*/
public static function setProxyPort($proxyPort)
{
self::$proxyPort = $proxyPort;
}

/**
* @return string | null
*/
public static function getProxyUser()
{
return self::$proxyUser;
}

/**
* @param string | null $proxyUser
*/
public static function setProxyUser($proxyUser)
{
self::$proxyUser = $proxyUser;
}

/**
* @return string | null
*/
public static function getProxyPassword()
{
return self::$proxyPassword;
}

/**
* @param string | null $proxyPassword
*/
public static function setProxyPassword($proxyPassword)
{
self::$proxyPassword = $proxyPassword;
}

/**
* @param string $sender
*/
Expand Down
5 changes: 3 additions & 2 deletions upload/system/library/sdk_v21/OpenPayU/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static function doPut($pathUrl, $data, $authType)
* @param $statusCode
* @param null $message
* @throws OpenPayU_Exception
* @throws OpenPayU_Exception_Request
* @throws OpenPayU_Exception_Authorization
* @throws OpenPayU_Exception_Network
* @throws OpenPayU_Exception_ServerMaintenance
Expand All @@ -82,11 +83,11 @@ public static function throwHttpStatusException($statusCode, $message = null)
{

$response = $message->getResponse();
$statusDesc = ($response->status && $response->status->statusDesc) ? $response->status->statusDesc : '';
$statusDesc = isset($response->status->statusDesc) ? $response->status->statusDesc : '';

switch ($statusCode) {
case 400:
throw new OpenPayU_Exception($message->getStatus().' - '.$statusDesc, $statusCode);
throw new OpenPayU_Exception_Request($message, $message->getStatus().' - '.$statusDesc, $statusCode);
break;

case 401:
Expand Down
19 changes: 19 additions & 0 deletions upload/system/library/sdk_v21/OpenPayU/HttpCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public static function doPayuRequest($requestType, $pathUrl, $auth, $data = null
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

if ($proxy = self::getProxy()) {
curl_setopt($ch, CURLOPT_PROXY, $proxy);
if ($proxyAuth = self::getProxyAuth()) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
}
}

$response = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

Expand Down Expand Up @@ -87,4 +94,16 @@ public static function readHeader($ch, $header)
return strlen($header);
}

private static function getProxy()
{
return OpenPayU_Configuration::getProxyHost() != null ? OpenPayU_Configuration::getProxyHost()
. (OpenPayU_Configuration::getProxyPort() ? ':' . OpenPayU_Configuration::getProxyPort() : '') : false;
}

private static function getProxyAuth()
{
return OpenPayU_Configuration::getProxyUser() != null ? OpenPayU_Configuration::getProxyUser()
. (OpenPayU_Configuration::getProxyPassword() ? ':' . OpenPayU_Configuration::getProxyPassword() : '') : false;
}

}
28 changes: 23 additions & 5 deletions upload/system/library/sdk_v21/OpenPayU/Oauth/Oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class OpenPayU_Oauth
*/
public static function getAccessToken($clientId = null, $clientSecret = null)
{
if (OpenPayU_Configuration::getOauthGrantType() === OauthGrantType::TRUSTED_MERCHANT) {
return self::retrieveAccessToken($clientId, $clientSecret);
}

$cacheKey = self::CACHE_KEY . OpenPayU_Configuration::getOauthClientId();

self::getOauthTokenCache();
Expand All @@ -28,21 +32,35 @@ public static function getAccessToken($clientId = null, $clientSecret = null)
}

self::$oauthTokenCache->invalidate($cacheKey);
$response = self::retrieveAccessToken($clientId, $clientSecret);
self::$oauthTokenCache->set($cacheKey, $response);

return $response;
}

/**
* @param $clientId
* @param $clientSecret
* @return OauthResultClientCredentials
* @throws OpenPayU_Exception_ServerError
*/
private static function retrieveAccessToken($clientId, $clientSecret)
{
$authType = new AuthType_TokenRequest();

$oauthUrl = OpenPayU_Configuration::getOauthEndpoint();
$data = array(
'grant_type' => OauthGrantType::CLIENT_CREDENTIAL,
'grant_type' => OpenPayU_Configuration::getOauthGrantType(),
'client_id' => $clientId ? $clientId : OpenPayU_Configuration::getOauthClientId(),
'client_secret' => $clientSecret ? $clientSecret : OpenPayU_Configuration::getOauthClientSecret()
);

$response = self::parseResponse(OpenPayU_Http::doPost($oauthUrl, http_build_query($data), $authType));

self::$oauthTokenCache->set($cacheKey, $response);
if (OpenPayU_Configuration::getOauthGrantType() === OauthGrantType::TRUSTED_MERCHANT) {
$data['email'] = OpenPayU_Configuration::getOauthEmail();
$data['ext_customer_id'] = OpenPayU_Configuration::getOauthExtCustomerId();
}

return $response;
return self::parseResponse(OpenPayU_Http::doPost($oauthUrl, http_build_query($data, '', '&'), $authType));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
abstract class OauthGrantType
{
const CLIENT_CREDENTIAL = 'client_credentials';
}
const TRUSTED_MERCHANT = 'trusted_merchant';
}
19 changes: 19 additions & 0 deletions upload/system/library/sdk_v21/OpenPayU/OpenPayUException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ class OpenPayU_Exception extends \Exception

}

class OpenPayU_Exception_Request extends OpenPayU_Exception
{
/** @var stdClass|null */
private $originalResponseMessage;

public function __construct($originalResponseMessage, $message = "", $code = 0, $previous = null)
{
$this->originalResponseMessage = $originalResponseMessage;

parent::__construct($message, $code, $previous);
}

/** @return null|stdClass */
public function getOriginalResponse()
{
return $this->originalResponseMessage;
}
}

class OpenPayU_Exception_Configuration extends OpenPayU_Exception
{

Expand Down
6 changes: 2 additions & 4 deletions upload/system/library/sdk_v21/OpenPayU/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,12 @@ public static function parseSignature($data)
*/
public static function verifySignature($message, $signature, $signatureKey, $algorithm = 'MD5')
{
$hash = '';

if (isset($signature)) {
if ($algorithm == 'MD5') {
if ($algorithm === 'MD5') {
$hash = md5($message . $signatureKey);
} else if (in_array($algorithm, array('SHA', 'SHA1', 'SHA-1'))) {
$hash = sha1($message . $signatureKey);
} else if (in_array($algorithm, array('SHA-256', 'SHA256', 'SHA_256'))) {
} else {
$hash = hash('sha256', $message . $signatureKey);
}

Expand Down
Loading

0 comments on commit d21cbd6

Please sign in to comment.