From 9caf75ee82ef8d829335eb5db70c7d2683404a59 Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Mon, 20 Jul 2015 11:28:17 -0500 Subject: [PATCH] Fixes #343: Future Payment Caching Issue --- lib/PayPal/Auth/OAuthTokenCredential.php | 4 ++++ sample/payments/CreateFuturePayment.php | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/PayPal/Auth/OAuthTokenCredential.php b/lib/PayPal/Auth/OAuthTokenCredential.php index f1d9e60..30749d6 100644 --- a/lib/PayPal/Auth/OAuthTokenCredential.php +++ b/lib/PayPal/Auth/OAuthTokenCredential.php @@ -125,6 +125,10 @@ class OAuthTokenCredential extends PayPalResourceModel */ public function getAccessToken($config) { + // Check if we already have accessToken in Cache + if ($this->accessToken && (time() - $this->tokenCreateTime) < ($this->tokenExpiresIn - self::$expiryBufferTime)) { + return $this->accessToken; + } // Check for persisted data first $token = AuthorizationCache::pull($config, $this->clientId); if ($token) { diff --git a/sample/payments/CreateFuturePayment.php b/sample/payments/CreateFuturePayment.php index 0c1d31c..ac7ec17 100644 --- a/sample/payments/CreateFuturePayment.php +++ b/sample/payments/CreateFuturePayment.php @@ -58,7 +58,7 @@ $payment->setIntent("authorize") // You need to get a permanent refresh token from the authorization code, retrieved from the mobile sdk. // authorization code from mobile sdk -$authorizationCode = 'EJfRuAqXEE95pdVMmOym_mftTbeJD03RBX-Zjg9pLCAhdLqLeRR6YSKTNsrbQGX7lFoZ3SxwFyxADEZbBOxpn023W9SA0JzSQAy-9eLdON5eDPAyMyKlHyNVS2DqBR2iWVfQGfudbd9MDoRxMEjIZbY'; +$authorizationCode = 'EK7_MAKlB4QxW1dWKnvnr_CEdLKnpH3vnGAf155Eg8yO8e_7VaQonsqIbTK9CR7tUsoIN2eCc5raOfaGbZDCT0j6k_BDE8GkyLgk8ulcQyR_3S-fgBzjMzBwNqpj3AALgCVR03zw1iT8HTsxZXp3s2U'; // Client Metadata id from mobile sdk // For more information look for PayPal-Client-Metadata-Id in https://developer.paypal.com/docs/api/#authentication--headers @@ -67,10 +67,10 @@ $clientMetadataId = '123123456'; try { // Exchange authorization_code for long living refresh token. You should store // it in a database for later use - $refreshToken = FuturePayment::getRefreshToken($authorizationCode, $apiContext); - + //$refreshToken = FuturePayment::getRefreshToken($authorizationCode, $apiContext); + echo $refreshToken; // Update the access token in apiContext - $payment->updateAccessToken($refreshToken, $apiContext); + $payment->updateAccessToken('NvtsgQLDxJXfFTOpYRS5Ad9S1kOd_QWCk4vkdPuBe9qDfKDBbKyioSMXeMwSaLKr4Lz_zoFEpZBiHrce4X4IcAfabH0', $apiContext); // For Sample Purposes Only. $request = clone $payment; @@ -87,7 +87,7 @@ try { } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY - ResultPrinter::printError("Future Payment", "Payment", null, $request, $ex); + ResultPrinter::printError("Future Payment", "Payment", null, $payment, $ex); exit(1); }