package com.pj.api.jh.service; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import cn.hutool.log.StaticLog; import com.pj.api.jh.bo.JhNotifyBO; import com.pj.api.jh.bo.JhNotifySignBO; import com.pj.api.jh.utils.JhHttpUtils; import com.pj.api.jh.utils.MerchantApiUtil; import com.pj.api.wx.bo.Attach; import com.pj.api.wx.bo.NotifyBO; import com.pj.api.wx.service.WxService; import com.pj.current.config.JhConfig; import com.pj.current.config.MyConfig; import com.pj.current.config.PartConfig; import com.pj.current.config.WxConfig; import com.pj.current.task.TaskService; import com.pj.project.tb_order.TbOrder; import com.pj.project.tb_order.TbOrderService; import com.pj.project4sp.global.BusinessException; import com.pj.utils.sg.AjaxError; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.nio.charset.Charset; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Auther: lzm * @Date: 2022/04/19/17:38 */ @Service @Transactional @Slf4j public class JhService { @Resource MyConfig myConfig; @Resource JhConfig jhConfig; @Resource PartConfig partConfig; @Resource WxService wxService; @Resource private WxConfig wxConfig; @Resource JhHttpUtils jhHttpUtils; @Resource private TbOrderService tbOrderService; @Resource private TaskService taskService; public Map initPay(HttpServletRequest request) throws Exception { if (!jhConfig.isEnable()) { return wxService.prePay(request); } String money = request.getParameter("money"); if (Double.valueOf(money) <= 0) { throw new BusinessException("金额不正确"); } String openid = request.getParameter("openid"); String calculateId = request.getParameter("calculateId"); String desc = handlerDesc(request.getParameter("desc")); String businessId = request.getParameter("b"); String c = request.getParameter("c"); String a = request.getParameter("a"); String i = request.getParameter("i"); String businessType = request.getParameter("businessType"); Attach atchMap = new Attach(); atchMap.setC(c).setB(businessId).setA(a).setI(i); String attchStr = JSONUtil.toJsonStr(atchMap); TbOrder dbOrder = tbOrderService.find30Seconds(attchStr, LocalDateTime.now().minusSeconds(wxConfig.getLimitPaySeconds()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); if (dbOrder != null && !StrUtil.equals(dbOrder.getOpenid(), openid)) { throw new Exception("其他人正在付款...."); } TbOrder successOrder = tbOrderService.findSuccessOrder(attchStr); if (successOrder != null && StrUtil.contains(attchStr, "b")) { throw new Exception("业务订单已支付...."); } String out_trade_no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + RandomUtil.randomNumbers(6); Map params = new HashMap<>(); // params.put("attach", JSONUtil.toJsonStr(atchMap)); params.put("productType", jhConfig.getProductType()); params.put("businessMerchantNo", jhConfig.getBusinessMerchantNo()); params.put("openId", openid); params.put("merchantNo", jhConfig.getBusinessMerchantNo()); params.put("tradeType", businessType); params.put("subTradeType", ""); String total_free = partConfig.isTestEnv() ? "0.01" : money + ""; params.put("orderPrice", total_free); params.put("outTradeNo", out_trade_no); params.put("productName", desc); params.put("orderIp", WxService.getIpAddress(request)); String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); params.put("orderTime", now); params.put("returnUrl", ""); params.put("notifyUrl", myConfig.getDomain() + "/jh/notify"); params.put("remark", desc); String paySecrit = jhConfig.getPaySecret(); String sign = MerchantApiUtil.getSign(params, paySecrit); params.put("sign", sign); String jsonParam = JSONUtil.toJsonStr(params); String initPayUrl = jhConfig.getServerUrl() + "/uaps-web-gateway/cnpPay/initPay"; String result = jhHttpUtils.postJson(initPayUrl, jsonParam); StaticLog.info("调用聚合支付返回:{}", result); JSONObject jsonResult = JSONUtil.parseObj(result); if (StrUtil.equals("0000", jsonResult.getStr("resultCode"))) { JSONObject payMessage = JSONUtil.parseObj(jsonResult.getStr("payMessage")); String p = payMessage.getStr("package"); Map r = wxService.getPayP(payMessage.getStr("timeStamp"), payMessage.getStr("nonceStr"), openid, p); StaticLog.info("re:{}", JSONUtil.toJsonStr(r)); JSONObject object = JSONUtil.parseObj(payMessage); object.set("outTradeNo", out_trade_no); TbOrder tbOrder = new TbOrder(); tbOrder.setAttach(attchStr) .setCalculateId(calculateId) .setOpenid(openid) .setOrderTime(new Date()) .setOutTradeNo(out_trade_no).setPrice(money); tbOrderService.save(tbOrder); // taskService.addTask(new CheckPayStatusTask(RandomUtil.randomNumbers(12), 2000, out_trade_no)); return BeanUtil.beanToMap(object); } throw new Exception("生成订单失败"); // return new HashMap<>(); } private String handlerDesc(String desc) { if (StrUtil.isEmpty(desc)) { return ""; } if (desc.getBytes(Charset.forName("utf-8")).length > 128) { desc = StrUtil.sub(desc, 0, desc.lastIndexOf("-")); desc = handlerDesc(desc); } return desc; } public Map buildCheckParams(String outTradeNo) { Map params = new HashMap<>(); params.put("businessMerchantNo", jhConfig.getBusinessMerchantNo()); params.put("outTradeNo", outTradeNo); String paySecrit = jhConfig.getPaySecret(); String sign = MerchantApiUtil.getSign(params, paySecrit); params.put("sign", sign); return params; } public JSONObject checkPayResult(String outTradeNo) { String url = jhConfig.getServerUrl() + "/uaps-web-gateway/query/singleOrder"; Map params = buildCheckParams(outTradeNo); String result = jhHttpUtils.postForm(url, params); return JSONUtil.parseObj(result); } @Async public void notifyResult(JhNotifyBO bo) { JhNotifySignBO signBO = new JhNotifyBO(); BeanUtil.copyProperties(bo, signBO); if (!MerchantApiUtil.isRightSign(BeanUtil.beanToMap(signBO), jhConfig.getPaySecret(), bo.getSign())) { log.error("无效签名:{}", JSONUtil.toJsonStr(bo)); return; } String orderStatus = bo.getTradeStatus(); if (!"SUCCESS".equals(orderStatus) && !"FINISH".equals(orderStatus)) { log.error("支付订单回调失败:{}", JSONUtil.toJsonStr(bo)); return; } NotifyBO notifyBO = new NotifyBO(); notifyBO.setOutTradeNo(bo.getOutTradeNo()).setTradeStatus(orderStatus) .setTransactionId(bo.getBankTrxNo()).setTimeEnd(bo.getSuccessTime()) .setTotalFee(NumberUtil.mul(bo.getOrderPrice(), 100 + "").toString()); wxService.WxNotify(notifyBO); } }