Search in sources :

Example 46 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.

the class OrderServiceImpl method wxPayNotify.

@Override
public void wxPayNotify(HttpServletRequest request, HttpServletResponse response) {
    String xmlResult = null;
    try {
        xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
    } catch (IOException e) {
        log.error(WxPayNotifyResponse.fail(e.getMessage()), e);
    }
    WxPayOrderNotifyResult result = null;
    try {
        result = wxPayService.parseOrderNotifyResult(xmlResult);
        if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())) {
            log.error(xmlResult);
        }
    } catch (WxPayException e) {
        log.error(e.getMessage(), e);
    }
    log.info("处理腾讯支付平台的订单支付, {}", result.getReturnMsg());
    String orderSn = result.getOutTradeNo();
    String payId = result.getTransactionId();
    // 分转化成元
    String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());
    Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
    if (order == null) {
        log.error("微信支付回调:订单不存在,orderSn:{}", orderSn);
        return;
    }
    // 检查这个订单是否已经处理过
    if (OrderUtil.hasPayed(order)) {
        log.error("微信支付回调:订单已经处理过了,orderSn:{}", orderSn);
        return;
    }
    // 检查支付订单金额
    if (!totalFee.equals(order.getActualPrice().toString())) {
        log.error("微信支付回调: 支付金额不符合,orderSn:{},totalFee:{}", order.getOrderSn(), totalFee);
        return;
    }
    order.setPayId(payId);
    order.setPayTime(LocalDateTime.now());
    order.setOrderStatus(OrderUtil.STATUS_PAY);
    order.setUpdateTime(new Date());
    if (!updateById(order)) {
        log.error("微信支付回调: 更新订单状态失败,order:{}", JSON.toJSONString(order.getOrderSn()));
        return;
    }
    // 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
    String email = iMemberService.getById(order.getUserId()).getEmail();
    if (StringUtils.isNotBlank(email)) {
        iMailService.sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl() + "/message/email");
    }
    // 删除redis中订单id
    redisCache.deleteZsetObject("order_zset", order.getId());
    // 取消订单超时未支付任务
    taskService.removeTask(new OrderUnpaidTask(order.getId()));
}
Also used : WxPayOrderNotifyResult(com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) IOException(java.io.IOException) OrderUnpaidTask(com.wayn.mobile.api.task.OrderUnpaidTask)

Example 47 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.

the class OrderServiceImpl method h5pay.

