/** * @project PayPal Login * @author Jiangjianhe * @date 2015-04-03 */ Class Paypallogin { Sandbox token link Private $_sanbox_oauth2_auth_uri = ' https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize '; Private $_live_oauth2_auth_uri = ' https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize '; Private $_acquire_user_profile_sandbox_url = ' https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/ V1/userinfo?schema=openid&access_token= '; Private $_acquire_user_profile_live_url = ' Https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo ? schema=openid&access_token= '; Sandbox token link Private $_token_service_sandbox_url = ' https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/ Tokenservice '; Private $_token_service_live_url = ' https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice '; Private $_sanbox_flag = true; Private $_client_id = null; Private $_client_secret = null; Private $_redirect_uri = null; Private $_state = '; Private $_scope = ' OpenID Email phone profile address https://uri.paypal.com/services/paypalattributes '; The scope parameter determines access permissions for the access token. Each parameter is detailed Url;:https://www.paypal-biz.com/product/login-with-paypal/index.html#configurebutton public $token = null; Public $protocol = "http"; /** * @name Constructors * @param $flag Sandbox environment */ Public function __construct ($redirect _uri, $client _id, $client _secret, $scope, $state, $flag = True) { $this->_sanbox_flag = $flag; $this->_redirect_uri = $redirect _uri; $this->_client_id = $client _id; $this->_client_secret = $client _secret; $this->_scope = $scope; $this->_state = $state; } /** * Create a PayPal request URL * @return String */ Public Function Create_request_url () { $oauth 2_auth_uri = $this->_sanbox_flag? $this->_sanbox_oauth2_auth_uri: $this->_live_oauth2_auth_uri; $url = $oauth 2_auth_uri. '? '. Http_build_query ( Array ' client_id ' = $this->_client_id,//Unique client identifier obtained through the application registration process. Necessary. ' Response_type ' = ' code ',//indicates that the authorization code is sent back to the application return URL. To make the access token invisible in the user agent, code a value is recommended. If you want to receive both the authorization code and the Id_token in the response, pass Code+id_token. Another possible response_type value is token--, which is mostly used by public clients such as JavaScript and mobile clients. ' Scope ' = $this->_scope,//;implode (', ', $this->scope), ' Redirect_uri ' = UrlEncode ($this->_redirect_uri),//The return URL of the application. The structure, host name, and Port must match the return URL that you set when registering the application. ' Nonce ' = Time (). Rand (),//opaque random identifiers, reduces the risk of replay attacks. The simple functions are: (timestamp + BASE64 encoding (random\[16\])). ' state ' = $this->_state,//CSRF Verification Code ) ); return $url; } /** * Get PayPal access token * @param string $code? * @return String access token */ Public Function Acquire_access_token ($code) { $accessToken = null; try { $postvals = sprintf ("client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s", $this- _client_id, $this->_client_secret, $code); if ($this->_sanbox_flag) $ch = Curl_init ($this->_token_service_sandbox_url); Else $ch = Curl_init ($this->_token_service_live_url); $options = Array ( Curlopt_post = 1, Curlopt_verbose = 1, Curlopt_postfields = $postvals, Curlopt_returntransfer = 1, Curlopt_ssl_verifypeer = FALSE, Curlopt_sslversion = 2 ); Curl_setopt_array ($ch, $options); $response = curl_exec ($ch); $error = Curl_error ($ch); Curl_close ($ch); if (! $response) { throw new Exception ("Error Retrieving access token:".) Curl_error ($ch)); } $jsonResponse = Json_decode ($response); if (Isset ($jsonResponse->access_token)) { $accessToken = $jsonResponse->access_token; } } catch (Exception $e) { throw new Exception ($e->getmessage (), 1); } return $accessToken; } /** * Get the PayPal user profile, decoded * @param string $accessToken * @return Object */ Public Function Acquire_paypal_user_profile ($accessToken) { try { if ($this->_sanbox_flag) $url = $this->_acquire_user_profile_sandbox_url. $accessToken; Else $url = $this->_acquire_user_profile_live_url. $accessToken; $ch = Curl_init ($url); $options = Array ( Curlopt_returntransfer = 1, Curlopt_ssl_verifypeer = FALSE, Curlopt_sslversion = 2 ); Curl_setopt_array ($ch, $options); $response = curl_exec ($ch); $error = Curl_error ($ch); Curl_close ($ch); if (! $response) { return false; } Return Json_decode ($response); } catch (Exception $e) { return false; } } } ?> |