|
|
@@ -94,6 +94,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
private final StationAccountRecordService stationAccountRecordService;
|
|
|
private final WashOrderService washOrderService;
|
|
|
private final MpMsgTemplateService mpMsgTemplateService;
|
|
|
+ private final RechargePromotionService rechargePromotionService;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -106,7 +107,8 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
PayLogService payLogService, AccountService accountService,
|
|
|
RefundLogService refundLogService,
|
|
|
ActivityService activityService, UserRechargeRightsService userRechargeRightsService,
|
|
|
- RechargeConfigService rechargeConfigService, StationAccountService stationAccountService, SplitRecordService splitRecordService, StationAccountRecordService stationAccountRecordService, WashOrderService washOrderService, MpMsgTemplateService mpMsgTemplateService) {
|
|
|
+ RechargeConfigService rechargeConfigService, StationAccountService stationAccountService, SplitRecordService splitRecordService, StationAccountRecordService stationAccountRecordService, WashOrderService washOrderService, MpMsgTemplateService mpMsgTemplateService,
|
|
|
+ RechargePromotionService rechargePromotionService) {
|
|
|
this.conf = conf;
|
|
|
this.walletDetailService = walletDetailService;
|
|
|
this.payLogService = payLogService;
|
|
|
@@ -120,6 +122,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
this.stationAccountRecordService = stationAccountRecordService;
|
|
|
this.washOrderService = washOrderService;
|
|
|
this.mpMsgTemplateService = mpMsgTemplateService;
|
|
|
+ this.rechargePromotionService = rechargePromotionService;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -261,6 +264,52 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
return service.prepayWithRequestPayment(request);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 专属优惠活动充值支付下单
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public PrepayWithRequestPaymentResponse promotionPay(String promotionToken, String stationId) {
|
|
|
+ var promotion = rechargePromotionService.getByToken(promotionToken);
|
|
|
+ if (promotion == null) {
|
|
|
+ throw new BusinessException("优惠活动不存在或已过期");
|
|
|
+ }
|
|
|
+
|
|
|
+ var userId = StpUtil.getLoginIdAsLong();
|
|
|
+ if (!rechargePromotionService.checkUserEligible(promotion, userId)) {
|
|
|
+ throw new BusinessException("您不在本次活动范围内");
|
|
|
+ }
|
|
|
+
|
|
|
+ var openid = StpUtil.getSession().getString("openid");
|
|
|
+ var outTradeNo = OrderUtils.getOrderNo();
|
|
|
+
|
|
|
+ var walletDetail = new WalletDetail()
|
|
|
+ .setType(WalletDetail.TYPE_充值)
|
|
|
+ .setStatus(WalletDetail.STATUS_待确认)
|
|
|
+ .setUserId(userId)
|
|
|
+ .setAmount(promotion.getRechargeAmount())
|
|
|
+ .setOrderNo(outTradeNo)
|
|
|
+ .setPromotionId(promotion.getId())
|
|
|
+ .setSource("WX_PAY");
|
|
|
+ walletDetailService.save(walletDetail);
|
|
|
+
|
|
|
+ PrepayRequest request = new PrepayRequest();
|
|
|
+ Amount amount = new Amount();
|
|
|
+ amount.setTotal(promotion.getRechargeAmount());
|
|
|
+ request.setAmount(amount);
|
|
|
+ request.setAppid(conf.getAppid());
|
|
|
+ request.setMchid(conf.getMchid());
|
|
|
+ request.setDescription("超级进化车生活充值(专属优惠)");
|
|
|
+ request.setNotifyUrl(conf.getNotifyUrl());
|
|
|
+ request.setOutTradeNo(outTradeNo);
|
|
|
+ request.setAttach(stationId);
|
|
|
+ Payer payer = new Payer();
|
|
|
+ payer.setOpenid(openid);
|
|
|
+ request.setPayer(payer);
|
|
|
+ JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
|
|
|
+ return service.prepayWithRequestPayment(request);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 微信支付订单号查询订单
|
|
|
*/
|
|
|
@@ -315,19 +364,28 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
if (walletDetail != null) {
|
|
|
|
|
|
var stationId = transaction.getAttach();
|
|
|
- var rechargeConfig = rechargeConfigService.getByStationAndAmount(stationId, walletDetail.getAmount());
|
|
|
- if (rechargeConfig == null) {
|
|
|
- LOGGER.error("微信支付回调:未找到充值配置,stationId={}, amount={}", stationId, walletDetail.getAmount());
|
|
|
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
|
|
+
|
|
|
+ // 确定赠款金额:优先使用优惠活动配置,否则使用站点充值配置
|
|
|
+ Integer grantsAmount;
|
|
|
+ if (walletDetail.getPromotionId() != null) {
|
|
|
+ var promotion = rechargePromotionService.getById(walletDetail.getPromotionId());
|
|
|
+ grantsAmount = promotion != null ? promotion.getGrantsAmount() : 0;
|
|
|
+ } else {
|
|
|
+ var rechargeConfig = rechargeConfigService.getByStationAndAmount(stationId, walletDetail.getAmount());
|
|
|
+ if (rechargeConfig == null) {
|
|
|
+ LOGGER.error("微信支付回调:未找到充值配置,stationId={}, amount={}", stationId, walletDetail.getAmount());
|
|
|
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
|
|
+ }
|
|
|
+ grantsAmount = rechargeConfig.getGrantsAmount();
|
|
|
}
|
|
|
|
|
|
// 更新余额(赠款计入不可退优惠金额)
|
|
|
var account = accountService.getAccountByUserId(walletDetail.getUserId());
|
|
|
- var grantsAmount = rechargeConfig.getGrantsAmount();
|
|
|
accountService.lambdaUpdate().setSql("balance = balance + {0}, recharge_balance = recharge_balance + {0}, grants_balance = grants_balance + {1}, discount_amount = discount_amount + {1}", transaction.getAmount().getTotal(), grantsAmount)
|
|
|
.eq(Account::getUserId, walletDetail.getUserId()).update();
|
|
|
|
|
|
walletDetail.setStatus(WalletDetail.STATUS_已确认); //已确认
|
|
|
+ walletDetail.setSource("WX_PAY");
|
|
|
walletDetail.setCurrency(transaction.getAmount().getCurrency());
|
|
|
walletDetail.setAmount(transaction.getAmount().getTotal());
|
|
|
walletDetail.setGrantsAmount(grantsAmount);
|
|
|
@@ -393,6 +451,54 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 线下充值(运营人员代充)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void offlineRecharge(Long userId, Integer amount, Integer grantsAmount, String remark) {
|
|
|
+ var account = accountService.getAccountByUserId(userId);
|
|
|
+ if (account == null) {
|
|
|
+ throw new BusinessException("用户账户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ var tradeNo = OrderUtils.getOrderNo();
|
|
|
+ int gAmount = grantsAmount != null ? grantsAmount : 0;
|
|
|
+
|
|
|
+ var walletDetail = new WalletDetail()
|
|
|
+ .setType(WalletDetail.TYPE_充值)
|
|
|
+ .setStatus(WalletDetail.STATUS_已确认)
|
|
|
+ .setUserId(userId)
|
|
|
+ .setAmount(amount)
|
|
|
+ .setGrantsAmount(gAmount)
|
|
|
+ .setOrderNo(tradeNo)
|
|
|
+ .setSource("MANUAL")
|
|
|
+ .setRemark(remark)
|
|
|
+ .setBeforeBalance(account.getBalance())
|
|
|
+ .setAfterBalance(account.getBalance() + amount)
|
|
|
+ .setBeforeGrantsBalance(account.getGrantsBalance())
|
|
|
+ .setAfterGrantsBalance(account.getGrantsBalance() + gAmount)
|
|
|
+ .setTransactionTime(LocalDateTime.now());
|
|
|
+ walletDetailService.save(walletDetail);
|
|
|
+
|
|
|
+ accountService.lambdaUpdate()
|
|
|
+ .setSql("balance = balance + {0}, recharge_balance = recharge_balance + {0}, grants_balance = grants_balance + {1}, discount_amount = discount_amount + {1}",
|
|
|
+ amount, gAmount)
|
|
|
+ .eq(Account::getUserId, userId)
|
|
|
+ .update();
|
|
|
+
|
|
|
+ var stationId = KymCache.INSTANCE.getUserStationId(userId);
|
|
|
+ var splitRecord = new SplitRecord()
|
|
|
+ .setFromStationId(stationId)
|
|
|
+ .setToStationId(stationId)
|
|
|
+ .setTradeNo(tradeNo)
|
|
|
+ .setAmount(amount)
|
|
|
+ .setType(SplitRecord.TYPE_RECHARGE);
|
|
|
+ splitRecordService.save(splitRecord);
|
|
|
+
|
|
|
+ LOGGER.info("线下充值完成:userId={}, amount={}, grantsAmount={}, tradeNo={}", userId, amount, gAmount, tradeNo);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 申请退款
|
|
|
*/
|