您好,登錄后才能下訂單哦!
<?php //私鑰和公鑰在芝麻信用后臺設(shè)置,官網(wǎng)有詳細說明 class ZhimaAction extends CommonAction { //芝麻信用網(wǎng)關(guān)地址 public $gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do"; //商戶私鑰文件 public $privateKeyFile = "商戶私鑰文件(絕對路徑)"; //芝麻公鑰文件 public $zmPublicKeyFile = "芝麻公鑰文件(絕對路徑)"; //數(shù)據(jù)編碼格式 public $charset = "UTF-8"; //應(yīng)用id public $app_id = "*******"; //要調(diào)用的接口名 public $method = "zhima.credit.score.get"; //來源平臺,默認為zmop public $platform = "zmop"; //接口版本,目前只支持1.0 public $version = "1.0"; // 加密后信息 RSA加密后的業(yè)務(wù)參數(shù) public $params = ""; // 加密后信息 對params參數(shù)加密前的簽名,算法為SHA1WithRSA public $sign = "1.0"; /* 加簽過程 1、在加密過程的第一步,我們得到了拼接在一起的業(yè)務(wù)參數(shù),同樣以芝麻信用評分為例,拼接的參數(shù)如下: transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496) 2、使用 SHA1WithRSA 算法以及商戶自己的私鑰進行簽名,得到 byte 數(shù)組 SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496)) 3、將 byte 數(shù)組進行 Base64 編碼,得到一個簽名的字符串 Base64(SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496))) 經(jīng)過了上述三步,我們便得到了業(yè)務(wù)參數(shù)的簽名,最后我們將這個簽名的值放入系統(tǒng)參數(shù) sign 中: sign=Base64(SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496))) 解密和驗簽 */ public function __construct() { parent::__construct(); //在官網(wǎng)下載 Vendor('zhima.zmop.ZmopClient'); Vendor('zhima.zmop.RSAUtil'); Vendor('zhima.zmop.ZhimaCreditIvsDetailGetRequest'); Vendor('zhima.ZmopSdk'); } public function grant(){ if(IS_POST){ $name = I('name');//姓名 $IDnumber = I('IDnumber'); //×××號碼 $client = new ZmopClient($this->gatewayUrl,$this->app_id,$this->charset,$this->privateKeyFile,$this->zmPublicKeyFile); $RSAUtil = new RSAUtil(); $identity_type ='2'; $identity_param =json_encode(array('certNo'=>$IDnumber,'name'=>$name,'certType'=>"IDENTITY_CARD")); $request['app_id'] = $this->app_id; $request['charset'] = $this->charset; $request['method'] = 'zhima.auth.info.authorize'; $request['version'] = $this->version; $request['platform'] = $this->platform; $request['params'] = $RSAUtil->rsaEncrypt($str,$this->zmPublicKeyFile); $request['sign'] = $RSAUtil->sign($str,$this->privateKeyFile); $request['identity_type'] = $identity_type; $request['identity_param'] = $identity_param; $str ='identity_type='.urlencode($identity_type).'&identity_param='.urlencode($identity_param).''; $request = new ZhimaAuthInfoAuthorizeRequest (); $request->setIdentityType ("2"); // $request->setParams ("2"); $request->setIdentityParam ($identity_param); //$request->setBizParams ( "{\"auth_code\":\"M_APPPC_CERT\",\"state\":\"透傳參數(shù)\"}" ); // $url = $client->generatePageRedirectInvokeUrl ( $request ); //dump($url); if($url){ $json['msg'] =1; $json['url'] =$url; }else{ $json['msg'] =0; $json['info'] ='參數(shù)錯誤'; } echo json_encode($json);exit; }else{ $json['msg'] =0; $json['info'] ='參數(shù)錯誤'; echo json_encode($json);exit; } } //返回 public function returndata(){ $params=$_GET['params']; $sign=$_GET['sign']; if(!$sign){ $this->redirect('Member/rz');exit; } // 判斷串中是否有%,有則需要decode // dump($sign); $params = strstr ( $params, '%' ) ? urldecode ( $params ) : $params; $sign = strstr ( $sign, '%' ) ? urldecode ( $sign ) : $sign; $client = new ZmopClient ( $this->gatewayUrl, $this->app_id, $this->charset, $this->privateKeyFile, $this->zmPublicKeyFile ); $result = $client->decryptAndVerifySign ( $params, $sign ); //轉(zhuǎn)數(shù)組 $parts = explode('&',$result); $array=array(); foreach($parts as $k=>$v){ $parts[$k] = explode('=',$v); $array[$parts[$k]['0']] = $parts[$k]['1']; } //dump($array['success']='false'); if($array['success']!='false'){ //dump($array);exit; $res =$this->ToRz($array); if($res->success){ //返回數(shù)據(jù) 更新會員信息 $is_zhima = M('member')->where(array('id'=>cookie('id')))->find(); if($is_zhima['zhima']!=$res->zm_score){ $save['open_id'] = $array['open_id']; $save['zhima'] = $res->zm_score; $save['state'] = '1'; $save['optime'] = time(); $save['rztime'] = time(); $member = M('member')->where(array('id'=>cookie('id')))->save($save); } echo '<script>alert("認證成功");window.location.href ="/Member/rz"</script>'; } ///dump($member); // dump($result); }else{ //echo '<script>alert("驗簽失敗");window.location.href ="/Member/rz"</script>'; $this->redirect('Member/rz');exit; } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。