Search in sources :

Example 1 with OrderHandleOption

use of com.wayn.common.core.util.OrderHandleOption in project waynboot-mall by wayn111.

the class OrderServiceImpl method refund.

@Override
public R refund(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.isRefund()) {
        return R.error(ReturnCodeEnum.ORDER_CANNOT_REFUND_ERROR);
    }
    // 设置订单申请退款状态
    order.setOrderStatus(OrderUtil.STATUS_REFUND);
    order.setUpdateTime(new Date());
    updateById(order);
    // 有用户申请退款,邮件通知运营人员
    String email = iMemberService.getById(order.getUserId()).getEmail();
    if (StringUtils.isNotEmpty(email)) {
        if (StringUtils.isNotBlank(email)) {
            iMailService.sendEmail("订单正在退款", order.toString(), email, WaynConfig.getMobileUrl() + "/message/email");
        }
    }
    return R.success();
}
Also used : OrderHandleOption(com.wayn.common.core.util.OrderHandleOption) ReturnCodeEnum(com.wayn.common.enums.ReturnCodeEnum)

Example 2 with OrderHandleOption

use of com.wayn.common.core.util.OrderHandleOption 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 3 with OrderHandleOption

use of com.wayn.common.core.util.OrderHandleOption 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 4 with OrderHandleOption

use of com.wayn.common.core.util.OrderHandleOption 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 5 with OrderHandleOption

use of com.wayn.common.core.util.OrderHandleOption 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

OrderHandleOption (com.wayn.common.core.util.OrderHandleOption)6 ReturnCodeEnum (com.wayn.common.enums.ReturnCodeEnum)6 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)4 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)4 Transactional (org.springframework.transaction.annotation.Transactional)4 BusinessException (com.wayn.common.exception.BusinessException)3 AlipayApiException (com.alipay.api.AlipayApiException)2 WxPayMpOrderResult (com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult)2 WxPayUnifiedOrderRequest (com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest)2 WxPayException (com.github.binarywang.wxpay.exception.WxPayException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 BigDecimal (java.math.BigDecimal)2 AlipayClient (com.alipay.api.AlipayClient)1 DefaultAlipayClient (com.alipay.api.DefaultAlipayClient)1 AlipayTradeWapPayRequest (com.alipay.api.request.AlipayTradeWapPayRequest)1 OrderUnpaidTask (com.wayn.mobile.api.task.OrderUnpaidTask)1