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

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

Tags: paypal授权登录

分享到: