JhService.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.pj.api.jh.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.NumberUtil;
  4. import cn.hutool.core.util.RandomUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.http.HttpUtil;
  7. import cn.hutool.json.JSONObject;
  8. import cn.hutool.json.JSONUtil;
  9. import cn.hutool.log.StaticLog;
  10. import com.pj.api.jh.task.CheckPayStatusTask;
  11. import com.pj.api.jh.utils.JhHttpUtils;
  12. import com.pj.api.jh.utils.MerchantApiUtil;
  13. import com.pj.api.jh.bo.JhNotifyBO;
  14. import com.pj.api.wx.bo.Attach;
  15. import com.pj.api.wx.bo.NotifyBO;
  16. import com.pj.api.wx.service.WxService;
  17. import com.pj.current.config.JhConfig;
  18. import com.pj.current.config.MyConfig;
  19. import com.pj.current.config.PartConfig;
  20. import com.pj.current.task.TaskService;
  21. import com.pj.project.tb_order.TbOrder;
  22. import com.pj.project.tb_order.TbOrderService;
  23. import com.pj.project4sp.global.BusinessException;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.springframework.scheduling.annotation.Async;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import javax.annotation.Resource;
  29. import javax.servlet.http.HttpServletRequest;
  30. import java.time.LocalDateTime;
  31. import java.time.format.DateTimeFormatter;
  32. import java.util.Date;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. /**
  36. * @Auther: lzm
  37. * @Date: 2022/04/19/17:38
  38. */
  39. @Service
  40. @Transactional
  41. @Slf4j
  42. public class JhService {
  43. @Resource
  44. MyConfig myConfig;
  45. @Resource
  46. JhConfig jhConfig;
  47. @Resource
  48. PartConfig partConfig;
  49. @Resource
  50. WxService wxService;
  51. @Resource
  52. JhHttpUtils jhHttpUtils;
  53. @Resource
  54. private TbOrderService tbOrderService;
  55. @Resource
  56. private TaskService taskService;
  57. public Map<String, ?> initPay(HttpServletRequest request) throws Exception {
  58. if (!jhConfig.isEnable()) {
  59. return wxService.prePay(request);
  60. }
  61. String money = request.getParameter("money");
  62. if (Double.valueOf(money) <= 0) {
  63. throw new BusinessException("金额不正确");
  64. }
  65. String openid = request.getParameter("openid");
  66. String type = request.getParameter("type");
  67. String desc = request.getParameter("desc");
  68. String businessId = request.getParameter("b");
  69. String c = request.getParameter("c");
  70. String a = request.getParameter("a");
  71. String tradeType = request.getParameter("tradeType");
  72. String businessType = request.getParameter("businessType");
  73. if (StrUtil.isEmpty(type)){
  74. throw new BusinessException("参数异常");
  75. }
  76. Attach atchMap = new Attach();
  77. atchMap.setC(c).setB(businessId).setA(a);
  78. String out_trade_no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + RandomUtil.randomNumbers(6);
  79. StaticLog.info("outTradeNo:{}", out_trade_no);
  80. Map<String, Object> params = new HashMap<>();
  81. // params.put("attach", JSONUtil.toJsonStr(atchMap));
  82. params.put("productType", jhConfig.getProductType());
  83. params.put("businessMerchantNo", jhConfig.getBusinessMerchantNo());
  84. params.put("openId", openid);
  85. params.put("merchantNo", jhConfig.getBusinessMerchantNo());
  86. params.put("tradeType", businessType);
  87. params.put("subTradeType", "");
  88. String total_free = partConfig.isTestEnv() ? "0.01" : money + "";
  89. log.info("order price:{}", total_free);
  90. params.put("orderPrice", total_free);
  91. params.put("outTradeNo", out_trade_no);
  92. params.put("productName", desc);
  93. params.put("orderIp", WxService.getIpAddress(request));
  94. String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  95. params.put("orderTime", now);
  96. params.put("returnUrl", "");
  97. params.put("notifyUrl", myConfig.getDomain() + "/jh/notify");
  98. params.put("remark", desc);
  99. String paySecrit = jhConfig.getPaySecret();
  100. String sign = MerchantApiUtil.getSign(params, paySecrit);
  101. params.put("sign", sign);
  102. String jsonParam = JSONUtil.toJsonStr(params);
  103. String initPayUrl = jhConfig.getServerUrl() + "/uaps-web-gateway/cnpPay/initPay";
  104. String result = jhHttpUtils.postJson(initPayUrl, jsonParam);
  105. StaticLog.info("调用聚合支付返回:{}", result);
  106. JSONObject jsonResult = JSONUtil.parseObj(result);
  107. if (StrUtil.equals("0000", jsonResult.getStr("resultCode"))) {
  108. JSONObject payMessage = JSONUtil.parseObj(jsonResult.getStr("payMessage"));
  109. String p = payMessage.getStr("package");
  110. Map<String, String> r = wxService.getPayP(payMessage.getStr("timeStamp"), payMessage.getStr("nonceStr"), openid, p);
  111. StaticLog.info("re:{}", JSONUtil.toJsonStr(r));
  112. JSONObject object = JSONUtil.parseObj(payMessage);
  113. object.set("outTradeNo", out_trade_no);
  114. TbOrder tbOrder = new TbOrder();
  115. tbOrder.setAttach(JSONUtil.toJsonStr(atchMap))
  116. .setOpenid(openid)
  117. .setOrderTime(new Date()).setBusinessType(Integer.valueOf(type))
  118. .setOutTradeNo(out_trade_no).setPrice(money);
  119. tbOrderService.save(tbOrder);
  120. taskService.addTask(new CheckPayStatusTask(RandomUtil.randomNumbers(12), 2000, out_trade_no));
  121. return BeanUtil.beanToMap(object);
  122. }
  123. throw new Exception("生成订单失败");
  124. }
  125. public Map<String, Object> buildCheckParams(String outTradeNo) {
  126. Map<String, Object> params = new HashMap<>();
  127. params.put("businessMerchantNo", jhConfig.getBusinessMerchantNo());
  128. params.put("outTradeNo", outTradeNo);
  129. String paySecrit = jhConfig.getPaySecret();
  130. String sign = MerchantApiUtil.getSign(params, paySecrit);
  131. params.put("sign", sign);
  132. return params;
  133. }
  134. public JSONObject checkPayResult(String outTradeNo) {
  135. String url = jhConfig.getServerUrl() + "/uaps-web-gateway/query/singleOrder";
  136. Map<String, Object> params = buildCheckParams(outTradeNo);
  137. String result = jhHttpUtils.postForm(url, params);
  138. return JSONUtil.parseObj(result);
  139. }
  140. @Async
  141. public void notifyResult(JhNotifyBO bo) {
  142. String orderStatus = bo.getTradeStatus();
  143. if (!"SUCCESS".equals(orderStatus) && !"FINISH".equals(orderStatus)) {
  144. return;
  145. }
  146. NotifyBO notifyBO = new NotifyBO();
  147. String outTradeNo = bo.getOutTradeNo();
  148. notifyBO.setOutTradeNo(bo.getOutTradeNo())
  149. .setAttach(bo.getAttach())
  150. .setTransactionId(bo.getBankTrxNo())
  151. .setTotalFee(NumberUtil.mul(bo.getOrderPrice(), 100 + "").toString());
  152. wxService.WxNotify(notifyBO);
  153. TbOrder tbOrder = tbOrderService.findByOutTradeNo(outTradeNo);
  154. tbOrder.setOrderStatus(orderStatus)
  155. .setTransactionId(bo.getBankTrxNo())
  156. .setCompleteDate(bo.getSuccessTime());
  157. tbOrderService.updateById(tbOrder);
  158. }
  159. }