diff --git a/mallinkService/src/main/java/com/iformall/pay/WxPayV3.java b/mallinkService/src/main/java/com/iformall/pay/WxPayV3.java index abfab8a60..0e601eadb 100644 --- a/mallinkService/src/main/java/com/iformall/pay/WxPayV3.java +++ b/mallinkService/src/main/java/com/iformall/pay/WxPayV3.java @@ -21,6 +21,8 @@ import com.iformall.service.pay.service.pay.wx.sft.entity.SFTPayQueryReq; import com.iformall.service.pay.service.pay.wx.v3.entity.V3CombinePayCommonMiniAppReq; import com.iformall.service.pay.service.pay.wx.v3.entity.V3PayCommonMiniAppReq; import com.iformall.service.pay.service.pay.wx.v3.entity.V3PayQueryReq; +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 org.slf4j.Logger; @@ -46,10 +48,12 @@ public class WxPayV3 { private static final String PAY_COMMON_CLOSE_ORDER_URL = "https://api.mch.weixin.qq.com/v3/pay/partner/transactions/out-trade-no/%s/close"; //小程序下单-合单支付-关闭订单 private static final String PAY_COMBINE_CLOSE_ORDER_URL = "https://api.mch.weixin.qq.com/v3/combine-transactions/out-trade-no/%s/close"; - //小程序下单-解冻 + //小程序下单-分账-解冻 private static final String UNFREEZE_URL = "https://api.mch.weixin.qq.com/v3/profitsharing/orders/unfreeze"; //小程序下单-分账 - private static final String SHARE_URL = "https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/orders"; + private static final String SHARE_URL = "https://api.mch.weixin.qq.com/v3/profitsharing/orders"; + //小程序下单-分账-查询 + private static final String SHARE_QUERY_URL = "https://api.mch.weixin.qq.com/v3/profitsharing/orders/%s"; private WxPayV3() { @@ -165,9 +169,23 @@ public class WxPayV3 { * 分账-解冻https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_1_5.shtml * @throws WxPayException */ - public static String payCombineClose(WxPayService payService,V3PayShareUnfreezeReq req) throws WxPayException { + public static String unfreezn(WxPayService payService,V3PayShareUnfreezeReq req) throws WxPayException { return payService.postV3WithWechatpaySerial(UNFREEZE_URL, JSON.toJSONString(req)); } + /** + * 分账 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_1_1.shtml + * @throws WxPayException + */ + public static String share(WxPayService payService,V3PayShareReq req) throws WxPayException { + return payService.postV3WithWechatpaySerial(SHARE_URL, JSON.toJSONString(req)); + } + /** + * 分账查询 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_1_2.shtml + * @throws WxPayException + */ + public static String shareQuery(WxPayService payService,V3PayShareQueryReq req) throws WxPayException { + return payService.postV3WithWechatpaySerial(String.format(SHARE_QUERY_URL, req.getOut_order_no()), JSON.toJSONString(req)); + } } diff --git a/mallinkService/src/main/java/com/iformall/service/WxProfitSharingReceiverService.java b/mallinkService/src/main/java/com/iformall/service/WxProfitSharingReceiverService.java index d088e906e..631a95323 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxProfitSharingReceiverService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxProfitSharingReceiverService.java @@ -45,7 +45,7 @@ public interface WxProfitSharingReceiverService { WxProfitSharingReceiver findReceiver(TenantEntity tenantEntity,Long merchantId, EnumAppPlat plat,EnumProfitSharingType sharingType); - List findReceivers(TenantEntity tenantEntity,Long merchantId); + List findReceivers(TenantEntity tenantEntity,Long merchantId,EnumAppPlat plat); ResultData addReceiver(WxMerchant merchant, WxProfitSharingReceiver receiver, List delList); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxMerchantServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxMerchantServiceImpl.java index da49fb64f..1eb11e3c9 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxMerchantServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxMerchantServiceImpl.java @@ -355,7 +355,7 @@ public class WxMerchantServiceImpl implements WxMerchantService { } //分账信息 - List receiver = wxProfitSharingReceiverService.findReceivers(wxMerchant,wxMerchant.getId()); + List receiver = wxProfitSharingReceiverService.findReceivers(wxMerchant,wxMerchant.getId(),null); if (receiver != null) { wxMerchant.setWxProfitSharingReceiver(receiver); } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java index a4bf7bd0a..a4f722592 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java @@ -665,7 +665,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ WxProfitSharingReceiver psReceiverQ = new WxProfitSharingReceiver(); psReceiverQ.updateTenantInfo(payAccount); psReceiverQ.setMerchantId(sharingOrder.getMerchantId()); - psReceiverQ.setSharingType(payServiceFactory.getPayShareAdapterService(dto.getPayWay(),payAccount.getPayVersion()).getProfitSharingType()); + psReceiverQ.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode()); psReceiverQ.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode()); List psReceiverList = wxProfitSharingReceiverMapper.findList(psReceiverQ); if (null == psReceiverList || psReceiverList.size() !=1 ) { diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingReceiverServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingReceiverServiceImpl.java index 25e80c5ef..e67f1e768 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingReceiverServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingReceiverServiceImpl.java @@ -130,10 +130,13 @@ public class WxProfitSharingReceiverServiceImpl implements WxProfitSharingReceiv } @Override - public List findReceivers(TenantEntity tenantEntity, Long merchantId) { + public List findReceivers(TenantEntity tenantEntity, Long merchantId,EnumAppPlat plat) { WxProfitSharingReceiver receiver = new WxProfitSharingReceiver(); receiver.updateTenantInfo(tenantEntity); receiver.setMerchantId(merchantId); + if (null != plat) { + receiver.setPlat(plat.getCode()); + } receiver.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode()); return wxProfitSharingReceiverMapper.findList(receiver); } diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareAdapterService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareAdapterService.java index d0b53245a..571cf5e33 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareAdapterService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareAdapterService.java @@ -8,6 +8,7 @@ import com.iformall.common.ResultData; import com.iformall.domain.dto.WxSharingOrderDto; import com.iformall.domain.po.*; import com.iformall.domain.po.base.TenantEntity; +import com.iformall.enums.EnumPayMchType; import com.iformall.enums.EnumPayWay; import com.iformall.enums.EnumProfitSharingOrderType; import com.iformall.enums.EnumProfitSharingReceiverType; @@ -30,12 +31,6 @@ public interface PayShareAdapterService { */ public Integer getSubsidyType(); - /** - * 分账类型 - * @return - */ - public Integer getProfitSharingType(); - /** * 获取分账订单初始状态 * @return diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareBaseAdapterService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareBaseAdapterService.java index 2c399441c..9053b492d 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareBaseAdapterService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareBaseAdapterService.java @@ -62,5 +62,49 @@ public abstract class PayShareBaseAdapterService implements PayShareAdapterServi }); return receivers.toJSONString(); } + + protected String calV3ReceiversAmount(WxSharingOrderDto sharingOrderDto,List psReceiverList) { + if (psReceiverList != null && psReceiverList.size() > 50) { + throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID); + } + + if (psReceiverList == null || psReceiverList.size() == 0) { //没有分账账户, + throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_UNKNOWN); + } + + if (psReceiverList.size() > 1 &&//计算分账账户合法性 + psReceiverList.stream().filter(r->(r.getParameter() == null)).count() > 0) { + throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID); + } + + //计算分账金额合法性 + psReceiverList.stream().forEach(r-> + { + if (r.getParameter() == null){ + r.setSharingAmount(sharingOrderDto.getShareAmount()); + r.setRateAmount(sharingOrderDto.getRateAmount()); + r.setRealRateAmount(sharingOrderDto.getRealRateAmount()); + r.setCommissionAmount(sharingOrderDto.getCommissionAmount()); + } else { + r.setSharingAmount(sharingOrderDto.getShareAmount()*Integer.valueOf(r.getParameter())/10000); + } + }); + if (psReceiverList.stream().mapToInt(r->r.getSharingAmount()).sum() > sharingOrderDto.getShareAmount()){ + throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_UNKNOWN); + } + + // 添加分账接收方 + JSONArray receivers = new JSONArray(); + psReceiverList.stream().forEach(receiver->{ + JSONObject jo = new JSONObject(); + jo.put("type",this.getShareAccount(receiver.getReceiverType()).getMessage()); + jo.put("account",receiver.getReceiverAccount()); + jo.put("amount", receiver.getSharingAmount()); + //jo.put("description",receiver.getReceiverComments()); //改为存ID, + jo.put("description",receiver.getId().toString()); //为重试做准备 + receivers.add(jo); + }); + return receivers.toJSONString(); + } } diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/douyin/TtPayShareService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/douyin/TtPayShareService.java index 26218b7f5..9ad25bf65 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/share/douyin/TtPayShareService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/douyin/TtPayShareService.java @@ -86,11 +86,6 @@ public class TtPayShareService extends PayShareBaseAdapterService{ return EnumMerchantSubsidyType.DOUYIN.getCode(); } - @Override - public Integer getProfitSharingType() { - return EnumProfitSharingType.PROFIT_SHARING_TYPE_DOUYIN.getCode(); - } - @Override public Integer getSharingOrderInitialStatus() { return EnumProfitSharingOrderStatus.PROFIT_SHARING_UNKNOWN.getCode(); @@ -410,7 +405,7 @@ public class TtPayShareService extends PayShareBaseAdapterService{ WxProfitSharingReceiver psReceiverQ = new WxProfitSharingReceiver(); psReceiverQ.updateTenantInfo(tenantEntity); psReceiverQ.setMerchantId(merchantId); - psReceiverQ.setSharingType(this.getProfitSharingType()); + psReceiverQ.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_DOUYIN.getCode()); psReceiverQ.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode()); psReceiverQ.setPlat(EnumAppPlat.TOUTIAO.getCode()); String payMsg = ""; diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/sft/WxPayShareSFTService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/sft/WxPayShareSFTService.java index ad7b53e27..43fd756b1 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/sft/WxPayShareSFTService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/sft/WxPayShareSFTService.java @@ -71,11 +71,6 @@ public class WxPayShareSFTService extends PayShareBaseAdapterService{ return EnumMerchantSubsidyType.WECHAT.getCode(); } - @Override - public Integer getProfitSharingType() { - return EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode(); - } - @Override public Integer getSharingOrderInitialStatus() { return EnumProfitSharingOrderStatus.PROFIT_SHARING_READY.getCode(); diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v2/WxPayShareService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v2/WxPayShareService.java index 38eb0e530..071010d05 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v2/WxPayShareService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v2/WxPayShareService.java @@ -64,11 +64,6 @@ public class WxPayShareService extends PayShareBaseAdapterService{ return EnumMerchantSubsidyType.WECHAT.getCode(); } - @Override - public Integer getProfitSharingType() { - return EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode(); - } - @Override public Integer getSharingOrderInitialStatus() { return EnumProfitSharingOrderStatus.PROFIT_SHARING_READY.getCode(); @@ -495,7 +490,7 @@ public class WxPayShareService extends PayShareBaseAdapterService{ WxProfitSharingReceiver psReceiverQ = new WxProfitSharingReceiver(); psReceiverQ.updateTenantInfo(tenantEntity); psReceiverQ.setMerchantId(merchantId); - psReceiverQ.setSharingType(getProfitSharingType()); + psReceiverQ.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode()); psReceiverQ.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode()); psReceiverQ.setPlat(EnumAppPlat.WX.getCode()); List psReceiverList = wxProfitSharingReceiverMapper.findList(psReceiverQ); diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/WxPayShareV3Service.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/WxPayShareV3Service.java index 50ae20fe5..5e85d0e85 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/WxPayShareV3Service.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/WxPayShareV3Service.java @@ -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.MaUtil; 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 psReceiverList = getReceiverList(sharingOrderDto, sharingOrderDto.getMerchantId(), null); + record.setReceivers(calV3ReceiversAmount(sharingOrderDto, psReceiverList)); + wxProfitSharingOrderMapper.updateById(record); + } + return record; + } + + @Override + public List 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 psReceiverList = getReceiverList(sharingOrderDto, sharingOrderDto.getMerchantId(), null); - record.setReceivers(calReceiversAmount(sharingOrderDto, psReceiverList)); - wxProfitSharingOrderMapper.updateById(record); - } - return record; - } - - @Override - public List 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 retList = new ArrayList(); - retList.add(receiver); - return retList; - } - - } diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/entity/V3PayShareQueryReq.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/entity/V3PayShareQueryReq.java new file mode 100644 index 000000000..2ddac2632 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/entity/V3PayShareQueryReq.java @@ -0,0 +1,14 @@ +package com.iformall.service.pay.service.share.wx.v3.entity; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +@Data +public class V3PayShareQueryReq { + + private String sub_mchid;//微信支付分配的子商户号,即分账的出资商户号。 + private String transaction_id;//微信支付订单号 + + @JSONField(serialize = false) + private String out_order_no;//商户订单号 +} diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/entity/V3PayShareReq.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/entity/V3PayShareReq.java new file mode 100644 index 000000000..d8e6941f4 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/v3/entity/V3PayShareReq.java @@ -0,0 +1,17 @@ +package com.iformall.service.pay.service.share.wx.v3.entity; + +import com.alibaba.fastjson.JSONArray; + +import lombok.Data; + +@Data +public class V3PayShareReq { + + private String sub_mchid;//微信支付分配的子商户号,即分账的出资商户号 + private String appid;//微信分配的服务商appid + private String sub_appid;//微信分配的子商户公众账号ID,分账接收方类型包含PERSONAL_SUB_OPENID时必填 + private String transaction_id;//微信支付订单号 + private String out_order_no;//商户分账单号 + private JSONArray receivers; + private boolean unfreeze_unsplit = true;//是否解冻剩余未分资金.1、如果为true,该笔订单剩余未分账的金额会解冻回分账方商户;2、如果为false,该笔订单剩余未分账的金额不会解冻回分账方商户,可以对该笔订单再次进行分账。 +}