支付宝小程序身份认证(拉取人脸识别 认证功能 +详细案例)

交互流程
支付宝身份核验的交互流程如下图所示:
支付宝小程序身份认证(拉取人脸识别 认证功能 +详细案例)_第1张图片
接入指引
第一步:创建小程序
第二步:添加功能
第三步:签约功能
第四步:调用接口

其中前三步看 开发文档:https://opendocs.alipay.com/mini/introduce/alipay-verfify
接口代码:

@GetMapping("/shenfenrz")
    @ResponseBody
    public HashMap<String, String> shenfenrz(String sfzh, String xm, String appid) throws AlipayApiException {

        HashMap<String, String> map = null;
        map = new HashMap<>();
        int i = sfrz.getapp_id(appid,sfzh,xm);
        System.err.println(i);
        if (i == 0) {
            AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
            AlipayUserCertifyOpenInitializeRequest request = new AlipayUserCertifyOpenInitializeRequest();
//          new AlipayUserCertifyInfoApplyRequest();
            //构造身份信息json对象
            JSONObject identityObj = new JSONObject();
            //身份类型,必填,详细取值范围请参考接口文档说明
            identityObj.put("identity_type", "CERT_INFO");
            //证件类型,必填,详细取值范围请参考接口文档说明
            identityObj.put("cert_type", "IDENTITY_CARD");
            //真实姓名,必填

            String regex = "\\d{15}(\\d{2}[0-9xX])?";
            String id = sfzh;

            if (id.matches(regex)) {
                //证件号码,必填
                identityObj.put("cert_no", sfzh);
            } else {
                map.put("message", "请认真核对身份证证件号码信息");
                map.put("success", "false");
            }
            identityObj.put("cert_name", xm);
            //证件号码,必填
//        identityObj.put("cert_no", sfzh);

            //构造商户配置json对象
            JSONObject merchantConfigObj = new JSONObject();
            // 设置回调地址,必填. 如果需要直接在支付宝APP里面打开回调地址使用alipay协议,参考下面的案例:appId用固定值 20000067,url替换为urlEncode后的业务回跳地址
            // alipays://platformapi/startapp?appId=20000067&url=https%3A%2F%2Fapp.cqkqinfo.com%2Fcertify%2FzmxyBackNew.do
            merchantConfigObj.put("return_url", "alipays://platformapi/startapp?appId=20000067&url=https%3A%2F%2Fapp.cqkqinfo.com%2Fcertify%2FzmxyBackNew.do");

            //构造身份认证初始化服务业务参数数据
            JSONObject bizContentObj = new JSONObject();
            //商户请求的唯一标识,推荐为uuid,必填
            String uuid = UUID.randomUUID().toString();
            bizContentObj.put("outer_order_no", uuid);
            bizContentObj.put("biz_code", "FACE");
            bizContentObj.put("identity_param", identityObj);
            bizContentObj.put("merchant_config", merchantConfigObj);
            request.setBizContent(bizContentObj.toString());

            //发起请求
            AlipayUserCertifyOpenInitializeResponse response = alipayClient.execute(request);
            String url = "";
            if (response.isSuccess()) {
                System.out.println("调用成功");
                //接口调用成功,从返回对象中获取certify_id
                String certifyId = response.getCertifyId();
                url = url(certifyId);
                //执行后续流程...
                map.put("certifyId", certifyId);
                map.put("url", url);
                if (certifyId != null && url != null) {
                    map.put("success", "2");

                } else {
                    map.put("success", "3");
                }
            } else {
                System.out.println("调用失败");
            }
            return map;
        } else {
            map.put("success", "1");
            NDShenPi ndShenPi = sfrz.getselect(sfzh, xm);
            System.err.println("ndShenPi:" + ndShenPi);
            if (ndShenPi != null) {
                map.put("id", String.valueOf(ndShenPi.getId()));
                map.put("BillNo", ndShenPi.getBillNo());
                map.put("OID", String.valueOf(ndShenPi.getOID()));
                map.put("ShiXiangMingCheng", ndShenPi.getShiXiangMingCheng());
                map.put("ShiXiangLeiXing", ndShenPi.getShiXiangLeiXing());
                map.put("ShouLiShiXian", ndShenPi.getShouLiShiXian());
                map.put("ShiXiangBianMa", ndShenPi.getShiXiangBianMa());
                map.put("ShouLiBuMen", ndShenPi.getShouLiBuMen());
                map.put("ShouLiChuangKou", ndShenPi.getShouLiChuangKou());
                map.put("ShouLiShiJian", ndShenPi.getShouLiShiJian());
                map.put("ShenQingRen", ndShenPi.getShenQingRen());
                map.put("ShenQingRenEN", ndShenPi.getShenQingRenEN());
                map.put("ZhengJianType", ndShenPi.getZhengJianType());
                map.put("ZhengJianHao", ndShenPi.getZhengJianHao());
                map.put("Img", ndShenPi.getImg());
                map.put("ShengRi", ndShenPi.getShengRi());
                map.put("XingBie", ndShenPi.getXingBie());
                map.put("MinZu", ndShenPi.getMinZu());
                map.put("GuoJia", ndShenPi.getGuoJia());
                map.put("HuKouSuoZaiDi", ndShenPi.getHuKouSuoZaiDi());
                map.put("XueLi", ndShenPi.getXueLi());
                map.put("XueLiQuanRiZhi", ndShenPi.getXueLiQuanRiZhi());
                map.put("QuanRiZhiZhuanYe", ndShenPi.getQuanRiZhiZhuanYe());
                map.put("HaiWai", ndShenPi.getHaiWai());
                map.put("XueLiZaiZhi", ndShenPi.getXueLiZaiZhi());
                map.put("ZaiZhiZhuanYe", ndShenPi.getZaiZhiZhuanYe());
                map.put("RenCaiLeiBie", ndShenPi.getRenCaiLeiBie());
                map.put("RenCaiChengHao", ndShenPi.getRenCaiChengHao());
                map.put("BiYeZhengShuBH", ndShenPi.getBiYeZhengShuBH());
                map.put("YJTime", ndShenPi.getYJTime());
                map.put("YouXiang", ndShenPi.getYouXiang());
                map.put("DianHua", ndShenPi.getDianHua());
                map.put("ShouJi", ndShenPi.getShouJi());
                map.put("YuanDanWei", ndShenPi.getYuanDanWei());
                map.put("YuanZhiCheng", ndShenPi.getYuanZhiCheng());
                map.put("YinJinLeiXing", ndShenPi.getYinJinLeiXing());
                map.put("QianYueQiXian", ndShenPi.getQianYueQiXian());
                map.put("ZhiCheng", ndShenPi.getZhiCheng());
                map.put("DanWei", ndShenPi.getDanWei());
                map.put("DiZhiQu", ndShenPi.getDiZhiQu());
                map.put("DiZhi", ndShenPi.getDiZhi());
                map.put("HangYe", ndShenPi.getHangYe());
                map.put("XingZhi", ndShenPi.getXingZhi());
                map.put("ShengZhi", ndShenPi.getShengZhi());
                map.put("FaRen", ndShenPi.getFaRen());
                map.put("LianXiRen", ndShenPi.getLianXiRen());
                map.put("LianXiDianHua", ndShenPi.getLianXiDianHua());
                map.put("GZShiJian", ndShenPi.getGZShiJian());
                map.put("GZGongZuo", ndShenPi.getGZGongZuo());
                map.put("GZZhiWu", ndShenPi.getGZZhiWu());
                map.put("JLMingCheng", ndShenPi.getJLMingCheng());
                map.put("JLShiJian", ndShenPi.getJLShiJian());
                map.put("JLBuMen", ndShenPi.getJLBuMen());
                map.put("QXingMing", ndShenPi.getQXingMing());
                map.put("QZhengHao", ndShenPi.getQZhengHao());
                map.put("QXingBie", ndShenPi.getQXingBie());
                map.put("QGuanXi", ndShenPi.getQGuanXi());
                map.put("QMingZu", ndShenPi.getQMingZu());
                map.put("QShengRi", ndShenPi.getQShengRi());
                map.put("QZhiWu", ndShenPi.getQZhiWu());
                map.put("QHuKouDiZhi", ndShenPi.getQHuKouDiZhi());
                map.put("QHuKou", ndShenPi.getQHuKou());
                map.put("YuanHuKou", ndShenPi.getYuanHuKou());
                map.put("YuanHuKouDiZhi", ndShenPi.getYuanHuKouDiZhi());
                map.put("HuKou", ndShenPi.getHuKou());
                map.put("HuKouDiZhi", ndShenPi.getHuKouDiZhi());
                map.put("GuestNo", ndShenPi.getGuestNo());
                map.put("GuestName", ndShenPi.getGuestName());
                map.put("BeiZhu", ndShenPi.getBeiZhu());
                map.put("ShouLiTiaoJian", ndShenPi.getShouLiTiaoJian());
                map.put("ShenQingShiJian", ndShenPi.getShenQingShiJian());
                map.put("Type", ndShenPi.getType());
                map.put("FromTo", ndShenPi.getFromTo());
                map.put("YuShenTxt", ndShenPi.getYuShenTxt());
                map.put("YuShenTime", ndShenPi.getYuShenTime());
                map.put("YuShenRen", ndShenPi.getYuShenRen());
                map.put("YuShenAccount", ndShenPi.getYuShenAccount());
                map.put("PJQStar", ndShenPi.getPJQStar());
                map.put("SmsNote", ndShenPi.getSmsNote());
                map.put("PJQMsg", ndShenPi.getPJQMsg());
                map.put("CardNo", ndShenPi.getCardNo());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if (ndShenPi.getCardDate() == null) {
                    map.put("CardDate", null);
                } else {
                    map.put("CardDate", sdf.format(ndShenPi.getCardDate()));
                }
                if (ndShenPi.getCardDateDQ() == null) {
                    map.put("CardDateDQ", null);

                } else {
                    map.put("CardDateDQ", sdf.format(ndShenPi.getCardDateDQ()));

                }

                if (ndShenPi.getCardFiveDQTime() == null) {
                    map.put("CardFiveDQTime", null);

                } else {
                    map.put("CardFiveDQTime", sdf.format(ndShenPi.getCardFiveDQTime()));

                }
                map.put("CardState", String.valueOf(ndShenPi.getCardState()));
                map.put("CardType", String.valueOf(ndShenPi.getCardType()));


                if (ndShenPi.getCardZKDate() == null) {
                    map.put("CardZKDate", null);

                } else {
                    map.put("CardZKDate", sdf.format(ndShenPi.getCardZKDate()));

                }


                map.put("CardSerial", ndShenPi.getCardSerial());
                map.put("RenCaiFlg", String.valueOf(ndShenPi.getRenCaiFlg()));
                map.put("RenCaiFlgContent", ndShenPi.getRenCaiFlgContent());
                map.put("CardTypeS", ndShenPi.getCardTypeS());
                if (ndShenPi.getRenCaiZXTime() == null) {
                    map.put("RenCaiZXTime", null);
                } else {
                    map.put("RenCaiZXTime", sdf.format(ndShenPi.getRenCaiZXTime()));
                }
                if (ndShenPi.getFuShenTime() == null) {
                    map.put("FuShenTime", null);
                } else {
                    map.put("FuShenTime", sdf.format(ndShenPi.getFuShenTime()));

                }

                if (ndShenPi.getAddTime() == null) {
                    map.put("AddTime", null);

                } else {
                    map.put("AddTime", sdf.format(ndShenPi.getAddTime()));
                }
                map.put("cardcode", ndShenPi.getCardcode());
                map.put("zzmm", ndShenPi.getZzmm());
                map.put("FuShen", ndShenPi.getFuShen());

            } else {
                map.put("success", "4");
            }
            return map;
        }

    }

