Updates to LIPP & Future Payments

- Updated LIPP Sample code
- Updated Future Payments to have helper functions for retrieving access token
- Updated Logging Syntax to include timestamp and response json
This commit is contained in:
japatel
2014-10-20 12:04:41 -05:00
parent dcc147ac7e
commit 84660cbb2a
42 changed files with 1156 additions and 789 deletions

View File

@@ -21,7 +21,8 @@ class FuturePayment extends Payment
* @param $correlationId
* @return $this
*/
public function create($apiContext = null, $correlationId = null) {
public function create($apiContext = null, $correlationId = null)
{
if ($apiContext == null) {
$apiContext = new ApiContext(self::$credential);
}
@@ -45,4 +46,31 @@ class FuturePayment extends Payment
return $this;
}
/**
* Get a Refresh Token from Authorization Code
*
* @param $authorizationCode
* @param ApiContext $apiContext
* @return string|null refresh token
*/
public static function getRefreshToken($authorizationCode, $apiContext = null)
{
$apiContext = $apiContext ? $apiContext : new ApiContext(self::$credential);
$credential = $apiContext->getCredential();
return $credential->getRefreshToken($apiContext->getConfig(), $authorizationCode);
}
/**
* Updates Access Token using long lived refresh token
*
* @param string|null $refreshToken
* @param ApiContext $apiContext
* @return void
*/
public function updateAccessToken($refreshToken, $apiContext)
{
$apiContext = $apiContext ? $apiContext : new ApiContext(self::$credential);
$apiContext->getCredential()->updateAccessToken($apiContext->getConfig(), $refreshToken);
}
}

View File

