2 namespace PayPal\Auth\Openid;
3 use PayPal\Core\PPConstants;
4 use PayPal\Common\PPApiContext;
5 use PayPal\Core\PPConstants;
6 class PPOpenIdSession {
9 * Returns the PayPal URL to which the user must be redirected to
10 * start the authentication / authorization process.
12 * @param string $redirectUri Uri on merchant website to where
13 * the user must be redirected to post paypal login
14 * @param array $scope The access privilges that you are requesting for
15 * from the user. Pass empty array for all scopes.
16 * See https://developer.paypal.com/webapps/developer/docs/classic/loginwithpaypal/ht_OpenIDConnect/#parameters for more
17 * @param PPApiContext $apiContext Optional API Context
19 public static function getAuthorizationUrl($redirectUri, $scope, $apiContext=null) {
21 if(is_null($apiContext)) {
22 $apiContext = new PPApiContext();
24 $config = $apiContext->getConfig();
26 $scope = count($scope) != 0 ? $scope : array('openid', 'profile', 'address', 'email', 'phone', 'https://uri.paypal.com/services/paypalattributes');
27 if(!in_array('openid', $scope)) {
31 'client_id' => $config['acct1.ClientId'],
32 'response_type' => 'code',
33 'scope' => implode(" ", $scope),
34 'redirect_uri' => $redirectUri
36 return sprintf("%s/v1/authorize?%s", self::getBaseUrl($config), http_build_query($params));
41 * Returns the URL to which the user must be redirected to
42 * logout from the OpenID provider (i.e. PayPal)
44 * @param string $redirectUri Uri on merchant website to where
45 * the user must be redirected to post logout
46 * @param string $idToken id_token from the TokenInfo object
47 * @param PayPal/Rest/APIContext $apiContext Optional API Context
49 public static function getLogoutUrl($redirectUri, $idToken, $apiContext=null) {
51 if(is_null($apiContext)) {
52 $apiContext = new PPApiContext();
54 $config = $apiContext->getConfig();
56 PPConstants::OPENID_REDIRECT_LIVE_URL;
58 'id_token' => $idToken,
59 'redirect_uri' => $redirectUri,
62 return sprintf("%s/v1/endsession?%s", self::getBaseUrl($config), http_build_query($params));
65 private static function getBaseUrl($config) {
67 if(array_key_exists('openid.RedirectUri', $config)) {
68 return $config['openid.RedirectUri'];
69 } else if (array_key_exists('mode', $config)) {
70 switch(strtoupper($config['mode'])) {
72 return PPConstants::OPENID_REDIRECT_SANDBOX_URL;
74 return PPConstants::OPENID_REDIRECT_LIVE_URL;