|  | @@ -19,7 +19,9 @@ import com.yonge.cooleshow.portal.dao.SmsCouponHistoryDao;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.portal.domain.*;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.portal.service.*;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.sdk.adapay.PaymentSdk;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.sdk.req.OrderCloseReq;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.sdk.req.OrderPayReq;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.sdk.res.OrderCloseRes;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.sdk.res.OrderPayRes;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.service.PaymentService;
 | 
	
		
			
				|  |  |  import org.slf4j.Logger;
 | 
	
	
		
			
				|  | @@ -291,14 +293,14 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
 | 
	
		
			
				|  |  |              if (timeOutOrder.getStatus() == 0) {
 | 
	
		
			
				|  |  |                  payingOrderHandle(timeOutOrder);
 | 
	
		
			
				|  |  |              } else if (timeOutOrder.getStatus() == 6) {
 | 
	
		
			
				|  |  | -                cancelOrder(timeOutOrder.getId());
 | 
	
		
			
				|  |  | +                cancelOrder(timeOutOrder.getId(), "超时取消");
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          return timeOutOrders.size();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public void cancelOrder(Long orderId) {
 | 
	
		
			
				|  |  | +    public void cancelOrder(Long orderId, String message) {
 | 
	
		
			
				|  |  |          //查询未付款的取消订单
 | 
	
		
			
				|  |  |          OmsOrderExample example = new OmsOrderExample();
 | 
	
		
			
				|  |  |          example.createCriteria().andIdEqualTo(orderId).andDeleteStatusEqualTo(0);
 | 
	
	
		
			
				|  | @@ -327,9 +329,22 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              UserOrderPayment orderPayment = userOrderPaymentService.getByOrderNo(cancelOrder.getOrderSn());
 | 
	
		
			
				|  |  |              if (null != orderPayment) {
 | 
	
		
			
				|  |  | +                // 发起关单
 | 
	
		
			
				|  |  | +                OrderCloseReq orderCloseReq = new OrderCloseReq();
 | 
	
		
			
				|  |  | +                orderCloseReq.setPaymentId(orderPayment.getTransNo());
 | 
	
		
			
				|  |  | +                orderCloseReq.setReason(message);
 | 
	
		
			
				|  |  | +                OrderCloseRes closeRes = adapayPaymentService.orderClose(orderCloseReq);
 | 
	
		
			
				|  |  | +                if (closeRes.isStatus()) {
 | 
	
		
			
				|  |  | +                    orderPayment.setCloseStatus(PayStatusEnum.pending.getCode());
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    orderPayment.setCloseStatus(PayStatusEnum.failed.getCode());
 | 
	
		
			
				|  |  | +                    orderPayment.setCloseFailMsg(closeRes.getFailMsg());
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                  //更新付款单
 | 
	
		
			
				|  |  |                  orderPayment.setStatus(PayStatusEnum.failed.getCode());
 | 
	
		
			
				|  |  | -                orderPayment.setPayFailMsg("交易取消");
 | 
	
		
			
				|  |  | +                orderPayment.setPayFailMsg(message);
 | 
	
		
			
				|  |  | +                orderPayment.setUpdateTime(new Date());
 | 
	
		
			
				|  |  |                  userOrderPaymentService.saveOrUpdate(orderPayment);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
	
		
			
				|  | @@ -459,6 +474,11 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
 | 
	
		
			
				|  |  |              orderPayRes.setMessage("订单不存在");
 | 
	
		
			
				|  |  |              return orderPayRes;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        if (detail.getStatus() != 0 && detail.getStatus() != 6) {
 | 
	
		
			
				|  |  | +            orderPayRes.setStatus(false);
 | 
	
		
			
				|  |  | +            orderPayRes.setMessage("订单已付款或已关闭");
 | 
	
		
			
				|  |  | +            return orderPayRes;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          UserOrderPayment userOrderPayment = userOrderPaymentService.getByOrderNo(detail.getOrderSn());
 | 
	
		
			
				|  |  |          payReq.orderStatus(detail.getStatus());
 | 
	
		
			
				|  |  |          payReq.setOrderNo(detail.getOrderSn());
 | 
	
	
		
			
				|  | @@ -471,13 +491,14 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
 | 
	
		
			
				|  |  |          // 支付中的返回拉起信息
 | 
	
		
			
				|  |  |          orderPayRes = adapayPaymentService.orderPay(payReq);
 | 
	
		
			
				|  |  |          if (!orderPayRes.isStatus()) {
 | 
	
		
			
				|  |  | -            if (orderPayRes.getPayStatus() != null
 | 
	
		
			
				|  |  | -            && PayStatusEnum.failed.getCode().equals(orderPayRes.getPayStatus().getCode())) {
 | 
	
		
			
				|  |  | -                // 异常将取消订单
 | 
	
		
			
				|  |  | -                cancelOrder(detail.getId());
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +            // 异常将取消订单
 | 
	
		
			
				|  |  | +            cancelOrder(detail.getId(),orderPayRes.getMessage());
 | 
	
		
			
				|  |  |              throw new BizException(orderPayRes.getMessage());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        if (!orderPayRes.getPay_amt().equals(detail.getPayAmount().setScale(2, RoundingMode.HALF_UP).toString())) {
 | 
	
		
			
				|  |  | +            cancelOrder(detail.getId(),"订单金额和实际支付金额不符");
 | 
	
		
			
				|  |  | +            throw new BizException("订单金额和实际支付金额不符");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          // 订单状态和付款状态都是付款中,直接返回付款拉起信息
 | 
	
		
			
				|  |  |          if (OrderStatusEnum.PAYING.getCode().equals(payReq.getOrderStatus().getCode())
 | 
	
		
			
				|  |  |                  && PayStatusEnum.pending.getCode().equals(orderPayRes.getPayStatus().getCode())) {
 | 
	
	
		
			
				|  | @@ -488,7 +509,7 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
 | 
	
		
			
				|  |  |          OmsOrder omsOrder = new OmsOrder();
 | 
	
		
			
				|  |  |          omsOrder.setId(detail.getId());
 | 
	
		
			
				|  |  |          if (orderPayRes.getPayStatus().getCode().equals(OrderStatusEnum.PAYING.getCode())) {
 | 
	
		
			
				|  |  | -            omsOrder.setStatus(1);
 | 
	
		
			
				|  |  | +            omsOrder.setStatus(0);
 | 
	
		
			
				|  |  |              orderMapper.updateByPrimaryKeySelective(omsOrder);
 | 
	
		
			
				|  |  |              // 保存订单支付表
 | 
	
		
			
				|  |  |              if (userOrderPayment == null) {
 | 
	
	
		
			
				|  | @@ -506,29 +527,104 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
 | 
	
		
			
				|  |  |              userOrderPaymentService.saveOrUpdate(userOrderPayment);
 | 
	
		
			
				|  |  |          }else {
 | 
	
		
			
				|  |  |              // 异常将取消订单
 | 
	
		
			
				|  |  | -            cancelOrder(detail.getId());
 | 
	
		
			
				|  |  | +            cancelOrder(detail.getId(), orderPayRes.getMessage());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          return orderPayRes;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public void orderCallback(String data) {
 | 
	
		
			
				|  |  | +    public void orderCallback(String data, String type) {
 | 
	
		
			
				|  |  |          JSONObject hfRes = JSONObject.parseObject(data);
 | 
	
		
			
				|  |  | -        if (PayStatusEnum.succeeded.getCode().equals(hfRes.getString("status"))) {
 | 
	
		
			
				|  |  | -            //订单完成
 | 
	
		
			
				|  |  | -            OmsOrderDetail orderDetail = detail(hfRes.getString("order_no"));
 | 
	
		
			
				|  |  | -            if (null == orderDetail) {
 | 
	
		
			
				|  |  | -                log.error("汇付支付回调,订单未找到。 req is {}", data);
 | 
	
		
			
				|  |  | -                return;
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            if (orderDetail.getStatus() == 0) {
 | 
	
		
			
				|  |  | -                orderSuccess(orderDetail, hfRes);
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                log.error("汇付支付回调,订单状态异常。 req is {}", data);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (type.equals("payment.succeeded")) {//支付成功
 | 
	
		
			
				|  |  | +            paymentSucceededHandle(hfRes);
 | 
	
		
			
				|  |  | +        } else if (type.equals("payment.failed")) {//支付失败
 | 
	
		
			
				|  |  | +            paymentFailedHandle(hfRes);
 | 
	
		
			
				|  |  | +        } else if (type.equals("payment.close.succeeded")) {//支付关单成功
 | 
	
		
			
				|  |  | +            paymentCloseSucceededHandle(hfRes);
 | 
	
		
			
				|  |  | +        } else if (type.equals("payment.close.failed")) {//支付关单失败
 | 
	
		
			
				|  |  | +            paymentCloseFailedHandle(hfRes);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /***
 | 
	
		
			
				|  |  | +     * 处理回调-支付成功
 | 
	
		
			
				|  |  | +     * @author liweifan
 | 
	
		
			
				|  |  | +     * @param: hfRes
 | 
	
		
			
				|  |  | +     * @updateTime 2022/4/27 15:09
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private void paymentSucceededHandle(JSONObject hfRes) {
 | 
	
		
			
				|  |  | +        //订单完成
 | 
	
		
			
				|  |  | +        OmsOrder detail = detail(hfRes.getString("order_no"));
 | 
	
		
			
				|  |  | +        if (null == detail) {
 | 
	
		
			
				|  |  | +            log.error("汇付支付回调,订单未找到。 req is {}", hfRes.toJSONString());
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (detail.getStatus() == 0) {
 | 
	
		
			
				|  |  | +            orderSuccess(detail, hfRes);
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            log.error("汇付支付回调,订单状态异常。 req is {}", hfRes.toJSONString());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /***
 | 
	
		
			
				|  |  | +     * 处理回调-支付失败
 | 
	
		
			
				|  |  | +     * @author liweifan
 | 
	
		
			
				|  |  | +     * @param: hfRes
 | 
	
		
			
				|  |  | +     * @updateTime 2022/4/27 15:09
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private void paymentFailedHandle(JSONObject hfRes) {
 | 
	
		
			
				|  |  | +        //订单完成
 | 
	
		
			
				|  |  | +        OmsOrder detail = detail(hfRes.getString("order_no"));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (null == detail) {
 | 
	
		
			
				|  |  | +            log.error("汇付支付回调,订单未找到。 req is {}", hfRes.toJSONString());
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (detail.getStatus() == 0) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            cancelOrder(detail.getId(), "支付回调失败");
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            log.error("汇付支付回调,订单状态异常。 req is {}", hfRes.toJSONString());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /***
 | 
	
		
			
				|  |  | +     * 支付关单成功
 | 
	
		
			
				|  |  | +     * @author liweifan
 | 
	
		
			
				|  |  | +     * @param: hfRes
 | 
	
		
			
				|  |  | +     * @updateTime 2022/4/27 15:33
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private void paymentCloseSucceededHandle(JSONObject hfRes) {
 | 
	
		
			
				|  |  | +        UserOrderPayment orderPayment = userOrderPaymentService.getByOrderNo(hfRes.getString("payment_id"));
 | 
	
		
			
				|  |  | +        if (null == orderPayment) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        orderPayment.setStatus(PayStatusEnum.succeeded.getCode());
 | 
	
		
			
				|  |  | +        orderPayment.setUpdateTime(new Date());
 | 
	
		
			
				|  |  | +        userOrderPaymentService.saveOrUpdate(orderPayment);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /***
 | 
	
		
			
				|  |  | +     * 支付关单失败
 | 
	
		
			
				|  |  | +     * @author liweifan
 | 
	
		
			
				|  |  | +     * @param: hfRes
 | 
	
		
			
				|  |  | +     * @updateTime 2022/4/27 15:33
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private void paymentCloseFailedHandle(JSONObject hfRes) {
 | 
	
		
			
				|  |  | +        UserOrderPayment orderPayment = userOrderPaymentService.getByOrderNo(hfRes.getString("payment_id"));
 | 
	
		
			
				|  |  | +        if (null == orderPayment) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        orderPayment.setStatus(PayStatusEnum.failed.getCode());
 | 
	
		
			
				|  |  | +        orderPayment.setPayFailMsg("支付关单回调失败-" + hfRes.getString("error_msg"));
 | 
	
		
			
				|  |  | +        orderPayment.setUpdateTime(new Date());
 | 
	
		
			
				|  |  | +        userOrderPaymentService.saveOrUpdate(orderPayment);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /***
 | 
	
		
			
				|  |  |       * 处理支付中订单
 | 
	
		
			
				|  |  |       * @author liweifan
 | 
	
	
		
			
				|  | @@ -546,11 +642,12 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
 | 
	
		
			
				|  |  |              Map<String, Object> resMap = paymentSdk.queryPayment(orderPayment.getTransNo());
 | 
	
		
			
				|  |  |              //支付失败
 | 
	
		
			
				|  |  |              if (PayStatusEnum.failed.getCode().equals(resMap.get("status").toString())) {
 | 
	
		
			
				|  |  | -                cancelOrder(userOrder.getId());
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            //支付成功
 | 
	
		
			
				|  |  | -            if (PayStatusEnum.succeeded.getCode().equals(resMap.get("status").toString())) {
 | 
	
		
			
				|  |  | +                cancelOrder(userOrder.getId(), "支付失败");
 | 
	
		
			
				|  |  | +            }else if (PayStatusEnum.succeeded.getCode().equals(resMap.get("status").toString())) {
 | 
	
		
			
				|  |  | +                // 支付成功
 | 
	
		
			
				|  |  |                  orderSuccess(userOrder,null);
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                cancelOrder(userOrder.getId(), "支付超时");
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          } catch (Exception e) {
 | 
	
		
			
				|  |  |              e.printStackTrace();
 | 
	
	
		
			
				|  | @@ -574,11 +671,15 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
 | 
	
		
			
				|  |  |          } else if (orderPayment.getPayChannel().equals("wx_lite")) {
 | 
	
		
			
				|  |  |              paySuccess(detail.getId(),2);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        String pay_amt = hfRes.getString("pay_amt");
 | 
	
		
			
				|  |  | +        if (!pay_amt.equals(detail.getPayAmount().setScale(2, RoundingMode.HALF_UP).toString())) {
 | 
	
		
			
				|  |  | +            orderPayment.setPayFailMsg("订单金额和实际支付金额不符");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          orderPayment.setStatus(PayStatusEnum.succeeded.getCode());
 | 
	
		
			
				|  |  |          orderPayment.setArrivalTime(new Date());
 | 
	
		
			
				|  |  |          if (null != hfRes) {
 | 
	
		
			
				|  |  |              try {
 | 
	
		
			
				|  |  | -                orderPayment.setPayAmt(new BigDecimal(hfRes.getString("pay_amt")));
 | 
	
		
			
				|  |  | +                orderPayment.setPayAmt(new BigDecimal(pay_amt));
 | 
	
		
			
				|  |  |                  orderPayment.setFeeAmt(new BigDecimal(hfRes.getString("fee_amt")));
 | 
	
		
			
				|  |  |              } catch (Exception e) {
 | 
	
		
			
				|  |  |                  e.printStackTrace();
 |