ThinkPHP implements PayPal Login

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

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.