recreate project
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
// ### Obtain Access Token From Refresh Token
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
use PayPal\Api\OpenIdTokeninfo;
|
||||
|
||||
// You can retrieve the refresh token by executing ObtainUserConsent.php and store the refresh token
|
||||
$refreshToken = 'yzX4AkmMyBKR4on7vB5he-tDu38s24Zy-kTibhSuqA8kTdy0Yinxj7NpAyULx0bxqC5G8dbXOt0aVMlMmtpiVmSzhcjVZhYDM7WUQLC9KpaXGBHyltJPkLLQkXE';
|
||||
|
||||
try {
|
||||
|
||||
$tokenInfo = new OpenIdTokeninfo();
|
||||
$tokenInfo = $tokenInfo->createFromRefreshToken(array('refresh_token' => $refreshToken), $apiContext);
|
||||
|
||||
} catch (Exception $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Obtained Access Token From Refresh Token", "Access Token", null, null, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Obtained Access Token From Refresh Token", "Access Token", $tokenInfo->getAccessToken(), null, $tokenInfo);
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
// ### Obtain Access Token From Refresh Token
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
use PayPal\Api\OpenIdTokeninfo;
|
||||
use PayPal\Api\OpenIdUserinfo;
|
||||
|
||||
// To obtain User Info, you have to follow three steps in general.
|
||||
// First, you need to obtain user's consent to retrieve the information you want.
|
||||
// This is explained in the example "ObtainUserConsent.php".
|
||||
|
||||
// Once you get the user's consent, the end result would be long lived refresh token.
|
||||
// This refresh token should be stored in a permanent storage for later use.
|
||||
|
||||
// Lastly, when you need to retrieve the user information, you need to generate the short lived access token
|
||||
// to retreive the information. The short lived access token can be retrieved using the example shown in
|
||||
// "GenerateAccessTokenFromRefreshToken.php", or as shown below
|
||||
|
||||
// You can retrieve the refresh token by executing ObtainUserConsent.php and store the refresh token
|
||||
$refreshToken = 'W1JmxG-Cogm-4aSc5Vlen37XaQTj74aQcQiTtXax5UgY7M_AJ--kLX8xNVk8LtCpmueFfcYlRK6UgQLJ-XHsxpw6kZzPpKKccRQeC4z2ldTMfXdIWajZ6CHuebs';
|
||||
|
||||
try {
|
||||
|
||||
$tokenInfo = new OpenIdTokeninfo();
|
||||
$tokenInfo = $tokenInfo->createFromRefreshToken(array('refresh_token' => $refreshToken), $apiContext);
|
||||
|
||||
$params = array('access_token' => $tokenInfo->getAccessToken());
|
||||
$userInfo = OpenIdUserinfo::getUserinfo($params, $apiContext);
|
||||
|
||||
} catch (Exception $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("User Information", "User Info", null, $params, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("User Information", "User Info", $userInfo->getUserId(), $params, $userInfo);
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
use PayPal\Api\OpenIdSession;
|
||||
|
||||
$baseUrl = getBaseUrl() . '/UserConsentRedirect.php?success=true';
|
||||
|
||||
// ### Get User Consent URL
|
||||
// The clientId is stored in the bootstrap file
|
||||
|
||||
//Get Authorization URL returns the redirect URL that could be used to get user's consent
|
||||
$redirectUrl = OpenIdSession::getAuthorizationUrl(
|
||||
$baseUrl,
|
||||
array('openid', 'profile', 'address', 'email', 'phone',
|
||||
'https://uri.paypal.com/services/paypalattributes', 'https://uri.paypal.com/services/expresscheckout'),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
$apiContext
|
||||
);
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Generated the User Consent URL", "URL", '<a href="'. $redirectUrl . '" >Click Here to Obtain User Consent</a>', $baseUrl, $redirectUrl);
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
use PayPal\Api\OpenIdTokeninfo;
|
||||
use PayPal\Exception\PayPalConnectionException;
|
||||
|
||||
session_start();
|
||||
|
||||
// ### User Consent Response
|
||||
// PayPal would redirect the user to the redirect_uri mentioned when creating the consent URL.
|
||||
// The user would then able to retrieve the access token by getting the code, which is returned as a GET parameter.
|
||||
if (isset($_GET['success']) && $_GET['success'] == 'true') {
|
||||
|
||||
$code = $_GET['code'];
|
||||
|
||||
try {
|
||||
// Obtain Authorization Code from Code, Client ID and Client Secret
|
||||
$accessToken = OpenIdTokeninfo::createFromAuthorizationCode(array('code' => $code), null, null, $apiContext);
|
||||
} catch (PayPalConnectionException $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Obtained Access Token", "Access Token", null, $_GET['code'], $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Obtained Access Token", "Access Token", $accessToken->getAccessToken(), $_GET['code'], $accessToken);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user