@Override
@Transactional(rollbackFor = Exception.class)
public R h5pay(String orderSn, Integer payType, HttpServletRequest request) {
    // 获取订单详情
    Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
    Long userId = order.getUserId();
    ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
    if (!ReturnCodeEnum.SUCCESS.equals(returnCodeEnum)) {
        return R.error(returnCodeEnum);
    }
    // 检测是否能够取消
    OrderHandleOption handleOption = OrderUtil.build(order);
    if (!handleOption.isPay()) {
        return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
    }
    // 保存支付方式
    boolean update = lambdaUpdate().set(Order::getPayType, payType).eq(Order::getOrderSn, orderSn).update();
    if (!update) {
        return R.error(ReturnCodeEnum.ORDER_SET_PAY_ERROR);
    }
    switch(Objects.requireNonNull(PayTypeEnum.of(payType))) {
        case WX:
            WxPayMpOrderResult result;
            try {
                WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
                orderRequest.setOutTradeNo(order.getOrderSn());
                orderRequest.setTradeType(WxPayConstants.TradeType.MWEB);
                orderRequest.setBody("订单:" + order.getOrderSn());
                // 元转成分
                int fee;
                BigDecimal actualPrice = order.getActualPrice();
                fee = actualPrice.multiply(new BigDecimal(100)).intValue();
                orderRequest.setTotalFee(fee);
                orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
                result = wxPayService.createOrder(orderRequest);
                return R.success().add("result", result);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
            }
        case ALI:
            // 初始化
            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGateway(), alipayConfig.getAppId(), alipayConfig.getRsaPrivateKey(), alipayConfig.getFormat(), alipayConfig.getCharset(), alipayConfig.getAlipayPublicKey(), alipayConfig.getSigntype());
            // 创建API对应的request,使用手机网站支付request
            AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
            // 在公共参数中设置回跳和通知地址
            String url = WaynConfig.getMobileUrl() + request.getContextPath();
            alipayRequest.setReturnUrl(url + "/returnOrders/" + orderSn + "/" + userId);
            alipayRequest.setNotifyUrl(url + "/paySuccess?payType=1&orderSn=" + orderSn);
            // 填充业务参数
            // 必填
            // 商户订单号,需保证在商户端不重复
            String out_trade_no = orderSn + new Random().nextInt(9999);
            // 销售产品码,与支付宝签约的产品码名称。目前仅支持FAST_INSTANT_TRADE_PAY
            String product_code = "FAST_INSTANT_TRADE_PAY";
            // 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]。
            BigDecimal actualPrice = order.getActualPrice();
            String total_amount = actualPrice.toString();
            // 订单标题
            String subject = "支付宝测试";
            // 选填
            // 商品描述,可空
            String body = "商品描述";
            alipayRequest.setBizContent("{" + "\"out_trade_no\":\"" + out_trade_no + "\"," + "\"product_code\":\"" + product_code + "\"," + "\"total_amount\":\"" + total_amount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\"}");
            // 请求
            String form;
            try {
                // 需要自行申请支付宝的沙箱账号、申请appID,并在配置文件中依次配置AppID、密钥、公钥,否则这里会报错。
                // 调用SDK生成表单
                form = alipayClient.pageExecute(alipayRequest).getBody();
                return R.success().add("form", form);
            } catch (AlipayApiException e) {
                log.error(e.getMessage(), e);
                return R.error(ReturnCodeEnum.ORDER_SUBMIT_ERROR);
            }
        case ALI_TEST:
            // 支付宝test,直接更新支付状态为已支付
            order.setPayId("xxxxx0987654321-ali");
            order.setPayTime(LocalDateTime.now());
            order.setOrderStatus(OrderUtil.STATUS_PAY);
            order.setUpdateTime(new Date());
            if (!updateById(order)) {
                return R.error(ReturnCodeEnum.ORDER_SUBMIT_ERROR);
            }
            // 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
            String email = iMemberService.getById(order.getUserId()).getEmail();
            if (StringUtils.isNotBlank(email)) {
                iMailService.sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl() + "/message/email");
            }
            // 删除redis中订单id
            redisCache.deleteZsetObject("order_zset", order.getId());
            // 取消订单超时未支付任务
            taskService.removeTask(new OrderUnpaidTask(order.getId()));
            return R.success();
        default:
            return R.error(ReturnCodeEnum.ORDER_NOT_SUPPORT_PAYWAY_ERROR);
    }
}
Also used : LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) WxPayUnifiedOrderRequest(com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest) ReturnCodeEnum(com.wayn.common.enums.ReturnCodeEnum) DefaultAlipayClient(com.alipay.api.DefaultAlipayClient) OrderUnpaidTask(com.wayn.mobile.api.task.OrderUnpaidTask) BigDecimal(java.math.BigDecimal) BusinessException(com.wayn.common.exception.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) AlipayApiException(com.alipay.api.AlipayApiException) OrderHandleOption(com.wayn.common.core.util.OrderHandleOption) WxPayMpOrderResult(com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult) DefaultAlipayClient(com.alipay.api.DefaultAlipayClient) AlipayClient(com.alipay.api.AlipayClient) AlipayApiException(com.alipay.api.AlipayApiException) AlipayTradeWapPayRequest(com.alipay.api.request.AlipayTradeWapPayRequest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 48 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.

the class OrderServiceImpl method prepay.

@Override
@Transactional(rollbackFor = Exception.class)
public R prepay(String orderSn, Integer payType, HttpServletRequest request) {
    // 获取订单详情
    Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
    ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
    if (!returnCodeEnum.equals(ReturnCodeEnum.SUCCESS)) {
        return R.error(returnCodeEnum);
    }
    // 检测是否能够取消
    OrderHandleOption handleOption = OrderUtil.build(order);
    if (!handleOption.isPay()) {
        return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
    }
    // 设置支付方式
    order.setPayType(payType);
    Member member = iMemberService.getById(MobileSecurityUtils.getUserId());
    String openid = member.getWeixinOpenid();
    if (openid == null) {
        return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
    }
    WxPayMpOrderResult result;
    try {
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        orderRequest.setOutTradeNo(order.getOrderSn());
        orderRequest.setOpenid(openid);
        orderRequest.setBody("订单:" + order.getOrderSn());
        // 元转成分
        int fee;
        BigDecimal actualPrice = order.getActualPrice();
        fee = actualPrice.multiply(new BigDecimal(100)).intValue();
        orderRequest.setTotalFee(fee);
        orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
        result = wxPayService.createOrder(orderRequest);
        return R.success().add("result", result);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
    }
}
Also used : OrderHandleOption(com.wayn.common.core.util.OrderHandleOption) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) WxPayMpOrderResult(com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult) WxPayUnifiedOrderRequest(com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest) ReturnCodeEnum(com.wayn.common.enums.ReturnCodeEnum) BigDecimal(java.math.BigDecimal) BusinessException(com.wayn.common.exception.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) AlipayApiException(com.alipay.api.AlipayApiException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 49 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.

the class OrderServiceImpl method cancel.

@Override
@Transactional(rollbackFor = Exception.class)
public R cancel(Long orderId) {
    Order order = getById(orderId);
    ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
    if (!ReturnCodeEnum.SUCCESS.equals(returnCodeEnum)) {
        return R.error(returnCodeEnum);
    }
    // 检测是否能够取消
    OrderHandleOption handleOption = OrderUtil.build(order);
    if (!handleOption.isCancel()) {
        return R.error(ReturnCodeEnum.ORDER_CANNOT_CANCAL_ERROR);
    }
    // 设置订单已取消状态
    order.setOrderStatus(OrderUtil.STATUS_CANCEL);
    order.setOrderEndTime(LocalDateTime.now());
    order.setUpdateTime(new Date());
    if (!updateById(order)) {
        throw new BusinessException("更新数据已失效");
    }
    // 商品货品数量增加
    List<OrderGoods> goodsList = iOrderGoodsService.list(new QueryWrapper<OrderGoods>().eq("order_id", orderId));
    for (OrderGoods orderGoods : goodsList) {
        Long productId = orderGoods.getProductId();
        Integer number = orderGoods.getNumber();
        if (!iGoodsProductService.addStock(productId, number)) {
            throw new BusinessException("商品货品库存增加失败");
        }
    }
    // releaseCoupon(orderId);
    return R.success();
}
Also used : OrderHandleOption(com.wayn.common.core.util.OrderHandleOption) BusinessException(com.wayn.common.exception.BusinessException) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ReturnCodeEnum(com.wayn.common.enums.ReturnCodeEnum) Transactional(org.springframework.transaction.annotation.Transactional)

Example 50 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.

the class OrderServiceImpl method delete.

@Override
@Transactional(rollbackFor = Exception.class)
public R delete(Long orderId) {
    Order order = getById(orderId);
    ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
    if (!ReturnCodeEnum.SUCCESS.equals(returnCodeEnum)) {
        return R.error(returnCodeEnum);
    }
    // 检测是否能够取消
    OrderHandleOption handleOption = OrderUtil.build(order);
    if (!handleOption.isDelete()) {
        return R.error(ReturnCodeEnum.ORDER_CANNOT_DELETE_ERROR);
    }
    // 删除订单
    removeById(orderId);
    // 删除订单商品
    iOrderGoodsService.remove(new QueryWrapper<OrderGoods>().eq("order_id", orderId));
    return R.success();
}
Also used : OrderHandleOption(com.wayn.common.core.util.OrderHandleOption) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ReturnCodeEnum(com.wayn.common.enums.ReturnCodeEnum) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)723 Transactional (org.springframework.transaction.annotation.Transactional)98 IPage (com.baomidou.mybatisplus.core.metadata.IPage)82 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)74 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)72 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)65 ArrayList (java.util.ArrayList)61 Session (org.apache.shiro.session.Session)61 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)60 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)55 Problem (top.hcode.hoj.pojo.entity.problem.Problem)50 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)45 Date (java.util.Date)44 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 HashMap (java.util.HashMap)34 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)34 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)34 ApiOperation (io.swagger.annotations.ApiOperation)32 HttpSession (javax.servlet.http.HttpSession)31 Judge (top.hcode.hoj.pojo.entity.judge.Judge)30