Browse Source

[支付][修改]:分帐错误处理逻辑更改

release_toaliyun_real
hupeng 7 years ago
parent
commit
b79d3fac2e
1 changed files with 170 additions and 169 deletions
  1. +170
    -169
      mallinkService/src/main/java/com/simple/service/impl/WxProfitSharingOrderServiceImpl.java

+ 170
- 169
mallinkService/src/main/java/com/simple/service/impl/WxProfitSharingOrderServiceImpl.java View File

@@ -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<WxProfitSharingReceiver> 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 <WxProfitSharingResult> resultList = new ArrayList<WxProfitSharingResult>();
for (int i=0;i<wxProfitSharingReceiverList.size();i++){
WxProfitSharingReceiver receiver = wxProfitSharingReceiverList.get(i);
Date currentDate = new Date();
JSONObject jo = new JSONObject();
jo.put("type",EnumProfitSharingReceiverType.getEnum(receiver.getReceiverType()).getMessage());
jo.put("account",receiver.getReceiverAccount());
jo.put("amount", total_amount); //temp: sharing all money with only owner
jo.put("description",receiver.getReceiverComments());
receivers.add(jo);

WxProfitSharingResult result = new WxProfitSharingResult();

result.setId(idworker.nextId());
result.setSharingOrderId(record.getId());
result.setSharingReceiverId(receiver.getId());
result.setPayAmount(total_amount);
result.setCreateTime(currentDate);
result.setUpdateTime(currentDate);
result.setSharingStatus(EnumProfitSharingResultStatus.PROFIT_SHARING_RESULT_PENDING.getCode());
resultList.add(result);
}
//分账提交
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<WxProfitSharingReceiver> 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 <WxProfitSharingResult> resultList = new ArrayList<WxProfitSharingResult>();
for (int i=0;i<wxProfitSharingReceiverList.size();i++){
WxProfitSharingReceiver receiver = wxProfitSharingReceiverList.get(i);
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("description",receiver.getReceiverComments());
receivers.add(jo);

wxProfitSharingP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingP), payAccount.getApiKey()));
WxProfitSharingResult result = new WxProfitSharingResult();

logger.info("wxProfitSharingP :" + wxProfitSharingP.toString());
String response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(wxProfitSharingP), payAccount.getCertPath(), payAccount.getMchId());

logger.info("response: " + response);
Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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 <WxProfitSharingResult> wxProfitSharingResultList = wxProfitSharingResultMapper.findList(result);
WxProfitSharingResult result = new WxProfitSharingResult();
result.setSharingOrderId(record.getId());
List <WxProfitSharingResult> wxProfitSharingResultList = wxProfitSharingResultMapper.findList(result);


WxProfitSharingReceiver wxProfitSharingReceiver;
WxProfitSharingResult wxProfitSharingResult;
WxProfitSharingReceiver wxProfitSharingReceiver;
WxProfitSharingResult wxProfitSharingResult;

for(int i=0; i<wxProfitSharingResultList.size();i++) {
wxProfitSharingResult = wxProfitSharingResultList.get(i);
wxProfitSharingReceiver = wxProfitSharingReceiverMapper
.selectByPrimaryKey(wxProfitSharingResult.getSharingReceiverId());
for(int i=0; i<wxProfitSharingResultList.size();i++) {
wxProfitSharingResult = wxProfitSharingResultList.get(i);
wxProfitSharingReceiver = wxProfitSharingReceiverMapper
.selectByPrimaryKey(wxProfitSharingResult.getSharingReceiverId());

for(int j=0; j<jReceivers.size();j++) {
JSONObject res = (JSONObject)jReceivers.get(j);
for(int j=0; j<jReceivers.size();j++) {
JSONObject res = (JSONObject)jReceivers.get(j);

if (res.getString("type").equals(wxProfitSharingReceiver.getReceiverType())
&& res.getString("account").equals(wxProfitSharingReceiver.getReceiverAccount())) {
if (res.getString("type").equals(wxProfitSharingReceiver.getReceiverType())
&& res.getString("account").equals(wxProfitSharingReceiver.getReceiverAccount())) {

wxProfitSharingResult.setFinishTime(res.getString("finish_time"));
wxProfitSharingResult.setUpdateTime(new Date());
wxProfitSharingResult.setSharingStatus(resultStatusMap.getIntValue(res.getString("result")));
wxProfitSharingResult.setFailedReason(res.getString("fail_reason"));
wxProfitSharingResultMapper.updateByPrimaryKey(wxProfitSharingResult);
}
wxProfitSharingResult.setFinishTime(res.getString("finish_time"));
wxProfitSharingResult.setUpdateTime(new Date());
wxProfitSharingResult.setSharingStatus(resultStatusMap.getIntValue(res.getString("result")));
wxProfitSharingResult.setFailedReason(res.getString("fail_reason"));
wxProfitSharingResultMapper.updateByPrimaryKey(wxProfitSharingResult);
}
}

return new ResultData(returnMap);

}catch (Exception e) {
throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage());
}

return new ResultData(returnMap);
}
}

Loading…
Cancel
Save