Paypal authorized login implementation
_ 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 () {$ oauth2_auth_uri = $ this-> _ sanbox_flag? $ This-> _ sanbox_oauth2_auth_uri: $ this-> _ live_oauth2_auth_uri; $ url = $ oauth2_auth_uri .'? '. Http_build_query (array ('Client _ id' => $ this-> _ client_id, // unique client identifier obtained through the application registration process. Required. 'Response _ type' => 'code', // indicates that the authorization code is sent to the response URL returned by the program. To make the access token invisible in the user proxy, we recommend that you use code 1. If you want to receive both the authorization code and id_token in the response, pass the code + id_token. Another possible value of response_type is token, which is mostly used by public clients such as javascript and mobile clients. 'Process' => $ this-> _ scope, //; implode (',', $ this-> scope ), 'redirect _ url' => urlencode ($ this-> _ redirect_uri), // The URL returned by the application. The structure, host name, and Port must match the returned URL you set when registering the application. 'Nonce '=> time (). rand (), // untransparent random identifier to reduce the risk of replay attacks. The simple function is: (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, expiration => 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 */publi C 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, rows => 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 ;}}?>