微信支付和连连支付



/**
* 微信充值
* @param session
* @param request
* @param money
* @return
*/
@RequestMapping(value = "/wechatpay")
@ResponseBody
public String wechatpay(HttpSession session, HttpServletRequest request,
@RequestParam(value = "money", required = false) BigDecimal money) {
// 账号信息
        String appid = PropertiesHandler.getConfigValue("APP_ID").toString(); // appId
        //String appsecret = WeChatParam.APP_SECRET; // appSecret
        String mch_id = PropertiesHandler.getConfigValue("MCH_ID").toString(); // 商业号
        String key = PropertiesHandler.getConfigValue("API_KEY").toString(); // key
        String currTime = PayCommonUtil.getCurrTime(); // 获取当前时间格式为24位
        String strTime = currTime.substring(8, currTime.length()); // 截取24位的第8位然后加上长度
        String strRandom = PayCommonUtil.buildRandom(4) + ""; // 产生随机数
        String nonce_str = strTime + strRandom;
        String order_price = money.multiply(new BigDecimal(100)).toString(); // 价格(注意:价格的单位是分)
        String body = "微信充值"; // 商品名称
        String fsNO = GenerateKeyNO.generate("FS");
        String out_trade_no = fsNO; // 订单号
        String spbill_create_ip = LLPayUtil.getIpAddr(request).replace("_", "."); // 获取发起电脑ip
        // 回调接口 
        String notify_url = PropertiesHandler.getConfigValue("NOTIFY_WCURL").toString(); // 回掉地址
        String trade_type = "NATIVE";
        SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
        packageParams.put("appid", appid);
        packageParams.put("mch_id", mch_id);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("body", body);
        packageParams.put("out_trade_no", out_trade_no);
        packageParams.put("total_fee", "1");
        packageParams.put("spbill_create_ip", "172.16.9.2");
        packageParams.put("notify_url", notify_url);
        packageParams.put("trade_type", trade_type);
        //加密
        String sign = PayCommonUtil.createSign("UTF-8", packageParams,key);
        packageParams.put("sign", sign);
        //解析
        String requestXML = PayCommonUtil.getRequestXml(packageParams);
        System.out.println(requestXML);
        //取值
        String resXml = HttpUtil.postData(PropertiesHandler.getConfigValue("UFDODER_URL").toString(), requestXML);
        String urlCode=null;
        try {
            Map map = XMLUtil.doXMLParse(resXml);
            urlCode = (String) map.get("code_url");
        if(urlCode != null){
Map<String, Object> charge = new HashMap<String, Object>();
        //获取当前登录用户的信息
        UserInfoVO userInfo = (UserInfoVO) session.getAttribute(SessionConstants.LOGIN_SESSION_ID);
                //存储充值信息
                charge.put("fsNO", out_trade_no);
                charge.put("userId", userInfo.getUserId());
                charge.put("bankCardNO", null);
                charge.put("payMoney", null);
                charge.put("payTime", sdfDate.format(new Date()));
                charge.put("llpayNO", "2"); //连连支付为多数,2为微信支付 3为支付宝
                charge.put("ret_code", null);
                charge.put("status", 0);
                charge.put("remark", null);
                charge.put("realName", userInfo.getRealName());
                charge.put("costs", 0);
                charge.put("bankCard", null);
                fundService.addDepositRecord(charge);
        }
            System.out.println("***********扫码链接"+urlCode);
} catch (Exception e) {
e.printStackTrace();
}
return urlCode;
}

