PHP implementation PayPal Authorized login
<?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 access to 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 constructor * @param $flag Sandbox environment/Public function __construct ($redirect _uri, $client _id, $client _secret, $SC
Ope, $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 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. In order for the access token to be invisible in the user agent, a <code>code</code> value is recommended. If you want to receive both authorization code and Id_token in the response, pass Code+id_token.
Another possible response_type value is that token--is mostly used by public clients such as JavaScript and mobile clients. ' Scope ' => $this->_scope,//;implode (', ', $this->scope), ' Redirect_uri ' => urlencode ($this->_redirec T_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 identifier, reduces the risk of replay attacks.
The Simple function is: (timestamp + Base64 encoding (random\[16\)).
' State ' => $this->_state,//CSRF authentication 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_sslversio
N => 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 Fun Ction Acquire_paypal_user_profile ($accessToken) {try {if ($this->_sanbox_flag) $url = $this->_ac Quire_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_SS
Lversion => 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 the entire contents of this article, I hope you can enjoy.