目录
接口说明
在对接一些金融app软件用到芝麻分接口,需要获取芝麻分前提是需要进行认证,获取芝麻分分为两个步骤:
- 用户认证(获取opneid)
- 获取芝麻分(后台可操作)
特别注意
在生成公钥之后需要在芝麻商户平台上面提交,然后芝麻平台会生成一个与之对应的芝麻公钥,在应用哪里复制过来,然后在上头部和尾部,中间公钥部分需要每隔64字符换一行(与私钥对应),不然会一直报错公钥错误,私钥错误之类的信息!
github地址:https://github.com/laiyuxiang/zmf_get
用户认证
- 获取芝麻分认证链接
$in = array();
$in['name'] = '身份证真实名称';
$in['certType'] = 'IDENTITY_CARD';
$in['certNo'] = '身份证号码';
$barr = array();
$barr['auth_code'] = 'M_H5';
$barr['channelType'] = 'apppc';
$barr['state'] = urlencode($param); //这个地方参数可以传入到回调地址 一般用于传输用户信息
$bjson = json_encode($barr);
$injson = json_encode($in);
$gatewayUrl = 'https://zmopenapi.zmxy.com.cn/openapi.do';
$appId = 'appid';
$charset = 'UTF-8';
$privateKeyFile = C('PRIVATE_KEY');
$zmPublicKeyFile = C('IPUBLIC_KEY');
vendor('ZmopSdk.ZmopSdk');
$client = new \ZmopClient($gatewayUrl,$appId,$charset,$privateKeyFile,$zmPublicKeyFile);
$request = new \ZhimaAuthInfoAuthorizeRequest();
$request->setChannel("apppc");
$request->setPlatform("zmop");
$request->setIdentityType("2");// 必要参数
$request->setIdentityParam($injson);// 必要参数
$request->setBizParams($bjson);//
$url = $client->generatePageRedirectInvokeUrl($request);
将这个url在浏览器打开,然后认证完成会返回给你用户的openid到回调地址
芝麻分获取
public function zmf_result(){
$gatewayUrl = 'https://zmopenapi.zmxy.com.cn/openapi.do';
$appId = 'appid';
$charset = 'UTF-8';
$privateKeyFile = C('PRIVATE_KEY'); //商户私钥
$zmPublicKeyFile = C('PUBLIC_KEY'); //芝麻公钥
$params = $_GET['params'];
$sign = $_GET['sign'];
vendor('ZmopSdk.ZmopSdk');
// 判断串中是否有%,有则需要decode
$params = str_replace(" ","+",$params);
$params = strstr ( $params, '%' ) ? urldecode ( $params ) : $params;
$sign = str_replace(" ","+",$sign);
$sign = strstr ( $sign, '%' ) ? urldecode ( $sign ) : $sign;
$client = new \ZmopClient ( $gatewayUrl, $appId, $charset, $privateKeyFile,$zmPublicKeyFile );
$result = $client->decryptAndVerifySign ( $params, $sign );
if($result){
$url = U('App/Apply/r_zmf');
$url .= '&'.$result;
$urlarr=parse_url($url);
parse_str($urlarr['query'],$parr);
if($parr['success'] == true){
$openid = $parr['open_id'];
$userinfo = $this->getUserinfoByToken($parr['state']);
//认证成功修改认证状态,并加入芝麻分
$client = new \ZmopClient($gatewayUrl,$appId,$charset,$privateKeyFile,$zmPublicKeyFile);
$request = new \ZhimaCreditScoreGetRequest();
$request->setChannel("apppc");
$request->setPlatform("zmop");
$request->setTransactionId($this->getTransactionId());// 必要参数 随机码
$request->setProductCode("w1010100100000000001");// 必要参数
$request->setOpenId($openid);// 必要参数openid
$response = $client->execute($request);
if($response->success == '1'){
$zmf = $response->zm_score;
}
}
}
}