JhService.java 5.7 KB

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