|
|
@@ -1,15 +1,38 @@
|
|
|
package com.yhlxj.service.order.impl;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alipay.api.internal.util.AlipaySignature;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.cyksj.common.constant.Constant;
|
|
|
+import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.common.snowflake.Sequence;
|
|
|
+import com.cyksj.common.util.Codec;
|
|
|
+import com.cyksj.common.util.Jsons;
|
|
|
+import com.cyksj.enums.GatewayApiCode;
|
|
|
+import com.yhlxj.dao.mapper.*;
|
|
|
import com.yhlxj.dao.model.dto.AliPayParams;
|
|
|
+import com.yhlxj.dao.model.dto.WebWholesaleConfigDto;
|
|
|
+import com.yhlxj.dao.model.entity.ShopConfig;
|
|
|
+import com.yhlxj.dao.model.entity.SysConfig;
|
|
|
+import com.yhlxj.dao.model.entity.User;
|
|
|
+import com.yhlxj.dao.model.entity.WebWholesaleOrderDon;
|
|
|
import com.yhlxj.dao.model.request.AlipayOrderDonReq;
|
|
|
+import com.yhlxj.dao.model.request.OrderAttach;
|
|
|
+import com.yhlxj.dao.model.request.WebOrderPayRequest;
|
|
|
import com.yhlxj.service.order.WebWholesaleOrderDonService;
|
|
|
import com.yhlxj.service.pay.AliPayCommonService;
|
|
|
+import com.yhlxj.service.zfb.BaseZfbConfig;
|
|
|
import com.yhlxj.service.zfb.ZfbProductCode;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
/**
|
|
|
* 项目名: yhlxj2
|
|
|
* 文件名: WebWholesaleOrderDonServiceImpl
|
|
|
@@ -21,6 +44,58 @@ import org.springframework.stereotype.Service;
|
|
|
@Slf4j
|
|
|
public class WebWholesaleOrderDonServiceImpl implements WebWholesaleOrderDonService {
|
|
|
private final AliPayCommonService aliPayCommonService;
|
|
|
+
|
|
|
+ private final SysConfigMapper sysConfigMapper;
|
|
|
+
|
|
|
+ private final UserMapper userMapper;
|
|
|
+
|
|
|
+ private static final Sequence SEQUENCE = new Sequence(0);
|
|
|
+
|
|
|
+ private final WebWholeSaleOrderDonMapper orderDonMapper;
|
|
|
+
|
|
|
+ private final ShopConfigMapper shopConfigMapper;
|
|
|
+
|
|
|
+ private final GoodsDonSkuMapper skuMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public WebWholesaleOrderDon submit(WebOrderPayRequest payRequest) throws Exception {
|
|
|
+ Long userId = payRequest.getUserId();
|
|
|
+ Integer payNum = payRequest.getNum();
|
|
|
+ SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
|
|
|
+ .eq(SysConfig::getSysKey, Constant.WEB_WHOLESALE_CONFIG_KEY)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (sysConfig == null || StrUtil.isEmpty(sysConfig.getSysValue())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("特价批发异常");
|
|
|
+ }
|
|
|
+ WebWholesaleConfigDto webWholesaleConfigDto = Jsons.parseObject(sysConfig.getSysValue(), WebWholesaleConfigDto.class);
|
|
|
+ Integer minNum = webWholesaleConfigDto.getNum();
|
|
|
+ if (payNum < minNum) {
|
|
|
+ throw BusinessRuntimeException.getInstance("起批数量最低为{0}", minNum);
|
|
|
+ }
|
|
|
+ //批发规格ids
|
|
|
+ List<Long> skuIds = webWholesaleConfigDto.getSkuIds();
|
|
|
+ BigDecimal singlePrice = webWholesaleConfigDto.getSingle();
|
|
|
+ WebWholesaleOrderDon orderDon = new WebWholesaleOrderDon();
|
|
|
+ orderDon.setNum(payNum);
|
|
|
+
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
|
|
|
+ if (user.getIsBlack()) throw BusinessRuntimeException.getInstance("系统检测操作异常,请稍后再试");
|
|
|
+ /* 生成订单 */
|
|
|
+ String donNo = SEQUENCE.nextId().toString();
|
|
|
+ orderDon.setOrderNo(donNo);
|
|
|
+ orderDon.setMoney(singlePrice.multiply(BigDecimal.valueOf(orderDon.getNum())));
|
|
|
+ orderDon.setStatus(WebWholesaleOrderDon.Status.noPayment);
|
|
|
+ orderDon.setUserId(userId);
|
|
|
+ orderDon.setType(WebWholesaleOrderDon.Type.getType(payRequest.getType()));
|
|
|
+ //设置订单支付商品信息
|
|
|
+ String payTitle = skuMapper.getPayGoodsTitles(skuIds);
|
|
|
+ orderDon.setPayTitle(payTitle);
|
|
|
+ orderDon.setSkuIds(skuIds.toString());
|
|
|
+ orderDonMapper.insert(orderDon);
|
|
|
+ return orderDon;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public AliPayParams zfbUnifiedPay(AlipayOrderDonReq re) throws Exception {
|
|
|
String productCode = StrUtil.EMPTY;
|
|
|
@@ -32,4 +107,49 @@ public class WebWholesaleOrderDonServiceImpl implements WebWholesaleOrderDonServ
|
|
|
}
|
|
|
return aliPayCommonService.unifiedPayWebWholesale(re.getOrderId(), productCode, re.getPayMode(), re.getReturnUrl());
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void aliPayNotify(Map<String, String> params) throws Exception {
|
|
|
+ //调用SDK验证签名
|
|
|
+ String body = Codec.DoBase64.custom().setData(params.get("body")).decodeAsText();
|
|
|
+ OrderAttach orderAttach = Jsons.parseObject(body, OrderAttach.class);
|
|
|
+ Long oderId = orderAttach.getO();
|
|
|
+ WebWholesaleOrderDon orderDon = orderDonMapper.selectById(oderId);
|
|
|
+ if (null == orderDon) {
|
|
|
+ throw BusinessRuntimeException.getInstance("订单不存在.");
|
|
|
+ }
|
|
|
+ String appId = params.get("app_id");
|
|
|
+ ShopConfig shopConfig = shopConfigMapper.selectOne(Wrappers.lambdaQuery(ShopConfig.class)
|
|
|
+ .eq(ShopConfig::getAppId, appId)
|
|
|
+ .last("limit 1"));
|
|
|
+ boolean signVerified = AlipaySignature.rsaCheckV1(params, shopConfig.getPublicKey(), BaseZfbConfig.charset, BaseZfbConfig.signType);
|
|
|
+ if (!signVerified) {
|
|
|
+ log.error("支付宝回调校验签名错误,自定义入参:{}", params.get("body"));
|
|
|
+ throw BusinessRuntimeException.getInstance("支付宝回调校验签名错误");
|
|
|
+ }
|
|
|
+ String transactionId = params.get("trade_no");
|
|
|
+ String payTimeStr = params.get("gmt_payment");
|
|
|
+ //买家支付宝账号
|
|
|
+ String buyerId = Optional.ofNullable(params.get("buyer_id")).orElse(params.get("buyer_open_id"));
|
|
|
+ if (!orderDon.getStatus().equals(WebWholesaleOrderDon.Status.noPayment) && !orderDon.getStatus().equals(WebWholesaleOrderDon.Status.close)) {
|
|
|
+ log.info("支付宝已回调,重复修改状态 id:{}", orderDon.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 更改订单状态
|
|
|
+ orderDon.setTransactionId(transactionId);
|
|
|
+ orderDon.setStatus(WebWholesaleOrderDon.Status.complete);
|
|
|
+ if (StrUtil.isNotBlank(payTimeStr)) {
|
|
|
+ orderDon.setPayTime(DateUtil.parse(payTimeStr, "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ }
|
|
|
+ orderDon.setBuyerId(buyerId);
|
|
|
+ orderDon.setShopId(appId);
|
|
|
+ orderDon.setType(WebWholesaleOrderDon.Type.ALI_PAY);
|
|
|
+ orderDonMapper.updateById(orderDon);
|
|
|
+ //回调
|
|
|
+ unifiedHandlerOrderNotify(orderDon);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void unifiedHandlerOrderNotify(WebWholesaleOrderDon orderDon) {
|
|
|
+ //订单关联兑换码 TODO
|
|
|
+ }
|
|
|
}
|