|
|
@@ -1,29 +1,32 @@
|
|
|
-package com.pj.api.wx;
|
|
|
+package com.pj.api.wx.service;
|
|
|
|
|
|
-import cn.hutool.core.net.Ipv4Util;
|
|
|
import cn.hutool.core.net.URLDecoder;
|
|
|
-import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.core.util.XmlUtil;
|
|
|
+import cn.hutool.crypto.SecureUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.pj.api.wx.WxUtils;
|
|
|
+import com.pj.api.wx.bo.*;
|
|
|
+import com.pj.api.wx.vo.PrePayVO;
|
|
|
import com.pj.current.config.MyConfig;
|
|
|
import com.pj.current.config.WxConfig;
|
|
|
import com.pj.project.tb_business.TbBusiness;
|
|
|
import com.pj.project.tb_business.TbBusinessService;
|
|
|
import com.pj.project.tb_business_car.TbBusinessCar;
|
|
|
import com.pj.project.tb_business_car.TbBusinessCarService;
|
|
|
-import com.pj.project.tb_charge_record.TbChargeRecordService;
|
|
|
+import com.pj.project.tb_business_item.TbBusinessItem;
|
|
|
+import com.pj.project.tb_business_item.TbBusinessItemService;
|
|
|
import com.pj.project.tb_costomer.TbCostomer;
|
|
|
import com.pj.project.tb_costomer.TbCostomerService;
|
|
|
import com.pj.project.tb_pay_record.TbPayRecord;
|
|
|
import com.pj.project.tb_pay_record.TbPayRecordService;
|
|
|
import com.pj.utils.cache.RedisUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -53,6 +56,9 @@ public class WxService {
|
|
|
TbCostomerService tbCostomerService;
|
|
|
@Resource
|
|
|
TbBusinessCarService tbBusinessCarService;
|
|
|
+ @Resource
|
|
|
+ @Lazy
|
|
|
+ TbBusinessItemService tbBusinessItemService;
|
|
|
|
|
|
/**
|
|
|
* 统一下单接口
|
|
|
@@ -60,29 +66,20 @@ public class WxService {
|
|
|
public Map<String, String> prePay(HttpServletRequest request) throws Exception {
|
|
|
String tradeType = request.getParameter("tradeType");
|
|
|
String money = request.getParameter("money");
|
|
|
- String carIds = request.getParameter("carId");
|
|
|
String openid = request.getParameter("openid");
|
|
|
- String businessId = request.getParameter("businessId");
|
|
|
+ String businessId = request.getParameter("b");
|
|
|
+ String c = request.getParameter("c");
|
|
|
+ String a = request.getParameter("a");
|
|
|
Attach atchMap = new Attach();
|
|
|
- atchMap.setC(carIds).setB(businessId);
|
|
|
+ atchMap.setC(c).setB(businessId).setA(a);
|
|
|
String out_trade_no = RandomUtil.randomString(32);
|
|
|
- if (StrUtil.isNotEmpty(businessId)) {
|
|
|
- TbBusiness tbBusiness = tbBusinessService.getById(businessId);
|
|
|
- out_trade_no = tbBusiness.getNo();
|
|
|
- } else {
|
|
|
- for (String id : StrUtil.splitTrim(carIds, ",")) {
|
|
|
- TbBusinessCar car = tbBusinessCarService.getById(id);
|
|
|
- out_trade_no = "c_" + car.getId();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
params.put("appid", wxConfig.getAppId());
|
|
|
params.put("mch_id", wxConfig.getMachId());
|
|
|
params.put("openid", openid);
|
|
|
params.put("nonce_str", RandomUtil.randomString(32));
|
|
|
params.put("body", "支付中心-业务订单支付");
|
|
|
- params.put("out_trade_no", out_trade_no+"_"+System.currentTimeMillis()/1000);
|
|
|
+ params.put("out_trade_no", out_trade_no);
|
|
|
params.put("attach", JSONUtil.toJsonStr(atchMap));
|
|
|
String total_free = Integer.parseInt(money) * 100 + "";
|
|
|
// String total_free = "1";
|
|
|
@@ -148,59 +145,46 @@ public class WxService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void WxNotify(HttpServletRequest request) throws Exception {
|
|
|
- Map<String, Object> result = wxUtils.msgToMap(request);
|
|
|
- String total_fee = result.get("total_fee").toString();
|
|
|
+ @Async
|
|
|
+ public void WxNotify(NotifyBO notifyBO) throws Exception {
|
|
|
+ String total_fee = notifyBO.getTotalFee();
|
|
|
BigDecimal money = new BigDecimal(total_fee).divide(new BigDecimal(100), 2, BigDecimal.ROUND_UP);
|
|
|
- log.info("wx pay notify;{}", JSONUtil.toJsonStr(result));
|
|
|
- String tradeNo = result.get("out_trade_no").toString();
|
|
|
- tradeNo=StrUtil.subBefore(tradeNo,"_",true);
|
|
|
- String attachStr = result.get("attach") == null ? "" : result.get("attach").toString();
|
|
|
- String transactionId = result.get("transaction_id").toString();
|
|
|
+ log.info("wx pay notify;{}", JSONUtil.toJsonStr(notifyBO));
|
|
|
+ String tradeNo =notifyBO.getOutTradeNo();
|
|
|
+ tradeNo = StrUtil.subBefore(tradeNo, "_", true);
|
|
|
+ String attachStr = notifyBO.getAttach();
|
|
|
+ String transactionId = notifyBO.getTransactionId();
|
|
|
Date now = new Date();
|
|
|
if (StrUtil.isNotEmpty(attachStr)) {
|
|
|
Attach attach = JSONUtil.toBean(attachStr, Attach.class);
|
|
|
+ List<PriceBO> cars = JSONUtil.toList(attach.getC(), PriceBO.class);
|
|
|
+ for (PriceBO bo1 : cars) {
|
|
|
+ TbBusinessCar car = tbBusinessCarService.getById(bo1.getId());
|
|
|
+ car.setPay(1).setMoney(car.getMoney().add(bo1.getP()));
|
|
|
+ tbBusinessCarService.updateById(car);
|
|
|
+ }
|
|
|
String businessId = attach.getB();
|
|
|
+
|
|
|
if (StrUtil.isNotEmpty(businessId)) {
|
|
|
+ List<TbBusinessItem>items=tbBusinessItemService.findByBusinessId(businessId);
|
|
|
+ items.forEach(tbBusinessItem -> tbBusinessItem.setPayStatus(1).setPayTime(now));
|
|
|
+ tbBusinessItemService.updateBatchById(items);
|
|
|
TbBusiness tbBusiness = tbBusinessService.getById(businessId);
|
|
|
- if (tbBusiness.getPayStatus() == 2) {
|
|
|
- return;
|
|
|
- }
|
|
|
- tbBusiness.setPayTime(now).setPayType(3)
|
|
|
- .setPayStatus(3).setAdminConfirmPay(1)
|
|
|
- .setConfirmInput(1).setConfirmInputTime(now)
|
|
|
+ tbBusiness.setPayStatus(tbBusiness.getPayMoney().add(money).equals(tbBusiness.getTotalMoney()) ? 3 : 4);
|
|
|
+ tbBusiness.setPayTime(now).setPayType(3).setConfirmInput(1).setConfirmInputTime(now)
|
|
|
+ .setPayMoney(tbBusiness.getPayMoney().add(money))
|
|
|
.setPayNo(transactionId);
|
|
|
tbBusinessService.updateById(tbBusiness);
|
|
|
- List<TbBusinessCar> cars = tbBusinessCarService.findByBusinessId(businessId);
|
|
|
- cars.forEach(tbBusinessCar -> tbBusinessCar.setPay(1).setPayTime(now).setMoney(tbBusinessCar.getBasePartMoney()));
|
|
|
- tbBusinessCarService.updateBatchById(cars);
|
|
|
- }
|
|
|
- String carIds = attach.getC();
|
|
|
- if (StrUtil.isNotEmpty(carIds) && StrUtil.isEmpty(businessId)) {
|
|
|
- StrUtil.splitTrim(carIds, ",").forEach(id -> {
|
|
|
- TbBusinessCar car = tbBusinessCarService.getById(id);
|
|
|
- car.setPay(1);
|
|
|
- car.setMoney(car.getBasePartMoney().add(money));
|
|
|
- tbBusinessCarService.updateById(car);
|
|
|
- });
|
|
|
+ TbPayRecord payRecord = new TbPayRecord();
|
|
|
+ TbCostomer tbCostomer = tbCostomerService.getById(tbBusiness.getCustomerId());
|
|
|
+ payRecord.setCreateTime(now)
|
|
|
+ .setOutTradeNo(tradeNo)
|
|
|
+ .setTransactionId(transactionId)
|
|
|
+ .setPayMoney(money)
|
|
|
+ .setCustomerId(tbCostomer.getId()).setCustomerName(tbCostomer.getName());
|
|
|
+ tbPayRecordService.save(payRecord);
|
|
|
}
|
|
|
}
|
|
|
- TbBusiness tbBusiness = tbBusinessService.finByNo(tradeNo);
|
|
|
- if (tradeNo.contains("c_")) {
|
|
|
- String busiCarId = tradeNo.replace("c_", "");
|
|
|
- TbBusinessCar tbBusinessCar = tbBusinessCarService.getById(busiCarId);
|
|
|
- tbBusiness=tbBusinessService.getById(tbBusinessCar.getBusinessId());
|
|
|
- }
|
|
|
- if (tbBusiness != null) {
|
|
|
- TbPayRecord payRecord = new TbPayRecord();
|
|
|
- TbCostomer tbCostomer = tbCostomerService.getById(tbBusiness.getCustomerId());
|
|
|
- payRecord.setCreateTime(now)
|
|
|
- .setOutTradeNo(tradeNo)
|
|
|
- .setTransactionId(transactionId)
|
|
|
- .setPayMoney(money)
|
|
|
- .setCustomerId(tbCostomer.getId()).setCustomerName(tbCostomer.getName());
|
|
|
- tbPayRecordService.save(payRecord);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
public String getRedirectUrl(String path, String state) {
|
|
|
@@ -234,7 +218,8 @@ public class WxService {
|
|
|
params.put("jsapi_ticket", jsTicket);
|
|
|
params.put("timestamp", nowTime);
|
|
|
params.put("url", url);
|
|
|
- params.put("sign", DigestUtils.sha1Hex(params.toString().substring(1, params.toString().lastIndexOf("}")).replaceAll(", ", "&")));
|
|
|
+ String sign= SecureUtil.sha1(params.toString().substring(1, params.toString().lastIndexOf("}")).replaceAll(", ", "&"));
|
|
|
+ params.put("sign",sign);
|
|
|
params.put("appId", wxConfig.getAppId());
|
|
|
return params;
|
|
|
}
|