@@ -1,48 +1,41 @@
package com.iformall.service.pay.service.share.wx.v3;
import java.text.ParseException;
import java.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.iformall.common.IdWorker;
import com.iformall.domain.dto.WxSharingOrderDto;
import com.iformall.domain.po.*;
import com.iformall.domain.po.base.TenantEntity;
import com.iformall.douyin.pay.enums.MerchantImportStatus;
import com.iformall.enums.*;
import com.iformall.mapper.*;
import net.sf.saxon.trans.Err;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.exception.MallinkException;
import com.iformall.pay.WxPayV3;
import com.iformall.pay.WxPayment;
import com.iformall.pay.WxProfitSharing;
import com.iformall.pay.WxProfitSharingFinishP;
import com.iformall.pay.WxProfitSharingP;
import com.iformall.pay.WxProfitSharingQueryP;
import com.iformall.pay.WxProfitSharingReceiverP;
import com.iformall.service.WxProfitSharingReceiverService;
import com.iformall.service.order.util.OrderHelper;
import com.iformall.service.pay.service.pay.douyin.v1.miniApp.EnumTtPayChannel;
import com.iformall.service.pay.service.share.PayShareAdapterService;
import com.iformall.service.pay.service.share.PayShareBaseAdapterService;
import com.iformall.service.pay.service.share.entity.ShareAccountResult;
import com.iformall.service.pay.service.share.entity.PayShareCalculateAmount;
import com.iformall.service.pay.service.share.entity.PayShareQueryResult;
import com.iformall.service.pay.service.share.entity.PayShareResult;
import com.iformall.service.pay.service.share.entity.ShareNotifyAdapterResult;
import com.iformall.service.pay.service.share.wx.v3.entity.V3PayShareQueryReq;
import com.iformall.service.pay.service.share.wx.v3.entity.V3PayShareReq;
import com.iformall.service.pay.service.share.wx.v3.entity.V3PayShareUnfreezeReq;
import com.iformall.utils.BeanUtils;
import com.iformall.utils.Utility ;
import com.iformall.utils.Ma Util;
import com.iformall.utils.XmlUtil;
import lombok.extern.slf4j.Slf4j;
@@ -57,17 +50,15 @@ public class WxPayShareV3Service extends PayShareBaseAdapterService{
@Lazy
@Autowired
WxProfitSharingReceiverService wxProfitSharingReceiverService;
@Autowired
MaUtil maUtil;
@Override
public Integer getSubsidyType() {
return EnumMerchantSubsidyType.WECHAT.getCode();
}
@Override
public Integer getProfitSharingType() {
return EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT_v2.getCode();
}
@Override
public Integer getSharingOrderInitialStatus() {
return EnumProfitSharingOrderStatus.PROFIT_SHARING_READY.getCode();
@@ -79,6 +70,60 @@ public class WxPayShareV3Service extends PayShareBaseAdapterService{
Integer realRateAmount = OrderHelper.getRealRateAmount(payAmount, payAccount);
return new PayShareCalculateAmount(payAmount,mallCharge,realRateAmount,0);
}
@Override
public WxProfitSharingOrder createSharingOrder(WxSharingOrderDto sharingOrderDto, Integer mchType,Integer share) {
if(EnumYesOrNo.NO.getCode().equals(share)){
throw new MallinkException(ErrorCode.SYS_PARAMETER_TYPE_ERROR.getCode(),"微信分账未开启");
}
//是否已创建分账订单
WxProfitSharingOrder record = new WxProfitSharingOrder();
record.updateTenantInfo(sharingOrderDto);
record.setOrderId(sharingOrderDto.getOrderId());
record = wxProfitSharingOrderMapper.selectOne(new QueryWrapper(record));
if (record == null) {
//创建分账订单
Date currentDate = new Date();
record = new WxProfitSharingOrder();
final IdWorker idworker = IdWorker.get();
record.setId(idworker.nextId());
record.updateTenantInfo(sharingOrderDto);
record.setTransactionId(sharingOrderDto.getTransactionId());
record.setOrderId(sharingOrderDto.getOrderId());
record.setPayAmount(sharingOrderDto.getPayAmount());
record.setMerchantId(sharingOrderDto.getMerchantId());
//直连模式,
if (mchType != EnumPayMchType.DIRECT.getCode()) {
if (null == sharingOrderDto.getPayMerchantId()) {
throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"当前订单的收款商户查询为空");
}
if (StringUtils.isBlank(sharingOrderDto.getPayMerchantMchId())) {
throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"当前订单的收款商户特约商户号进件信息查询为空");
}
record.setPayMerchantId(sharingOrderDto.getPayMerchantId());
record.setPayMerchantMchId(sharingOrderDto.getPayMerchantMchId());
}
record.setSharingStatus(this.getSharingOrderInitialStatus());
record.setCreateTime(currentDate);
record.setUpdateTime(currentDate);
record.setPayTimeStart(sharingOrderDto.getPayTimeStart());
record.setPayTimeEnd(sharingOrderDto.getPayTimeEnd());
record.setType(sharingOrderDto.getType());
wxProfitSharingOrderMapper.insert(record);
}
//如果是总分模式,则需要加上收款账户
if (mchType != EnumPayMchType.TOTAL.getCode()) {
List<WxProfitSharingReceiver> psReceiverList = getReceiverList(sharingOrderDto, sharingOrderDto.getMerchantId(), null);
record.setReceivers(calV3ReceiversAmount(sharingOrderDto, psReceiverList));
wxProfitSharingOrderMapper.updateById(record);
}
return record;
}
@Override
public List<WxProfitSharingReceiver> getReceiverList(TenantEntity tenantEntity, Long merchantId, Integer ttPayWay) {
return wxProfitSharingReceiverService.findReceivers(tenantEntity, merchantId,EnumAppPlat.WX);
}
//无需分账,直接解冻
@Override
@@ -87,26 +132,96 @@ public class WxPayShareV3Service extends PayShareBaseAdapterService{
//直连模式
if (payAccount.getMchType() == EnumPayMchType.DIRECT.getCode()) {
req.setSub_mchid(record.getPayMerchantMchId());
//总分
}else {
req.setSub_mchid(payAccount.getSubMchId());
}
return null;
req.setTransaction_id(transcationId);
req.setOut_order_no(String.valueOf(record.getId()));
req.setDescription(appInfo.getName()+"支付订单:"+record.getOrderId());
WxPayService payService = maUtil.getWxPayService(appInfo, payAccount);
String response = null;;
try {
response = WxPayV3.unfreezn(payService, req);
if (StringUtils.isBlank(response)) {
return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(),"分账解冻请求失败.返回值为空", null,null);
}
} catch (WxPayException e) {
log.error("wx v3 noreciver share error:",e);
return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(),ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()+":"+e.getMessage(), null,null);
}
JSONObject result = JSON.parseObject(response);
return new PayShareResult(true, null,"success", result,result.getString("order_id"));
}
@Override
public PayShareResult haveReciversShare(WxAppinfo appInfo,WxPayAccount payAccount,WxProfitSharingOrder record,String transcationId,JSONArray receivers,EnumProfitSharingOrderType shareType ) throws MallinkException {
//只能直连模式,
if (payAccount.getMchType() != EnumPayMchType.DIRECT.getCode()) {
throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"当前支付版本不支持非直连模式");
}
return null;
V3PayShareReq req = new V3PayShareReq();
//直连模式
if (payAccount.getMchType() == EnumPayMchType.DIRECT.getCode()) {
req.setSub_mchid(record.getPayMerchantMchId());
//总分
}else {
req.setSub_mchid(payAccount.getSubMchId());
}
req.setAppid(appInfo.getParentAppId());
req.setSub_appid(appInfo.getAppId());
req.setTransaction_id(transcationId);
req.setOut_order_no(String.valueOf(record.getId()));
req.setReceivers(receivers);
WxPayService payService = maUtil.getWxPayService(appInfo, payAccount);
String response = null;
try {
response = WxPayV3.share(payService, req);
if (StringUtils.isBlank(response)) {
return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(),"分账请求失败.返回值为空", null,null);
}
} catch (Exception e) {
log.error("wx v3 recivers share error:",e);
return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(), ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()+":"+e.getMessage(), null, null);
}
JSONObject result = JSON.parseObject(response);
return new PayShareResult(true, null, "success",result, result.getString("order_id"));
}
@Override
public PayShareQueryResult shareQuery(WxAppinfo appInfo, WxPayAccount payAccount, WxProfitSharingOrder record) throws MallinkException {
return null;
V3PayShareQueryReq req = new V3PayShareQueryReq();
//直连模式
if (payAccount.getMchType() == EnumPayMchType.DIRECT.getCode()) {
req.setSub_mchid(record.getPayMerchantMchId());
//总分
}else {
req.setSub_mchid(payAccount.getSubMchId());
}
req.setTransaction_id(record.getTransactionId());
req.setOut_order_no(String.valueOf(record.getId()));
WxPayService payService = maUtil.getWxPayService(appInfo, payAccount);
String response = null;
try {
response = WxPayV3.shareQuery(payService, req);
if (StringUtils.isBlank(response)) {
throw new MallinkException(EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(),"分账请求失败.返回值为空");
}
}catch (Exception e){
throw new MallinkException(ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getCode(), ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getMessage()+e.getMessage());
}
JSONObject result = JSON.parseObject(response);
String return_code = result.getString("state");
if (!"FINISHED".equals(return_code)) {
throw new MallinkException(ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getCode(), "分账状态:"+return_code);
}
String out_order_no = result.getString("out_order_no");
if (StringUtils.isBlank(out_order_no) || !out_order_no.equals(record.getId().toString())){
throw new MallinkException(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID.getCode(),"out_order_no:["+out_order_no+"] WxProfitSharingOrder:["+record.getId()+"]");
}
111111111111111
return new PayShareQueryResult(true, 200, "success",result, JSON.toJSONString(result.get("receivers")));
}
@@ -164,66 +279,4 @@ public class WxPayShareV3Service extends PayShareBaseAdapterService{
return new ResultData(ErrorCode.SYS_PARAMETER_TYPE_ERROR.getCode(),"微信分账无需处理");
}
@Override
public WxProfitSharingOrder createSharingOrder(WxSharingOrderDto sharingOrderDto, Integer mchType,Integer share) {
if(EnumYesOrNo.NO.getCode().equals(share)){
throw new MallinkException(ErrorCode.SYS_PARAMETER_TYPE_ERROR.getCode(),"微信分账未开启");
}
//是否已创建分账订单
WxProfitSharingOrder record = new WxProfitSharingOrder();
record.updateTenantInfo(sharingOrderDto);
record.setOrderId(sharingOrderDto.getOrderId());
record = wxProfitSharingOrderMapper.selectOne(new QueryWrapper(record));
if (record == null) {
//创建分账订单
Date currentDate = new Date();
record = new WxProfitSharingOrder();
final IdWorker idworker = IdWorker.get();
record.setId(idworker.nextId());
record.updateTenantInfo(sharingOrderDto);
record.setTransactionId(sharingOrderDto.getTransactionId());
record.setOrderId(sharingOrderDto.getOrderId());
record.setPayAmount(sharingOrderDto.getPayAmount());
record.setMerchantId(sharingOrderDto.getMerchantId());
//直连模式,
if (mchType != EnumPayMchType.DIRECT.getCode()) {
if (null == sharingOrderDto.getPayMerchantId()) {
throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"当前订单的收款商户查询为空");
}
if (StringUtils.isBlank(sharingOrderDto.getPayMerchantMchId())) {
throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"当前订单的收款商户特约商户号进件信息查询为空");
}
record.setPayMerchantId(sharingOrderDto.getPayMerchantId());
record.setPayMerchantMchId(sharingOrderDto.getPayMerchantMchId());
}
record.setSharingStatus(this.getSharingOrderInitialStatus());
record.setCreateTime(currentDate);
record.setUpdateTime(currentDate);
record.setPayTimeStart(sharingOrderDto.getPayTimeStart());
record.setPayTimeEnd(sharingOrderDto.getPayTimeEnd());
record.setType(sharingOrderDto.getType());
wxProfitSharingOrderMapper.insert(record);
}
//如果是总分模式,则需要加上收款账户
if (mchType != EnumPayMchType.TOTAL.getCode()) {
List<WxProfitSharingReceiver> psReceiverList = getReceiverList(sharingOrderDto, sharingOrderDto.getMerchantId(), null);
record.setReceivers(calReceiversAmount(sharingOrderDto, psReceiverList));
wxProfitSharingOrderMapper.updateById(record);
}
return record;
}
@Override
public List<WxProfitSharingReceiver> getReceiverList(TenantEntity tenantEntity, Long merchantId, Integer ttPayWay) {
//查询进件特约商户
WxProfitSharingReceiver receiver = wxProfitSharingReceiverService.findReceiver(tenantEntity, merchantId, EnumAppPlat.WX, EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT_v2);
if (null == receiver) {
return null;
}
List<WxProfitSharingReceiver> retList = new ArrayList<WxProfitSharingReceiver>();
retList.add(receiver);
return retList;
}
}