当前位置:首页 > PHP教程 > php应用 > 列表

php实现paypal 授权登录

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-27 13:53:38 浏览: 评论:0 

本文给大家分享的是php实现的paypal授权登录的代码,十分的简单实用,有需要的小伙伴可以参考下。

php实现paypal 授权登录:

  1. <?php 
  2.    
  3. /** 
  4.  * @project   paypal login 
  5.  * @author   jiangjianhe  
  6.  * @date   2015-04-03 
  7.  */ 
  8.    
  9.    
  10. class paypallogin 
  11. { 
  12.    
  13.   //沙箱token链接 
  14.   private $_sanbox_oauth2_auth_uri = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize'; 
  15.   private $_live_oauth2_auth_uri = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize'; 
  16.      
  17.   private $_acquire_user_profile_sandbox_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?schema=openid&access_token='; 
  18.   private $_acquire_user_profile_live_url = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?schema=openid&access_token='; 
  19.    
  20.   //沙箱token链接 
  21.   private $_token_service_sandbox_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice';  
  22.   private $_token_service_live_url = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice'; 
  23.   private $_sanbox_flag = true; 
  24.   private $_client_id = null; 
  25.   private $_client_secret = null; 
  26.   private $_redirect_uri = null; 
  27.   private $_state = ''; 
  28.   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 
  29.    
  30.   public $token = null; 
  31.   public $protocol = "http"; 
  32.    
  33.    
  34.   /** 
  35.   * @name 构造函数 
  36.   * @param $flag 是否沙箱环境 
  37.   */ 
  38.   public function __construct($redirect_uri, $client_id,$client_secret,$scope,$state,$flag = true) 
  39.   { 
  40.     $this->_sanbox_flag = $flag; 
  41.     $this->_redirect_uri = $redirect_uri; 
  42.     $this->_client_id = $client_id; 
  43.     $this->_client_secret = $client_secret; 
  44.     $this->_scope = $scope; 
  45.     $this->_state = $state; 
  46.   } 
  47.    
  48.   /** 
  49.    * 创建paypal request url 
  50.    * @return string 
  51.    */ 
  52.   public function create_request_url() 
  53.   { 
  54.     $oauth2_auth_uri = $this->_sanbox_flag ? $this->_sanbox_oauth2_auth_uri :$this->_live_oauth2_auth_uri; 
  55.     $url = $oauth2_auth_uri.'?'. 
  56.     http_build_query( 
  57.       array( 
  58.         'client_id' => $this->_client_id, //通过应用程序注册流程获得的唯一客户端标识符。必需。 
  59.         'response_type' =>'code', //表明授权代码被发送回应用程序返回URL。为了使访问令牌在用户代理中不可见, 建议使用<code>code</code>一值。如果您希望在响应中同时收到授权代码和 id_token ,请传递 code+id_token。另一个可能的 response_type 值是 token ——大部分由javascript和移动客户端等公共客户端使用。 
  60.         'scope' => $this->_scope,//;implode(',', $this->scope), 
  61.         'redirect_uri' => urlencode($this->_redirect_uri), //应用程序的返回URL。结构、主机名和端口必须与您在注册应用程序时设置的返回URL相符。 
  62.         'nonce' => time().rand(), //不透明的随机标识符,可减少重放攻击风险。简单的函数是:(timestamp + Base64 encoding (random\[16\]))。 
  63.         'state' => $this->_state, // CSRF验证码 
  64.       ) 
  65.     ); 
  66.     return $url; 
  67.   } 
  68.    
  69.   /** 
  70.    * get PayPal access token 
  71.    * @param string $code ? 
  72.    * @return string    access token 
  73.    */ 
  74.   public function acquire_access_token($code ) { 
  75.     $accessToken = null; 
  76.    
  77.     try { 
  78.       $postvals = sprintf("client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s",$this->_client_id,$this->_client_secret,$code); 
  79.       if($this->_sanbox_flag) 
  80.         $ch = curl_init($this->_token_service_sandbox_url); 
  81.       else 
  82.         $ch = curl_init($this->_token_service_live_url);  
  83.    
  84.       $options = array( 
  85.         CURLOPT_POST      => 1, 
  86.         CURLOPT_VERBOSE    => 1, 
  87.         CURLOPT_POSTFIELDS   => $postvals, 
  88.         CURLOPT_RETURNTRANSFER => 1, 
  89.         CURLOPT_SSL_VERIFYPEER => FALSE, 
  90.         //CURLOPT_SSLVERSION => 2 
  91.       ); 
  92.    
  93.       curl_setopt_array($ch, $options); 
  94.       $response = curl_exec($ch); 
  95.       $error = curl_error($ch); 
  96.    
  97.       curl_close( $ch ); 
  98.    
  99.       if (!$response ) { 
  100.         throw new Exception( "Error retrieving access token: " . curl_error($ch)); 
  101.       } 
  102.       $jsonResponse = json_decode($response ); 
  103.    
  104.       if ( isset( $jsonResponse->access_token) ) { 
  105.         $accessToken = $jsonResponse->access_token; 
  106.       } 
  107.    
  108.     } catch( Exception $e) { 
  109.       throw new Exception($e->getMessage(), 1); 
  110.     } 
  111.    
  112.     return $accessToken; 
  113.   } 
  114.    
  115.   /** 
  116.    * get the PayPal user profile, decoded 
  117.    * @param string $accessToken 
  118.    * @return object 
  119.    */ 
  120.   public function acquire_paypal_user_profile($accessToken ) { 
  121.     try { 
  122.       if($this->_sanbox_flag) 
  123.         $url = $this->_acquire_user_profile_sandbox_url . $accessToken; 
  124.       else 
  125.         $url = $this->_acquire_user_profile_live_url . $accessToken;   
  126.    
  127.       $ch = curl_init( $url ); 
  128.       $options = array( 
  129.         CURLOPT_RETURNTRANSFER => 1, 
  130.         CURLOPT_SSL_VERIFYPEER => FALSE, 
  131.         //CURLOPT_SSLVERSION => 2 
  132.       ); 
  133.       curl_setopt_array($ch, $options); 
  134.    
  135.       $response = curl_exec($ch); 
  136.       $error = curl_error( $ch); 
  137.       curl_close( $ch ); 
  138.    
  139.       if (!$response )  
  140.       { 
  141.         return false; 
  142.       } 
  143.       return json_decode($response); 
  144.     } catch( Exception $e ) { 
  145.       return false; 
  146.     } 
  147.   } 
  148. } 
  149. ?> 

以上所述就是本文的全部内容了,希望大家能够喜欢。

Tags: paypal授权登录

分享到: