Browse Source

[分账][新增]

release_toaliyun_real
Stormeye Wu 7 years ago
parent
commit
a9d086edcf
2 changed files with 88 additions and 37 deletions
  1. +24
    -2
      mallinkService/src/main/java/com/iformall/pay/WxProfitSharing.java
  2. +64
    -35
      mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java

+ 24
- 2
mallinkService/src/main/java/com/iformall/pay/WxProfitSharing.java View File

@@ -9,8 +9,12 @@ import org.slf4j.LoggerFactory;
public class WxProfitSharing {
protected static Logger log = LoggerFactory.getLogger(WxProfitSharing.class);

// 分账接口
// 单次分账接口
private static final String PROFIT_SHARING_URL = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
// 多次分账接口
private static final String PROFIT_MULTI_SHARING_URL = "https://api.mch.weixin.qq.com/secapi/pay/multiprofitsharing";
// 分账完结接口
private static final String PROFIT_SHARING_FINISH_URL = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish";
// 查询接口
private static final String PROFIT_SHARING_QUERY_URL = "https://api.mch.weixin.qq.com/pay/profitsharingquery";
// 添加分账账户
@@ -23,7 +27,7 @@ public class WxProfitSharing {
}

/**
* 分账下单
* 单次分账下单
* @param params
* @return
*/
@@ -31,6 +35,24 @@ public class WxProfitSharing {
return doPostSSL(PROFIT_SHARING_URL, params, certPath, certPass);
}

/**
* 多次分账下单
* @param params
* @return
*/
public static String pushMultiOrder(Map<String, String> params, String certPath, String certPass) {
return doPostSSL(PROFIT_MULTI_SHARING_URL, params, certPath, certPass);
}

/**
* 分账完结
* @param params
* @return
*/
public static String finishOrder(Map<String, String> params, String certPath, String certPass) {
return doPostSSL(PROFIT_SHARING_FINISH_URL, params, certPath, certPass);
}

/**
* 分账查询
* @param params


+ 64
- 35
mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java View File

@@ -137,20 +137,54 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
}

@Override
@Transactional(isolation=Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
public ResultData createSharingOrder(WxPayOrder wxPayOrder, Long merchantId) {
//添加分账接受方
WxProfitSharingReceiver psReceiverQ = new WxProfitSharingReceiver();
psReceiverQ.setTenantId(wxPayOrder.getTenantId());
psReceiverQ.setMerchantId(merchantId);
psReceiverQ.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode());
psReceiverQ.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode());
List<WxProfitSharingReceiver> psReceiverList = wxProfitSharingReceiverMapper.findList(psReceiverQ);
if (psReceiverList.size()<=0 || psReceiverList.size() > 50) {
throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
}

return createPSOrder(wxPayOrder, merchantId, psReceiverList, false);
}

@Transactional(isolation=Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
private ResultData createPSOrder(WxPayOrder wxPayOrder, Long merchantId, List<WxProfitSharingReceiver> psReceiverList, boolean multiCmd) {
final IdWorker idworker = IdWorker.get();

WxAppinfo appInfo = getAppinfo(wxPayOrder);
WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId());

// TODO
WxOrder wxOrder = wxOrderMapper.selectByPrimaryKey(wxPayOrder.getOrderId());

//是否已创建分账订单
//单次分账检查是否已创建分账订单
WxProfitSharingOrder record = new WxProfitSharingOrder();
record.setOrderId(wxPayOrder.getId());
record = wxProfitSharingOrderMapper.selectOne(record);
if (record == null) {
if(!multiCmd) {
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(merchantId);
record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_UNKNOWN.getCode());
record.setCreateTime(currentDate);
record.setUpdateTime(currentDate);
record.setPayTimeStart(currentDate);
record.setPayTimeEnd(currentDate);
wxProfitSharingOrderMapper.insertSelective(record);
}
} else {
//创建分账订单
Date currentDate = new Date();
record = new WxProfitSharingOrder();
@@ -168,31 +202,11 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
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.setTenantId(wxOrder.getTenantId());
wxProfitSharingReceiver.setMerchantId(merchantId);
wxProfitSharingReceiver.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode());
wxProfitSharingReceiver.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode());
List<WxProfitSharingReceiver> wxProfitSharingReceiverList = wxProfitSharingReceiverMapper.findList(wxProfitSharingReceiver);
if (wxProfitSharingReceiverList.size()<=0 || wxProfitSharingReceiverList.size() > 50) {
throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
}

// 添加分账接收方
JSONArray receivers = new JSONArray();
List <WxProfitSharingResult> resultList = new ArrayList<WxProfitSharingResult>();
for (int i=0;i<wxProfitSharingReceiverList.size();i++){
WxProfitSharingReceiver receiver = wxProfitSharingReceiverList.get(i);
for (int i=0;i<psReceiverList.size();i++){
WxProfitSharingReceiver receiver = psReceiverList.get(i);
Date currentDate = new Date();
JSONObject jo = new JSONObject();
jo.put("type",EnumProfitSharingReceiverType.getEnum(receiver.getReceiverType()).getMessage());
@@ -215,20 +229,34 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
resultList.add(result);
}

wxProfitSharingP.setReceivers(receivers.toJSONString());
//分账提交
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(wxPayOrder.getTransactionId());
psCmd.setOut_order_no(record.getId().toString());
psCmd.setSign_type("HMAC-SHA256");
psCmd.setReceivers(receivers.toJSONString());

String response;
try {
wxProfitSharingP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingP), payAccount.getApiKey()));
logger.info("wxProfitSharingP :" + wxProfitSharingP.toString());
response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(wxProfitSharingP), payAccount.getCertPath(), payAccount.getMchId());
}catch (Exception e) {
psCmd.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(psCmd), payAccount.getApiKey()));
if(!multiCmd) {
response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(psCmd), payAccount.getCertPath(), payAccount.getMchId());
} else {
response = WxProfitSharing.pushMultiOrder(BeanUtils.toStringMap(psCmd), payAccount.getCertPath(), 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);
}

logger.info("response: " + response);
Map<String, String> returnMap = WxPayment.xmlToMap(response);
String return_code = returnMap.get("return_code");
@@ -261,10 +289,11 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_ACCEPTED.getCode());
record.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
for (WxProfitSharingResult result:resultList)
for (WxProfitSharingResult result:resultList) {
wxProfitSharingResultMapper.insertSelective(result);
return new ResultData(returnMap);
}

return new ResultData(returnMap);
}

@Override


Loading…
Cancel
Save