3 Star 0 Fork 0

Gitee 极速下载 / zmxy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/bmqb/zmxy
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

芝麻信用 NodeJS SDK (非官方)

NPM version Build Status Dependencies Status codecov npm License

接入流程

1. 注册芝麻信用商家服务平台并签约

2. 在芝麻信用商家服务平台创建商家应用并交换公钥

与芝麻信用交换公钥的步骤:

  1. 进入openssl
  2. 运行genrsa -out app_private_key.pem 1024生成一个私钥
  3. 运行rsa -in app_private_key.pem -pubout -out app_public_key.pem生成公钥
  4. 进入芝麻信用后台,将生成的公钥 去除 首行 / 尾行 / 换行符 粘贴进应用的公钥一栏
  5. 应用创建后,点击应用的芝麻公钥一栏,得到芝麻信用的公钥(原始格式)
  6. 将其整理为RSA公钥形式, 即加入首行,尾行,以及每64字符就换行, 并保存为 zmxy_public_key.pem

至此我们一共有三个秘钥文件,分别是:

  • app_private_key.pem 应用私钥
  • zmxy_public_key.pem 芝麻信用公钥
  • app_public_key.pem 应用公钥(仅用于调试)

3. 使用SDK

在任意Node项目中可以通过npm安装

npm install zmxy --save

在保存秘钥的文件夹下创建app.js文件

const fs = require('fs');
const ZmxyClient = require('zmxy').default;

const zmxy = new ZmxyClient({
  appId: '123456',   //芝麻应用App ID
  appPrivateKey: fs.readFileSync(`${__dirname}/app_private_key.pem`),  //App私钥
  zmxyPublicKey: fs.readFileSync(`${__dirname}/zmxy_public_key.pem`)   //芝麻公钥
});
zmxy.verifyIvs({
  name: '张三',
  mobile: '12345678901'
}).then(({ result }) => {
  console.log(result);
}).catch((err) => {
  console.error(err);
});

请求成功后可以看到控制台打印芝麻信用的返回

{ success: true,
  biz_no: 'ZM2016102030000000XXXXXXXXX',
  ivs_detail:
   [ { code: 'PHONE_Match_Recency_Bad',
       description: '电话号码与其他信息匹配,但匹配后的信息近期不活跃' },
     { code: 'NAME_Match_Reliability_Bad',
       description: '姓名与其他信息匹配,但匹配后的信息未经认证' } ],
  ivs_score: 72 }

SDK功能

获得欺诈评分

zmxy.getIvsScore({
  name: '张三',
  cert_no: '532926200804058748'
}).then(({ result }) => {
  console.log(result.score); //output is risk score such as 89
});

反欺诈信息验证

zmxy.verifyIvs({
  name: '张三',
  cert_no: '532926200804058748'
}).then(({ result }) => {
  console.log(result.verify_code); //output is an Array contains risk codes such as ['V_CN_NA', 'V_PH_NA']
});

欺诈关注名单

zmxy.getIvsWatchList({
  name: '张三',
  cert_no: '532926200804058748'
}).then(({ result }) => {
  console.log(result.riskCode); //output is an Array contains risk codes such as ['R_CN_002']
});

查询芝麻评分

由于芝麻评分需要用户授权,因此首先需要生成授权URL

生成授权URL

授权URL可以通过姓名+身份证或者手机号得到, 同时又支持PC网页和H5页面,排列组合共4种情况如下

手机号+PC

const { url } = zmxyClient.getAuthorizeUrl({
  mobile: '12345678901'
});

手机号+H5

const { url } = zmxyClient.getAuthorizeUrl({
  mobile: '12345678901'
}, 'h5');

身份证+PC

const { url } = zmxyClient.getAuthorizeUrl({
  name: '张三',
  certNo: '111111111111111111'
});

身份证+H5

const { url } = zmxyClient.getAuthorizeUrl({
  name: '张三',
  certNo: '111111111111111111'
}, 'h5');

获得OpenId

获得授权URL后, 可以在浏览器中打开此URL,根据提示授权后会被重定向到芝麻应用的回调地址, 地址Query中的params参数会携带OpenId, 可以通过如下方式获得

const { open_id } = zmxyClient.getOpenId(params);

查询芝麻信用分

有了OpenID后就可以查询芝麻信用分

zmxyClient.getCreditScore('openid').then((r) => {
  console.log(r.result.zm_score)
});

查询行业关注名单

同上获得OpenId后可做如下查询

zmxyClient.verifyWatchlist('openid').then((r) => {
  console.log(r.result.is_matched)
});

芝麻认证

流程与芝麻认证文档一致

1. 初始化并生成biz_no

let bizNo = null;
zmxyClient.initCertification('张三', '310105912123123412').then((r) => {
  bizNo = r.result.biz_no; //output is as ZM201703093000000727200705771480
});

2. 通过biz_no生成芝麻认证URL

const url = zmxyClient.getCertificationCertifyUrl(bizNo, 'http://localhost');

3. 设备访问芝麻认证URL,再使用bizNo查询芝麻认证结果

zmxyClient.queryCertification(bizNo).then((r) => {
  console.log(r.result.passed); //output is true or false
});

开启调试

由于SDK使用了request, 所以可以直接在命令行中通过环境变量开启Debug模式

NODE_DEBUG=request node app.js
The MIT License (MIT) Copyright (c) <2016> <AlloVince> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/mirrors/zmxy.git
git@gitee.com:mirrors/zmxy.git
mirrors
zmxy
zmxy
master

搜索帮助