PayPalApiOpenIdSession

The PayPal for WooCommerce PayPal Api OpenIdSession class.

Defined (1)

The class is defined in the following location(s).

/classes/lib/PayPal/Api/OpenIdSession.php  
  1. class OpenIdSession 
  2.  
  3. /** 
  4. * Returns the PayPal URL to which the user must be redirected to 
  5. * start the authentication / authorization process. 
  6. * @param string $redirectUri Uri on merchant website to where 
  7. * the user must be redirected to post paypal login 
  8. * @param array $scope The access privilges that you are requesting for 
  9. * from the user. Pass empty array for all scopes. 
  10. * @param string $clientId client id from developer portal 
  11. * See https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/#attributes for more 
  12. * @param null $nonce 
  13. * @param null $state 
  14. * @param ApiContext $apiContext Optional API Context 
  15. * @return string Authorization URL 
  16. */ 
  17. public static function getAuthorizationUrl($redirectUri, $scope, $clientId, $nonce = null, $state = null, $apiContext = null) 
  18. $apiContext = $apiContext ? $apiContext : new ApiContext(); 
  19. $config = $apiContext->getConfig(); 
  20.  
  21. if ($apiContext->get($clientId)) { 
  22. $clientId = $apiContext->get($clientId); 
  23.  
  24. $clientId = $clientId ? $clientId : $apiContext->getCredential()->getClientId(); 
  25.  
  26. $scope = count($scope) != 0 ? $scope : array('openid', 'profile', 'address', 'email', 'phone',  
  27. 'https://uri.paypal.com/services/paypalattributes', 'https://uri.paypal.com/services/expresscheckout'); 
  28. if (!in_array('openid', $scope)) { 
  29. $scope[] = 'openid'; 
  30.  
  31. $params = array( 
  32. 'client_id' => $clientId,  
  33. 'response_type' => 'code',  
  34. 'scope' => implode(" ", $scope),  
  35. 'redirect_uri' => $redirectUri 
  36. ); 
  37.  
  38. if ($nonce) { 
  39. $params['nonce'] = $nonce; 
  40. if ($state) { 
  41. $params['state'] = $state; 
  42. return sprintf("%s/v1/authorize?%s", self::getBaseUrl($config), http_build_query($params)); 
  43.  
  44.  
  45. /** 
  46. * Returns the URL to which the user must be redirected to 
  47. * logout from the OpenID provider (i.e. PayPal) 
  48. * @param string $redirectUri Uri on merchant website to where 
  49. * the user must be redirected to post logout 
  50. * @param string $idToken id_token from the TokenInfo object 
  51. * @param ApiContext $apiContext Optional API Context 
  52. * @return string logout URL 
  53. */ 
  54. public static function getLogoutUrl($redirectUri, $idToken, $apiContext = null) 
  55.  
  56. if (is_null($apiContext)) { 
  57. $apiContext = new ApiContext(); 
  58. $config = $apiContext->getConfig(); 
  59.  
  60. $params = array( 
  61. 'id_token' => $idToken,  
  62. 'redirect_uri' => $redirectUri,  
  63. 'logout' => 'true' 
  64. ); 
  65. return sprintf("%s/v1/endsession?%s", self::getBaseUrl($config), http_build_query($params)); 
  66.  
  67. /** 
  68. * Gets the base URL for the Redirect URI 
  69. * @param $config 
  70. * @return null|string 
  71. */ 
  72. private static function getBaseUrl($config) 
  73.  
  74. if (array_key_exists('openid.RedirectUri', $config)) { 
  75. return $config['openid.RedirectUri']; 
  76. } else if (array_key_exists('mode', $config)) { 
  77. switch (strtoupper($config['mode'])) { 
  78. case 'SANDBOX': 
  79. return PayPalConstants::OPENID_REDIRECT_SANDBOX_URL; 
  80. case 'LIVE': 
  81. return PayPalConstants::OPENID_REDIRECT_LIVE_URL; 
  82. return null;