forked from LiveCarta/PayPal-PHP-SDK
Enabling Future Payments
- Added feature to create future payments - Updated Authentication model to retrieve long lived refresh token - Updated Documentation - Updated Sample
This commit is contained in:
48
lib/PayPal/Api/FuturePayment.php
Normal file
48
lib/PayPal/Api/FuturePayment.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Api;
|
||||
|
||||
use PayPal\Common\PPModel;
|
||||
use PayPal\Rest\ApiContext;
|
||||
use PayPal\Transport\PPRestCall;
|
||||
|
||||
/**
|
||||
* Class FuturePayment
|
||||
*
|
||||
* @package PayPal\Api
|
||||
*/
|
||||
class FuturePayment extends Payment
|
||||
{
|
||||
|
||||
/**
|
||||
* Extends the Payment object to create future payments
|
||||
*
|
||||
* @param null $apiContext
|
||||
* @param $correlationId
|
||||
* @return $this
|
||||
*/
|
||||
public function create($apiContext = null, $correlationId = null) {
|
||||
if ($apiContext == null) {
|
||||
$apiContext = new ApiContext(self::$credential);
|
||||
}
|
||||
if (($correlationId == null || trim($correlationId) == "")) {
|
||||
throw new \InvalidArgumentException("correlationId cannot be null or empty");
|
||||
}
|
||||
$payLoad = $this->toJSON();
|
||||
$call = new PPRestCall($apiContext);
|
||||
$json = $call->execute(
|
||||
array('PayPal\Rest\RestHandler'),
|
||||
"/v1/payments/payment",
|
||||
"POST",
|
||||
$payLoad,
|
||||
[
|
||||
'Paypal-Application-Correlation-Id' => $correlationId,
|
||||
'PAYPAL-CLIENT-METADATA-ID' => $correlationId
|
||||
]
|
||||
);
|
||||
$this->fromJson($json);
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ class Payment extends PPModel implements IResource
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
private static $credential;
|
||||
protected static $credential;
|
||||
|
||||
/**
|
||||
* Set Credential
|
||||
@@ -467,14 +467,14 @@ class Payment extends PPModel implements IResource
|
||||
$payLoad = "";
|
||||
|
||||
$allowedParams = array(
|
||||
'count' => 1,
|
||||
'start_id' => 1,
|
||||
'count' => 1,
|
||||
'start_id' => 1,
|
||||
'start_index' => 1,
|
||||
'start_time' => 1,
|
||||
'end_time' => 1,
|
||||
'payee_id' => 1,
|
||||
'sort_by' => 1,
|
||||
'sort_order' => 1,
|
||||
'start_time' => 1,
|
||||
'end_time' => 1,
|
||||
'payee_id' => 1,
|
||||
'sort_by' => 1,
|
||||
'sort_order' => 1,
|
||||
);
|
||||
|
||||
if ($apiContext == null) {
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
<?php
|
||||
/**
|
||||
* Oauth Token credential
|
||||
*/
|
||||
|
||||
namespace PayPal\Auth;
|
||||
|
||||
use PayPal\Common\PPUserAgent;
|
||||
use PayPal\Core\PPConnectionManager;
|
||||
use PayPal\Core\PPConstants;
|
||||
use PayPal\Core\PPHttpConfig;
|
||||
use PayPal\Core\PPHttpConnection;
|
||||
use PayPal\Core\PPLoggingManager;
|
||||
use PayPal\Exception\PPConfigurationException;
|
||||
use PayPal\Rest\RestHandler;
|
||||
@@ -49,7 +46,7 @@ class OAuthTokenCredential
|
||||
/**
|
||||
* Generated Access Token
|
||||
*
|
||||
* @var $accessToken
|
||||
* @var string $accessToken
|
||||
*/
|
||||
private $accessToken;
|
||||
|
||||
@@ -95,26 +92,64 @@ class OAuthTokenCredential
|
||||
// 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)) {
|
||||
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->_generateAccessToken($config);
|
||||
$this->updateAccessToken($config);
|
||||
}
|
||||
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new access token
|
||||
* Get a Refresh Token from Authorization Code
|
||||
*
|
||||
* @param $config
|
||||
*
|
||||
* @return null
|
||||
* @param $authorizationCode
|
||||
* @return string|null
|
||||
*/
|
||||
private function _generateAccessToken($config)
|
||||
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 [] $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(
|
||||
@@ -126,20 +161,40 @@ class OAuthTokenCredential
|
||||
$httpConfiguration = $this->getOAuthHttpConfiguration($config);
|
||||
$httpConfiguration->setHeaders($headers);
|
||||
|
||||
$connection = PPConnectionManager::getInstance()->getConnection($httpConfiguration, $config);
|
||||
$res = $connection->execute("grant_type=client_credentials");
|
||||
$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);
|
||||
$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 [] $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;
|
||||
}
|
||||
@@ -170,7 +225,9 @@ class OAuthTokenCredential
|
||||
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');
|
||||
throw new PPConfigurationException(
|
||||
'You must set one of service.endpoint or mode parameters in your configuration'
|
||||
);
|
||||
}
|
||||
|
||||
$baseEndpoint = rtrim(trim($baseEndpoint), '/');
|
||||
|
||||
Reference in New Issue
Block a user