@@ -5,10 +5,9 @@ import java.util.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.dto.WxSharingOrderDto;
import com.iformall.domain.po.*;
import com.iformall.enums.*;
import com.iformall.exception.MallinkException;
@@ -53,6 +52,8 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
@Autowired
WxCUserMapper wxCUserMapper;
@Autowired
WxPayOrderMapper wxPayOrderMapper;
final JSONObject errorMap = JSON.parseObject("{" +
"\"SYSTEMERROR\":{\"detail\":\"接口返回错误\",\"reason\":\"系统超时\",\"resolution\":\"系统异常,请用相同参数重新调用\"}," +
@@ -88,40 +89,12 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
"\"RETURNED\": 4," +
"\"CLOSED\": 5}");
@Override
public PageInfo<WxProfitSharingOrder> listAsPage(WxProfitSharingOrder record, Integer pageIndex, Integer pageSize) {
return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxProfitSharingOrderMapper.findList(record));
}
@Override
public WxProfitSharingOrder getById(Long id) {
return wxProfitSharingOrderMapper.selectByPrimaryKey(id);
}
@Override
public void saveOrUpdate(WxProfitSharingOrder record) {
if (record.getId() == null) {
//record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
final IdWorker idWorker = IdWorker.get();
record.setId(idWorker.nextId());
wxProfitSharingOrderMapper.insertSelective(record);
} else {
wxProfitSharingOrderMapper.updateByPrimaryKeySelective(record);
}
}
@Override
public void deleteById(Long id) {
wxProfitSharingOrderMapper.deleteByPrimaryKey(id);
}
@Autowired
WxPayOrderMapper wxPayOrderMapper;
private WxAppinfo getAppinfo(WxPayOrder wxPayOrder) {
private WxAppinfo getAppinfo(Long cUserId) {
WxAppinfo wxAppinfo = new WxAppinfo();
WxCUser wxCUser = wxCUserMapper.selectByPrimaryKey(wxPayOrder.get cUserId() );
WxCUser wxCUser = wxCUserMapper.selectByPrimaryKey(cUserId);
if (wxCUser == null)
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
@@ -133,13 +106,24 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
return wxAppinfo;
}
@Override
@Transactional(isolation=Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
public ResultData createSharingOrder(WxPayOrder wxPayOrder, Long merchantId) {
//添加分账接受方
public ResultData createSharingOrder(WxSharingOrderDto sharingOrderDto) {
final IdWorker idworker = IdWorker.get();
if (!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode()) &&
!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode())){
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
}
WxAppinfo appInfo = getAppinfo(sharingOrderDto.getcUserId());
WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId());
//准备分账列表
WxProfitSharingReceiver psReceiverQ = new WxProfitSharingReceiver();
psReceiverQ.setTenantId(wxPayOrder.getTenantId());
psReceiverQ.setMerchantId(merchantId);
psReceiverQ.setTenantId(sharingOrderDto .getTenantId());
psReceiverQ.setMerchantId(sharingOrderDto.getMerchantId() );
psReceiverQ.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode());
psReceiverQ.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode());
List<WxProfitSharingReceiver> psReceiverList = wxProfitSharingReceiverMapper.findList(psReceiverQ);
@@ -147,84 +131,60 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
}
return createPSOrder(wxPayOrder, merchantId, psReceiverList, false);
}
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();
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 {
record.setOrderId(sharingOrderDto.getOrderId());
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.get Id());
record.setPayAmount(wxPayOrder .getPayAmount());
record.setMerchantId(merchantId );
record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_UNKNOWN.getCode());
record.setTenantId(sharingOrderDto.getTenantId());
record.setTransactionId(sharingOrderDto.getTransactionId());
record.setOrderId(sharingOrderDto.getOrderId());
record.setPayAmount(sharingOrderDto.getPayAmount());
record.setMerchantId(sharingOrderDto.getMerchantId());
record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_UNKNOWN.getCode());
record.setCreateTime(currentDate);
record.setUpdateTime(currentDate);
record.setPayTimeStart(currentDate);
record.setPayTimeEnd(currentDate);
record.setType(sharingOrderDto.getType());
wxProfitSharingOrderMapper.insertSelective(record);
}
// 添加分账接收方
final WxProfitSharingOrder frecord = record;
JSONArray receivers = new JSONArray();
List <WxProfitSharingResult> resultList = new ArrayList<WxProfitSharingResult>();
for (int i=0;i<psReceiverList.size();i++){
WxProfitSharingReceiver receiver = psReceiverList.get(i);
psReceiverList.stream().forEach(receiver->{
Date currentDate = new Date();
JSONObject jo = new JSONObject();
jo.put("type",EnumProfitSharingReceiverType.getEnum(receiver.getReceiverType()).getMessage());
jo.put("account",receiver.getReceiverAccount());
jo.put("amount", wxPayOrder .getShareAmount()); //temp: sharing all money with only owner
jo.put("amount", sharingOrderDto .getShareAmount()); //temp: sharing all money with only owner
jo.put("description",receiver.getReceiverComments());
receivers.add(jo);
WxProfitSharingResult result = new WxProfitSharingResult();
result.setId(idworker.nextId());
result.setTenantId(record.getTenantId());
result.setMerchantId(record.getMerchantId());
result.setSharingOrderId(record.getId());
result.setTenantId(f record.getTenantId());
result.setMerchantId(f record.getMerchantId());
result.setSharingOrderId(f record.getId());
result.setSharingReceiverId(receiver.getId());
result.setPayAmount(wxPayOrder .getShareAmount());
result.setPayAmount(sharingOrderDto .getShareAmount());
result.setCreateTime(currentDate);
result.setUpdateTime(currentDate);
result.setSharingStatus(EnumProfitSharingResultStatus.PROFIT_SHARING_RESULT_PENDING.getCode());
resultList.add(result);
}
});
//分账提交
WxProfitSharingP psCmd = new WxProfitSharingP();
@@ -233,7 +193,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
psCmd.setAppid(appInfo.getParentAppId());
psCmd.setSub_appid(appInfo.getAppId());
psCmd.setNonce_str(Utility.generate32UUID());
psCmd.setTransaction_id(wxPayOrder .getTransactionId());
psCmd.setTransaction_id(sharingOrderDto .getTransactionId());
psCmd.setOut_order_no(record.getId().toString());
psCmd.setSign_type("HMAC-SHA256");
psCmd.setReceivers(receivers.toJSONString());
@@ -241,13 +201,15 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
String response;
try {
psCmd.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(psCmd), payAccount.getApiKey()));
if(!multiCmd ) {
if(sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode()) ) {
response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(psCmd), payAccount.getCertPath(), payAccount.getMchId());
} else {
} 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(EnumProfitSharingStatus.PROFIT_SHARING_REQ_FAILED.getCode());
record.setSharingStatus(EnumProfitSharingOrder Status.PROFIT_SHARING_REQ_FAILED.getCode());
record.setErrorMsg(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage());
record.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
@@ -259,7 +221,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
String return_code = returnMap.get("return_code");
if (!"SUCCESS".equals(return_code)) {
record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_REQ_FAILED.getCode());
record.setSharingStatus(EnumProfitSharingOrder Status.PROFIT_SHARING_REQ_FAILED.getCode());
record.setErrorMsg(returnMap.get("return_msg"));
record.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
@@ -275,7 +237,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
String result_code = returnMap.get("result_code");
if (!"SUCCESS".equals(result_code)) {
record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_APPLY_FAILED.getCode());
record.setSharingStatus(EnumProfitSharingOrder Status.PROFIT_SHARING_APPLY_FAILED.getCode());
record.setUpdateTime(new Date());
record.setErrorMsg(returnMap.get("err_code_des"));
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
@@ -283,7 +245,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
}
record.setSharingOrderNo(returnMap.get("order_id"));
record.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_ACCEPTED.getCode());
record.setSharingStatus(EnumProfitSharingOrder Status.PROFIT_SHARING_ACCEPTED.getCode());
record.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
for (WxProfitSharingResult result:resultList) {
@@ -295,15 +257,16 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public ResultData querySharingOrder(WxPayOrder wxPay Order) {
public ResultData querySharingOrder(WxProfitSharingOrder wxProfitSharing Order) {
WxProfitSharingOrder record = new WxProfitSharingOrder();
WxAppinfo appInfo = getAppinfo(wxPayOrder);
WxPayOrder wxPayOrder = wxPayOrderMapper.selectByPrimaryKey(wxProfitSharingOrder.getOrderId());
WxAppinfo appInfo = getAppinfo(wxPayOrder.getcUserId());
WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId());
//是否已创建分账订单
record.setOrderId(wxPayOrder.get Id());
record.setOrderId(wxProfitSharingOrder.getOrder Id());
record = wxProfitSharingOrderMapper.selectOne(record);
if (record == null)
return new ResultData(ErrorCode.ORDER_IS_NOT_FIND);
@@ -313,7 +276,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
wxProfitSharingQueryP.setMch_id(payAccount.getMchId());
wxProfitSharingQueryP.setSub_mch_id(payAccount.getSubMchId());
wxProfitSharingQueryP.setNonce_str(Utility.generate32UUID());
wxProfitSharingQueryP.setTransaction_id(wxPayOrder.getTransactionId ());
wxProfitSharingQueryP.setTransaction_id(wxProfitSharingOrder.getTransaction ());
wxProfitSharingQueryP.setOut_order_no(record.getId().toString());
wxProfitSharingQueryP.setSign_type("HMAC-SHA256");
String response;
@@ -415,11 +378,42 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
}
// 分账完结
private ResultData finishPSOrder(WxPayOrder wxPayOrder) {
WxAppinfo appInfo = getAppinfo(wxPayOrder);
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public ResultData finishSharingOrder(WxSharingOrderDto sharingOrderDto) {
final IdWorker idworker = IdWorker.get();
if (!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_FINISH.getCode())){
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
}
WxAppinfo appInfo = getAppinfo(sharingOrderDto.getcUserId());
WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId());
WxOrder wxOrder = wxOrderMapper.selectByPrimaryKey(wxPayOrder.getOrderId());
//分账检查是否已创建分账订单
WxProfitSharingOrder record = new WxProfitSharingOrder();
record.setOrderId(sharingOrderDto.getOrderId());
record = wxProfitSharingOrderMapper.selectOne(record);
if (record == null) {
//创建分账订单
Date currentDate = new Date();
record = new WxProfitSharingOrder();
record.setId(idworker.nextId());
record.setTenantId(sharingOrderDto.getTenantId());
record.setTransactionId(sharingOrderDto.getTransactionId());
record.setOrderId(sharingOrderDto.getOrderId());
record.setPayAmount(sharingOrderDto.getPayAmount());
record.setMerchantId(sharingOrderDto.getMerchantId());
record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_UNKNOWN.getCode());
record.setCreateTime(currentDate);
record.setUpdateTime(currentDate);
record.setPayTimeStart(currentDate);
record.setPayTimeEnd(currentDate);
record.setType(sharingOrderDto.getType());
wxProfitSharingOrderMapper.insertSelective(record);
}
//分账提交
WxProfitSharingFinishP psFCmd = new WxProfitSharingFinishP();
@@ -427,8 +421,8 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
psFCmd.setSub_mch_id(payAccount.getSubMchId());
psFCmd.setAppid(appInfo.getParentAppId());
psFCmd.setNonce_str(Utility.generate32UUID());
psFCmd.setTransaction_id(wxPayOrder .getTransactionId());
psFCmd.setOut_order_no(wxPayO rd er.getId().toString());
psFCmd.setTransaction_id(sharingOrderDto .getTransactionId());
psFCmd.setOut_order_no(reco rd .getId().toString());
psFCmd.setSign_type("HMAC-SHA256");
String response;
@@ -436,6 +430,10 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
psFCmd.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(psFCmd), payAccount.getApiKey()));
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.updateByPrimaryKey(record);
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED);
}
@@ -444,18 +442,33 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
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.updateByPrimaryKey(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.updateByPrimaryKey(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());
record.setUpdateTime(new Date());
record.setErrorMsg(returnMap.get("err_code_des"));
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
return new ResultData(ErrorCode.PROFIT_SHARING_APPLY_FAILED.getCode(), returnMap.get("err_code_des"));
}
record.setSharingOrderNo(returnMap.get("order_id"));
record.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
record.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
return new ResultData(returnMap);
}
}