|
|
@@ -0,0 +1,255 @@
|
|
|
+package com.zhongshu.payment.server.core.service;
|
|
|
+
|
|
|
+import com.github.microservice.auth.security.helper.AuthHelper;
|
|
|
+import com.github.microservice.components.data.base.util.PageEntityUtil;
|
|
|
+import com.wechat.pay.java.service.transferbatch.model.*;
|
|
|
+import com.zhongshu.card.client.model.feign.ProjectWxPayParam;
|
|
|
+import com.zhongshu.card.client.model.org.OrgUserDetailParam;
|
|
|
+import com.zhongshu.card.client.model.org.OrganizationUserModel;
|
|
|
+import com.zhongshu.card.client.model.payment.paySetting.WxPayConfigModel;
|
|
|
+import com.zhongshu.card.client.service.OrganizationFeignService;
|
|
|
+import com.zhongshu.card.client.service.ProjectPaySettingFeignService;
|
|
|
+import com.zhongshu.payment.client.model.RechargeRecordModel;
|
|
|
+import com.zhongshu.payment.client.model.TransferModel;
|
|
|
+import com.zhongshu.payment.client.model.param.AmountUpdateParam;
|
|
|
+import com.zhongshu.payment.client.model.param.ExamineParam;
|
|
|
+import com.zhongshu.payment.client.model.param.RechargeParam;
|
|
|
+import com.zhongshu.payment.client.model.param.TransferSearchParam;
|
|
|
+import com.zhongshu.payment.client.ret.ResultContent;
|
|
|
+import com.zhongshu.payment.client.types.RechargeState;
|
|
|
+import com.zhongshu.payment.client.types.TradeType;
|
|
|
+import com.zhongshu.payment.server.core.dao.RechargeRecordDao;
|
|
|
+import com.zhongshu.payment.server.core.dao.WalletDao;
|
|
|
+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.impl.WalletFeignServiceImpl;
|
|
|
+import com.zhongshu.payment.server.core.service.wxPaymentV3.WxTransferBatchService;
|
|
|
+import com.zhongshu.payment.server.core.utils.CommonUtil;
|
|
|
+import io.netty.util.internal.StringUtil;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 提现
|
|
|
+ * @author wjf
|
|
|
+ * @date 2024/7/29
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class TransferService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WalletDao walletDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RechargeRecordDao rechargeRecordDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WxTransferBatchService wxTransferBatchService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProjectPaySettingFeignService paySettingFeignService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WalletFeignServiceImpl walletFeignService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AuthHelper authHelper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OrganizationFeignService organizationFeignService;
|
|
|
+
|
|
|
+ /** 发起提现 */
|
|
|
+ @Transactional
|
|
|
+ public Object create(RechargeParam param){
|
|
|
+ String userId = authHelper.getCurrentUser().getUserId();
|
|
|
+ Wallet userWallet = walletDao.findTop1ById(param.getWalletId());
|
|
|
+ //判断钱包是否初始化
|
|
|
+ if (userWallet==null){
|
|
|
+ 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.Take);
|
|
|
+ rechargeRecord.setRechargeState(RechargeState.TakeReview);
|
|
|
+ rechargeRecord.setWallet(userWallet);
|
|
|
+ rechargeRecord.setUserId(userId);
|
|
|
+ //设置人员信息
|
|
|
+ OrgUserDetailParam orgUserDetailParam = new OrgUserDetailParam();
|
|
|
+ orgUserDetailParam.setOid(userWallet.getOid());
|
|
|
+ orgUserDetailParam.setUserId(userWallet.getUserId());
|
|
|
+ com.zhongshu.card.client.ret.ResultContent<OrganizationUserModel> orgUserDetail = organizationFeignService.getOrgUserDetail(orgUserDetailParam);
|
|
|
+ if (orgUserDetail.isFailed()){
|
|
|
+ return ResultContent.buildFail("获取用户信息失败");
|
|
|
+ }
|
|
|
+ rechargeRecord.setUserInfo(orgUserDetail.getContent());
|
|
|
+
|
|
|
+ AmountUpdateParam amountUpdateParam = new AmountUpdateParam();
|
|
|
+ amountUpdateParam.setOutTradeNo(rechargeRecord.getOutTradeNo());
|
|
|
+ amountUpdateParam.setOid(rechargeRecord.getOid());
|
|
|
+ amountUpdateParam.setUserId(userId);
|
|
|
+ amountUpdateParam.setTotal(rechargeRecord.getTotal());
|
|
|
+ amountUpdateParam.setSchoolId(rechargeRecord.getSchoolId());
|
|
|
+ walletFeignService.commitTake(amountUpdateParam);
|
|
|
+
|
|
|
+ RechargeRecord record = rechargeRecordDao.save(rechargeRecord);
|
|
|
+ return ResultContent.buildSuccess(toModel(record));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Object passTransfer(ExamineParam param){
|
|
|
+ RechargeRecord record = rechargeRecordDao.findByOutTradeNo(param.getOutTradeNo());
|
|
|
+ if (record==null){
|
|
|
+ return ResultContent.buildFail("充值订单:"+param.getOutTradeNo()+"不存在");
|
|
|
+ }
|
|
|
+ Wallet userWallet = walletDao.findTop1ById(record.getWallet().getId());
|
|
|
+
|
|
|
+ //判断微信支付配置
|
|
|
+ ProjectWxPayParam wxPayParam = new ProjectWxPayParam();
|
|
|
+ wxPayParam.setAppId(record.getAppid());
|
|
|
+ com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> paySetResult = paySettingFeignService.getProjectWxPayConfig(wxPayParam);
|
|
|
+ if (paySetResult.isFailed()){
|
|
|
+ return ResultContent.buildFail("获取微信支付配置失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtil.isNullOrEmpty(param.getRemark())){
|
|
|
+ param.setRemark("余额提现");
|
|
|
+ }
|
|
|
+
|
|
|
+ InitiateBatchTransferRequest request = new InitiateBatchTransferRequest();
|
|
|
+ request.setAppid(record.getAppid());
|
|
|
+
|
|
|
+ request.setOutBatchNo(record.getId());
|
|
|
+ request.setBatchRemark(param.getRemark());
|
|
|
+ request.setTotalAmount(record.getTotal().longValue());
|
|
|
+ request.setTotalNum(1);
|
|
|
+
|
|
|
+ TransferDetailInput transferDetailInput = new TransferDetailInput();
|
|
|
+ transferDetailInput.setOpenid(userWallet.getOpenid());
|
|
|
+ transferDetailInput.setOutDetailNo(record.getOutTradeNo());
|
|
|
+ transferDetailInput.setTransferAmount(record.getTotal().longValue());
|
|
|
+ transferDetailInput.setTransferRemark(param.getRemark());
|
|
|
+ if(record.getTotal().compareTo(BigDecimal.valueOf(2000L))>=0){
|
|
|
+ //获取用户信息
|
|
|
+ transferDetailInput.setUserName("用户名");
|
|
|
+ }
|
|
|
+ if (record.getTotal().compareTo(BigDecimal.valueOf(0.3))<0){
|
|
|
+ transferDetailInput.setUserName(null);
|
|
|
+ }
|
|
|
+ request.setTransferDetailList(List.of(transferDetailInput));
|
|
|
+ //设置转账场景id
|
|
|
+ request.setTransferSceneId("");
|
|
|
+
|
|
|
+ InitiateBatchTransferResponse initiateBatchTransferResponse = wxTransferBatchService.InitiateBatchTransferResponse(request, paySetResult.getContent());
|
|
|
+ TransferModel transferModel = new TransferModel();
|
|
|
+ BeanUtils.copyProperties(initiateBatchTransferResponse, transferModel);
|
|
|
+ record.setTransferModel(transferModel);
|
|
|
+ if (initiateBatchTransferResponse.getBatchStatus().equals("ACCEPTED")){
|
|
|
+ record.setRechargeState(RechargeState.RECHARGING);
|
|
|
+ }
|
|
|
+ if (initiateBatchTransferResponse.getBatchStatus().equals("PROCESSING")){
|
|
|
+ record.setRechargeState(RechargeState.Taking);
|
|
|
+ }
|
|
|
+ if (initiateBatchTransferResponse.getBatchStatus().equals("FINISHED")){
|
|
|
+ record.setRechargeState(RechargeState.TakeSuccess);
|
|
|
+ }
|
|
|
+ if (initiateBatchTransferResponse.getBatchStatus().equals("CLOSED")){
|
|
|
+ record.setRechargeState(RechargeState.CLOSED);
|
|
|
+ }
|
|
|
+ rechargeRecordDao.save(record);
|
|
|
+ return ResultContent.buildSuccess(record);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Object refuseTransfer(ExamineParam param){
|
|
|
+ RechargeRecord record = rechargeRecordDao.findByOutTradeNo(param.getOutTradeNo());
|
|
|
+ if (record==null){
|
|
|
+ return ResultContent.buildFail("充值订单:"+param.getOutTradeNo()+"不存在");
|
|
|
+ }
|
|
|
+ record.setRechargeState(RechargeState.TakeRefuse);
|
|
|
+ record.setRemark(param.getRemark());
|
|
|
+ rechargeRecordDao.save(record);
|
|
|
+
|
|
|
+ AmountUpdateParam amountUpdateParam = new AmountUpdateParam();
|
|
|
+ amountUpdateParam.setOutTradeNo(record.getOutTradeNo());
|
|
|
+ amountUpdateParam.setOid(record.getOid());
|
|
|
+ amountUpdateParam.setUserId(record.getUserId());
|
|
|
+ amountUpdateParam.setTotal(record.getTotal());
|
|
|
+ amountUpdateParam.setSchoolId(record.getSchoolId());
|
|
|
+ walletFeignService.failTake(amountUpdateParam);
|
|
|
+ return ResultContent.buildSuccess(toModel(record));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object pageTransfer(Pageable pageable, TransferSearchParam param){
|
|
|
+ Page<RechargeRecord> page = rechargeRecordDao.pageTransfer(pageable, param);
|
|
|
+ Page<RechargeRecordModel> pageModel = PageEntityUtil.concurrent2PageModel(page, this::toModel);
|
|
|
+ return ResultContent.buildContent(pageModel);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Object getTransferState(String outTradeNo){
|
|
|
+ RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
|
|
|
+
|
|
|
+ ProjectWxPayParam param = new ProjectWxPayParam();
|
|
|
+ param.setAppId(record.getAppid());
|
|
|
+ com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> configResult = paySettingFeignService.getProjectWxPayConfig(param);
|
|
|
+ if (configResult.isFailed()){
|
|
|
+ return ResultContent.buildFail("获取微信支付配置失败");
|
|
|
+ }
|
|
|
+ GetTransferDetailByOutNoRequest request = new GetTransferDetailByOutNoRequest();
|
|
|
+ request.setOutBatchNo(record.getId());
|
|
|
+ request.setOutDetailNo(record.getOutTradeNo());
|
|
|
+ TransferDetailEntity transferDetailByOutNo = wxTransferBatchService.getTransferDetailByOutNo(request, configResult.getContent());
|
|
|
+ if (transferDetailByOutNo.getDetailStatus().equals("INIT")){
|
|
|
+ record.setRechargeState(RechargeState.Taking);
|
|
|
+ record.setRechargeStateDesc("系统转账校验中");
|
|
|
+ }
|
|
|
+ if (transferDetailByOutNo.getDetailStatus().equals("WAIT_PAY")){
|
|
|
+ record.setRechargeState(RechargeState.Taking);
|
|
|
+ record.setRechargeStateDesc("待商户确认");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (transferDetailByOutNo.getDetailStatus().equals("PROCESSING")){
|
|
|
+ record.setRechargeState(RechargeState.Taking);
|
|
|
+ record.setRechargeStateDesc("正在处理中");
|
|
|
+ }
|
|
|
+ if (transferDetailByOutNo.getDetailStatus().equals("SUCCESS")){
|
|
|
+ record.setRechargeState(RechargeState.TakeSuccess);
|
|
|
+ record.setRechargeStateDesc("提现成功");
|
|
|
+ }
|
|
|
+ if (transferDetailByOutNo.getDetailStatus().equals("FAIL")){
|
|
|
+ record.setRechargeState(RechargeState.TakeFail);
|
|
|
+ record.setRechargeStateDesc(transferDetailByOutNo.getFailReason().name());
|
|
|
+ AmountUpdateParam amountUpdateParam = new AmountUpdateParam();
|
|
|
+ amountUpdateParam.setOutTradeNo(record.getOutTradeNo());
|
|
|
+ amountUpdateParam.setOid(record.getOid());
|
|
|
+ amountUpdateParam.setUserId(record.getUserId());
|
|
|
+ amountUpdateParam.setTotal(record.getTotal());
|
|
|
+ amountUpdateParam.setSchoolId(record.getSchoolId());
|
|
|
+ walletFeignService.failTake(amountUpdateParam);
|
|
|
+ }
|
|
|
+ rechargeRecordDao.save(record);
|
|
|
+ return ResultContent.buildSuccess(toModel(record));
|
|
|
+ }
|
|
|
+
|
|
|
+ private RechargeRecordModel toModel(RechargeRecord record) {
|
|
|
+ RechargeRecordModel model = new RechargeRecordModel();
|
|
|
+ if (record!=null){
|
|
|
+ org.springframework.beans.BeanUtils.copyProperties(record, model, "total");
|
|
|
+ model.setTotal(record.getTotal().intValue());
|
|
|
+ model.setUserInfo(record.getUserInfo());
|
|
|
+ }
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+}
|