亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: paypal 授權(quán)登錄實現(xiàn) [打印本頁]

作者: bigstarr    時間: 2015-07-20 13:08
標(biāo)題: paypal 授權(quán)登錄實現(xiàn)
[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 參數(shù)決定訪問令牌的訪問權(quán)限 各個參數(shù)詳解url;:https://www.paypal-biz.com/product/login-with-paypal/index.html#configureButton

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


  26.     /**
  27.     * @name 構(gòu)造函數(shù)
  28.     * @param $flag 是否沙箱環(huán)境
  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.      * 創(chuàng)建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, //通過應(yīng)用程序注冊流程獲得的唯一客戶端標(biāo)識符。必需。
  50.                 'response_type' =>'code', //表明授權(quán)代碼被發(fā)送回應(yīng)用程序返回URL。為了使訪問令牌在用戶代理中不可見, 建議使用<code>code</code>一值。如果您希望在響應(yīng)中同時收到授權(quán)代碼和 id_token ,請傳遞 code+id_token。另一個可能的 response_type 值是 token ——大部分由javascript和移動客戶端等公共客戶端使用。
  51.                 'scope' => $this->_scope,//;implode(',', $this->scope),
  52.                 'redirect_uri' => urlencode($this->_redirect_uri), //應(yīng)用程序的返回URL。結(jié)構(gòu)、主機(jī)名和端口必須與您在注冊應(yīng)用程序時設(shè)置的返回URL相符。
  53.                 'nonce' => time().rand(), //不透明的隨機(jī)標(biāo)識符,可減少重放攻擊風(fēng)險。簡單的函數(shù)是:(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. ?>
復(fù)制代碼





歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2