@@ -20,6 +20,7 @@ import com.iformall.pay.WxProfitSharingQueryP;
import com.iformall.service.WxProfitSharingOrderService;
import com.iformall.utils.BeanUtils;
import com.iformall.utils.Utility;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -121,7 +122,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
@Override
public void test() {
createSharingOrder(wxPayOrderMapper.selectByPrimaryKey(new Long(190403470006681600 L)));
querySharingOrder(wxPayOrderMapper.selectByPrimaryKey(new Long(220465483168612352 L)));
}
private WxAppinfo getAppinfo(WxPayOrder wxPayOrder) {
@@ -129,12 +130,12 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
WxCUser wxCUser = wxCUserMapper.selectByPrimaryKey(wxPayOrder.getcUserId());
if (wxCUser == null)
throw new MallinkException(ErrorCode.USER_IS_EMPTY.getCode(), ErrorCode.USER_IS_EMPTY.getMessage() );
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
wxAppinfo.setAppId(wxCUser.getAppId());
wxAppinfo = wxAppinfoMapper.selectOne(wxAppinfo);
if (wxAppinfo == null)
throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND.getCode(), ErrorCode.APP_ID_NOT_FOUND.getMessage() );
throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND);
return wxAppinfo;
}
@@ -190,7 +191,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
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.getCode(), ErrorCode.PROFIT_SHARING_RECEIVER_INVALID.getMessage() );
throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
}
JSONArray receivers = new JSONArray();
@@ -231,7 +232,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
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() );
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED);
}
logger.info("response: " + response);
Map<String, String> returnMap = WxPayment.xmlToMap(response);
@@ -249,7 +250,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
record.setErrorMsg(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage());
record.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
return new ResultData(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage() );
return new ResultData(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getCode());
}
String result_code = returnMap.get("result_code");
@@ -272,6 +273,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
}
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public ResultData querySharingOrder(WxPayOrder wxPayOrder) {
WxProfitSharingOrder record = new WxProfitSharingOrder();
@@ -283,7 +285,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
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() );
return new ResultData(ErrorCode.ORDER_IS_NOT_FIND);
//分账查询提交
WxProfitSharingQueryP wxProfitSharingQueryP = new WxProfitSharingQueryP();;
@@ -291,12 +293,12 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
wxProfitSharingQueryP.setSub_mch_id(payAccount.getSubMchId());
wxProfitSharingQueryP.setNonce_str(Utility.generate32UUID());
wxProfitSharingQueryP.setTransaction_id(wxPayOrder.getTransactionId());
wxProfitSharingQueryP.setOut_trade _no(record.getId().toString());
wxProfitSharingQueryP.setOut_order _no(record.getId().toString());
wxProfitSharingQueryP.setSign_type("HMAC-SHA256");
String response;
try {
wxProfitSharingQueryP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getApiKey()));
response = WxProfitSharing.push Order(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getCertPath(), payAccount.getMchId());
wxProfitSharingQueryP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingQueryP),payAccount.getApiKey()));
response = WxProfitSharing.query Order(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());
}
@@ -307,12 +309,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
}
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());
return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID);
}
String result_code = returnMap.get("result_code");
@@ -320,6 +317,11 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_APPLY_FAILED.getCode(), returnMap.get("result_msg"));
}
String out_order_no = returnMap.get("out_order_no");
if (StringUtils.isBlank(out_order_no) || !out_order_no.equals(record.getId().toString())){
return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID);
}
record.setSharingStatus((Integer) statusMap.get(returnMap.get("status")));
record.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateByPrimaryKey(record);
@@ -331,30 +333,63 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
result.setSharingOrderId(record.getId());
List <WxProfitSharingResult> wxProfitSharingResultList = wxProfitSharingResultMapper.findList(result);
WxProfitSharingReceiver wxProfitSharingReceiver;
WxProfitSharingResult wxProfitSharingResult;
for(int i=0; i<wxProfitSharingResultList.size();i++) {
wxProfitSharingResult = wxProfitSharingResultList.get(i);
wxProfitSharingReceiver = wxProfitSharingReceiverMapper
for(int j = 0; j < jReceivers.size() ; j++) {
JSONObject res = (JSONObject)jReceivers.get(j);
JSONObject des = new JSONObject();
des.put("type",res.getString("type"));
des.put("account",res.getString("account"));
des.put("description",res.getString("description"));
des.put("amount",res.getString("amount"));
int i = 0;
for(i = 0; i<wxProfitSharingResultList.size();i++) {
WxProfitSharingResult wxProfitSharingResult = wxProfitSharingResultList.get(i);
WxProfitSharingReceiver wxProfitSharingReceiver = wxProfitSharingReceiverMapper
.selectByPrimaryKey(wxProfitSharingResult.getSharingReceiverId());
if (wxProfitSharingReceiver == null) continue;
for(int j=0; j<jReceivers.size();j++) {
JSONObject res = (JSONObject)jReceivers.get(j);
if (res.getString("type").equals(wxProfitSharingReceiver.getReceiverType())
if (res.getString("type").equals(EnumProfitSharingReceiverType.getEnum(wxProfitSharingReceiver.getReceiverType()).getMessage())
&& 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"));
wxProfitSharingResult.setDescription(des.toJSONString());
wxProfitSharingResultMapper.updateByPrimaryKey(wxProfitSharingResult);
break;
}
}
}
if (i == wxProfitSharingResultList.size()){
WxProfitSharingResult wxProfitSharingResult = new WxProfitSharingResult();
wxProfitSharingResult.setTenantId(record.getTenantId());
wxProfitSharingResult.setMerchantId(record.getMerchantId());
wxProfitSharingResult.setSharingOrderId(record.getId());
wxProfitSharingResult.setSharingReceiverId(0L); //剩余钱分给特约商户,没有相应的接受者
List<WxProfitSharingResult> list = wxProfitSharingResultMapper.findList(wxProfitSharingResult);
if (list.size() > 0) {
wxProfitSharingResult = list.get(0);
}
wxProfitSharingResult.setPayAmount(Integer.valueOf(res.getString("amount")));
wxProfitSharingResult.setFinishTime(res.getString("finish_time"));
wxProfitSharingResult.setSharingStatus(resultStatusMap.getIntValue(res.getString("result")));
wxProfitSharingResult.setFailedReason(res.getString("fail_reason"));
wxProfitSharingResult.setDescription(des.toJSONString());
if (wxProfitSharingResult.getId() != null){
wxProfitSharingResult.setUpdateTime(new Date());
wxProfitSharingResultMapper.updateByPrimaryKeySelective(wxProfitSharingResult);
}
else {
wxProfitSharingResult.setCreateTime(new Date());
wxProfitSharingResult.setUpdateTime(new Date());
wxProfitSharingResult.setId(IdWorker.get().nextId());
wxProfitSharingResultMapper.insertSelective(wxProfitSharingResult);
}
}
}
return new ResultData(returnMap);
}
}