/** * @t1est * TODO: Fix Test. This test is disabled */ public function t1estOperations() { $clientId = 'AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd'; $clientSecret = 'ELtVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX'; $params = array('code' => '<FILLME>', 'redirect_uri' => 'https://devtools-paypal.com/', 'client_id' => $clientId, 'client_secret' => $clientSecret); $accessToken = OpenIdTokeninfo::createFromAuthorizationCode($params); $this->assertNotNull($accessToken); $params = array('refresh_token' => $accessToken->getRefreshToken(), 'client_id' => $clientId, 'client_secret' => $clientSecret); $accessToken = $accessToken->createFromRefreshToken($params); $this->assertNotNull($accessToken); }
<?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) { ResultPrinter::printError("Obtained Access Token", "Access Token", null, $_GET['code'], $ex); exit(1); } ResultPrinter::printResult("Obtained Access Token", "Access Token", $accessToken->getAccessToken(), $_GET['code'], $accessToken); }
<?php require_once TOOLKIT . '/class.administrationpage.php'; $paypalExtension = ExtensionManager::create('paypal'); use PayPal\Api\OpenIdTokeninfo; use PayPal\Api\OpenIdUserinfo; 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['code'])) { $code = $_GET['code']; try { // Obtain Authorization Code from Code, Client ID and Client Secret $accessToken = OpenIdTokeninfo::createFromAuthorizationCode(array('code' => $code), null, null, $paypalExtension->getApiContext()); } 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); var_dump($ex); exit(1); } $cookie = new Cookie('paypal', TWO_WEEKS, __SYM_COOKIE_PATH__, null, true); $cookie->set('token', $accessToken->getAccessToken()); $cookie->set('refresh-token', $accessToken->getRefreshToken()); $params = array('access_token' => $accessToken->getAccessToken()); $userInfo = OpenIdUserinfo::getUserinfo($params, $paypalExtension->getApiContext()); $userAddress = $userInfo->getAddress(); $address = array('street' => $userAddress->getStreetAddress(), 'locality' => $userAddress->getLocality(), 'region' => $userAddress->getRegion(), 'country' => $userAddress->getCountry(), 'postal-code' => $userAddress->getPostalCode()); $user = array('id' => $userInfo->getUserId(), 'name' => $userInfo->getGivenName(), 'surname' => $userInfo->getFamilyName(), 'email' => $userInfo->getEmail(), 'date-of-birth' => $userInfo->getBirthday(), 'mobile' => $userInfo->getPhoneNumber(), 'address' => $address); $cookie->set('user', $user);
/** * Creates an Access Token from an Authorization Code. * * @path /v1/identity/openidconnect/tokenservice * @method POST * @param array $params (allowed values are client_id, client_secret, grant_type, code and redirect_uri) * (required) client_id from developer portal * (required) client_secret from developer portal * (required) code is Authorization code previously received from the authorization server * (required) redirect_uri Redirection endpoint that must match the one provided during the * authorization request that ended in receiving the authorization code. * (optional) grant_type is the Token grant type. Defaults to authorization_code * @param string $clientId * @param string $clientSecret * @param ApiContext $apiContext Optional API Context * @param PayPalRestCall $restCall * @return OpenIdTokeninfo */ public static function createFromAuthorizationCode($params, $clientId = null, $clientSecret = null, $apiContext = null, $restCall = null) { static $allowedParams = array('grant_type' => 1, 'code' => 1, 'redirect_uri' => 1); if (!array_key_exists('grant_type', $params)) { $params['grant_type'] = 'authorization_code'; } $apiContext = $apiContext ? $apiContext : new ApiContext(self::$credential); if (sizeof($apiContext->get($clientId)) > 0) { $clientId = $apiContext->get($clientId); } if (sizeof($apiContext->get($clientSecret)) > 0) { $clientSecret = $apiContext->get($clientSecret); } $clientId = $clientId ? $clientId : $apiContext->getCredential()->getClientId(); $clientSecret = $clientSecret ? $clientSecret : $apiContext->getCredential()->getClientSecret(); $json = self::executeCall("/v1/identity/openidconnect/tokenservice", "POST", http_build_query(array_intersect_key($params, $allowedParams)), array('Content-Type' => 'application/x-www-form-urlencoded', 'Authorization' => 'Basic ' . base64_encode($clientId . ":" . $clientSecret)), $apiContext, $restCall); $token = new OpenIdTokeninfo(); $token->fromJson($json); return $token; }
$scope = array('openid', 'email'); #More info: https://developer.paypal.com/docs/integration/direct/identity/attributes/ ####################################################### error_reporting(E_ALL); ini_set('display_errors', 1); require_once dirname(__FILE__) . '/paypal-sdk/autoload.php'; use PayPal\Api\OpenIdSession; use PayPal\Api\OpenIdTokeninfo; use PayPal\Api\OpenIdUserinfo; use PayPal\Rest\ApiContext; use PayPal\Auth\OAuthTokenCredential; ####################################################### #Step 1: display link to paypal if (!isset($_GET['code']) and !isset($_GET['error'])) { $openidurl = OpenIdSession::getAuthorizationUrl($app_return_url, $scope, $paypal_client_id); echo 'Click here to <a href="https://www.paypal.com/webapps/auth/protocol/openidconnect' . $openidurl . '">log in with paypal</a>!'; die; } ####################################################### #Check return data for errors if (isset($_GET['error'])) { die('<h1>Error:</h1><pre>' . print_r($_GET, TRUE)); } ####################################################### #Step 2: get user data $api_context = new ApiContext(new OAuthTokenCredential($paypal_client_id, $paypal_secret)); $api_context->setConfig(array('mode' => 'live')); $token = OpenIdTokeninfo::createFromAuthorizationCode(array('code' => $_GET['code']), $paypal_client_id, $paypal_secret, $api_context); $user = OpenIdUserinfo::getUserinfo(json_decode($token, true), $api_context); $user = json_decode($user, true); die('<h1>Success!</h1><p>User data:</p><pre>' . print_r($user, true) . '</pre>');