/**
  * @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);
}
Beispiel #3
0
<?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);
$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>');