从零到企业级SSM电商项目实战教程(三十九)查询订单支付状态功能开发

OrderController.java

@RequestMapping("alipay_callback.do")
@ResponseBody
public Object alipayCallback(HttpServletRequest request) {
    Map params = Maps.newHashMap();

    Map requestParams = request.getParameterMap();
    for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
        String name = (String) iter.next();
        String[] values = (String[]) requestParams.get(name);
        String valueStr = "";
        for (int i = 0; i < values.length; i++) {
            valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
        }
        params.put(name, valueStr);
    }
    logger.info("支付宝回调,sign:{},trade_status:{},参数:{}", params.get("sign"), params.get("trade_status"), params.toString());

    //非常重要的一点,验证回调的正确性(确认是支付宝发送的),并且避免重复通知
    params.remove("sign_type");
    try {
        boolean alipayRSACheckedV2 = AlipaySignature.rsaCheckV2(params, Configs.getAlipayPublicKey(), "utf-8", Configs.getSignType());

        if (!alipayRSACheckedV2) {
            return ServerResponse.createByErrorMessage("非法结束,验证不通过,再恶意请求将报警。");
        }

    } catch (AlipayApiException e) {
        logger.error("支付宝验证回调异常", e);
    }

    //验证各种数据
    ServerResponse serverResponse = iOrderService.aliCallback(params);
    if (serverResponse.isSuccess()) {
        return Const.AlipayCallback.RESPONSE_SUCCESS;
    }
    return Const.AlipayCallback.RESPONSE_FAILED;
}

@RequestMapping("query_order_pay_status.do")
@ResponseBody
public ServerResponse queryOrderPayStatus(HttpSession session, Long orderNo) {
    User user = (User) session.getAttribute(Const.CURRENT_USER);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }
    ServerResponse serverResponse = iOrderService.queryOrderPayStatus(user.getId(), orderNo);
    if (serverResponse.isSuccess()) {
        return ServerResponse.createBySuccess(true);
    }
    return ServerResponse.createBySuccess(false);
}

IOrderService.java

ServerResponse aliCallback(Map params);

ServerResponse queryOrderPayStatus(Integer userId, Long orderNo);

OrderServiceImpl

public ServerResponse aliCallback(Map params) {
    Long orderNo = Long.parseLong(params.get("out_trade_no"));
    String tradeNo = params.get("trade_no");
    String tradeStatus = params.get("trade_status");

    Order order = orderMapper.selectByOrderNo(orderNo);
    if (order == null) {
        return ServerResponse.createByErrorMessage("非本商城的订单,回调忽略");
    }
    if (order.getStatus() >= Const.OrderStatusEnum.PAID.getCode()) {
        return ServerResponse.createBySuccess("支付宝重复调用");
    }
    if (Const.AlipayCallback.TRADE_STATUS_TRADE_SUCCESS.equals(tradeStatus)) {
        order.setPaymentTime(DateTimeUtil.strToDate(params.get("gmt_payment")));
        order.setStatus(Const.OrderStatusEnum.PAID.getCode());
        orderMapper.updateByPrimaryKeySelective(order);
    }
    PayInfo payInfo = new PayInfo();
    payInfo.setUserId(order.getUserId());
    payInfo.setOrderNo(order.getOrderNo());
    payInfo.setPayPlatform(Const.PayPlatformEnum.ALIPAY.getCode());
    payInfo.setPlatformNumber(tradeNo);
    payInfo.setPlatformStatus(tradeStatus);

    payInfoMapper.insert(payInfo);

    return ServerResponse.createBySuccess();

}

public ServerResponse queryOrderPayStatus(Integer userId, Long orderNo) {
    Order order = orderMapper.selectByUserIdAndOrderNo(userId, orderNo);
    if (order == null) {
        return ServerResponse.createByErrorMessage("用户没有该订单");
    }
    if (order.getStatus() >= Const.OrderStatusEnum.PAID.getCode()) {
        return ServerResponse.createBySuccess();
    }
    return ServerResponse.createByError();
}

OrderMapper.java

Order selectByOrderNo(Long orderNo);

OrderMapper.xml


Const.java

public enum OrderStatusEnum{
    CANCELED(0,"已取消"),
    NO_PAY(10,"未支付"),
    PAID(20,"已付款"),
    SHIPPED(40,"已发货"),
    ORDER_SUCCESS(50,"订单完成"),
    ORDER_CLOSE(60,"订单关闭");


    OrderStatusEnum(int code,String value){
        this.code = code;
        this.value = value;
    }
    private String value;
    private int code;

    public String getValue() {
        return value;
    }

    public int getCode() {
        return code;
    }

    public static OrderStatusEnum codeOf(int code){
        for(OrderStatusEnum orderStatusEnum : values()){
            if(orderStatusEnum.getCode() == code){
                return orderStatusEnum;
            }
        }
        throw new RuntimeException("没有找到对应的枚举");
    }
}
public interface  AlipayCallback{
    String TRADE_STATUS_WAIT_BUYER_PAY = "WAIT_BUYER_PAY";
    String TRADE_STATUS_TRADE_SUCCESS = "TRADE_SUCCESS";

    String RESPONSE_SUCCESS = "success";
    String RESPONSE_FAILED = "failed";
}

public enum PayPlatformEnum{
    ALIPAY(1,"支付宝");

    PayPlatformEnum(int code,String value){
        this.code = code;
        this.value = value;
    }
    private String value;
    private int code;

    public String getValue() {
        return value;
    }

    public int getCode() {
        return code;
    }
}
本站所有文章均来自互联网,如有侵权,请联系站长删除。极客文库 » 从零到企业级SSM电商项目实战教程(三十九)查询订单支付状态功能开发
分享到:
赞(0)

评论抢沙发

评论前必须登录!