diff --git a/mallinkService/src/main/java/com/simple/service/impl/WxProfitSharingOrderServiceImpl.java b/mallinkService/src/main/java/com/simple/service/impl/WxProfitSharingOrderServiceImpl.java index 1ebf7d071..46d3b7268 100644 --- a/mallinkService/src/main/java/com/simple/service/impl/WxProfitSharingOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/simple/service/impl/WxProfitSharingOrderServiceImpl.java @@ -143,212 +143,213 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ public ResultData createSharingOrder(WxPayOrder wxPayOrder) { final IdWorker idworker = IdWorker.get(); - try { - WxAppinfo appInfo = getAppinfo(wxPayOrder); - WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId()); + WxAppinfo appInfo = getAppinfo(wxPayOrder); + WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId()); - WxOrder wxOrder = wxOrderMapper.selectByPrimaryKey(wxPayOrder.getOrderId()); + WxOrder wxOrder = wxOrderMapper.selectByPrimaryKey(wxPayOrder.getOrderId()); - //是否已创建分账订单 - WxProfitSharingOrder record = new WxProfitSharingOrder(); + //是否已创建分账订单 + WxProfitSharingOrder record = new WxProfitSharingOrder(); + record.setOrderId(wxPayOrder.getId()); + record = wxProfitSharingOrderMapper.selectOne(record); + if (record == null) { + //创建分账订单 + Date currentDate = new Date(); + record = new WxProfitSharingOrder(); + record.setId(idworker.nextId()); + record.setTenantId(wxPayOrder.getTenantId()); + record.setTransactionId(wxPayOrder.getTransactionId()); record.setOrderId(wxPayOrder.getId()); - record = wxProfitSharingOrderMapper.selectOne(record); - if (record == null) { - //创建分账订单 - Date currentDate = new Date(); - record = new WxProfitSharingOrder(); - record.setId(idworker.nextId()); - record.setTenantId(wxPayOrder.getTenantId()); - record.setTransactionId(wxPayOrder.getTransactionId()); - record.setOrderId(wxPayOrder.getId()); - record.setPayAmount(wxPayOrder.getPayAmount()); - record.setMerchantId(wxOrder.getMerchantId()); - record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_UNKNOWN.getCode()); - record.setCreateTime(currentDate); - record.setUpdateTime(currentDate); - record.setPayTimeStart(currentDate); - record.setPayTimeEnd(currentDate); - wxProfitSharingOrderMapper.insertSelective(record); - } + record.setPayAmount(wxPayOrder.getPayAmount()); + record.setMerchantId(wxOrder.getMerchantId()); + record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_UNKNOWN.getCode()); + record.setCreateTime(currentDate); + record.setUpdateTime(currentDate); + record.setPayTimeStart(currentDate); + record.setPayTimeEnd(currentDate); + wxProfitSharingOrderMapper.insertSelective(record); + } - //分账提交 - WxProfitSharingP wxProfitSharingP = new WxProfitSharingP(); - wxProfitSharingP.setAppid(appInfo.getParentAppId()); - wxProfitSharingP.setMch_id(payAccount.getMchId()); - wxProfitSharingP.setSub_appid(appInfo.getAppId()); - wxProfitSharingP.setSub_mch_id(payAccount.getSubMchId()); - wxProfitSharingP.setNonce_str(Utility.generate32UUID()); - wxProfitSharingP.setTransaction_id(wxPayOrder.getTransactionId()); - wxProfitSharingP.setOut_order_no(record.getId().toString()); - wxProfitSharingP.setSign_type("HMAC-SHA256"); - //添加分账接受方 - WxProfitSharingReceiver wxProfitSharingReceiver = new WxProfitSharingReceiver(); - wxProfitSharingReceiver.setMerchantId(wxOrder.getMerchantId()); - wxProfitSharingReceiver.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode()); - List wxProfitSharingReceiverList = wxProfitSharingReceiverMapper.findList(wxProfitSharingReceiver); - if (wxProfitSharingReceiverList.size()<=0 || wxProfitSharingReceiverList.size() > 50) { - throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RECEIVER_INVALID.getMessage()); - } - // 计算分账总金额,总金额-0.6% - Float dChargeFee = record.getPayAmount()*1.0f*payAccount.getRate()/1000; - Integer total_amount = record.getPayAmount() - Math.round(dChargeFee.floatValue()); - JSONArray receivers = new JSONArray(); - List resultList = new ArrayList(); - for (int i=0;i wxProfitSharingReceiverList = wxProfitSharingReceiverMapper.findList(wxProfitSharingReceiver); + if (wxProfitSharingReceiverList.size()<=0 || wxProfitSharingReceiverList.size() > 50) { + throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RECEIVER_INVALID.getMessage()); + } - wxProfitSharingP.setReceivers(receivers.toJSONString()); + JSONArray receivers = new JSONArray(); + List resultList = new ArrayList(); + for (int i=0;i returnMap = WxPayment.xmlToMap(response); - String return_code = returnMap.get("return_code"); - - if (!"SUCCESS".equals(return_code)) { - record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_REQ_FAILED.getCode()); - record.setErrorMsg(returnMap.get("return_msg")); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateByPrimaryKey(record); - return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(), returnMap.get("return_msg")); - } + result.setId(idworker.nextId()); + result.setSharingOrderId(record.getId()); + result.setSharingReceiverId(receiver.getId()); + result.setPayAmount(wxPayOrder.getShareAmount()); + result.setCreateTime(currentDate); + result.setUpdateTime(currentDate); + result.setSharingStatus(EnumProfitSharingResultStatus.PROFIT_SHARING_RESULT_PENDING.getCode()); + resultList.add(result); + } - if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ - record.setErrorMsg("返回值校验失败"); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateByPrimaryKey(record); - return new ResultData(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); - } + wxProfitSharingP.setReceivers(receivers.toJSONString()); - String result_code = returnMap.get("result_code"); - if (!"SUCCESS".equals(result_code)) { - record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_APPLY_FAILED.getCode()); - record.setUpdateTime(new Date()); - record.setErrorMsg(returnMap.get("result_msg")); - wxProfitSharingOrderMapper.updateByPrimaryKey(record); - return new ResultData(ErrorCode.PROFIT_SHARING_APPLY_FAILED.getCode(), returnMap.get("result_msg")); - } + String response; + try { + wxProfitSharingP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingP), payAccount.getApiKey())); + logger.info("wxProfitSharingP :" + wxProfitSharingP.toString()); + response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(wxProfitSharingP), "D:/cert/apiclient_cert.p12", payAccount.getMchId()); + }catch (Exception e) { + record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_REQ_FAILED.getCode()); + record.setErrorMsg(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()); + record.setUpdateTime(new Date()); + wxProfitSharingOrderMapper.updateByPrimaryKey(record); + return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(), ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()+e.getMessage()); + } + logger.info("response: " + response); + Map returnMap = WxPayment.xmlToMap(response); + String return_code = returnMap.get("return_code"); + + if (!"SUCCESS".equals(return_code)) { + record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_REQ_FAILED.getCode()); + record.setErrorMsg(returnMap.get("return_msg")); + record.setUpdateTime(new Date()); + wxProfitSharingOrderMapper.updateByPrimaryKey(record); + return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(), returnMap.get("return_msg")); + } - record.setSharingOrderNo(returnMap.get("order_id")); - record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_ACCEPTED.getCode()); + if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ + record.setErrorMsg(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); record.setUpdateTime(new Date()); wxProfitSharingOrderMapper.updateByPrimaryKey(record); - wxProfitSharingResultMapper.insertList(resultList); - return new ResultData(returnMap); + return new ResultData(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); + } - } catch (Exception e) { - throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); + String result_code = returnMap.get("result_code"); + if (!"SUCCESS".equals(result_code)) { + record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_APPLY_FAILED.getCode()); + record.setUpdateTime(new Date()); + record.setErrorMsg(returnMap.get("result_msg")); + wxProfitSharingOrderMapper.updateByPrimaryKey(record); + return new ResultData(ErrorCode.PROFIT_SHARING_APPLY_FAILED.getCode(), returnMap.get("result_msg")); } + + record.setSharingOrderNo(returnMap.get("order_id")); + record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_ACCEPTED.getCode()); + record.setUpdateTime(new Date()); + wxProfitSharingOrderMapper.updateByPrimaryKey(record); + for (WxProfitSharingResult result:resultList) + wxProfitSharingResultMapper.insertSelective(result); + return new ResultData(returnMap); + } @Override public ResultData querySharingOrder(WxPayOrder wxPayOrder) { WxProfitSharingOrder record = new WxProfitSharingOrder(); - try { - WxAppinfo appInfo = getAppinfo(wxPayOrder); - WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId()); - - //是否已创建分账订单 - record.setOrderId(wxPayOrder.getId()); - record = wxProfitSharingOrderMapper.selectOne(record); - if (record == null) - return new ResultData(ErrorCode.ORDER_IS_NOT_FIND.getCode(), ErrorCode.ORDER_IS_NOT_FIND.getMessage()); - - //分账查询提交 - WxProfitSharingQueryP wxProfitSharingQueryP = new WxProfitSharingQueryP();; - wxProfitSharingQueryP.setMch_id(payAccount.getMchId()); - wxProfitSharingQueryP.setSub_mch_id(payAccount.getSubMchId()); - wxProfitSharingQueryP.setNonce_str(Utility.generate32UUID()); - wxProfitSharingQueryP.setTransaction_id(wxPayOrder.getTransactionId()); - wxProfitSharingQueryP.setOut_trade_no(record.getId().toString()); - wxProfitSharingQueryP.setSign_type("HMAC-SHA256"); + WxAppinfo appInfo = getAppinfo(wxPayOrder); + WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId()); + + //是否已创建分账订单 + record.setOrderId(wxPayOrder.getId()); + record = wxProfitSharingOrderMapper.selectOne(record); + if (record == null) + return new ResultData(ErrorCode.ORDER_IS_NOT_FIND.getCode(), ErrorCode.ORDER_IS_NOT_FIND.getMessage()); + + //分账查询提交 + WxProfitSharingQueryP wxProfitSharingQueryP = new WxProfitSharingQueryP();; + wxProfitSharingQueryP.setMch_id(payAccount.getMchId()); + wxProfitSharingQueryP.setSub_mch_id(payAccount.getSubMchId()); + wxProfitSharingQueryP.setNonce_str(Utility.generate32UUID()); + wxProfitSharingQueryP.setTransaction_id(wxPayOrder.getTransactionId()); + wxProfitSharingQueryP.setOut_trade_no(record.getId().toString()); + wxProfitSharingQueryP.setSign_type("HMAC-SHA256"); + String response; + try { wxProfitSharingQueryP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getApiKey())); - String response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getCertPath(), payAccount.getMchId()); - - Map returnMap = WxPayment.xmlToMap(response); - String return_code = returnMap.get("return_code"); - if (!"SUCCESS".equals(return_code)) { - return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getCode(), returnMap.get("return_msg")); - } + response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getCertPath(), payAccount.getMchId()); + }catch (Exception e){ + return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getCode(), ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getMessage()+e.getMessage()); + } + Map returnMap = WxPayment.xmlToMap(response); + String return_code = returnMap.get("return_code"); + if (!"SUCCESS".equals(return_code)) { + return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getCode(), returnMap.get("return_msg")); + } - if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ - return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); - } + if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ + return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); + } - String out_order_no = returnMap.get("out_order_no"); - if (!out_order_no.equals(record.getId().toString())){ - return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); - } + String out_order_no = returnMap.get("out_order_no"); + if (!out_order_no.equals(record.getId().toString())){ + return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); + } - String result_code = returnMap.get("result_code"); - if (!"SUCCESS".equals(result_code)) { - return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_APPLY_FAILED.getCode(), returnMap.get("result_msg")); - } + String result_code = returnMap.get("result_code"); + if (!"SUCCESS".equals(result_code)) { + return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_APPLY_FAILED.getCode(), returnMap.get("result_msg")); + } - record.setSharingStatus((Integer) statusMap.get(returnMap.get("status"))); - record.setUpdateTime(new Date()); - wxProfitSharingOrderMapper.updateByPrimaryKey(record); + record.setSharingStatus((Integer) statusMap.get(returnMap.get("status"))); + record.setUpdateTime(new Date()); + wxProfitSharingOrderMapper.updateByPrimaryKey(record); - String receivers = returnMap.get("receivers"); - JSONArray jReceivers = JSONArray.parseArray(receivers); + String receivers = returnMap.get("receivers"); + JSONArray jReceivers = JSONArray.parseArray(receivers); - WxProfitSharingResult result = new WxProfitSharingResult(); - result.setSharingOrderId(record.getId()); - List wxProfitSharingResultList = wxProfitSharingResultMapper.findList(result); + WxProfitSharingResult result = new WxProfitSharingResult(); + result.setSharingOrderId(record.getId()); + List wxProfitSharingResultList = wxProfitSharingResultMapper.findList(result); - WxProfitSharingReceiver wxProfitSharingReceiver; - WxProfitSharingResult wxProfitSharingResult; + WxProfitSharingReceiver wxProfitSharingReceiver; + WxProfitSharingResult wxProfitSharingResult; - for(int i=0; i