/**
* 微信充值回调
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/wechat_notify")
public String wechat_notify(HttpServletRequest request,HttpServletResponse response) throws Exception{
//读取参数
InputStream inputStream ;
StringBuffer sb = new StringBuffer();
inputStream = request.getInputStream();
String s ;
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((s = in.readLine()) != null){
sb.append(s);
}
in.close();
inputStream.close();

//解析xml成map
Map<String, String> m = new HashMap<String, String>();
m = XMLUtil.doXMLParse(sb.toString());

//过滤空 设置 TreeMap
SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
Iterator it = m.keySet().iterator();
while (it.hasNext()) {
String parameter = (String) it.next();
String parameterValue = m.get(parameter);

String v = "";
if(null != parameterValue) {
v = parameterValue.trim();
}
packageParams.put(parameter, v);
}

// 账号信息
        String key = PropertiesHandler.getConfigValue("API_KEY").toString(); // key
        
        logger.info(packageParams);
   //判断签名是否正确
   if(PayCommonUtil.isTenpaySign("UTF-8", packageParams,key)) {
       //------------------------------
       //处理业务开始
       //------------------------------
       String resXml = "";
       if("SUCCESS".equals((String)packageParams.get("result_code"))){
        // 这里是支付成功
           //执行自己的业务逻辑
        String mch_id = (String)packageParams.get("mch_id"); //返回商户号
        String openid = (String)packageParams.get("openid"); //用户标识
        String is_subscribe = (String)packageParams.get("is_subscribe"); //是否关注公众号
        String out_trade_no = (String)packageParams.get("out_trade_no");
        String total_fee = (String)packageParams.get("total_fee"); //返回金额
       
        logger.info("mch_id:"+mch_id);
        logger.info("openid:"+openid);
        logger.info("is_subscribe:"+is_subscribe);
        logger.info("out_trade_no:"+out_trade_no);
        logger.info("total_fee:"+total_fee);
        //分转换成元
        BigDecimal money =  new BigDecimal(total_fee).divide(new BigDecimal(100));
           //执行自己的业务逻辑
Map<String, Object> map = new HashMap<String, Object>();
map.put("fsNO", out_trade_no);
DepositRecordVO userInfo = fundService.queryDepositByFsNO(map);
       fundService.updateChargeInfoByWechatPay(userInfo, money);
       
        logger.info("支付成功");
           //通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.
           resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
                   + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
           
       } else {
        logger.info("支付失败,错误信息:" + packageParams.get("err_code"));
           resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
                   + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
       }
       //------------------------------
       //处理业务完毕
       //------------------------------
       BufferedOutputStream out = new BufferedOutputStream(
       response.getOutputStream());
       out.write(resXml.getBytes());
       out.flush();
       out.close();
   }else{
    logger.info("通知签名验证失败");
   }
   return "fund/notify_url";

}

连连支付:

public Map<String, Object> llpay(
@RequestParam(value = "money", required = false) String money, 
HttpSession session) {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> charge = new HashMap<String, Object>();
Map<String, Object> model = new HashMap<String, Object>();
// 获取当前登录用户的信息,真是姓名,手机号,身份证,已经认证过的
UserInfoVO userInfo = (UserInfoVO) session.getAttribute(SessionConstants.LOGIN_SESSION_ID);
map.put("userId", userInfo.getUserId());
// 获取银行信息
BankCardInfoVO bank = fundService.queryUserBankCard(map);
    //连连需要的参数
    PaymentInfo paymentInfo = new PaymentInfo();
        //银行账号姓名
paymentInfo.setAcct_name(bank.getRealName());
        //银行卡号
        paymentInfo.setCard_no(bank.getBankCardNO());
//商户业务类型(虚拟商品销售:101001 实物商品销售:109001) 
paymentInfo.setBusi_partner(PropertiesHandler.getConfigValue("BUSI_PARTNER").toString());
        //订单时间YYYYMMDDH24MISS
paymentInfo.setDt_order(LLPayUtil.getCurrentDateTimeStr());
        //商戶从自己系统中获取用户身份信息(认证支付必须将用户身份信息传输给连连,且修改标记flag_modify设置成1:不可修改)
        //不可修改
paymentInfo.setFlag_modify("1");
        //身份证号
paymentInfo.setId_no(bank.getIdCardNO());
        //0默认,身份证֤
paymentInfo.setId_type("0");
        //订单备注信息
        paymentInfo.setInfo_order("用户(" + bank.getRealName() + ")充值" + money + "元");
        //交易金额
paymentInfo.setMoney_order(money);
        //商品名称
        paymentInfo.setName_goods("充值");
        //订单号
        String fs = GenerateKeyNO.generate("FS");
paymentInfo.setNo_order(fs);
        //存储充值信息
        charge.put("fsNO", fs);
        charge.put("userId", userInfo.getUserId());
        charge.put("bankCardNO", null);
        charge.put("payMoney", null);
        charge.put("payTime", sdfDate.format(new Date()));
        charge.put("llpayNO", null);
        charge.put("ret_code", null);
        charge.put("status", 0);
        charge.put("remark", null);
        charge.put("realName", userInfo.getRealName());
        charge.put("costs", 0);
        fundService.addDepositRecord(charge);
        //服务器异步通知地址
paymentInfo.setNotify_url(PropertiesHandler.getConfigValue("NOTIFY_URL").toString());
        //支付交易商户编号
paymentInfo.setOid_partner(PropertiesHandler.getConfigValue("OID_PARTNER2").toString());
        //风控参数
ToPayServlet item = new ToPayServlet();
paymentInfo.setRisk_item(item.createRiskItem(bank.getPhone(), bank.getRealName(), bank.getIdCardNO(), userInfo.getUserId()));
        //签名方式MD5
paymentInfo.setSign_type(PropertiesHandler.getConfigValue("SIGN_TYPE").toString());
//时间戳
paymentInfo.setTimestamp(LLPayUtil.getCurrentDateTimeStr());
        //支付结束回显url
paymentInfo.setUrl_return(PropertiesHandler.getConfigValue("URL_RETURN").toString());
        //商户用户唯一编号
paymentInfo.setUser_id(userInfo.getUserId());
        //订单有效时间
paymentInfo.setValid_order("10080"); //7天
//版本号
paymentInfo.setVersion(PropertiesHandler.getConfigValue("VERSION").toString());
//MD5钥匙
String md5_key = PropertiesHandler.getConfigValue("MD5_KEY2").toString();
//RSA钥匙
String rsa_private = PropertiesHandler.getConfigValue("TRADER_PRI_KEY").toString();
// 进行连连参数传递
PaymentInfo pay = LLPayTools.plainPay(paymentInfo, rsa_private, md5_key);
if(pay != null){
model.put("paymentInfo", pay);
model.put("res", true);
}else{
model.put("res", false);
}
return model;



/**
* notify_url页面
* @return
*/
@RequestMapping(value = "/notify_url", method=RequestMethod.POST)
public String notify_url(Model model, HttpServletRequest req, HttpServletResponse resp) {
try {
Map<String, Object> map = new HashMap<String, Object>();
System.out.println("进入支付同步通知数据接收处理");
    req.setCharacterEncoding("UTF-8");
    resp.setCharacterEncoding("UTF-8"); 
    resp.setHeader("Content-type", "text/html;charset=UTF-8"); 
       RetBean retBean = new RetBean();
       String reqStr = LLPayUtil.readReqStr(req);
       if (LLPayUtil.isnull(reqStr))
       {
           retBean.setRet_code("9999");
           retBean.setRet_msg("交易失败");
           resp.getWriter().write(JSON.toJSONString(retBean));
           resp.getWriter().flush();
                logger.error("交易失败");
           return "交易失败";
       }
       logger.error(reqStr);
       System.out.println("接收支付异步通知数据:【" + reqStr + "】");
      try
       {
           if (!LLPayUtil.checkSign(JSON.parseObject(reqStr), PropertiesHandler.getConfigValue("TRADER_PRI_KEY").toString(),
            PropertiesHandler.getConfigValue("MD5_KEY").toString()))
           {
               retBean.setRet_code("9999");
               retBean.setRet_msg("验签失败");
               resp.getWriter().write(JSON.toJSONString(retBean));
               resp.getWriter().flush();
               System.out.println("支付异步通知验签失败");
               logger.error("支付异步通知验签失败");
               return "支付异步通知验签失败";
           }
       } catch (Exception e)
       {
           System.out.println("异步通知报文解析异常:" + e);
           retBean.setRet_code("9999");
           retBean.setRet_msg("交易失败");
           resp.getWriter().write(JSON.toJSONString(retBean));
           resp.getWriter().flush();
                logger.error("交易失败");
           return "交易失败";
       }
       retBean.setRet_code("0000");
       retBean.setRet_msg("交易成功");
       resp.getWriter().write(JSON.toJSONString(retBean));
       resp.getWriter().flush();
       System.out.println("支付异步通知数据接收处理成功");
       //解析异步通知对象
       PayDataBean payDataBean = JSON.parseObject(reqStr, PayDataBean.class);
map.put("fsNO", payDataBean.getNo_order());
DepositRecordVO userInfo = fundService.queryDepositByFsNO(map);
map.put("userId", userInfo.getUserId());
// 获取银行信息
BankCardInfoVO bank = fundService.queryUserBankCard(map);
       fundService.updateChargeInfo(payDataBean, userInfo, bank);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
return "充值失败";
}
return "fund/notify_url";
}


