2021-02-03-java-支付宝


支付宝预付款-获取收款码

支付宝官方文档:
https://opendocs.alipay.com/apis/api_1/alipay.trade.precreate
支付宝沙箱环境配置:https://opendocs.alipay.com/apis/01da4m
支付宝异步回调官方文档:https://opendocs.alipay.com/open/270/105902
用到的jar包:

<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>4.11.33.ALL</version>
</dependency>

获取支付宝收款码的代码代码:

public String getQrCode(int reType) {
     
    // gateway支付宝的网关,正式环境为https://openapi.alipay.com/gateway.do, 沙箱环境为https://openapi.alipaydev.com/gateway.do
    // appId为支付宝提供的应用id
    // privateKey支付宝应用密钥
    // alipayPublicKey支付宝公钥,主意和支付宝应用公钥进行区分
    AlipayClient alipayClient = new DefaultAlipayClient(gateway, appId, privateKey, "JSON", CHARSET_UTF8, alipayPublicKey, "RSA2");
    // 发起App支付请求
    AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest ();
    // 回调接口设置,notifyUrl必须是公网可访问到的地址
    request.setNotifyUrl(notifyUrl);
    AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
    // 订单描述
    model.setBody("订单描述");
    // 订单标题
    model.setSubject(dictItem.getItemName());
    // 商户订单号 就是商户后台生成的订单号
    model.setOutTradeNo(String.valueOf(123456));
    // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天 (屁股后面的字母一定要带,不然报错)
    model.setTimeoutExpress("30");
    // 订单总金额 ,默认单位为元,精确到小数点后两位,取值范围[0.01,100000000]
    model.setTotalAmount("99");
    request.setBizModel(model);
    // 通过api的方法请求阿里接口获得反馈
    try {
     
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
     
            JSONObject respJson = JSON.parseObject(response.getBody());
            JSONObject rsj = (JSONObject) respJson.get("alipay_trade_precreate_response");
            // 返回数据,例如:https://qr.alipay.com/bavh4wjlxf12tper3a
            return (String) rsj.get("qr_code");
        } else {
     
            throw new BadArgumentException("获取收款码错误:" + response.toString() );
        }
    } catch (AlipayApiException e) {
     
        throw new BadArgumentException("获取收款码错误", e);
    }
    return "";
}

前端将字符串转成二维码:
html:

<div id="qrcode" ref="qrcode"></div>

vue种引入:

npm install qrcode2 --save

js:

//生成二维码, url是获取的二维码
qrcodeScan (url) {
     
  this.qrcode = new QRCode('qrcode', {
     
    width: 200,  // 二维码宽度
    height: 200, // 二维码高度
    text: url
  })
}

支付宝回调:
验证成功,接口需要返回:success
验证失败,接口需要返回:failure

public String callback(HttpServletRequest request) {
     
    Map<String, String> params = new HashMap<String, String>();
    Map requestParams = request.getParameterMap();
    for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
     
        String name = (String) iter.next();
        String[] values = (String[]) requestParams.get(name);
        String valueStr = "";
        for (int i = 0; i < values.length; i++) {
     
            valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
        }
        params.put(name, valueStr);
    }
    String paramsJson = JSON.toJSONString(params);
    // 交易状态
    String ttadeStatus = params.get("trade_status");
    // 验签
    boolean callbackVerification = false;
    try {
     
        // 支付宝返回的数据如果存在list,要注意数据是否正确,正确的list,例如:[{"amount":"0.01","fundChannel":"PCREDIT"}]
        // 引号的转码应该进行更正,如下种的fund_bill_list
                if(params.get("fund_bill_list") != null) {
     
            params.put("fund_bill_list", params.get("fund_bill_list").replaceAll(""", "\""));
        }
        boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayPublicKey, CHARSET_UTF8, "RSA2");
        if (signVerified) {
     
            // 这里可添加数据验证
            callbackVerification = true;
        }
    } catch (AlipayApiException e) {
     
        throw new BadArgumentException("验签失败", e);
    }

    // 验签成功
    if (callbackVerification ) {
     
        // 可以根据需求在次做自己的业务
        return "success";
    } else {
     
        // 验签失败
        log.info("支付宝回调签名认证失败,signVerified=false, paramsJson:{}", paramsJson);
        return "failure";
    }
}

你可能感兴趣的:(java-支付宝,java)