首页 > php教程 > php手册 > 正文

ThinkPHP实现PayPal登录

WBOY
发布: 2016-06-07 11:37:34
原创
1304 人浏览过

最近做跨境电子商务程序的第三方登录用到了PayPal登录,发上来给需要的人。
<?php <br /> namespace Home\Controller;<br> use Think\Controller;<br> class User extends Controller{<br>     public function paypal(){<br>         $_SESSION['state'] = md5(uniqid(rand(), TRUE)); <br>         $client_id = '你的client_id';<br>         $client_secret = '你的client_secret';<br>         $nonce = time() . rand();<br>         $app_return_url = 'http://yourdomain/user/paypal_return';  //这里的返回地址必须要与你在paypal上创建client_id时填写的返回地址一致<br>         $scopes = 'profile+email+address+phone';<br>     $paypal_auth_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?"<br>             ."client_id=".$client_id<br>             ."&response_type=code"<br>             ."&scope=".$scopes<br>             ."&nonce=".$nonce<br>             ."&state=".$_SESSION['state']<br>             ."&redirect_uri=".urlencode($app_return_url);<br>         //echo $paypal_auth_url;exit;<br>     header("Location: $paypal_auth_url"); <br>     }<br> <br>     /**<br>      * Paypal返回地址<br>      * @return void<br>      */<br>     public function paypal_return(){<br>         $code = trim($_GET['code']);<br>         <br>         $client_id = '你的client_id';<br>         $client_secret = '你的client_secret';<br>         //根据授权码获取到access_token    <br>         if(!isset($_SESSION['access_token'])){    <br>             $token_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice";    <br>         $postvals = "client_id=".$client_id<br>             ."&client_secret=".$client_secret<br>             ."&grant_type=authorization_code"<br>             ."&code=".$code;<br>         $response = Http::fsockopenDownload($token_url,array(<br>             'post' => $postvals,<br>         ));<br>         $atoken = json_decode($response);<br>         $access_token = $atoken->access_token;<br>         $_SESSION['access_token'] = $access_token;  //token可以保存起来使用,貌似15分钟后才会失效<br>       }<br>        //获取到用户资料<br>        $profile_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?"<br>             ."schema=openid"<br>             ."&access_token=".$_SESSION['access_token'];<br>     $profile = file_get_contents($profile_url);<br>     $profile = json_decode($profile);<br>     if(isset($profile->error)){<br>         exit($profile->message);<br>     }<br>     //注意:有些资料不一定会返回<br>     $email = $profile->email;<br>     $name = $profile->name;<br>     $first_name = $profile->family_name;<br>     $last_name = $profile->given_name;<br>     $phone = $profile->phone_number;<br>     $locale = $profile->locale;<br>     $address = $profile->address;<br>   }<br> }<br> ?>

AD:真正免费,域名+虚机+企业邮箱=0元

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!