Paypal authorized login implementation-PHP source code

Source: Internet
Author: User
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 ;}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.