Php implements paypal authorized login, php implements paypal authorization
Paypal authorized login using php
<? Php/*** @ project paypal login * @ author jiangjianhe * @ date 2015-04-03 */class paypallogin {// sandbox token link private $ _ sanbox_oauth2_auth_uri = 'https: // export private $ _ live_oauth2_auth_uri = 'https: // your private $ _ acquire_user_profile_sandbox_url = 'https: // www. sandbox. pa Ypal.com/web#/auth/protocol/openidconnect/v1/userinfo? Schema = openid & access_token = '; private $ _ acquire_user_profile_live_url = 'https: // www.paypal.com/web#/auth/protocol/openidconnect/v1/userinfo? Schema = openid & access_token = '; // sandbox token link private $ _ token_service_sandbox_url = 'https: // your private $ _ token_service_live_url = 'https: // define private $ _ sanbox_flag = true; private $ _ client_id = null; private $ _ client_secret = null; private $ _ redirect_uri = null; p Rivate $ _ state = ''; private $ _ scope = 'openid email phone profile address https://uri.paypal.com/services/paypalattributes'; // the url of each parameter that determines the access permission of the access token ;: https://www.paypal-biz.com/product/login-with-paypal/index.html#configureButton public $ token = null; public $ protocol = "http "; /*** @ name constructor * @ 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 () {$ 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 to the user agent, we recommend that you use the <code> code </code> value. 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, expiration => 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 obje Ct */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, rows => FALSE, // CURLOPT_SSLVERSION => 2); curl_setopt_array ($ ch, $ options); $ resp Onse = curl_exec ($ ch); $ error = curl_error ($ ch); curl_close ($ ch); if (! $ Response) {return false;} return json_decode ($ response) ;}catch (Exception $ e) {return false ;}}?>
The above is all the content of this article. I hope you will like it.