|
|
@@ -2,6 +2,7 @@ package com.zhongshu.payment.server.core.service;
|
|
|
|
|
|
import ch.qos.logback.core.util.StringUtil;
|
|
|
import com.github.microservice.auth.security.helper.AuthHelper;
|
|
|
+import com.github.microservice.core.helper.ApplicationContextHolder;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
|
|
|
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
|
|
|
@@ -18,41 +19,49 @@ import com.zhongshu.payment.client.model.PrePayModel;
|
|
|
import com.zhongshu.payment.client.model.RechargeRecordModel;
|
|
|
import com.zhongshu.payment.client.model.param.OrderParam;
|
|
|
import com.zhongshu.payment.client.model.param.RechargeParam;
|
|
|
+import com.zhongshu.payment.client.payModel.commn.PayNotityParam;
|
|
|
import com.zhongshu.payment.client.types.RechargeState;
|
|
|
import com.zhongshu.payment.client.types.TradeType;
|
|
|
import com.zhongshu.payment.client.types.WalletState;
|
|
|
import com.zhongshu.payment.client.types.WalletType;
|
|
|
+import com.zhongshu.payment.server.core.dao.ExecuteMethodInfoDao;
|
|
|
import com.zhongshu.payment.server.core.dao.RechargeRecordDao;
|
|
|
import com.zhongshu.payment.server.core.dao.WalletDao;
|
|
|
import com.zhongshu.payment.server.core.dao.WxPayConfigDao;
|
|
|
import com.zhongshu.payment.server.core.dataConfig.TimeConfig;
|
|
|
+import com.zhongshu.payment.server.core.domain.unionFrictionlessPay.ExecuteMethodInfo;
|
|
|
import com.zhongshu.payment.server.core.domain.wallet.RechargeRecord;
|
|
|
import com.zhongshu.payment.server.core.domain.wallet.Wallet;
|
|
|
+import com.zhongshu.payment.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.payment.server.core.service.impl.WalletFeignServiceImpl;
|
|
|
+import com.zhongshu.payment.server.core.service.pay.SuperPayService;
|
|
|
import com.zhongshu.payment.server.core.service.pay.impl.wxPaymentV3.WxPaymentMainService;
|
|
|
import com.zhongshu.payment.server.core.utils.CommonUtil;
|
|
|
import com.zhongshu.payment.server.core.utils.DateUtils;
|
|
|
import com.zhongshu.payment.server.core.utils.wx.WechatCUtil;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
- * 充值服务
|
|
|
+ * 交易服务
|
|
|
*
|
|
|
* @author wjf
|
|
|
* @date 2024/7/25
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-public class RechargeService {
|
|
|
+public class RechargeService extends SuperService {
|
|
|
|
|
|
@Autowired
|
|
|
AuthHelper authHelper;
|
|
|
@@ -81,38 +90,31 @@ public class RechargeService {
|
|
|
@Autowired
|
|
|
OrganizationFeignService organizationFeignService;
|
|
|
|
|
|
-// private String buildOutTradeNo(){
|
|
|
-//
|
|
|
-// if (rechargeRecordDao.existsByOutTradeNo(outTradeNo)){
|
|
|
-// buildOutTradeNo();
|
|
|
-// }
|
|
|
-// return outTradeNo;
|
|
|
-// }
|
|
|
+ @Autowired
|
|
|
+ ExecuteMethodInfoDao executeMethodInfoDao;
|
|
|
|
|
|
/**
|
|
|
- * 创建充值订单
|
|
|
+ * 创建充值本地订单
|
|
|
*/
|
|
|
- public Object create(RechargeParam param) {
|
|
|
+ public Object createLocalOrder(RechargeParam param) {
|
|
|
String userId = authHelper.getCurrentUser().getUserId();
|
|
|
Wallet userWallet = walletDao.findTop1ById(param.getWalletId());
|
|
|
//判断钱包是否初始化
|
|
|
if (userWallet == null) {
|
|
|
return ResultContent.buildFail("用户钱包未开通");
|
|
|
}
|
|
|
-
|
|
|
if (userWallet.getDataState().equals(WalletState.Disable)) {
|
|
|
return ResultContent.buildFail("钱包已停用");
|
|
|
}
|
|
|
-
|
|
|
if (userWallet.getWalletType().equals(WalletType.Shop)) {
|
|
|
return ResultContent.buildFail("商户提现暂未开通线上充值功能");
|
|
|
}
|
|
|
-
|
|
|
// 创建充值订单
|
|
|
RechargeRecord rechargeRecord = new RechargeRecord();
|
|
|
org.springframework.beans.BeanUtils.copyProperties(param, rechargeRecord, "total");
|
|
|
String outTradeNo = CommonUtil.UUID();
|
|
|
rechargeRecord.setOutTradeNo(outTradeNo);
|
|
|
+ // 金额
|
|
|
rechargeRecord.setTotal(BigDecimal.valueOf(param.getTotal()));
|
|
|
rechargeRecord.setOid(userWallet.getOid());
|
|
|
rechargeRecord.setTradeType(TradeType.Charge);
|
|
|
@@ -133,7 +135,6 @@ public class RechargeService {
|
|
|
long createTIme = System.currentTimeMillis();
|
|
|
rechargeRecord.setCreateTime(createTIme);
|
|
|
rechargeRecord.setTimes();
|
|
|
-
|
|
|
// 订单过期时间
|
|
|
rechargeRecord.setExpireTime(createTIme + TimeConfig.rechargeExpireTime);
|
|
|
RechargeRecord record = rechargeRecordDao.save(rechargeRecord);
|
|
|
@@ -141,22 +142,11 @@ public class RechargeService {
|
|
|
return ResultContent.buildSuccess(model);
|
|
|
}
|
|
|
|
|
|
- private RechargeRecordModel toModel(RechargeRecord record) {
|
|
|
- RechargeRecordModel model = new RechargeRecordModel();
|
|
|
- if (record != null) {
|
|
|
- BeanUtils.copyProperties(record, model, "total");
|
|
|
- model.setTotal(record.getTotal().intValue());
|
|
|
- model.setPrePayModel(record.getPrePayModel());
|
|
|
- model.setUserInfo(record.getUserInfo());
|
|
|
- }
|
|
|
- return model;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * 下单 (微信下单)
|
|
|
+ * 创建渠道订单
|
|
|
*/
|
|
|
@Transactional
|
|
|
- public ResultContent order(OrderParam param) {
|
|
|
+ public ResultContent createChannelOrder(OrderParam param) {
|
|
|
RechargeRecord record = rechargeRecordDao.findByOutTradeNo(param.getOutTradeNo());
|
|
|
if (record == null) {
|
|
|
return ResultContent.buildFail("充值订单:" + param.getOutTradeNo() + "不存在");
|
|
|
@@ -171,42 +161,36 @@ public class RechargeService {
|
|
|
rechargeRecordDao.save(record);
|
|
|
return ResultContent.buildFail("订单支付过期,已关闭");
|
|
|
}
|
|
|
-
|
|
|
if (StringUtil.isNullOrEmpty(param.getOpenid())) {
|
|
|
return ResultContent.buildFail("缺少用户授权参数openId");
|
|
|
}
|
|
|
-
|
|
|
- // 根据appId 得到微信支付配置
|
|
|
- ProjectWxPayParam wxPayParam = new ProjectWxPayParam();
|
|
|
- wxPayParam.setAppId(record.getAppid());
|
|
|
- ResultContent<WxPayConfigModel> paySetResult = paySettingFeignService.getProjectWxPayConfig(wxPayParam);
|
|
|
- if (paySetResult.isFailed()) {
|
|
|
- return ResultContent.buildFail("获取微信支付配置失败");
|
|
|
+ ExecuteMethodInfo executeMethodInfo = executeMethodInfoDao.findTopByPaymentType(record.getPaymentType());
|
|
|
+ if (ObjectUtils.isEmpty(executeMethodInfo)) {
|
|
|
+ return ResultContent.buildFail(String.format("", record.getPaymentType().getRemark()));
|
|
|
}
|
|
|
+ SuperPayService service = (SuperPayService) ApplicationContextHolder.getContext().getBean(executeMethodInfo.getBeanName());
|
|
|
+ return service.createChannelOrder(param);
|
|
|
+ }
|
|
|
|
|
|
- //调用微信支付--下单接口
|
|
|
- PrepayRequest request = new PrepayRequest();
|
|
|
- Amount amount = new Amount();
|
|
|
- amount.setTotal(record.getTotal().intValue());
|
|
|
- request.setAmount(amount);
|
|
|
- request.setDescription(record.getDescription());
|
|
|
- request.setOutTradeNo(param.getOutTradeNo());
|
|
|
- //设置交易失效时间,15分钟
|
|
|
- request.setTimeExpire(DateUtils.paresTime(record.getExpireTime(), DateUtils.patternWx));
|
|
|
- Payer payer = new Payer();
|
|
|
- payer.setOpenid(param.getOpenid());
|
|
|
- request.setPayer(payer);
|
|
|
- // 微信下单
|
|
|
- ResultContent<PrePayModel> prepayResult = wxPaymentService.prepay(request, paySetResult.getContent());
|
|
|
- if (prepayResult.isFailed()) {
|
|
|
- return prepayResult;
|
|
|
+ /**
|
|
|
+ * 渠道支付回调
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param outTradeNo
|
|
|
+ */
|
|
|
+ public ResultContent payNotify(HttpServletRequest request, String outTradeNo) {
|
|
|
+ RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
|
|
|
+ if (record == null) {
|
|
|
+ return ResultContent.buildFail("充值订单:" + outTradeNo + "不存在");
|
|
|
}
|
|
|
- record.setOpenid(param.getOpenid());
|
|
|
- record.setPrePayModel(prepayResult.getContent());
|
|
|
- record.setRechargeState(RechargeState.NOTPAY);
|
|
|
- record.setCommit(true);
|
|
|
- rechargeRecordDao.save(record);
|
|
|
- return prepayResult;
|
|
|
+ PayNotityParam param = new PayNotityParam();
|
|
|
+ param.setOutTradeNo(outTradeNo);
|
|
|
+ ExecuteMethodInfo executeMethodInfo = executeMethodInfoDao.findTopByPaymentType(record.getPaymentType());
|
|
|
+ if (ObjectUtils.isEmpty(executeMethodInfo)) {
|
|
|
+ return ResultContent.buildFail(String.format("", record.getPaymentType().getRemark()));
|
|
|
+ }
|
|
|
+ SuperPayService service = (SuperPayService) ApplicationContextHolder.getContext().getBean(executeMethodInfo.getBeanName());
|
|
|
+ return service.payNotify(request, param);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -304,17 +288,14 @@ public class RechargeService {
|
|
|
return ResultContent.buildSuccess(toModel(record));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取用户openid
|
|
|
- */
|
|
|
- @SneakyThrows
|
|
|
- public String getOpenId(String jscode) {
|
|
|
- MiniAppUserInfoVo miniAppUserInfo = wechatCUtil.getMiniAppUserInfo(jscode);
|
|
|
- if (miniAppUserInfo == null || io.netty.util.internal.StringUtil.isNullOrEmpty(miniAppUserInfo.getOpenid())) {
|
|
|
- log.info("无法获取用户信息");
|
|
|
- return null;
|
|
|
+ private RechargeRecordModel toModel(RechargeRecord record) {
|
|
|
+ RechargeRecordModel model = new RechargeRecordModel();
|
|
|
+ if (record != null) {
|
|
|
+ BeanUtils.copyProperties(record, model, "total");
|
|
|
+ model.setTotal(record.getTotal().intValue());
|
|
|
+ model.setPrePayModel(record.getPrePayModel());
|
|
|
+ model.setUserInfo(record.getUserInfo());
|
|
|
}
|
|
|
- log.info("用户openid:{}", miniAppUserInfo.getOpenid());
|
|
|
- return miniAppUserInfo.getOpenid();
|
|
|
+ return model;
|
|
|
}
|
|
|
}
|