java调用支付宝支付接口_个人网站怎么接入支付宝接口[通俗易懂]

java调用支付宝支付接口_个人网站怎么接入支付宝接口[通俗易懂]支付宝官方文档地址:https://opendocs.alipay.com/apis/api_2/alipay.user.certify.open.initializehttps://m

java调用支付宝支付接口_个人网站怎么接入支付宝接口[通俗易懂]"

支付宝官方文档地址:https://opendocs.alipay.com/apis/api_2/alipay.user.certify.open.initialize

<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
       <groupId>com.alipay.sdk</groupId>
       <artifactId>alipay-sdk-java</artifactId>
       <version>4.10.0.ALL</version>
</dependency>

初始化服务

    private static String PRIVATEKEY = "***";
    private static String PUBLICKEY = "***";
    private static String APPID = "***";
    private static String SERVICEURL = "https://openapi.alipay.com/gateway.do";

    /**
     * 第一步 组装参数 获取certifyId
     *
     * @throws AlipayApiException
     */
    public void init() throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(SERVICEURL, APPID, PRIVATEKEY, "json", "UTF-8", PUBLICKEY, "RSA2");
        AlipayUserCertifyOpenInitializeRequest request = new AlipayUserCertifyOpenInitializeRequest();
        //构造身份信息json对象,身份类型,证件类型,真实姓名,证件号码,详细取值参考文档
        JSONObject identityObj = new JSONObject();
        identityObj.put("identity_type", "CERT_INFO");
        identityObj.put("cert_type", "IDENTITY_CARD");
        identityObj.put("cert_name", "姓名");
        identityObj.put("cert_no", "123456");
        //构造商户配置json对象
        JSONObject merchantConfigObj = new JSONObject();
        // 设置回调地址
        merchantConfigObj.put("return_url", "/回调地址");
        //构造身份认证初始化服务业务参数数据
        JSONObject bizContent = new JSONObject();\
        //商户请求的唯一标识,商户要保证其唯一性,值为32位长度的字母数字组合;建议:前面几位字符是商户自定义的简称,中间可以使用一段时间,后段可以使用一个随机或递增序列
        bizContent.put("outer_order_no", "ZGYD201809132323000001234");
        bizContent.put("biz_code", "FACE");
        bizContent.put("identity_param", identityObj);
        bizContent.put("merchant_config", merchantConfigObj);
        request.setBizContent(bizContent.toJSONString());
        System.out.println(bizContent.toJSONString());
        AlipayUserCertifyOpenInitializeResponse response = alipayClient.execute(request);
        System.out.println(request.toString());
        if (response.isSuccess()) {
            String certifyId = response.getCertifyId();
            System.out.println(certifyId);
        } else {
            System.out.println("获取certifyId失败");
        }
    }

部分值需要从文档中选取:身份认证初始化服务
根据初始化得到的certifyId获取url;

    /**
     * 根据certifyId获取url
     *
     * @param certifyId
     * @return
     * @throws AlipayApiException
     */
    public String getUrl(String certifyId) throws AlipayApiException {
        String url = "";
        AlipayClient alipayClient = new DefaultAlipayClient(SERVICEURL, APPID, PRIVATEKEY, "json", "UTF-8", PUBLICKEY, "RSA2");
        AlipayUserCertifyOpenCertifyRequest request = new AlipayUserCertifyOpenCertifyRequest();
        JSONObject bizContentObj = new JSONObject();
        bizContentObj.put("certify_id", certifyId);
        request.setBizContent(bizContentObj.toJSONString());
        AlipayUserCertifyOpenCertifyResponse response = alipayClient.pageExecute(request, "GET");
        if (response.isSuccess()) {
            url = response.getBody();
            System.out.println(url);
        }
        return url;
    }

进行认证(前端进行)

身份认证记录查询

    /**
     * 查询身份验证结果
     * @param certify_id
     * @throws AlipayApiException
     */
    public void query(String certifyId) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(SERVICEURL, APPID, PRIVATEKEY, "json", "UTF-8", PUBLICKEY, "RSA2");
        AlipayUserCertifyOpenQueryRequest request = new AlipayUserCertifyOpenQueryRequest();
        JSONObject bizContentObj = new JSONObject();
        bizContentObj.put("certify_id", certifyId);
        request.setBizContent(bizContentObj.toJSONString());
        AlipayUserCertifyOpenQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            JSONObject body = JSONObject.parseObject(response.getBody());
            System.out.println(body.toString());
            String bodyString = body.getString("alipay_user_certify_open_query_response");
            JSONObject jsonObject = JSONObject.parseObject(bodyString);
            String passed = jsonObject.getString("passed");
            System.out.println(passed);
        } else {
            System.out.println("调用失败");
        }
    }

接口返回值中的body:{“alipay_user_certify_open_query_response”:{“msg”:”Success”,”code”:”10000″,”material_info”:”{}”,”passed”:”F”},”sign”:”******”}

通过response.getPassed()拿到的是空,可能jar包版本问题(没试其他版本);

不足之处,请多指教。

今天的文章java调用支付宝支付接口_个人网站怎么接入支付宝接口[通俗易懂]分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/87306.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注