Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to use PayPal to log on to a third-party user who has recently made a cross-border e-commerce program and send it to the desired person.
Namespace Home \ Controller;
Use Think \ Controller;
Class User extends Controller {
Public function paypal (){
$ _ SESSION ['state'] = md5 (uniqid (rand (), TRUE ));
$ Client_id = 'your client_id ';
$ Client_secret = 'your client_secret ';
$ Nonce = time (). rand ();
$ App_return_url = 'HTTP: // yourdomain/user/paypal_return '; // The return address must be the same as the return address you entered when creating client_id on paypal.
$ Scopes = 'profile + email + address + phone ';
$ Paypal_auth_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize? "
. "Client_id =". $ client_id
. "& Response_type = code"
. "& Scope =". $ scopes
. "& Nonce =". $ nonce
. "& State =". $ _ SESSION ['state']
. "& Redirect_uri =". urlencode ($ app_return_url );
// Echo $ paypal_auth_url; exit;
Header ("Location: $ paypal_auth_url ");
}
/**
* Paypal return address
* @ Return void
*/
Public function paypal_return (){
$ Code = trim ($ _ GET ['code']);
$ Client_id = 'your client_id ';
$ Client_secret = 'your client_secret ';
// Obtain access_token based on the authorization code
If (! Isset ($ _ SESSION ['Access _ token']) {
$ Token_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice ";
$ Postvals = "client_id =". $ client_id
. "& Client_secret =". $ client_secret
. "& Grant_type = authorization_code"
. "& Code =". $ code;
$ Response = Http: fsockopenDownload ($ token_url, array (
'Post' => $ postvals,
));
$ Atoken = json_decode ($ response );
$ Access_token = $ atoken-> access_token;
$ _ SESSION ['Access _ token'] = $ access_token; // the token can be saved and used. It seems that it will expire after 15 minutes.
}
// Obtain user information
$ Profile_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo? "
. "Schema = openid"
. "& Access_token =". $ _ SESSION ['Access _ token'];
$ Profile = file_get_contents ($ profile_url );
$ Profile = json_decode ($ profile );
If (isset ($ profile-> error )){
Exit ($ profile-> message );
}
// Note: Some data may not be returned
$ Email = $ profile-> email;
$ Name = $ profile-> name;
$ First_name = $ profile-> family_name;
$ Last_name = $ profile-> given_name;
$ Phone = $ profile-> phone_number;
$ Locale = $ profile-> locale;
$ Address = $ profile-> address;
}
}
?>
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB