From 9caf75ee82ef8d829335eb5db70c7d2683404a59 Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Mon, 20 Jul 2015 11:28:17 -0500 Subject: [PATCH 1/2] 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); } From 36f0dd7de9b7643e07197b0f040f3831db38a14e Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Tue, 21 Jul 2015 11:23:41 -0500 Subject: [PATCH 2/2] Fixing Future Payment Sample --- sample/doc/assets/behavior.js | 120 +++++++++++-------- sample/doc/payments/CreateFuturePayment.html | 7 +- sample/payments/CreateFuturePayment.php | 6 +- 3 files changed, 79 insertions(+), 54 deletions(-) diff --git a/sample/doc/assets/behavior.js b/sample/doc/assets/behavior.js index 2e339d9..9b43888 100644 --- a/sample/doc/assets/behavior.js +++ b/sample/doc/assets/behavior.js @@ -490,53 +490,7 @@ f.event={add:function(a,c,d,e,g){var h,i,j,k,l,m,n,o,p,q,r,s;if(!(a.nodeType===3 "pageTitle": "invoice/UpdateInvoice", "title": "UpdateInvoice" }, - "depth": 2, - "outline": [ - { - "type": "heading", - "data": { - "level": 1, - "title": "Update Invoice Sample", - "slug": "update-invoice-sample" - }, - "depth": 1, - "children": [ - { - "type": "heading", - "data": { - "level": 3, - "title": "Update Invoice", - "slug": "update-invoice" - }, - "depth": 3 - }, { - "type": "heading", - "data": { - "level": 3, - "title": "NOTE: These are the work-around added to the", - "slug": "note-these-are-the-work-around-added-to-the" - }, - "depth": 3 - }, { - "type": "heading", - "data": { - "level": 3, - "title": "Update Invoice", - "slug": "update-invoice" - }, - "depth": 3 - }, { - "type": "heading", - "data": { - "level": 3, - "title": "Retrieve Invoice", - "slug": "retrieve-invoice" - }, - "depth": 3 - } - ] - } - ] + "depth": 2 } ] }, { @@ -1008,7 +962,77 @@ f.event={add:function(a,c,d,e,g){var h,i,j,k,l,m,n,o,p,q,r,s;if(!(a.nodeType===3 "pageTitle": "payments/CreateFuturePayment", "title": "CreateFuturePayment" }, - "depth": 2 + "depth": 2, + "outline": [ + { + "type": "heading", + "data": { + "level": 1, + "title": "Create Payment using PayPal as payment method", + "slug": "create-payment-using-paypal-as-payment-method" + }, + "depth": 1, + "children": [ + { + "type": "heading", + "data": { + "level": 3, + "title": "Payer", + "slug": "payer" + }, + "depth": 3 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Amount", + "slug": "amount" + }, + "depth": 3 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Transaction", + "slug": "transaction" + }, + "depth": 3 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Redirect urls", + "slug": "redirect-urls" + }, + "depth": 3 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Payment", + "slug": "payment" + }, + "depth": 3 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Get Refresh Token", + "slug": "get-refresh-token" + }, + "depth": 3 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Create Future Payment", + "slug": "create-future-payment" + }, + "depth": 3 + } + ] + } + ] }, { "type": "file", "data": { diff --git a/sample/doc/payments/CreateFuturePayment.html b/sample/doc/payments/CreateFuturePayment.html index 14f6101..0fb1e81 100644 --- a/sample/doc/payments/CreateFuturePayment.html +++ b/sample/doc/payments/CreateFuturePayment.html @@ -3,10 +3,11 @@ PayPal Account based Payment. API used: /v1/payments/payment

require __DIR__ . '/../bootstrap.php'; use PayPal\Api\Amount; -use PayPal\Api\Payer; use PayPal\Api\FuturePayment; +use PayPal\Api\Payer; use PayPal\Api\RedirectUrls; use PayPal\Api\Transaction; + session_start();

Payer

A resource representing a Payer that funds a payment For paypal account payments, set payment method @@ -33,7 +34,7 @@ the above types and intent set to 'sale'

$payer) ->setRedirectUrls($redirectUrls) ->setTransactions(array($transaction));

Get Refresh Token

-

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';

Client Metadata id from mobile sdk +

You need to get a permanent refresh token from the authorization code, retrieved from the mobile sdk.

authorization code from mobile sdk

$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

$clientMetadataId = '123123456'; try {

Exchange authorization_code for long living refresh token. You should store @@ -46,7 +47,7 @@ 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.

$payment->create($apiContext, $clientMetadataId); -} 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); +} catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Future Payment", "Payment", null, $payment, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Future Payment", "Payment", $payment->getId(), $request, $payment); diff --git a/sample/payments/CreateFuturePayment.php b/sample/payments/CreateFuturePayment.php index ac7ec17..c3e3f59 100644 --- a/sample/payments/CreateFuturePayment.php +++ b/sample/payments/CreateFuturePayment.php @@ -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); - echo $refreshToken; + $refreshToken = FuturePayment::getRefreshToken($authorizationCode, $apiContext); + // Update the access token in apiContext - $payment->updateAccessToken('NvtsgQLDxJXfFTOpYRS5Ad9S1kOd_QWCk4vkdPuBe9qDfKDBbKyioSMXeMwSaLKr4Lz_zoFEpZBiHrce4X4IcAfabH0', $apiContext); + $payment->updateAccessToken($refreshToken, $apiContext); // For Sample Purposes Only. $request = clone $payment;