@@ -1,236 +1,271 @@
<?php
namespace PayPal\Auth;
use PayPal\Common\PPUserAgent;
use PayPal\Core\PPConstants;
use PayPal\Core\PPHttpConfig;
use PayPal\Core\PPHttpConnection;
use PayPal\Core\PPLoggingManager;
use PayPal\Exception\PPConfigurationException;
use PayPal\Rest\RestHandler;
/**
* Class OAuthTokenCredential
*/
class OAuthTokenCredential
{
/**
* Private Variable
*
* @var int $expiryBufferTime
*/
private static $expiryBufferTime = 120;
/**
* Private Variable
*
* @var \PayPal\Core\PPLoggingManager $logger
*/
private $logger;
/**
* Client ID as obtained from the developer portal
*
* @var string $clientId
*/
private $clientId;
/**
* Client secret as obtained from the developer portal
*
* @var string $clientSecret
*/
private $clientSecret;
/**
* Generated Access Token
*
* @var string $accessToken
*/
private $accessToken;
/**
* Seconds for with access token is valid
*
* @var $tokenExpiresIn
*/
private $tokenExpiresIn;
/**
* Last time (in milliseconds) when access token was generated
*
* @var $tokenCreateTime
*/
private $tokenCreateTime;
/**
* Construct
*
* @param string $clientId client id obtained from the developer portal
* @param string $clientSecret client secret obtained from the developer portal
*/
public function __construct($clientId, $clientSecret)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->logger = PPLoggingManager::getInstance(__CLASS__);
}
/**
* Get AccessToken
*
* @param $config
*
* @return null|string
*/
public function getAccessToken($config)
{
// Check if Access Token is not null and has not expired.
// The API returns expiry time as a relative time unit
// We use a buffer time when checking for token expiry to account
// for API call delays and any delay between the time the token is
// retrieved and subsequently used
if (
$this->accessToken != null &&
(time() - $this->tokenCreateTime) > ($this->tokenExpiresIn - self::$expiryBufferTime)
) {
$this->accessToken = null;
}
// If accessToken is Null, obtain a new token
if ($this->accessToken == null) {
$this->updateAccessToken($config);
}
return $this->accessToken;
}
/**
* Get a Refresh Token from Authorization Code
*
* @param $config
* @param $authorizationCode
* @return string|null
*/
public function getRefreshToken($config, $authorizationCode) //Which comes from Mobile.
{
$payload =
"grant_type=authorization_code&code=".
$authorizationCode.
"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=token";
$jsonResponse = $this->getToken($config, $payload);
if ($jsonResponse != null && isset($jsonResponse["refresh_token"])) {
return $jsonResponse['refresh_token'];
}
}
/**
* Updates Access Token based on given input
*
* @param $config
* @param string|null $refreshToken
* @return string
*/
public function updateAccessToken($config, $refreshToken = null)
{
$this->generateAccessToken($config, $refreshToken);
return $this->accessToken;
}
/**
* Retrieves the token based on the input configuration
*
* @param array $config
* @param string $payload
* @return mixed
* @throws PPConfigurationException
* @throws \PayPal\Exception\PPConnectionException
*/
private function getToken($config, $payload)
{
$base64ClientID = base64_encode($this->clientId . ":" . $this->clientSecret);
$headers = array(
"User-Agent" => PPUserAgent::getValue(RestHandler::$sdkName, RestHandler::$sdkVersion),
"Authorization" => "Basic " . $base64ClientID,
"Accept" => "*/*"
);
$httpConfiguration = $this->getOAuthHttpConfiguration($config);
$httpConfiguration->setHeaders($headers);
$connection = new PPHttpConnection($httpConfiguration, $config);
$res = $connection->execute($payload);
$jsonResponse = json_decode($res, true);
if ($jsonResponse == null || !isset($jsonResponse["access_token"]) || !isset($jsonResponse["expires_in"])) {
$this->accessToken = null;
$this->tokenExpiresIn = null;
$this->logger->warning(
"Could not generate new Access token. Invalid response from server: " . $jsonResponse
);
} else {
$this->accessToken = $jsonResponse["access_token"];
$this->tokenExpiresIn = $jsonResponse["expires_in"];
}
$this->tokenCreateTime = time();
return $jsonResponse;
}
/**
* Generates a new access token
*
* @param array $config
* @return null
*/
private function generateAccessToken($config, $refreshToken = null)
{
$payload = "grant_type=client_credentials";
if ($refreshToken != null) {
// If the refresh token is provided, it would get access token using refresh token
// Used for Future Payments
$payload = "grant_type=refresh_token&refresh_token=$refreshToken";
}
$this->getToken($config, $payload);
return $this->accessToken;
}
/**
* Get HttpConfiguration object for OAuth API
*
* @param array $config
*
* @return PPHttpConfig
* @throws \PayPal\Exception\PPConfigurationException
*/
private function getOAuthHttpConfiguration($config)
{
if (isset($config['oauth.EndPoint'])) {
$baseEndpoint = $config['oauth.EndPoint'];
} else if (isset($config['service.EndPoint'])) {
$baseEndpoint = $config['service.EndPoint'];
} else if (isset($config['mode'])) {
switch (strtoupper($config['mode'])) {
case 'SANDBOX':
$baseEndpoint = PPConstants::REST_SANDBOX_ENDPOINT;
break;
case 'LIVE':
$baseEndpoint = PPConstants::REST_LIVE_ENDPOINT;
break;
default:
throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
}
} else {
throw new PPConfigurationException(
'You must set one of service.endpoint or mode parameters in your configuration'
);
}
$baseEndpoint = rtrim(trim($baseEndpoint), '/');
return new PPHttpConfig($baseEndpoint . "/v1/oauth2/token", "POST");
}
}
<?php
namespace PayPal\Auth;
use PayPal\Common\PPUserAgent;
use PayPal\Common\ResourceModel;
use PayPal\Core\PPConstants;
use PayPal\Core\PPHttpConfig;
use PayPal\Core\PPHttpConnection;
use PayPal\Core\PPLoggingManager;
use PayPal\Exception\PPConfigurationException;
use PayPal\Rest\RestHandler;
/**
* Class OAuthTokenCredential
*/
class OAuthTokenCredential extends ResourceModel
{
/**
* Private Variable
*
* @var int $expiryBufferTime
*/
private static $expiryBufferTime = 120;
/**
* Private Variable
*
* @var \PayPal\Core\PPLoggingManager $logger
*/
private $logger;
/**
* Client ID as obtained from the developer portal
*
* @var string $clientId
*/
private $clientId;
/**
* Client secret as obtained from the developer portal
*
* @var string $clientSecret
*/
private $clientSecret;
/**
* Generated Access Token
*
* @var string $accessToken
*/
private $accessToken;
/**
* Seconds for with access token is valid
*
* @var $tokenExpiresIn
*/
private $tokenExpiresIn;
/**
* Last time (in milliseconds) when access token was generated
*
* @var $tokenCreateTime
*/
private $tokenCreateTime;
/**
* Construct
*
* @param string $clientId client id obtained from the developer portal
* @param string $clientSecret client secret obtained from the developer portal
*/
public function __construct($clientId, $clientSecret)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->logger = PPLoggingManager::getInstance(__CLASS__);
}
/**
* Get Client ID
*
* @return string
*/
public function getClientId()
{
return $this->clientId;
}
/**
* Get Client Secret
*
* @return string
*/
public function getClientSecret()
{
return $this->clientSecret;
}
/**
* Get AccessToken
*
* @param $config
*
* @return null|string
*/
public function getAccessToken($config)
{
// Check if Access Token is not null and has not expired.
// The API returns expiry time as a relative time unit
// We use a buffer time when checking for token expiry to account
// for API call delays and any delay between the time the token is
// retrieved and subsequently used
if (
$this->accessToken != null &&
(time() - $this->tokenCreateTime) > ($this->tokenExpiresIn - self::$expiryBufferTime)
) {
$this->accessToken = null;
}
// If accessToken is Null, obtain a new token
if ($this->accessToken == null) {
$this->updateAccessToken($config);
}
return $this->accessToken;
}
/**
* Get a Refresh Token from Authorization Code
*
* @param $config
* @param $authorizationCode
* @param array $params optional arrays to override defaults
* @return string|null
*/
public function getRefreshToken($config, $authorizationCode = null, $params = array())
{
static $allowedParams = array(
'grant_type' => 'authorization_code',
'code' => 1,
'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob',
'response_type' => 'token'
);
$params = is_array($params) ? $params : array();
if ($authorizationCode) {
//Override the authorizationCode if value is explicitly set
$params['code'] = $authorizationCode;
}
$payload = http_build_query(array_merge($allowedParams, array_intersect_key($params, $allowedParams)));
$response = $this->getToken($config, $this->clientId, $this->clientSecret, $payload);
if ($response != null && isset($response["refresh_token"])) {
return $response['refresh_token'];
}
}
/**
* Updates Access Token based on given input
*
* @param $config
* @param string|null $refreshToken
* @return string
*/
public function updateAccessToken($config, $refreshToken = null)
{
$this->generateAccessToken($config, $refreshToken);
return $this->accessToken;
}
/**
* Retrieves the token based on the input configuration
*
* @param array $config
* @param string $payload
* @return mixed
* @throws PPConfigurationException
* @throws \PayPal\Exception\PPConnectionException
*/
private function getToken($config, $clientId, $clientSecret, $payload)
{
$base64ClientID = base64_encode($clientId . ":" . $clientSecret);
$headers = array(
"User-Agent" => PPUserAgent::getValue(RestHandler::$sdkName, RestHandler::$sdkVersion),
"Authorization" => "Basic " . $base64ClientID,
"Accept" => "*/*"
);
$httpConfiguration = self::getOAuthHttpConfiguration($config);
$httpConfiguration->setHeaders($headers);
$connection = new PPHttpConnection($httpConfiguration, $config);
$res = $connection->execute($payload);
$response = json_decode($res, true);
return $response;
}
/**
* Generates a new access token
*
* @param array $config
* @return null
*/
private function generateAccessToken($config, $refreshToken = null)
{
$params = array('grant_type' => 'client_credentials');
if ($refreshToken != null) {
// If the refresh token is provided, it would get access token using refresh token
// Used for Future Payments
$params['grant_type'] = 'refresh_token';
$params['refresh_token'] = $refreshToken;
}
$payload = http_build_query($params);
$response = $this->getToken($config, $this->clientId, $this->clientSecret, $payload);
if ($response == null || !isset($response["access_token"]) || !isset($response["expires_in"])) {
$this->accessToken = null;
$this->tokenExpiresIn = null;
$this->logger->warning(
"Could not generate new Access token. Invalid response from server: " . $response
);
} else {
$this->accessToken = $response["access_token"];
$this->tokenExpiresIn = $response["expires_in"];
}
$this->tokenCreateTime = time();
return $this->accessToken;
}
/**
* Get HttpConfiguration object for OAuth API
*
* @param array $config
*
* @return PPHttpConfig
* @throws \PayPal\Exception\PPConfigurationException
*/
private static function getOAuthHttpConfiguration($config)
{
if (isset($config['oauth.EndPoint'])) {
$baseEndpoint = $config['oauth.EndPoint'];
} else if (isset($config['service.EndPoint'])) {
$baseEndpoint = $config['service.EndPoint'];
} else if (isset($config['mode'])) {
switch (strtoupper($config['mode'])) {
case 'SANDBOX':
$baseEndpoint = PPConstants::REST_SANDBOX_ENDPOINT;
break;
case 'LIVE':
$baseEndpoint = PPConstants::REST_LIVE_ENDPOINT;
break;
default:
throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
}
} else {
throw new PPConfigurationException(
'You must set one of service.endpoint or mode parameters in your configuration'
);
}
$baseEndpoint = rtrim(trim($baseEndpoint), '/');
return new PPHttpConfig($baseEndpoint . "/v1/oauth2/token", "POST");
}
}