响应结果:

{
    "alipay_user_certify_open_initialize_response": {
        "code": "10000",
        "msg": "Success",
        "certify_id": "2109b5e671aa3ff2eb4851816c65828f"
    },
    "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}

会生成一个 certify_id,根据这个id去拉取身份认证页面

public String url(String certifyId) throws AlipayApiException {
        //参考代码如下:
        //获取alipay client
        AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayUserCertifyOpenCertifyRequest request = new AlipayUserCertifyOpenCertifyRequest();

        //设置certifyId
        JSONObject bizContentObj = new JSONObject();
        bizContentObj.put("certify_id", certifyId);
        request.setBizContent(bizContentObj.toString());

        //生成请求链接,这里一定要使用GET模式
        AlipayUserCertifyOpenCertifyResponse response = alipayClient.pageExecute(request, "GET");

        String certifyUrl = "";
        if (response.isSuccess()) {
            System.out.println("开始认证服务调用成功");
            certifyUrl = response.getBody();

            System.err.println("certifyUrl:" + certifyUrl);
            //执行后续流程...


//
        } else {
            System.out.println("调用失败");
        }
        return certifyUrl;
    }

响应结果:

https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2015111100758155&biz_content=%7B%22certify_id%22%3A%22ZM201611253000000121200404215172%22%7D&charset=GBK&format=json&method=alipay.user.certify.open.certify&sign=MhtfosO8AKbwctDgfGitzLvhbcvi%2FMv3iBES7fRnIXn%2BHcdwq9UWltTs6mEvjk2UoHdLoFrvcSJipiE3sL8kdJMd51t87vcwPCfk7BA5KPwa4%2B1IYzYaK6WwbqOoQB%2FqiJVfni602HiE%2BZAomW7WA3Tjhjy3D%2B9xrLFCipiroDQ%3D&sign_type=RSA2&timestamp=2016-11-25+15%3A00%3A59&version=1.0&sign=MhtfosO8AKbwctDgfGitzLvhbcvi%2FMv3iBES7fRnIXn%2BHcdwq9UWltTs6mEvjk2UoHdLoFrvcSJipiE3sL8kdJMd51t87vcwPCfk7BA5KPwa4%2B1IYzYaK6WwbqOoQB%2FqiJVfni602HiE%2BZAomW7WA3Tjhjy3D%2B9xrLFCipiroDQ%3D

然后去那这个地址去拉取身份验证页面:

要使用小程序接入身份验证功能,唤起身份验证示例代码如下:

 /**
  * 唤起认证流程
  * 参数: certifyId、url 需要通过支付宝 openapi 开放平台网关接口获取
  * 详细说明可查看文档下方的参数说明
 **/

my.startAPVerify({
    
url: 'https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-4.9.5.ALL&app_id=2019111569179437&biz_content=%7B%22certify_id%22%3A%22a74bXXXXXXb0e4d42b9283bcfXXXXc1%22%7D&charset=UTF-8&format=json&method=alipay.user.certify.open.certify&sign=GPKGQpWQtkGWn98N%2F7HS8LOXli4dw1MH5LdK%2BZmR4k1j1PPvRubHZIq6jj5EQPi9CYtEYVGCUllb1pOGjgymWOGempjCiE9jx4CfyOD2SZ1JdTIw75dvkLQIlotd7Tli0BiaqCkpfv7b36bh2ORqFDGt8Zc50RI8AWqi8EyHDoMtq2ENGk8LXRcBNeeKUL0Fo9htOzgM3JXd5cqg8uPBhBawBAW7%2Bt5yT7dvogZYuMCAfaanUiPnjcWkxyobINxb4dusKybxRBHt0CXnxrLZi97Bdh6PnCqDZ36gYbw99P0TqG%2BJaUFWqLnbmu21OarI1m9aO6k1sFzb6g%3D%3D&sign_type=RSA2×tamp=2020-05-28+18%3A12%3A04&version=1.0',
certifyId: "your_certifyId",
success: function (res) {
  console.log('success', res)
},
fail: function (res) {
  console.log('fail', res)
},
complete: function (res) {
  console.log('complete', res)
}
})

基本完成以上功能,不清楚的请留言咨询谢谢!!!

你可能感兴趣的:(支付宝小程序)