/**
* url_return页面
* @return
*/
@RequestMapping(value = "/url_return", method=RequestMethod.POST)
public String url_return(Model model, HttpServletRequest req, HttpServletResponse resp) {
try {
Map<String, Object> map = new HashMap<String, Object>();
System.out.println("进入支付同步通知数据接收处理");
    req.setCharacterEncoding("UTF-8");
    resp.setCharacterEncoding("UTF-8"); 
    resp.setHeader("Content-type", "text/html;charset=UTF-8"); 
String no_order = req.getParameter("no_order");
    String oid_paybill = req.getParameter("oid_paybill");
map.put("fsNO", no_order);
DepositRecordVO userInfo = fundService.queryDepositByFsNO(map);
    PayDataBean payDataBean = new PayDataBean();
       payDataBean.setSign(req.getParameter("sign"));
       payDataBean.setSign_type(req.getParameter("sign_type"));
       payDataBean.setOid_partner(req.getParameter("oid_partner"));
       payDataBean.setDt_order(req.getParameter("dt_order"));
       payDataBean.setNo_order(no_order);
       payDataBean.setOid_paybill(oid_paybill);
       payDataBean.setMoney_order(req.getParameter("money_order"));
       payDataBean.setResult_pay(req.getParameter("result_pay"));
       payDataBean.setSettle_date(req.getParameter("settle_date"));
       payDataBean.setInfo_order(req.getParameter("info_order"));
       payDataBean.setPay_type(req.getParameter("pay_type"));
       payDataBean.setBank_code(req.getParameter("bank_code"));
       payDataBean.setUser_id(userInfo.getUserId());
       model.addAttribute("payDataBean", payDataBean);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
return "支付同步通知加载数据错误";
}
return "fund/url_return";

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值