View File

@@ -28,6 +28,9 @@ class PPOpenIdSession
if ($apiContext->get($clientId)) {
$clientId = $apiContext->get($clientId);
}
$clientId = $clientId ? $clientId : $apiContext->getCredential()->getClientId();
$scope = count($scope) != 0 ? $scope : array('openid', 'profile', 'address', 'email', 'phone',
'https://uri.paypal.com/services/paypalattributes', 'https://uri.paypal.com/services/expresscheckout');
if (!in_array('openid', $scope)) {

View File

@@ -164,6 +164,7 @@ class PPOpenIdTokeninfo extends ResourceModel
if (!array_key_exists('grant_type', $params)) {
$params['grant_type'] = 'authorization_code';
}
$apiContext = $apiContext ? $apiContext : new ApiContext(self::$credential);
if (sizeof($apiContext->get($clientId)) > 0) {
$clientId = $apiContext->get($clientId);
@@ -172,6 +173,10 @@ class PPOpenIdTokeninfo extends ResourceModel
if (sizeof($apiContext->get($clientSecret)) > 0) {
$clientSecret = $apiContext->get($clientSecret);
}
$clientId = $clientId ? $clientId : $apiContext->getCredential()->getClientId();
$clientSecret = $clientSecret ? $clientSecret : $apiContext->getCredential()->getClientSecret();
$json = self::executeCall(
"/v1/identity/openidconnect/tokenservice",
"POST",
@@ -205,6 +210,7 @@ class PPOpenIdTokeninfo extends ResourceModel
public function createFromRefreshToken($params, $apiContext = null)
{
static $allowedParams = array('grant_type' => 1, 'refresh_token' => 1, 'scope' => 1);
$apiContext = $apiContext ? $apiContext : new ApiContext(self::$credential);
if (!array_key_exists('grant_type', $params)) {
$params['grant_type'] = 'refresh_token';
@@ -213,13 +219,16 @@ class PPOpenIdTokeninfo extends ResourceModel
$params['refresh_token'] = $this->getRefreshToken();
}
$clientId = isset($params['client_id']) ? $params['client_id'] : $apiContext->getCredential()->getClientId();
$clientSecret = isset($params['client_secret']) ? $params['client_secret'] : $apiContext->getCredential()->getClientSecret();
$json = self::executeCall(
"/v1/identity/openidconnect/tokenservice",
"POST",
http_build_query(array_intersect_key($params, $allowedParams)),
array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic ' . base64_encode($params['client_id'] . ":" . $params['client_secret'])
'Authorization' => 'Basic ' . base64_encode($clientId . ":" . $clientSecret)
),
$apiContext
);

View File

@@ -466,6 +466,8 @@ class PPOpenIdUserinfo extends ResourceModel
{
static $allowedParams = array('schema' => 1);
$params = is_array($params) ? $params : array();
if (!array_key_exists('schema', $params)) {
$params['schema'] = 'openid';
}

View File

@@ -79,8 +79,7 @@ class PPHttpConnection
public function execute($data)
{
//Initialize the logger
$this->logger->fine("Connecting to " . $this->httpConfig->getUrl());
$this->logger->fine("Payload " . $data);
$this->logger->info($this->httpConfig->getMethod() . ' ' . $this->httpConfig->getUrl());
//Initialize Curl Options
$ch = curl_init($this->httpConfig->getUrl());
@@ -100,18 +99,19 @@ class PPHttpConnection
//Default Option if Method not of given types in switch case
if ($this->httpConfig->getMethod() != NULL) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
$this->logger->info("Method : " . $this->httpConfig->getMethod());
}
//Logging Each Headers for debugging purposes
foreach ($this->getHttpHeaders() as $header) {
//TODO: Strip out credentials and other secure info when logging.
$this->logger->info("Adding header $header");
$this->logger->fine($header);
}
$this->logger->fine("Payload : " . $data . "\n");
//Execute Curl Request
$result = curl_exec($ch);
//Retry if Certificate Exception
if (curl_errno($ch) == 60) {
$this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
@@ -151,6 +151,7 @@ class PPHttpConnection
"Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. " .
"Retried $retries times."
);
$this->logger->fine("Response : " . $result . "\n\n");
$ex->setData($result);
throw $ex;
} else if ($httpStatus < 200 || $httpStatus >= 300) {
@@ -159,10 +160,13 @@ class PPHttpConnection
"Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}.",
$httpStatus
);
$this->logger->fine("Response : " . $result . "\n\n");
$ex->setData($result);
throw $ex;
}
$this->logger->fine("Response : " . $result . "\n\n");
//Return result object
return $result;
}

View File

@@ -70,6 +70,11 @@ class PPLoggingManager
*/
public function __construct()
{
// To suppress the warning during the date() invocation in logs, we would default the timezone to GMT.
if (!ini_get('date.timezone')) {
date_default_timezone_set('GMT');
}
$config = PPConfigManager::getInstance()->getConfigHashmap();
$this->isLoggingEnabled = (array_key_exists('log.LogEnabled', $config) && $config['log.LogEnabled'] == '1');
@@ -93,7 +98,7 @@ class PPLoggingManager
private function log($message, $level = PPLoggingLevel::INFO)
{
if ($this->isLoggingEnabled && ($level <= $this->loggingLevel)) {
error_log($this->loggerName . ": $message\n", 3, $this->loggerFile);
error_log("[" . date('d-m-Y h:i:s') . "] " . $this->loggerName . ": $message\n", 3, $this->loggerFile);
}
}

View File

@@ -72,7 +72,6 @@ class PPRestCall
}
$connection = new PPHttpConnection($httpConfig, $config);
$response = $connection->execute($data);
$this->logger->fine($response . PHP_EOL);
return $response;
}