use of com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult in project fw-cloud-framework by liuweijw.
the class PayNotifyController method doNotifyResult.
public String doNotifyResult(HttpServletRequest request, HttpServletResponse response) {
String logPrefix = "【微信支付回调通知】";
log.info("====== 开始接收微信支付回调通知 ======");
try {
String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
log.info("{}通知请求数据:reqStr={}", logPrefix, xmlResult);
if (StringHelper.isBlank(xmlResult)) {
return WxPayNotifyResponse.fail("FAIL");
}
WxPayServiceImpl wxPayService = new WxPayServiceImpl();
WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlResult);
// 验证业务数据是否正确
if (!PayConstant.RETURN_VALUE_SUCCESS.equalsIgnoreCase(result.getResultCode()) || !PayConstant.RETURN_VALUE_SUCCESS.equalsIgnoreCase(result.getResultCode())) {
log.error("returnCode={},resultCode={},errCode={},errCodeDes={}", result.getReturnCode(), result.getResultCode(), result.getErrCode(), result.getErrCodeDes());
return WxPayNotifyResponse.fail("notify data failed");
}
// 总金额
Integer total_fee = result.getTotalFee();
// 商户系统订单号
String out_trade_no = result.getOutTradeNo();
// 查询payOrder记录
String payOrderId = out_trade_no;
PayOrder payOrder = payOrderService.findPayOrderByOrderId(payOrderId);
if (null == payOrder) {
log.error("Can't found payOrder form db. payOrderId={}, ", payOrderId);
return WxPayNotifyResponse.fail("未查询到相应订单信息");
}
// 查询payChannel记录
String mchId = payOrder.getMch_id();
String channelId = payOrder.getChannelId();
PayChannel payChannel = payChannelService.findPayChannel(channelId, mchId);
if (null == payChannel) {
log.error("Can't found payChannel form db. mchId={} channelId={}, ", payOrderId, mchId, channelId);
return WxPayNotifyResponse.fail("未查询到订单相关渠道信息");
}
WxPayConfig wxPayConfig = WxPayUtil.getWxPayConfig(payChannel.getParam(), result.getTradeType(), wxPayProperties.getCertRootPath(), wxPayProperties.getNotifyUrl());
wxPayService.setConfig(wxPayConfig);
// 签名校验
result.checkResult(wxPayService, null, false);
// 核对金额
long wxPayAmt = new BigDecimal(total_fee).longValue();
long dbPayAmt = payOrder.getAmount().longValue();
if (dbPayAmt != wxPayAmt) {
log.error("db payOrder record payPrice not equals total_fee. total_fee={},payOrderId={}", total_fee, payOrderId);
return WxPayNotifyResponse.fail("支付金额不正确");
}
// 处理订单
// 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
int payStatus = payOrder.getStatus();
if (payStatus == PayConstant.PAY_STATUS_COMPLETE) {
// 处理完成
log.info("====== 订单已经完成直接返回 ======");
return WxPayNotifyResponse.success("OK");
}
if (payStatus != PayConstant.PAY_STATUS_SUCCESS) {
boolean updatePayOrderRows = payOrderService.updatePayOrderStatus4Success(payOrder.getPayOrderId());
if (!updatePayOrderRows) {
log.error("{}更新支付状态失败,将payOrderId={},更新payStatus={}失败", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_FAILED);
return WxPayNotifyResponse.fail("处理订单失败");
}
log.error("{}更新支付状态成功,将payOrderId={},更新payStatus={}成功", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
}
// 业务系统后端通知
this.notifyService.notifyPayOrder(payOrder);
log.info("====== 完成接收微信支付回调通知 ======");
return WxPayNotifyResponse.success("OK");
} catch (WxPayException e) {
// 出现业务错误
log.error("微信回调结果异常,异常原因" + e);
log.info("{}请求数据result_code=FAIL", logPrefix);
log.info("err_code:", e.getErrCode());
log.info("err_code_des:", e.getErrCodeDes());
return WxPayNotifyResponse.fail(e.getMessage());
} catch (Exception e) {
log.error("微信回调结果异常,异常原因" + e);
return WxPayNotifyResponse.fail(e.getMessage());
}
}
Aggregations