免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1930 | 回复: 0
打印 上一主题 下一主题

paypal 授权登录实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-20 13:08 |只看该作者 |倒序浏览
[PHP]代码
  1. <?php

  2. /**
  3. * @project     paypal login
  4. * @author      jiangjianhe
  5. * @date      2015-04-03
  6. */


  7. class paypallogin
  8. {

  9.     //沙箱token链接
  10.     private $_sanbox_oauth2_auth_uri = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize';
  11.     private $_live_oauth2_auth_uri = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize';
  12.      
  13.     private $_acquire_user_profile_sandbox_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?schema=openid&access_token=';
  14.     private $_acquire_user_profile_live_url = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?schema=openid&access_token=';

  15.     //沙箱token链接
  16.     private $_token_service_sandbox_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice';
  17.     private $_token_service_live_url  = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice';
  18.     private $_sanbox_flag = true;
  19.     private $_client_id = null;
  20.     private $_client_secret = null;
  21.     private $_redirect_uri = null;
  22.     private $_state = '';
  23.     private $_scope = 'openid email phone profile address https://uri.paypal.com/services/paypalattributes'; //scope 参数决定访问令牌的访问权限 各个参数详解url;:https://www.paypal-biz.com/product/login-with-paypal/index.html#configureButton

  24.     public $token = null;
  25.     public $protocol = "http";


  26.     /**
  27.     * @name 构造函数
  28.     * @param $flag 是否沙箱环境
  29.     */
  30.     public function __construct($redirect_uri, $client_id,$client_secret,$scope,$state,$flag = true)
  31.     {
  32.         $this->_sanbox_flag = $flag;
  33.         $this->_redirect_uri = $redirect_uri;
  34.         $this->_client_id = $client_id;
  35.         $this->_client_secret = $client_secret;
  36.         $this->_scope = $scope;
  37.         $this->_state = $state;
  38.     }

  39.     /**
  40.      * 创建paypal request url
  41.      * @return string
  42.      */
  43.     public function create_request_url()
  44.     {
  45.         $oauth2_auth_uri = $this->_sanbox_flag ? $this->_sanbox_oauth2_auth_uri :$this->_live_oauth2_auth_uri;
  46.         $url =  $oauth2_auth_uri.'?'.
  47.         http_build_query(
  48.             array(
  49.                 'client_id' => $this->_client_id, //通过应用程序注册流程获得的唯一客户端标识符。必需。
  50.                 'response_type' =>'code', //表明授权代码被发送回应用程序返回URL。为了使访问令牌在用户代理中不可见, 建议使用<code>code</code>一值。如果您希望在响应中同时收到授权代码和 id_token ,请传递 code+id_token。另一个可能的 response_type 值是 token ——大部分由javascript和移动客户端等公共客户端使用。
  51.                 'scope' => $this->_scope,//;implode(',', $this->scope),
  52.                 'redirect_uri' => urlencode($this->_redirect_uri), //应用程序的返回URL。结构、主机名和端口必须与您在注册应用程序时设置的返回URL相符。
  53.                 'nonce' => time().rand(), //不透明的随机标识符,可减少重放攻击风险。简单的函数是:(timestamp + Base64 encoding (random\[16\]))。
  54.                 'state' => $this->_state, // CSRF验证码
  55.             )
  56.         );
  57.         return $url;
  58.     }

  59.     /**
  60.      * get PayPal access token
  61.      * @param  string $code ?
  62.      * @return string       access token
  63.      */
  64.     public function acquire_access_token($code ) {
  65.         $accessToken = null;

  66.         try {
  67.             $postvals = sprintf("client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s",$this->_client_id,$this->_client_secret,$code);
  68.             if($this->_sanbox_flag)
  69.                $ch = curl_init($this->_token_service_sandbox_url);
  70.             else
  71.                $ch = curl_init($this->_token_service_live_url);  

  72.             $options = array(
  73.                 CURLOPT_POST           => 1,
  74.                 CURLOPT_VERBOSE        => 1,
  75.                 CURLOPT_POSTFIELDS     => $postvals,
  76.                 CURLOPT_RETURNTRANSFER => 1,
  77.                 CURLOPT_SSL_VERIFYPEER => FALSE,
  78.                 //CURLOPT_SSLVERSION => 2
  79.             );

  80.             curl_setopt_array($ch, $options);
  81.             $response = curl_exec($ch);
  82.             $error = curl_error($ch);

  83.             curl_close( $ch );

  84.             if (!$response ) {
  85.                 throw new Exception( "Error retrieving access token: " . curl_error($ch));
  86.             }
  87.             $jsonResponse = json_decode($response );

  88.             if ( isset( $jsonResponse->access_token) ) {
  89.                 $accessToken = $jsonResponse->access_token;
  90.             }

  91.         } catch( Exception $e) {
  92.             throw new Exception($e->getMessage(), 1);
  93.         }

  94.         return $accessToken;
  95.     }

  96.     /**
  97.      * get the PayPal user profile, decoded
  98.      * @param  string $accessToken
  99.      * @return object
  100.      */
  101.     public function acquire_paypal_user_profile($accessToken ) {
  102.         try {
  103.             if($this->_sanbox_flag)
  104.                $url = $this->_acquire_user_profile_sandbox_url . $accessToken;
  105.             else
  106.                 $url = $this->_acquire_user_profile_live_url . $accessToken;   

  107.             $ch = curl_init( $url );
  108.             $options = array(
  109.                 CURLOPT_RETURNTRANSFER => 1,
  110.                 CURLOPT_SSL_VERIFYPEER => FALSE,
  111.                 //CURLOPT_SSLVERSION => 2
  112.             );
  113.             curl_setopt_array($ch, $options);

  114.             $response = curl_exec($ch);
  115.             $error = curl_error( $ch);
  116.             curl_close( $ch );

  117.             if (!$response )
  118.             {
  119.                 return false;
  120.             }
  121.             return json_decode($response);
  122.         } catch( Exception $e ) {
  123.             return false;
  124.         }
  125.     }

  126.    

  127.      

  128. }
  129. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP