|
@@ -1,35 +1,65 @@
|
|
package cn.cslg.permission.service.weixinpay;
|
|
package cn.cslg.permission.service.weixinpay;
|
|
|
|
|
|
|
|
+import cn.cslg.permission.common.model.qiaobi.payOrder.OrderGoodDTO;
|
|
|
|
+import cn.cslg.permission.common.model.qiaobi.payOrder.PayOrderAddMessageDTO;
|
|
|
|
+import cn.cslg.permission.common.model.qiaobi.strategy.QueryGoodsStrategyVO;
|
|
import cn.cslg.permission.common.model.weixinpay.GetAuthorizationVO;
|
|
import cn.cslg.permission.common.model.weixinpay.GetAuthorizationVO;
|
|
import cn.cslg.permission.common.utils.BatchNoUtil;
|
|
import cn.cslg.permission.common.utils.BatchNoUtil;
|
|
|
|
+import cn.cslg.permission.common.utils.common.BigDecimalUtil;
|
|
|
|
+import cn.cslg.permission.domain.qiaobi.Goods;
|
|
|
|
+import cn.cslg.permission.domain.qiaobi.PayOrder;
|
|
|
|
+import cn.cslg.permission.exception.ExceptionEnum;
|
|
|
|
+import cn.cslg.permission.exception.XiaoShiException;
|
|
|
|
+import cn.cslg.permission.factory.strategy.StrategyFatcory;
|
|
|
|
+import cn.cslg.permission.factory.strategy.StrategyImp;
|
|
|
|
+import cn.cslg.permission.mapper.qiaobi.MarketStrategyMapper;
|
|
|
|
+import cn.cslg.permission.service.qiaobi.GoodsService;
|
|
|
|
+import cn.cslg.permission.service.qiaobi.PayOrderService;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.Gson;
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class WeixinPayService {
|
|
public class WeixinPayService {
|
|
@Autowired
|
|
@Autowired
|
|
private AuthorizationService authorizationService;
|
|
private AuthorizationService authorizationService;
|
|
-
|
|
|
|
- public void getPayTickets() {
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private MarketStrategyMapper marketStrategyMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private GoodsService goodsService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private StrategyFatcory strategyFatcory;
|
|
|
|
+ @Autowired
|
|
|
|
+ private PayOrderService payOrderService;
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public String getPayTickets(PayOrderAddMessageDTO payOrderAddMessageDTO) {
|
|
|
|
+ String codeUrl="";
|
|
|
|
+ payOrderAddMessageDTO = this.calculatePrice(payOrderAddMessageDTO);
|
|
|
|
+ PayOrder payOrder = payOrderService.addPayOrder(payOrderAddMessageDTO);
|
|
String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
|
|
String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
|
|
- String tradeNo = BatchNoUtil.getOrderNo();
|
|
|
|
|
|
+ String tradeNo = payOrder.getTradeNo();
|
|
com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest prepayRequest = new com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest();
|
|
com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest prepayRequest = new com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest();
|
|
String appId = "wxe5f9b6e3d532e6c3";
|
|
String appId = "wxe5f9b6e3d532e6c3";
|
|
- String merchantId="1718408246";
|
|
|
|
- String notifyUrl="https://xsip.cn/xiaoshi-weixinback/weixinpay/success";
|
|
|
|
|
|
+ String merchantId = "1718408246";
|
|
|
|
+ String notifyUrl = "https://fms.xsip.cn/permission/qiaoBi/pay/success";
|
|
prepayRequest.setAppid(appId);
|
|
prepayRequest.setAppid(appId);
|
|
prepayRequest.setMchid(merchantId);
|
|
prepayRequest.setMchid(merchantId);
|
|
prepayRequest.setOutTradeNo(tradeNo);
|
|
prepayRequest.setOutTradeNo(tradeNo);
|
|
prepayRequest.setDescription("商品信息");
|
|
prepayRequest.setDescription("商品信息");
|
|
prepayRequest.setNotifyUrl(notifyUrl);
|
|
prepayRequest.setNotifyUrl(notifyUrl);
|
|
com.wechat.pay.java.service.payments.nativepay.model.Amount amount = new com.wechat.pay.java.service.payments.nativepay.model.Amount();
|
|
com.wechat.pay.java.service.payments.nativepay.model.Amount amount = new com.wechat.pay.java.service.payments.nativepay.model.Amount();
|
|
- amount.setTotal(1);
|
|
|
|
|
|
+ Double truePrice = payOrder.getTruePrice();
|
|
|
|
+ Double d = BigDecimalUtil.multiplication(truePrice, 100);
|
|
|
|
+ Integer IntPrice = d.intValue();
|
|
|
|
+ amount.setTotal(IntPrice);
|
|
amount.setCurrency("CNY");
|
|
amount.setCurrency("CNY");
|
|
prepayRequest.setAmount(amount);
|
|
prepayRequest.setAmount(amount);
|
|
String param = new Gson().toJson(prepayRequest);
|
|
String param = new Gson().toJson(prepayRequest);
|
|
@@ -52,15 +82,68 @@ public class WeixinPayService {
|
|
Response response = okHttpClient.newCall(request).execute();
|
|
Response response = okHttpClient.newCall(request).execute();
|
|
// 判断请求是否成功
|
|
// 判断请求是否成功
|
|
// if (response.isSuccessful()) {
|
|
// if (response.isSuccessful()) {
|
|
- // 打印服务端返回结果
|
|
|
|
- String a = Objects.requireNonNull(response.body()).string();
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(a);
|
|
|
|
-
|
|
|
|
|
|
+ // 打印服务端返回结果
|
|
|
|
+ String a = Objects.requireNonNull(response.body()).string();
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(a);
|
|
|
|
+ Object codeUrlObject= jsonObject.get("code_url");
|
|
|
|
+ if(codeUrlObject==null){
|
|
|
|
+ throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR,"请求微信支付接口错误,请联系管理员排查");
|
|
|
|
+ }
|
|
|
|
+ codeUrl =codeUrlObject.toString();
|
|
// }
|
|
// }
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
+ return codeUrl;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 计算商品总价
|
|
|
|
+ *
|
|
|
|
+ * @param payOrderAddMessageDTO
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public PayOrderAddMessageDTO calculatePrice(PayOrderAddMessageDTO payOrderAddMessageDTO) {
|
|
|
|
+ Double totalPrice = 0d;
|
|
|
|
+ Double orgTotalPrice = 0d;
|
|
|
|
+ if (payOrderAddMessageDTO == null || payOrderAddMessageDTO.getOrderGoodDTOList() == null || payOrderAddMessageDTO.getOrderGoodDTOList().isEmpty()) {
|
|
|
|
+ throw new XiaoShiException(ExceptionEnum.BUSINESS_CHECK, "请选择商品");
|
|
|
|
+ }
|
|
|
|
+ List<OrderGoodDTO> orderGoodDTOList = payOrderAddMessageDTO.getOrderGoodDTOList();
|
|
|
|
+ List<Integer> goodsIds = orderGoodDTOList.stream().map(OrderGoodDTO::getGoodId).collect(Collectors.toList());
|
|
|
|
+ if (goodsIds == null || goodsIds.isEmpty()) {
|
|
|
|
+ throw new XiaoShiException(ExceptionEnum.BUSINESS_CHECK, "请选择商品");
|
|
|
|
+ }
|
|
|
|
+ List<Goods> goods = goodsService.queryByIds(goodsIds);
|
|
|
|
+ List<QueryGoodsStrategyVO> queryGoodsStrategyVOS = marketStrategyMapper.queryStrategyByGoodsId(goodsIds);
|
|
|
|
+ for (OrderGoodDTO item : orderGoodDTOList) {
|
|
|
|
+ Integer temGoodId = item.getGoodId();
|
|
|
|
+ Goods goods1 = goods.stream().filter(tem -> tem.getId().equals(temGoodId)).findFirst().orElse(null);
|
|
|
|
+ if (goods1 == null) {
|
|
|
|
+ throw new XiaoShiException(ExceptionEnum.BUSINESS_CHECK, "有伪造的商品数据");
|
|
|
|
+ }
|
|
|
|
+ item.setPrice(goods1.getPrice());
|
|
|
|
+ item.setGoods(goods1);
|
|
|
|
+ if (item.getNum() == null || item.getNum() <= 0) {
|
|
|
|
+ throw new XiaoShiException(ExceptionEnum.BUSINESS_CHECK, "商品数量不能小于0");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ Double orgPrice = BigDecimalUtil.multiplication(item.getPrice(), item.getNum());
|
|
|
|
+ orgTotalPrice = BigDecimalUtil.add(orgPrice, orgTotalPrice);
|
|
|
|
+ Double goodsTotalPrice = BigDecimalUtil.multiplication(item.getPrice(), item.getNum());
|
|
|
|
+ item.setTotalPrice(goodsTotalPrice);
|
|
|
|
+ QueryGoodsStrategyVO queryGoodsStrategyVO = queryGoodsStrategyVOS.stream().filter(tem -> tem.getGoodsId().equals(temGoodId)).findFirst().orElse(null);
|
|
|
|
+ item.setQueryGoodsStrategyVO(queryGoodsStrategyVO);
|
|
|
|
+ if (queryGoodsStrategyVO != null) {
|
|
|
|
+ String methodName = queryGoodsStrategyVO.getStrategyName();
|
|
|
|
+ StrategyImp strategyImp = strategyFatcory.getClass(methodName);
|
|
|
|
+ item = strategyImp.DoGoodStrategy(item);
|
|
|
|
+ }
|
|
|
|
+ totalPrice = BigDecimalUtil.add(totalPrice, item.getTotalPrice());
|
|
|
|
+ }
|
|
|
|
+ payOrderAddMessageDTO.setTotalPrice(totalPrice);
|
|
|
|
+ payOrderAddMessageDTO.setOrgTotalPrice(orgTotalPrice);
|
|
|
|
+ return payOrderAddMessageDTO;
|
|
}
|
|
}
|
|
}
|
|
}
|