Sign in with the Paypal API: Get basic user information

I am using JavaScript to login to PayPal.

My JavaScript code looks like this and seems to work fine.

<script type="text/javascript" src="https://www.paypalobjects.com/js/external/api.js"></script>
<script type="text/javascript">
paypal.use( ["login"], function(login) {
  login.render ({
    "appid": "<%=paypalAppId%>",
    "scopes": "profile email address phone https://uri.paypal.com/services/paypalattributes",
    "containerid": "paypalLogin",
    "locale": "en-gb",
    "returnurl": "http://www.domain.net/plogin.html"
  });
});
</script>  

      

However, I am afraid of the next step. Using JavaScript or Classic ASP, I am trying to get the following details of a subscribed user:

  • E-mail address
  • Name
  • Surname
  • unique identifier (optional)
  • Address (optional)
  • Phone number

I did this with Facebook, Google and LinkedIn using examples available on their respective sites. However, with Paypal, I cannot find useful examples on how to do this ...

+3


source to share


1 answer


I manage to do this with the following code. It never worked for me the way Paypal does. So I am doing everything in one code instead of jumping. testingPaypalIdentity.php is the url callback which I put as the "return url" and the same one you should put in your Paypal app.

Basically the code checks if there is a code that is user permission, if it then generates tokens, if there is a refresh token then it looks for the user information and prints it.

require __DIR__ . '/bootstrap.php';

use PayPal\Api\OpenIdSession;
use PayPal\Api\OpenIdTokeninfo;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Api\OpenIdUserinfo;
$baseUrl = getBaseUrl() . '/testingPaypalIdentity.php?success=true';
$tmp;
//Get Authorization URL returns the redirect URL that could be used to get             user consent
$redirectUrl = OpenIdSession::getAuthorizationUrl(
$baseUrl,
array('openid', 'profile', 'address', 'email', 'phone',
    'https://uri.paypal.com/services/paypalattributes',
    'https://uri.paypal.com/services/expresscheckout',
    'https://uri.paypal.com/services/invoicing'),
    null,
    null,
    null,
    $apiContext
);
$refreshToken='';
if (isset($_GET['code']) && $_GET['code'] != '') {

    $code = $_GET['code'];

    try {
        $accessToken = OpenIdTokeninfo::createFromAuthorizationCode(array('code' => $code), null, null, $apiContext);
    } catch (PayPalConnectionException $ex) {
        echo $_GET['code']." ".$ex;
        //ResultPrinter::printError("Obtained Access Token", "Access Token", null, $_GET['code'], $ex);
        exit(1);
    }
    $refreshToken=$accessToken->getRefreshToken();
    print_r($accessToken);
    //ResultPrinter::printResult("Obtained Access Token", "Access Token",     $accessToken->getAccessToken(), $_GET['code'], $accessToken);
}
if($refreshToken!='') {
    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
        echo $ex;
        exit(1);
    }
    $tmp=$userInfo;
    print_r($userInfo);
}

      



And check the OpenIdUserinfo class to get the information you need, for example:

$email = $userInfo.getEmail();

      

Sorry for the bad english, and if you need more information please ask. Greetings

0


source







All Articles