This article will share with you the php-implemented paypal authorized login code, which is very simple and practical. If you need it, you can refer to it.
This article will share with you the php-implemented paypal authorized login code, which is very simple and practical. If you need it, you can refer to it.
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://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 the access permission of the access token ;: https://www.paypal-biz.com/product/login-with-paypal/index.html#configureButtonpublic $ 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 st Ring */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. We recommend that you usecodeA 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, C URLOPT_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 object */public fu Nction acquire_paypal_user_profile ($ accesireen) {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 ;}}?>
The above is all the content of this article. I hope you will like it.