From 1f76657d598f3baf7795fa1d077474e85df1051f Mon Sep 17 00:00:00 2001 From: xiaohanzi Date: Fri, 31 Jul 2020 23:06:05 +0800 Subject: [PATCH] update --- .../service/WxCouponOrderService.java | 2 +- .../service/WxProfitSharingOrderService.java | 4 +- .../impl/WxCouponOrderServiceImpl.java | 15 +- .../service/impl/WxPayOrderServiceImpl.java | 8 +- .../impl/WxProfitSharingOrderServiceImpl.java | 150 ++++-------------- .../service/pay/PayServiceFactory.java | 31 +++- .../service/pay/entity/PayShareResult.java | 22 +++ .../pay/service/PayShareAdapterService.java | 9 -- .../service/{ => pay}/CDrivingPayService.java | 2 +- .../service/{ => pay}/CPassivePayService.java | 2 +- .../service/{ => pay}/PayAdapterService.java | 2 +- .../miniApp/wx/BaseWxPayAdapterService.java | 4 +- .../wx/appPay/WxMiniAppPayAdapterService.java | 6 +- .../wx/maPay/WxMiniMaPayAdapterService.java | 6 +- .../service/payway/MiniAppPayBaseService.java | 11 -- .../service/share/PayShareAdapterService.java | 53 +++++++ .../share/PayShareBaseAdapterService.java | 11 ++ .../service/share/wx/WxPayShareService.java | 133 ++++++++++++++++ 18 files changed, 303 insertions(+), 168 deletions(-) create mode 100644 mallinkService/src/main/java/com/iformall/service/pay/entity/PayShareResult.java delete mode 100644 mallinkService/src/main/java/com/iformall/service/pay/service/PayShareAdapterService.java rename mallinkService/src/main/java/com/iformall/service/pay/service/{ => pay}/CDrivingPayService.java (72%) rename mallinkService/src/main/java/com/iformall/service/pay/service/{ => pay}/CPassivePayService.java (95%) rename mallinkService/src/main/java/com/iformall/service/pay/service/{ => pay}/PayAdapterService.java (97%) rename mallinkService/src/main/java/com/iformall/service/pay/service/{ => pay}/miniApp/wx/BaseWxPayAdapterService.java (92%) rename mallinkService/src/main/java/com/iformall/service/pay/service/{ => pay}/miniApp/wx/appPay/WxMiniAppPayAdapterService.java (98%) rename mallinkService/src/main/java/com/iformall/service/pay/service/{ => pay}/miniApp/wx/maPay/WxMiniMaPayAdapterService.java (98%) delete mode 100644 mallinkService/src/main/java/com/iformall/service/pay/service/payway/MiniAppPayBaseService.java create mode 100644 mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareAdapterService.java create mode 100644 mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareBaseAdapterService.java create mode 100644 mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/WxPayShareService.java diff --git a/mallinkService/src/main/java/com/iformall/service/WxCouponOrderService.java b/mallinkService/src/main/java/com/iformall/service/WxCouponOrderService.java index 6dd607849..8fdbcc368 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxCouponOrderService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxCouponOrderService.java @@ -88,7 +88,7 @@ public interface WxCouponOrderService { * 核销分账 * @param couponOrder */ - void shareAfterVerify(WxCouponOrder couponOrder, Long merchantId); + void shareAfterVerify(WxCouponOrder couponOrder, Long merchantId,Integer payWay); /** * 核销后补贴 diff --git a/mallinkService/src/main/java/com/iformall/service/WxProfitSharingOrderService.java b/mallinkService/src/main/java/com/iformall/service/WxProfitSharingOrderService.java index 2413e27ce..a457e7a80 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxProfitSharingOrderService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxProfitSharingOrderService.java @@ -16,7 +16,7 @@ public interface WxProfitSharingOrderService { /** * 创建分账订单 */ - ResultData createSharingOrder(WxSharingOrderDto wxSharingOrderDto); + ResultData createSharingOrder(WxSharingOrderDto wxSharingOrderDto,Integer payway); /** * 分账重试 @@ -31,7 +31,7 @@ public interface WxProfitSharingOrderService { /** * 结束分账订单 */ - ResultData finishSharingOrder(WxSharingOrderDto sharingOrderDto); + ResultData finishSharingOrder(WxSharingOrderDto sharingOrderDto,Integer payway); PageInfo listAsPage(WxProfitSharingOrderQueryVo order, Integer pageNum, Integer pageSize); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java index f52d76a82..6f1f516f5 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java @@ -25,6 +25,7 @@ import com.iformall.mapper.*; import com.iformall.mq.MqBaseProducer; import com.iformall.pay.WxPayConstant; import com.iformall.service.*; +import com.iformall.service.pay.PayServiceFactory; import com.iformall.utils.Constant; import com.iformall.utils.DateUtils; import com.iformall.utils.PayUtils; @@ -133,6 +134,9 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { @Autowired WxPayOrderMapper payOrderMapper; + + @Autowired + PayServiceFactory payServiceFactory; @Autowired @Qualifier("couponDetailRedisTemplate") @@ -484,7 +488,8 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { } else { try { // 核销分账 - shareAfterVerify(couponOrder, bUser.getMerchantId()); + shareAfterVerify(couponOrder, bUser.getMerchantId(),couponOrder.getPayVendor()); + 111111111111111111111 } catch (Exception e) { logger.error(e.getMessage()); } @@ -543,9 +548,7 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { merchantSubsidy.setRealPayment(couponOrder.getCouponPrice()); merchantSubsidy.setSubsidy(couponOrder.getCouponPrice()); merchantSubsidy.setRealSubsidy(couponOrder.getCouponPrice()); - couponOrder.getPayVendor(); - 11111111111 - merchantSubsidy.setType(EnumMerchantSubsidyType.WECHAT.getCode()); + merchantSubsidy.setType(payServiceFactory.getPayShareAdapterService(couponOrder.getPayVendor()).getType()); merchantSubsidy.setCreateDate(curDate); merchantSubsidy.setUpdateDate(curDate); merchantSubsidy.setStatus(EnumMerchantSubsidyStatus.NOT_SUBSIDY.getCode()); @@ -554,7 +557,7 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { @Override @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) - public void shareAfterVerify(WxCouponOrder couponOrder, Long merchantId) { + public void shareAfterVerify(WxCouponOrder couponOrder, Long merchantId,Integer payWay) { // 微信分账 try { WxPayOrder wxPayOrder = new WxPayOrder(); @@ -575,7 +578,7 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { wxSharingOrderDto.updateTenantInfo(wxPayOrder); wxSharingOrderDto.setTransactionId(wxPayOrder.getTransactionId()); wxSharingOrderDto.setShareAmount(wxPayOrder.getShareAmount()); - wxProfitSharingOrderService.createSharingOrder(wxSharingOrderDto); + wxProfitSharingOrderService.createSharingOrder(wxSharingOrderDto,payWay); } } catch (Exception e) { logger.error("微信分账: " + e.getMessage()); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxPayOrderServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxPayOrderServiceImpl.java index 1d165715b..a8ab15b7a 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxPayOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxPayOrderServiceImpl.java @@ -23,9 +23,9 @@ import com.iformall.service.pay.entity.CreateCUser; import com.iformall.service.pay.entity.PayAdapterResult; import com.iformall.service.pay.entity.PayExtraParam; import com.iformall.service.pay.entity.PayQueryAdapterResult; -import com.iformall.service.pay.service.CDrivingPayService; -import com.iformall.service.pay.service.CPassivePayService; -import com.iformall.service.pay.service.PayAdapterService; +import com.iformall.service.pay.service.pay.CDrivingPayService; +import com.iformall.service.pay.service.pay.CPassivePayService; +import com.iformall.service.pay.service.pay.PayAdapterService; import com.iformall.utils.*; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -1141,7 +1141,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { logger.error(e.getMessage()); } // 更新后处理 - wxCouponOrderService.sendInsideCouponVerifyMsg(unPayOrder, unPayOrder.getPosPayOrderId(), user.getId(),payWay); + wxCouponOrderService.sendInsideCouponVerifyMsg(unPayOrder, unPayOrder.getPosPayOrderId(), user.getId()); } } } catch (Exception e) { 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 fa29005a2..e2db1f825 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java @@ -20,6 +20,8 @@ import com.iformall.exception.MallinkException; import com.iformall.mapper.*; import com.iformall.pay.*; import com.iformall.service.WxProfitSharingOrderService; +import com.iformall.service.pay.PayServiceFactory; +import com.iformall.service.pay.entity.PayShareResult; import com.iformall.utils.BeanUtils; import com.iformall.utils.DateUtils; import com.iformall.utils.Utility; @@ -66,6 +68,9 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ @Autowired WxCardSpendMapper wxCardSpendMapper; + + @Autowired + PayServiceFactory payServiceFactory; final JSONObject errorMap = JSON.parseObject("{" + "\"SYSTEMERROR\":{\"detail\":\"接口返回错误\",\"reason\":\"系统超时\",\"resolution\":\"系统异常,请用相同参数重新调用\"}," + @@ -238,7 +243,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ @Override @Transactional(isolation=Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) - public ResultData createSharingOrder(WxSharingOrderDto sharingOrderDto) { + public ResultData createSharingOrder(WxSharingOrderDto sharingOrderDto,Integer payWay) { final IdWorker idworker = IdWorker.get(); if (!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode()) && @@ -253,7 +258,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ WxProfitSharingReceiver psReceiverQ = new WxProfitSharingReceiver(); psReceiverQ.updateTenantInfo(payAccount); psReceiverQ.setMerchantId(sharingOrderDto.getMerchantId()); - psReceiverQ.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode()); + psReceiverQ.setSharingType(payServiceFactory.getPayShareAdapterService(payWay).getProfitSharingType()); psReceiverQ.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode()); List psReceiverList = wxProfitSharingReceiverMapper.findList(psReceiverQ); if (psReceiverList.size() < 0 || psReceiverList.size() > 50) { @@ -269,9 +274,8 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ // 分账完结 sharingOrderDto.setShareAmount(sharingOrderDto.getPayAmount()+sharingOrderDto.getRateAmount()); } - return finishSharingOrder(sharingOrderDto); + return finishSharingOrder(sharingOrderDto,payWay); } - //计算分账账户合法性 if (psReceiverList.size() > 1 && psReceiverList.stream().filter(r->(r.getParameter() == null)).count() > 0) { @@ -325,7 +329,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ psReceiverList.stream().forEach(receiver->{ Date currentDate = new Date(); JSONObject jo = new JSONObject(); - jo.put("type",EnumProfitSharingReceiverType.getEnum(receiver.getReceiverType()).getMessage()); + jo.put("type",payServiceFactory.getPayShareAdapterService(payWay).getShareAccount(receiver.getReceiverType()).getMessage()); jo.put("account",receiver.getReceiverAccount()); jo.put("amount", receiver.getSharingAmount()); //jo.put("description",receiver.getReceiverComments()); //改为存ID, @@ -346,78 +350,31 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ resultList.add(result); }); - - //分账提交 - WxProfitSharingP psCmd = new WxProfitSharingP(); - psCmd.setMch_id(payAccount.getMchId()); - psCmd.setSub_mch_id(payAccount.getSubMchId()); - psCmd.setAppid(appInfo.getParentAppId()); - psCmd.setSub_appid(appInfo.getAppId()); - psCmd.setNonce_str(Utility.generate32UUID()); - psCmd.setTransaction_id(sharingOrderDto.getTransactionId()); - psCmd.setOut_order_no(record.getId().toString()); - psCmd.setSign_type("HMAC-SHA256"); - psCmd.setReceivers(receivers.toJSONString()); - record.setReceivers(receivers.toJSONString()); wxProfitSharingOrderMapper.updateById(record); - String response; - try { - psCmd.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(psCmd), payAccount.getApiKey())); - logger.info("request:" + psCmd.toString()); - if(sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode())) { - response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(psCmd), payAccount.getCertPath(), payAccount.getMchId()); - } else if(sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode())) { - response = WxProfitSharing.pushMultiOrder(BeanUtils.toStringMap(psCmd), payAccount.getCertPath(), payAccount.getMchId()); - } else { - throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR); - } - } catch (Exception e) { - record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode()); - record.setErrorMsg(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateById(record); - return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED); - } - - logger.info("response: " + response); - Map returnMap = WxPayment.xmlToMap(response); - String return_code = returnMap.get("return_code"); - - if (!"SUCCESS".equals(return_code)) { - record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode()); - record.setErrorMsg(returnMap.get("return_msg")); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateById(record); - return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(), returnMap.get("return_msg")); - } - - if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ - record.setErrorMsg(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateById(record); - return new ResultData(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getCode()); + EnumProfitSharingOrderType shareType = EnumProfitSharingOrderType.getEnum(sharingOrderDto.getType()); + if (null == shareType) { + new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),sharingOrderDto.getType()+"在分账方式中不存在。EnumProfitSharingOrderType"); + } + PayShareResult shareResult = payServiceFactory.getPayShareAdapterService(payWay).haveReciversShare(appInfo, payAccount, frecord, + sharingOrderDto.getTransactionId(), receivers, shareType); + if (!shareResult.isSuccess()) { + record.setSharingStatus(shareResult.getCode()); + record.setErrorMsg(shareResult.getMsg()); + record.setUpdateTime(new Date()); + wxProfitSharingOrderMapper.updateById(record); + return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),record.getErrorMsg()); } - - String result_code = returnMap.get("result_code"); - if (!"SUCCESS".equals(result_code)) { - record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_APPLY_FAILED.getCode()); - record.setUpdateTime(new Date()); - record.setErrorMsg(returnMap.get("err_code_des")); - wxProfitSharingOrderMapper.updateById(record); - return new ResultData(ErrorCode.PROFIT_SHARING_APPLY_FAILED.getCode(), returnMap.get("err_code_des")); - } - - record.setSharingOrderNo(returnMap.get("order_id")); + + record.setSharingOrderNo(shareResult.getShareOrderNo()); record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode()); record.setUpdateTime(new Date()); wxProfitSharingOrderMapper.updateById(record); for (WxProfitSharingResult result:resultList) { wxProfitSharingResultMapper.insert(result); } - - return new ResultData(returnMap); + return new ResultData(shareResult.getData()); } @Override @@ -561,7 +518,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ // 分账完结 @Override @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) - public ResultData finishSharingOrder(WxSharingOrderDto sharingOrderDto) { + public ResultData finishSharingOrder(WxSharingOrderDto sharingOrderDto,Integer payWay) { final IdWorker idworker = IdWorker.get(); if (!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE_FINISH.getCode()) @@ -593,63 +550,20 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ wxProfitSharingOrderMapper.insert(record); //分账提交 - WxProfitSharingFinishP psFCmd = new WxProfitSharingFinishP(); - psFCmd.setMch_id(payAccount.getMchId()); - psFCmd.setSub_mch_id(payAccount.getSubMchId()); - psFCmd.setAppid(appInfo.getParentAppId()); - psFCmd.setNonce_str(Utility.generate32UUID()); - psFCmd.setTransaction_id(sharingOrderDto.getTransactionId()); - psFCmd.setOut_order_no(record.getId().toString()); - psFCmd.setAmount(sharingOrderDto.getShareAmount()); - psFCmd.setDescription("分账已完结"); - psFCmd.setSign_type("HMAC-SHA256"); - - String response; - try { - psFCmd.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(psFCmd), payAccount.getApiKey())); - logger.info("request:" + psFCmd.toString()); - response = WxProfitSharing.finishOrder(BeanUtils.toStringMap(psFCmd), payAccount.getCertPath(), payAccount.getMchId()); - } catch (Exception e) { - record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode()); - record.setErrorMsg(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateById(record); - return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED); - } - - logger.info("response: " + response); - Map returnMap = WxPayment.xmlToMap(response); - String return_code = returnMap.get("return_code"); - - if (!"SUCCESS".equals(return_code)) { - record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode()); - record.setErrorMsg(returnMap.get("return_msg")); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateById(record); - return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(), returnMap.get("return_msg")); - } - - if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ - record.setErrorMsg(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateById(record); - return new ResultData(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getCode()); - } - - String result_code = returnMap.get("result_code"); - if (!"SUCCESS".equals(result_code)) { - record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_APPLY_FAILED.getCode()); + PayShareResult shareResult = payServiceFactory.getPayShareAdapterService(payWay).noReciverShare(appInfo,payAccount,record,sharingOrderDto.getTransactionId(),sharingOrderDto.getShareAmount()); + if (!shareResult.isSuccess()) { + record.setSharingStatus(shareResult.getCode()); + record.setErrorMsg(shareResult.getMsg()); record.setUpdateTime(new Date()); - record.setErrorMsg(returnMap.get("err_code_des")); wxProfitSharingOrderMapper.updateById(record); - return new ResultData(ErrorCode.PROFIT_SHARING_APPLY_FAILED.getCode(), returnMap.get("err_code_des")); + return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),record.getErrorMsg()); } - record.setSharingOrderNo(returnMap.get("order_id")); + record.setSharingOrderNo(shareResult.getShareOrderNo()); record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode()); record.setUpdateTime(new Date()); wxProfitSharingOrderMapper.updateById(record); - return new ResultData(returnMap); + return new ResultData(shareResult.getData()); } @Override diff --git a/mallinkService/src/main/java/com/iformall/service/pay/PayServiceFactory.java b/mallinkService/src/main/java/com/iformall/service/pay/PayServiceFactory.java index 6df0b3c28..5daa321c7 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/PayServiceFactory.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/PayServiceFactory.java @@ -11,11 +11,13 @@ import com.iformall.domain.po.WxPayOrder; import com.iformall.enums.EnumPayWay; import com.iformall.exception.MallinkException; import com.iformall.service.WxPayOrderService; -import com.iformall.service.pay.service.CDrivingPayService; -import com.iformall.service.pay.service.CPassivePayService; -import com.iformall.service.pay.service.PayAdapterService; -import com.iformall.service.pay.service.miniApp.wx.appPay.WxMiniAppPayAdapterService; -import com.iformall.service.pay.service.miniApp.wx.maPay.WxMiniMaPayAdapterService; +import com.iformall.service.pay.service.pay.CDrivingPayService; +import com.iformall.service.pay.service.pay.CPassivePayService; +import com.iformall.service.pay.service.pay.PayAdapterService; +import com.iformall.service.pay.service.pay.miniApp.wx.appPay.WxMiniAppPayAdapterService; +import com.iformall.service.pay.service.pay.miniApp.wx.maPay.WxMiniMaPayAdapterService; +import com.iformall.service.pay.service.share.PayShareAdapterService; +import com.iformall.service.pay.service.share.wx.WxPayShareService; /** * 支付方式工厂 @@ -25,6 +27,7 @@ import com.iformall.service.pay.service.miniApp.wx.maPay.WxMiniMaPayAdapterServi public class PayServiceFactory { private Map serviceMap = null; + private Map shareMap = null; @Autowired WxMiniAppPayAdapterService wxMiniAppPayService; @@ -32,6 +35,8 @@ public class PayServiceFactory { WxMiniMaPayAdapterService wxMiniAppMaPayService; @Autowired WxPayOrderService wxPayOrderService; + @Autowired + WxPayShareService wxShareService; public PayAdapterService getPayAdapterService(Integer type) throws MallinkException{ PayAdapterService service = getServiceMap().get(type); @@ -74,6 +79,20 @@ public class PayServiceFactory { return serviceMap; } + private Map getShareMap(){ + if (null != shareMap ) { + shareMap = new ConcurrentHashMap(); + shareMap.put(EnumPayWay.PAY_WAY_WECHAT.getCode(), wxShareService); + shareMap.put(EnumPayWay.PAY_WAY_WECHAT_MA.getCode(), wxShareService); + } + return shareMap; + } - + public PayShareAdapterService getPayShareAdapterService(Integer type) throws MallinkException{ + PayShareAdapterService shareService = getShareMap().get(type); + if (null == shareService) { + throw new MallinkException(ErrorCode.SYS_SERVER_ERROR.getCode(),"payType["+type+"] 分账service未找到"); + } + return shareService; + } } diff --git a/mallinkService/src/main/java/com/iformall/service/pay/entity/PayShareResult.java b/mallinkService/src/main/java/com/iformall/service/pay/entity/PayShareResult.java new file mode 100644 index 000000000..0bff92338 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/pay/entity/PayShareResult.java @@ -0,0 +1,22 @@ +package com.iformall.service.pay.entity; + +import java.io.Serializable; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class PayShareResult implements Serializable{ + + private static final long serialVersionUID = -3701322871877301001L; + + private boolean isSuccess; + private Integer code; + private String msg; + private Object data; + private String shareOrderNo; + + + +} diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/PayShareAdapterService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/PayShareAdapterService.java deleted file mode 100644 index a6e774411..000000000 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/PayShareAdapterService.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.iformall.service.pay.service; - -/** - * 分账接口 - * @author alascor - */ -public interface PayShareAdapterService { - -} diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/CDrivingPayService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/CDrivingPayService.java similarity index 72% rename from mallinkService/src/main/java/com/iformall/service/pay/service/CDrivingPayService.java rename to mallinkService/src/main/java/com/iformall/service/pay/service/pay/CDrivingPayService.java index cd346366b..e0011e7cd 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/CDrivingPayService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/CDrivingPayService.java @@ -1,4 +1,4 @@ -package com.iformall.service.pay.service; +package com.iformall.service.pay.service.pay; /** * C端主动发起的支付 diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/CPassivePayService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/CPassivePayService.java similarity index 95% rename from mallinkService/src/main/java/com/iformall/service/pay/service/CPassivePayService.java rename to mallinkService/src/main/java/com/iformall/service/pay/service/pay/CPassivePayService.java index 7a8759719..f218d7d09 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/CPassivePayService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/CPassivePayService.java @@ -1,4 +1,4 @@ -package com.iformall.service.pay.service; +package com.iformall.service.pay.service.pay; import java.util.Date; diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/PayAdapterService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/PayAdapterService.java similarity index 97% rename from mallinkService/src/main/java/com/iformall/service/pay/service/PayAdapterService.java rename to mallinkService/src/main/java/com/iformall/service/pay/service/pay/PayAdapterService.java index 80557468b..450bb3d70 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/PayAdapterService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/PayAdapterService.java @@ -1,4 +1,4 @@ -package com.iformall.service.pay.service; +package com.iformall.service.pay.service.pay; import java.util.Date; import com.iformall.domain.po.WxAppinfo; diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/BaseWxPayAdapterService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/BaseWxPayAdapterService.java similarity index 92% rename from mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/BaseWxPayAdapterService.java rename to mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/BaseWxPayAdapterService.java index f069e09f9..26e88e954 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/BaseWxPayAdapterService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/BaseWxPayAdapterService.java @@ -1,4 +1,4 @@ -package com.iformall.service.pay.service.miniApp.wx; +package com.iformall.service.pay.service.pay.miniApp.wx; import java.util.Map; @@ -8,7 +8,7 @@ import com.iformall.domain.po.WxPayAccount; import com.iformall.domain.po.WxPayOrder; import com.iformall.service.helper.WxPayOrderServiceHelper; import com.iformall.service.pay.entity.PayQueryAdapterResult; -import com.iformall.service.pay.service.PayAdapterService; +import com.iformall.service.pay.service.pay.PayAdapterService; public class BaseWxPayAdapterService { diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/appPay/WxMiniAppPayAdapterService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/appPay/WxMiniAppPayAdapterService.java similarity index 98% rename from mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/appPay/WxMiniAppPayAdapterService.java rename to mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/appPay/WxMiniAppPayAdapterService.java index b6956eaef..53b979470 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/appPay/WxMiniAppPayAdapterService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/appPay/WxMiniAppPayAdapterService.java @@ -1,4 +1,4 @@ -package com.iformall.service.pay.service.miniApp.wx.appPay; +package com.iformall.service.pay.service.pay.miniApp.wx.appPay; import java.util.Date; import java.util.Map; @@ -25,8 +25,8 @@ import com.iformall.pay.WxPayment; import com.iformall.service.pay.entity.PayAdapterResult; import com.iformall.service.pay.entity.PayExtraParam; import com.iformall.service.pay.entity.PayQueryAdapterResult; -import com.iformall.service.pay.service.CDrivingPayService; -import com.iformall.service.pay.service.miniApp.wx.BaseWxPayAdapterService; +import com.iformall.service.pay.service.pay.CDrivingPayService; +import com.iformall.service.pay.service.pay.miniApp.wx.BaseWxPayAdapterService; import com.iformall.utils.BeanUtils; import com.iformall.utils.MapUtil; import com.iformall.utils.Utility; diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/maPay/WxMiniMaPayAdapterService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/maPay/WxMiniMaPayAdapterService.java similarity index 98% rename from mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/maPay/WxMiniMaPayAdapterService.java rename to mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/maPay/WxMiniMaPayAdapterService.java index f0577d143..745c1686c 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/miniApp/wx/maPay/WxMiniMaPayAdapterService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/miniApp/wx/maPay/WxMiniMaPayAdapterService.java @@ -1,4 +1,4 @@ -package com.iformall.service.pay.service.miniApp.wx.maPay; +package com.iformall.service.pay.service.pay.miniApp.wx.maPay; import java.util.Date; import java.util.Map; @@ -26,8 +26,8 @@ import com.iformall.service.pay.entity.CreateCUser; import com.iformall.service.pay.entity.PayAdapterResult; import com.iformall.service.pay.entity.PayExtraParam; import com.iformall.service.pay.entity.PayQueryAdapterResult; -import com.iformall.service.pay.service.CPassivePayService; -import com.iformall.service.pay.service.miniApp.wx.BaseWxPayAdapterService; +import com.iformall.service.pay.service.pay.CPassivePayService; +import com.iformall.service.pay.service.pay.miniApp.wx.BaseWxPayAdapterService; import com.iformall.utils.BeanUtils; import com.iformall.utils.Utility; import lombok.extern.slf4j.Slf4j; diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/payway/MiniAppPayBaseService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/payway/MiniAppPayBaseService.java deleted file mode 100644 index cf00a9c9c..000000000 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/payway/MiniAppPayBaseService.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.iformall.service.pay.service.payway; - -import com.iformall.service.pay.service.PayAdapterService; - -/** - * 小程序app支付service - * @author alascor - */ -public abstract class MiniAppPayBaseService implements PayAdapterService{ - -} 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 new file mode 100644 index 000000000..309b21e9e --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareAdapterService.java @@ -0,0 +1,53 @@ +package com.iformall.service.pay.service.share; + +import com.alibaba.fastjson.JSONArray; +import com.iformall.domain.po.WxAppinfo; +import com.iformall.domain.po.WxPayAccount; +import com.iformall.domain.po.WxProfitSharingOrder; +import com.iformall.enums.EnumProfitSharingOrderType; +import com.iformall.enums.EnumProfitSharingReceiverType; +import com.iformall.exception.MallinkException; +import com.iformall.service.pay.entity.PayShareResult; + +/** + * 分账接口 + * @author alascor + */ +public interface PayShareAdapterService { + + public Integer getType(); + + public Integer getProfitSharingType(); + + /** + * 无分账接收方分账 ,钱直接分给小程序方 调用API分账 + * @param appInfo + * @param payAccount + * @param record + * @param transcationId + * @param amount 分账金额 + * @return + */ + public PayShareResult noReciverShare(WxAppinfo appInfo,WxPayAccount payAccount,WxProfitSharingOrder record,String transcationId,Integer amount); + + /** + * 有分账接收账号分账 ,钱分给小程序方,还要分给分账账号 调用API分账 + * @param appInfo + * @param payAccount + * @param record + * @param transcationId + * @param receivers 接收方账号 {"type":${EnumProfitSharingReceiverType.message},"account":${String},"amount":${Integer},"description":${id}} + * @param shareType + * @return + * @throws MallinkException + */ + public PayShareResult haveReciversShare(WxAppinfo appInfo,WxPayAccount payAccount,WxProfitSharingOrder record,String transcationId,JSONArray receivers,EnumProfitSharingOrderType shareType ) throws MallinkException; + + /** + * 获取分账账号类型 + * @param accountType + * @return + */ + public EnumProfitSharingReceiverType getShareAccount(Integer accountType); + +} 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 new file mode 100644 index 000000000..f22ad2a7a --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/PayShareBaseAdapterService.java @@ -0,0 +1,11 @@ +package com.iformall.service.pay.service.share; + +import com.iformall.enums.EnumProfitSharingReceiverType; +public abstract class PayShareBaseAdapterService implements PayShareAdapterService{ + + @Override + public EnumProfitSharingReceiverType getShareAccount(Integer accountType) { + return EnumProfitSharingReceiverType.getEnum(accountType); + } + +} diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/WxPayShareService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/WxPayShareService.java new file mode 100644 index 000000000..965d32629 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/share/wx/WxPayShareService.java @@ -0,0 +1,133 @@ +package com.iformall.service.pay.service.share.wx; + +import java.util.Map; +import org.springframework.stereotype.Service; + +import com.alibaba.fastjson.JSONArray; +import com.iformall.common.ErrorCode; +import com.iformall.domain.po.WxAppinfo; +import com.iformall.domain.po.WxPayAccount; +import com.iformall.domain.po.WxProfitSharingOrder; +import com.iformall.enums.EnumMerchantSubsidyType; +import com.iformall.enums.EnumProfitSharingOrderStatus; +import com.iformall.enums.EnumProfitSharingOrderType; +import com.iformall.enums.EnumProfitSharingReceiverType; +import com.iformall.enums.EnumProfitSharingType; +import com.iformall.exception.MallinkException; +import com.iformall.pay.WxPayment; +import com.iformall.pay.WxProfitSharing; +import com.iformall.pay.WxProfitSharingFinishP; +import com.iformall.pay.WxProfitSharingP; +import com.iformall.service.pay.entity.PayShareResult; +import com.iformall.service.pay.service.share.PayShareAdapterService; +import com.iformall.service.pay.service.share.PayShareBaseAdapterService; +import com.iformall.utils.BeanUtils; +import com.iformall.utils.Utility; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class WxPayShareService extends PayShareBaseAdapterService{ + + @Override + public Integer getType() { + return EnumMerchantSubsidyType.WECHAT.getCode(); + } + + @Override + public Integer getProfitSharingType() { + return EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode(); + } + + @Override + public PayShareResult noReciverShare(WxAppinfo appInfo,WxPayAccount payAccount,WxProfitSharingOrder record,String transcationId,Integer amount) { + + WxProfitSharingFinishP psFCmd = new WxProfitSharingFinishP(); + psFCmd.setMch_id(payAccount.getMchId()); + psFCmd.setSub_mch_id(payAccount.getSubMchId()); + psFCmd.setAppid(appInfo.getParentAppId()); + psFCmd.setNonce_str(Utility.generate32UUID()); + psFCmd.setTransaction_id(transcationId); + psFCmd.setOut_order_no(record.getId().toString()); + psFCmd.setAmount(amount); + psFCmd.setDescription("分账已完结"); + psFCmd.setSign_type("HMAC-SHA256"); + + String response; + try { + psFCmd.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(psFCmd), payAccount.getApiKey())); + log.info("request:" + psFCmd.toString()); + response = WxProfitSharing.finishOrder(BeanUtils.toStringMap(psFCmd), payAccount.getCertPath(), payAccount.getMchId()); + } catch (Exception e) { + log.error("wx noreciver share error:",e); + return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(),ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()+":"+e.getMessage(), null,null); + } + + log.info("response: " + response); + Map returnMap = WxPayment.xmlToMap(response); + String return_code = returnMap.get("return_code"); + + if (!"SUCCESS".equals(return_code)) { + return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(),returnMap.get("return_msg"), null,null); + } + if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ + return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(),ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage(), null,null); + } + String result_code = returnMap.get("result_code"); + if (!"SUCCESS".equals(result_code)) { + return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_APPLY_FAILED.getCode(),returnMap.get("err_code_des"), null,null); + } + + return new PayShareResult(true, null,"success", returnMap,returnMap.get("order_id")); + } + + @Override + public PayShareResult haveReciversShare(WxAppinfo appInfo,WxPayAccount payAccount,WxProfitSharingOrder record,String transcationId,JSONArray receivers,EnumProfitSharingOrderType shareType ) throws MallinkException { + //分账提交 + WxProfitSharingP psCmd = new WxProfitSharingP(); + psCmd.setMch_id(payAccount.getMchId()); + psCmd.setSub_mch_id(payAccount.getSubMchId()); + psCmd.setAppid(appInfo.getParentAppId()); + psCmd.setSub_appid(appInfo.getAppId()); + psCmd.setNonce_str(Utility.generate32UUID()); + psCmd.setTransaction_id(transcationId); + psCmd.setOut_order_no(record.getId().toString()); + psCmd.setSign_type("HMAC-SHA256"); + psCmd.setReceivers(receivers.toJSONString()); + + String response; + try { + psCmd.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(psCmd), payAccount.getApiKey())); + log.info("request:" + psCmd.toString()); + if(shareType.getCode().intValue()==EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode().intValue()) { + response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(psCmd), payAccount.getCertPath(), payAccount.getMchId()); + } else if(shareType.getCode().intValue()==EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode().intValue()) { + response = WxProfitSharing.pushMultiOrder(BeanUtils.toStringMap(psCmd), payAccount.getCertPath(), payAccount.getMchId()); + } else { + throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR); + } + } catch (Exception e) { + log.error("wx recivers share error:",e); + return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(), ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()+":"+e.getMessage(), null, null); + } + + log.info("response: " + response); + Map returnMap = WxPayment.xmlToMap(response); + String return_code = returnMap.get("return_code"); + + if (!"SUCCESS".equals(return_code)) { + return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(), returnMap.get("return_msg"), null, null); + } + + if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ + return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_REQ_FAILED.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage(), null, null); + } + + String result_code = returnMap.get("result_code"); + if (!"SUCCESS".equals(result_code)) { + return new PayShareResult(false, EnumProfitSharingOrderStatus.PROFIT_SHARING_APPLY_FAILED.getCode(), returnMap.get("err_code_des"), null, null); + } + + return new PayShareResult(true, null, "success",returnMap, returnMap.get("order_id")); + } +}