支付寶二(網頁授權獲取使用者資訊)
阿新 • • 發佈:2019-01-08
1. 下載SDK
我使用的是 PHP版本的 ,你們根據需要下載對應的
2. 解壓包到對應目錄
3. 在程式中 引入包檔案 AopSdk.php 並初始化
貼上部分程式碼:(基於CI 框架)
//授權回撥 public function auth(){ $params = $this->input->get(NULL,true); if(isset($params['redirect_url'])){ $this->session->set_userdata('alipay_XXX',$params['redirect_XXX']); } if(isset($params['wx_uri'])){ $this->session->set_userdata('alipay_wx_XXX',$params['wx_XXX']); } if(isset($params['auth_XXX'])){ $this->session->set_userdata('alipay_auth_XXX',$params['auth_XXX']); } $auth_type = isset($params['auth_XXX']) ? $params['auth_XXX'] :$this->session->userdata('alipay_auth_XXX');//授權型別,如果需要靜默授權請傳值過來否則預設為auth_userinfo $auth_type = $auth_type ? $auth_type :'auth_userinfo'; if(!isset($params['auth_code'])){ $redirect_url = urlencode(AUTH_DOMAIN.'auth/'); $auth_url = ALIPAY_AUTH_URL ."?app_id={$this->alipay_appid}&scope={$auth_type}&redirect_uri={$redirect_url}&state=XXX"; header("Location:$auth_url");exit; }else{ $auth_code = $_GET['auth_code']; $http_query = array( 'app_id' => $this->alipay_appid, 'method' => 'alipay.system.oauth.token', 'charset'=> $this->AopClient->charset, 'sign_type'=>'RSA', 'timestamp'=>date('Y-m-d H:i:s'), 'version'=> 1.0, 'grant_type'=>'authorization_code', 'code'=>$auth_code ); $user_url = ALIPAY_API_URL ; $sign = $this->AopClient->rsaSign($http_query); $http_query['sign'] = $sign; $res_str = $this->curl->simple_post($user_url,$http_query); $res_str = iconv('GBK','UTF-8',$res_str); $res_arr = json_decode($res_str,true); //授權出錯 終止授權 if(isset($res_arr['error_response'])||(isset($res_arr['alipay_system_oauth_token_response']) && isset($res_arr['alipay_system_oauth_token_response']['code']))){ $err_code = isset($res_arr['error_response'])? $res_arr['error_response']['code'] : $res_arr['alipay_system_oauth_token_response']['code']; die("支付寶授權出錯【出錯碼{$err_code}】"); } //靜默授權直接拼接資料 if($auth_type=='auth_base'){ $userinfo['openid'] = $res_arr['alipay_system_oauth_token_response']['user_id']; $userinfo['nickname'] ='XXX'; $userinfo['headimgurl'] =''; $userinfo['sex'] ='2'; }else{ $access_token = $res_arr['alipay_system_oauth_token_response']['access_token']; $user_query = array( 'method'=>'alipay.user.userinfo.share', 'timestamp'=> date('Y-m-d H:i:s'), 'app_id'=>$this->alipay_appid, 'auth_token'=>$access_token, 'charset'=>$this->AopClient->charset, 'version'=>'1.0', 'sign_type'=>'RSA' ); $usersign = $this->AopClient->rsaSign($user_query); $user_query['sign'] = $usersign; $user_info_str = $this->curl->simple_post($user_url,$user_query); $user_info_str = iconv('GBK','UTF-8',$user_info_str); $user_info = json_decode($user_info_str,true); if(isset($user_info['error_response'])||(isset($user_info['alipay_user_userinfo_share_response']) && isset($user_info['alipay_user_userinfo_share_response']['code']))){ $err_code = isset($user_info['error_response'])? $user_info['error_response']['code'] : $user_info['alipay_user_userinfo_share_response']['code']; } $userinfo = array( 'openid' => $user_info['alipay_user_userinfo_share_response']['user_id'], 'nickname'=> $user_info['alipay_user_userinfo_share_response']['real_name'], 'headimgurl' =>$user_info['alipay_user_userinfo_share_response']['avatar'], 'sex' =>$user_info['alipay_user_userinfo_share_response']['gender'] == 'F' ? 2 : 1, ); } if(!$this->ci_redis->get($params['auth_code']) || $this->ci_redis->get($params['auth_code']) == 'false'){ $this->ci_redis->set($params['auth_code'],json_encode($userinfo),300); } $redirect_url = base64_decode($this->session->userdata('alipay_redirect_url')); $wx_uri = $this->session->userdata('alipay_wx_XXX'); header("Location:".$redirect_url.'?code='.$params['auth_code'].'&wx_uri='.$wx_uri);exit; } }
【注意】
【2】網頁授權私鑰來自 軟體生成的 rsa_private_key.pem 不是 rsa_private_key_pkcs8.pem
且私鑰字串拷貝出來 去掉頭尾 和 換行符
【3】網頁授權 不能用沙箱 app 來掃取 正式地址,否則會一直跳轉
【4】使用 auth_userinfo 獲取到的userId 與 支付生成的 buyerId 不是同一個,而與靜默授權 auth_base 取到的
userId 是同一個,所以 建議使用靜默授權,反正支付寶網頁授權獲取使用者資訊,文件有點奇葩,說好的非靜默授權能獲取到使用者的暱稱和頭像,結果一個木有獲取到,問客服,客服說文件僅供參考,我就呵呵了。。。