工行二维码支付接口及回调接口

  1. 工行的接口文档详见:工行开放平台地址
  2. 生成二维码
/**
     * 获取支付二维码
     * 
     * @param outTradeNo
     * @param orderAmt
     * @param attach
     * @param notifyUrl
     * @return
     */
    public static JSONObject getPayQrCode(String outTradeNo, String orderAmt, String attach, String notifyUrl) {
        Date date = new Date();
        JSONObject json = new JSONObject();
        try {
            // 签名类型为RSA2时,需传入appid,私钥和网关公钥,签名类型使用定值IcbcConstants.SIGN_TYPE_RSA2,其他参数使用缺省值
            DefaultIcbcClient client = new DefaultIcbcClient(IcbcConfig.APP_ID, IcbcConstants.SIGN_TYPE_RSA2, IcbcConfig.MY_PRIVATE_KEY, IcbcConfig.APIGW_PUBLIC_KEY);
            QrcodeGenerateRequestV2 request = new QrcodeGenerateRequestV2();
            request.setServiceUrl(IcbcConfig.SERVICE_URL);
            QrcodeGenerateRequestV2Biz bizContent = new QrcodeGenerateRequestV2Biz();
            // 商户线下档案编号(特约商户12位,特约部门15位)
            bizContent.setMerId(IcbcConfig.MER_ID);
            // e生活档案编号
            bizContent.setStoreCode(IcbcConfig.STORE_CODE);
            // 商户系统订单号
            bizContent.setOutTradeNo(outTradeNo);
            // 订单总金额,单位:分
            bizContent.setOrderAmt(orderAmt);
            // 商户订单生成日期,格式:yyyyMMdd
            bizContent.setTradeDate(DateFormatUtils.toIntYMD(date));
            // 商户订单生成时间,格式:HHmmss
            bizContent.setTradeTime(DateFormatUtils.toIntHMS(date));
            // 商户附加数据,最多21个汉字字符,原样返回
            bizContent.setAttach(attach);
            // 二维码有效期,单位:秒,必须小于24小时
            bizContent.setPayExpire(IcbcConfig.PAY_EXPIRE);
            // 商户接收支付成功通知消息URL,当notify_flag为1时必输
            bizContent.setNotifyUrl(notifyUrl);
            // 商户订单生成的机器IP
            bizContent.setTporderCreateIp(InetAddress.getLocalHost().getHostAddress());
            // 扫码后是否需要跳转分行,0-否;1-是;非1按0处理
            bizContent.setSpFlag(IcbcConfig.SP_FLAG);
            // 商户是否开启通知接口,0-否;1-是;非1按0处理
            bizContent.setNotifyFlag(IcbcConfig.NOTIFY_FLAG);
            request.setBizContent(bizContent);
            QrcodeGenerateResponseV2 response = new QrcodeGenerateResponseV2();
            try {
                response = client.execute(request, System.currentTimeMillis() + RandomStringUtils.random(5, Num62.N10_CHARS));
                if (response.isSuccess()) {
                    json.put("success", true);
                    json.put("qrCode", response.getQrcode());
                } else {
                    json.put("success", false);
                    json.put("msg", response.getReturnMsg());
                }
            } catch (IcbcApiException e) {
                json.put("success", false);
                json.put("msg", e.getMessage());
            }
        } catch (Exception e) {
            json.put("success", false);
            json.put("msg", e.getMessage());
        }
        return json;
    }
  1. 二维码回调接口
/**
     * 期刊订阅支付回调
     * 
     * @param request
     * @param response
     */
    @RequestMapping(value = "/subscribeNotifyNotice.jspx")
    public void subscribeNotifyNotice(HttpServletRequest request, HttpServletResponse response) throws Exception {
        log.info("支付成功, 进入通知接口...");
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> 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] + ",";
            }
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        String sign = (String)params.get("sign");
        params.remove("sign");
        String path = "/jkcms/subscribeNotifyNotice.jspx";
        String content = WebUtils.buildOrderedSignStr(path, params);
        boolean signVerified = IcbcSignature.verify(content, IcbcConfig.SIGN_TYPE_RSA, IcbcConfig.APIGW_PUBLIC_KEY, IcbcConfig.CHARSET_UTF8, sign);
        if (signVerified) {
            JSONObject json = JSONObject.fromObject(params.get("biz_content"));
            if (json != null) {
                if (json.getInt("return_code") == 0) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
                    JournalSubscribe journalSubscribe = journalSubscribeMng.findBySubscribeId(json.getString("out_trade_no"));
                    if (journalSubscribe != null) {
                        journalSubscribe.setState(1);
                        journalSubscribe.setMerchantOrderNo(json.getString("order_id"));
                        journalSubscribe.setPayTime(simpleDateFormat.parse(json.getString("pay_time")));
                        journalSubscribeMng.update(journalSubscribe);
                    } else {
                        log.info("订单不存在...");
                    }
                } else {
                    log.info("支付失败,error={}", json.getString("return_msg"));
                }
            } else {
                log.info("biz_content转换json失败...");
            }
        } else {
            log.info("支付验签失败...");
        }
    }

你可能感兴趣的:(工行二维码支付接口及回调接口)