Browse Source

[系统][新增]:分账定时从微信拉取分账结果

release_toaliyun_real
hupeng 7 years ago
parent
commit
46b7ced1f1
7 changed files with 150 additions and 37 deletions
  1. +65
    -0
      mallinkSchedule/src/main/java/com/iformall/schedule/SharingOrderResultSchedule.java
  2. +12
    -0
      mallinkService/src/main/java/com/iformall/domain/po/WxProfitSharingResult.java
  3. +2
    -2
      mallinkService/src/main/java/com/iformall/pay/WxProfitSharing.java
  4. +4
    -4
      mallinkService/src/main/java/com/iformall/pay/WxProfitSharingQueryP.java
  5. +64
    -29
      mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java
  6. +1
    -1
      mallinkService/src/main/resources/mapper/WxProfitSharingOrderMapper.xml
  7. +2
    -1
      mallinkService/src/main/resources/mapper/WxProfitSharingResultMapper.xml

+ 65
- 0
mallinkSchedule/src/main/java/com/iformall/schedule/SharingOrderResultSchedule.java View File

@@ -0,0 +1,65 @@
package com.iformall.schedule;

import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.WxPayOrder;
import com.iformall.domain.po.WxProfitSharingOrder;
import com.iformall.enums.EnumProfitSharingStatus;
import com.iformall.mapper.*;
import com.iformall.service.WxPayOrderService;
import com.iformall.service.WxProfitSharingOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SharingOrderResultSchedule {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private WxProfitSharingOrderService wxProfitSharingOrderService;

@Autowired
private WxProfitSharingOrderMapper wxProfitSharingOrderMapper;

@Autowired
private WxPayOrderMapper wxPayOrderMapper;

@Scheduled(cron = "0 15 0 * * ?") // 每天凌晨00:15更新分账结果
//@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
public void sharingOrderResultSchedule() {

WxProfitSharingOrder wxProfitSharingOrder = new WxProfitSharingOrder();
wxProfitSharingOrder.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_ACCEPTED.getCode());
List<WxProfitSharingOrder> list = wxProfitSharingOrderMapper.findList(wxProfitSharingOrder);
for(WxProfitSharingOrder sharingOrder : list) {
try {
WxPayOrder wxPayOrder = wxPayOrderMapper.selectByPrimaryKey(sharingOrder.getOrderId());
wxProfitSharingOrderService.querySharingOrder(wxPayOrder);
} catch (Exception e){
logger.error("状态更新失败:" + EnumProfitSharingStatus.PROFIT_SHARING_ACCEPTED.getMessage()
+ " ID:" +sharingOrder.getId()
+ " MSG:" + e.getMessage());
}
}

wxProfitSharingOrder.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_PROCESSING.getCode());
list = wxProfitSharingOrderMapper.findList(wxProfitSharingOrder);
for(WxProfitSharingOrder sharingOrder : list) {
try {
WxPayOrder wxPayOrder = wxPayOrderMapper.selectByPrimaryKey(sharingOrder.getOrderId());
wxProfitSharingOrderService.querySharingOrder(wxPayOrder);
} catch (Exception e){
logger.error("状态更新失败:" + EnumProfitSharingStatus.PROFIT_SHARING_PROCESSING.getMessage()
+ " ID:" +sharingOrder.getId()
+ " MSG:" + e.getMessage());
}
}

}

}

+ 12
- 0
mallinkService/src/main/java/com/iformall/domain/po/WxProfitSharingResult.java View File

@@ -71,6 +71,11 @@ public class WxProfitSharingResult implements Serializable {
/*微信返回的失败原因**/
@io.swagger.annotations.ApiModelProperty(value="微信返回的失败原因",name="failedReason")
private String failedReason;
/*描述**/
@io.swagger.annotations.ApiModelProperty(value="描述",name="description")
private String description;


public Long getSharingOrderId() {
return sharingOrderId;
}
@@ -136,6 +141,13 @@ public class WxProfitSharingResult implements Serializable {
this.tenantId = tenantId;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public static enum Field
{


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

@@ -36,8 +36,8 @@ public class WxProfitSharing {
* @param params
* @return
*/
public static String queryOrder(Map<String, String> params) {
return doPost(PROFIT_SHARING_QUERY_URL, params);
public static String queryOrder(Map<String, String> params, String certPath, String certPass) {
return doPostSSL(PROFIT_SHARING_QUERY_URL, params, certPath, certPass);
}




+ 4
- 4
mallinkService/src/main/java/com/iformall/pay/WxProfitSharingQueryP.java View File

@@ -14,7 +14,7 @@ public class WxProfitSharingQueryP implements Serializable {
private String sign; // 签名
private String sign_type; // 签名方法
private String transaction_id ; // 支付订单号
private String out_trade_no; // 商户分账单号
private String out_order_no; // 商户分账单号


public String getMch_id() {
@@ -62,10 +62,10 @@ public class WxProfitSharingQueryP implements Serializable {
this.transaction_id = transaction_id;
}

public String getOut_trade_no() { return out_trade_no; }
public String getOut_order_no() { return out_order_no; }

public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
public void setOut_order_no(String out_order_no) {
this.out_order_no = out_order_no;
}

}

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

@@ -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(190403470006681600L)));
querySharingOrder(wxPayOrderMapper.selectByPrimaryKey(new Long(220465483168612352L)));
}

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.pushOrder(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getCertPath(), payAccount.getMchId());
wxProfitSharingQueryP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingQueryP),payAccount.getApiKey()));
response = WxProfitSharing.queryOrder(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);
}
}

+ 1
- 1
mallinkService/src/main/resources/mapper/WxProfitSharingOrderMapper.xml View File

@@ -10,7 +10,7 @@
<result column="pay_time_end" jdbcType="TIMESTAMP" property="payTimeEnd" />
<result column="pay_time_start" jdbcType="TIMESTAMP" property="payTimeStart" />
<result column="transaction_id" jdbcType="VARCHAR" property="transactionId" />
<result column="sharing_order_no" jdbcType="VARCHAR" property="sharingOrderId" />
<result column="sharing_order_no" jdbcType="VARCHAR" property="sharingOrderNo" />
<result column="error_msg" jdbcType="VARCHAR" property="errorMsg" />
<result column="sharing_status" jdbcType="INTEGER" property="sharingStatus" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />


+ 2
- 1
mallinkService/src/main/resources/mapper/WxProfitSharingResultMapper.xml View File

@@ -13,10 +13,11 @@
<result column="sharing_status" jdbcType="INTEGER" property="sharingStatus" />
<result column="finish_time" jdbcType="VARCHAR" property="finishTime" />
<result column="failed_reason" jdbcType="VARCHAR" property="failedReason" />
<result column="description" jdbcType="VARCHAR" property="description" />
</resultMap>
<sql id="allColumns">
`id`,`tenant_id`,`merchant_id`,`sharing_order_id`,`sharing_receiver_id`,`pay_amount`,`update_time`,`create_time`,`sharing_status`,`finish_time`,`failed_reason`
`id`,`tenant_id`,`merchant_id`,`sharing_order_id`,`sharing_receiver_id`,`pay_amount`,`update_time`,`create_time`,`sharing_status`,`finish_time`,`failed_reason`,`description`
</sql>

<sql id="dynamicWhereConditions">


Loading…
Cancel
Save