diff --git a/lib/PayPal/Api/FuturePayment.php b/lib/PayPal/Api/FuturePayment.php new file mode 100644 index 00000000..5883a632 --- /dev/null +++ b/lib/PayPal/Api/FuturePayment.php @@ -0,0 +1,48 @@ +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; + + } +} diff --git a/lib/PayPal/Api/Payment.php b/lib/PayPal/Api/Payment.php index b601cef8..ddd26995 100644 --- a/lib/PayPal/Api/Payment.php +++ b/lib/PayPal/Api/Payment.php @@ -26,7 +26,7 @@ class Payment extends PPModel implements IResource /** * @var */ - private static $credential; + protected static $credential; /** * Set Credential @@ -467,14 +467,14 @@ public static function all($params, $apiContext = null) $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) { diff --git a/lib/PayPal/Auth/OAuthTokenCredential.php b/lib/PayPal/Auth/OAuthTokenCredential.php index 36b6dd0e..f0d624f5 100644 --- a/lib/PayPal/Auth/OAuthTokenCredential.php +++ b/lib/PayPal/Auth/OAuthTokenCredential.php @@ -1,14 +1,11 @@ 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 + * @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 * - * @return null + * @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 _generateAccessToken($config) + private function getToken($config, $payload) { $base64ClientID = base64_encode($this->clientId . ":" . $this->clientSecret); $headers = array( @@ -126,20 +161,40 @@ private function _generateAccessToken($config) $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 @@ private function getOAuthHttpConfiguration($config) 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), '/'); diff --git a/sample/doc/payments/CreateFuturePayment.html b/sample/doc/payments/CreateFuturePayment.html new file mode 100644 index 00000000..b502fbfb --- /dev/null +++ b/sample/doc/payments/CreateFuturePayment.html @@ -0,0 +1,70 @@ +
A resource representing a Payer that funds a payment +For paypal account payments, set payment method +to 'paypal'.
Lets you specify a payment amount. +You can also specify additional details +such as shipping, tax.
A transaction defines the contract of a +payment - what is the payment for and who +is fulfilling it.
Set the urls that the buyer must be redirected to after +payment approval/ cancellation.
A Payment Resource; create one using +the above types and intent set to 'sale'
You need to get a permanent refresh token from the authorization code, retrieved from the mobile sdk.
authorization code from mobile sdk
correlation id from mobile sdk
Exchange authorization_code for long living refresh token. You should store +it in a database for later use
Update the access token in apiContext
Create a payment by calling the 'create' method
+passing it a valid apiContext.
+(See bootstrap.php for more on ApiContext
)
+The return object contains the state and the
+url to which the buyer must be redirected to
+for payment approval
+Please note that currently future payments works only with Paypal as a funding instrument.
toJSON(JSON_PRETTY_PRINT);?>+Back + +
Create Payment using PayPal as payment method
+This sample code demonstrates how you can process a +PayPal Account based Payment. +API used: /v1/payments/payment