Search in sources :

Example 1 with WxPayRefundResult

use of com.github.binarywang.wxpay.bean.result.WxPayRefundResult in project litemall by linlinjava.

the class AdminAftersaleController method refund.

@RequiresPermissions("admin:aftersale:refund")
@RequiresPermissionsDesc(menu = { "商城管理", "售后管理" }, button = "退款")
@PostMapping("/refund")
public Object refund(@RequestBody LitemallAftersale aftersale) {
    Integer id = aftersale.getId();
    LitemallAftersale aftersaleOne = aftersaleService.findById(id);
    if (aftersaleOne == null) {
        return ResponseUtil.badArgumentValue();
    }
    if (!aftersaleOne.getStatus().equals(AftersaleConstant.STATUS_RECEPT)) {
        return ResponseUtil.fail(AdminResponseCode.AFTERSALE_NOT_ALLOWED, "售后不能进行退款操作");
    }
    Integer orderId = aftersaleOne.getOrderId();
    LitemallOrder order = orderService.findById(orderId);
    // 微信退款
    WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
    wxPayRefundRequest.setOutTradeNo(order.getOrderSn());
    wxPayRefundRequest.setOutRefundNo("refund_" + order.getOrderSn());
    // 元转成分
    Integer totalFee = aftersaleOne.getAmount().multiply(new BigDecimal(100)).intValue();
    wxPayRefundRequest.setTotalFee(order.getActualPrice().multiply(new BigDecimal(100)).intValue());
    wxPayRefundRequest.setRefundFee(totalFee);
    WxPayRefundResult wxPayRefundResult;
    try {
        wxPayRefundResult = wxPayService.refund(wxPayRefundRequest);
    } catch (WxPayException e) {
        logger.error(e.getMessage(), e);
        return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
    }
    if (!wxPayRefundResult.getReturnCode().equals("SUCCESS")) {
        logger.warn("refund fail: " + wxPayRefundResult.getReturnMsg());
        return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
    }
    if (!wxPayRefundResult.getResultCode().equals("SUCCESS")) {
        logger.warn("refund fail: " + wxPayRefundResult.getReturnMsg());
        return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
    }
    aftersaleOne.setStatus(AftersaleConstant.STATUS_REFUND);
    aftersaleOne.setHandleTime(LocalDateTime.now());
    aftersaleService.updateById(aftersaleOne);
    orderService.updateAftersaleStatus(orderId, AftersaleConstant.STATUS_REFUND);
    // 开发者也可以删除一下代码,在其他地方增加商品货品入库操作
    if (aftersale.getType().equals(AftersaleConstant.TYPE_GOODS_REQUIRED)) {
        List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
        for (LitemallOrderGoods orderGoods : orderGoodsList) {
            Integer productId = orderGoods.getProductId();
            Short number = orderGoods.getNumber();
            goodsProductService.addStock(productId, number);
        }
    }
    // 发送短信通知,这里采用异步发送
    // 退款成功通知用户, 例如“您申请的订单退款 [ 单号:{1} ] 已成功,请耐心等待到账。”
    // TODO 注意订单号只发后6位
    notifyService.notifySmsTemplate(order.getMobile(), NotifyType.REFUND, new String[] { order.getOrderSn().substring(8, 14) });
    logHelper.logOrderSucceed("退款", "订单编号 " + order.getOrderSn() + " 售后编号 " + aftersale.getAftersaleSn());
    return ResponseUtil.ok();
}
Also used : LitemallOrder(org.linlinjava.litemall.db.domain.LitemallOrder) WxPayRefundRequest(com.github.binarywang.wxpay.bean.request.WxPayRefundRequest) LitemallOrderGoods(org.linlinjava.litemall.db.domain.LitemallOrderGoods) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) WxPayRefundResult(com.github.binarywang.wxpay.bean.result.WxPayRefundResult) LitemallAftersale(org.linlinjava.litemall.db.domain.LitemallAftersale) BigDecimal(java.math.BigDecimal) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresPermissionsDesc(org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc)

Example 2 with WxPayRefundResult

use of com.github.binarywang.wxpay.bean.result.WxPayRefundResult in project litemall by linlinjava.

the class AdminOrderService method refund.

/**
 * 订单退款
 * <p>
 * 1. 检测当前订单是否能够退款;
 * 2. 微信退款操作;
 * 3. 设置订单退款确认状态;
 * 4. 订单商品库存回库。
 * <p>
 * TODO
 * 虽然接入了微信退款API,但是从安全角度考虑,建议开发者删除这里微信退款代码,采用以下两步走步骤:
 * 1. 管理员登录微信官方支付平台点击退款操作进行退款
 * 2. 管理员登录litemall管理后台点击退款操作进行订单状态修改和商品库存回库
 *
 * @param body 订单信息,{ orderId:xxx }
 * @return 订单退款操作结果
 */
@Transactional
public Object refund(String body) {
    Integer orderId = JacksonUtil.parseInteger(body, "orderId");
    String refundMoney = JacksonUtil.parseString(body, "refundMoney");
    if (orderId == null) {
        return ResponseUtil.badArgument();
    }
    if (StringUtils.isEmpty(refundMoney)) {
        return ResponseUtil.badArgument();
    }
    LitemallOrder order = orderService.findById(orderId);
    if (order == null) {
        return ResponseUtil.badArgument();
    }
    if (order.getActualPrice().compareTo(new BigDecimal(refundMoney)) != 0) {
        return ResponseUtil.badArgumentValue();
    }
    // 如果订单不是退款状态,则不能退款
    if (!order.getOrderStatus().equals(OrderUtil.STATUS_REFUND)) {
        return ResponseUtil.fail(ORDER_CONFIRM_NOT_ALLOWED, "订单不能确认收货");
    }
    // 微信退款
    WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
    wxPayRefundRequest.setOutTradeNo(order.getOrderSn());
    wxPayRefundRequest.setOutRefundNo("refund_" + order.getOrderSn());
    // 元转成分
    Integer totalFee = order.getActualPrice().multiply(new BigDecimal(100)).intValue();
    wxPayRefundRequest.setTotalFee(totalFee);
    wxPayRefundRequest.setRefundFee(totalFee);
    WxPayRefundResult wxPayRefundResult;
    try {
        wxPayRefundResult = wxPayService.refund(wxPayRefundRequest);
    } catch (WxPayException e) {
        logger.error(e.getMessage(), e);
        return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
    }
    if (!wxPayRefundResult.getReturnCode().equals("SUCCESS")) {
        logger.warn("refund fail: " + wxPayRefundResult.getReturnMsg());
        return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
    }
    if (!wxPayRefundResult.getResultCode().equals("SUCCESS")) {
        logger.warn("refund fail: " + wxPayRefundResult.getReturnMsg());
        return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
    }
    LocalDateTime now = LocalDateTime.now();
    // 设置订单取消状态
    order.setOrderStatus(OrderUtil.STATUS_REFUND_CONFIRM);
    order.setEndTime(now);
    // 记录订单退款相关信息
    order.setRefundAmount(order.getActualPrice());
    order.setRefundType("微信退款接口");
    order.setRefundContent(wxPayRefundResult.getRefundId());
    order.setRefundTime(now);
    if (orderService.updateWithOptimisticLocker(order) == 0) {
        throw new RuntimeException("更新数据已失效");
    }
    // 商品货品数量增加
    List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
    for (LitemallOrderGoods orderGoods : orderGoodsList) {
        Integer productId = orderGoods.getProductId();
        Short number = orderGoods.getNumber();
        if (productService.addStock(productId, number) == 0) {
            throw new RuntimeException("商品货品库存增加失败");
        }
    }
    // 返还优惠券
    List<LitemallCouponUser> couponUsers = couponUserService.findByOid(orderId);
    for (LitemallCouponUser couponUser : couponUsers) {
        // 优惠券状态设置为可使用
        couponUser.setStatus(CouponUserConstant.STATUS_USABLE);
        couponUser.setUpdateTime(LocalDateTime.now());
        couponUserService.update(couponUser);
    }
    // TODO 发送邮件和短信通知,这里采用异步发送
    // 退款成功通知用户, 例如“您申请的订单退款 [ 单号:{1} ] 已成功,请耐心等待到账。”
    // 注意订单号只发后6位
    notifyService.notifySmsTemplate(order.getMobile(), NotifyType.REFUND, new String[] { order.getOrderSn().substring(8, 14) });
    logHelper.logOrderSucceed("退款", "订单编号 " + order.getOrderSn());
    return ResponseUtil.ok();
}
Also used : LocalDateTime(java.time.LocalDateTime) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) WxPayRefundResult(com.github.binarywang.wxpay.bean.result.WxPayRefundResult) BigDecimal(java.math.BigDecimal) WxPayRefundRequest(com.github.binarywang.wxpay.bean.request.WxPayRefundRequest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

WxPayRefundRequest (com.github.binarywang.wxpay.bean.request.WxPayRefundRequest)2 WxPayRefundResult (com.github.binarywang.wxpay.bean.result.WxPayRefundResult)2 WxPayException (com.github.binarywang.wxpay.exception.WxPayException)2 BigDecimal (java.math.BigDecimal)2 LocalDateTime (java.time.LocalDateTime)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 RequiresPermissionsDesc (org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc)1 LitemallAftersale (org.linlinjava.litemall.db.domain.LitemallAftersale)1 LitemallOrder (org.linlinjava.litemall.db.domain.LitemallOrder)1 LitemallOrderGoods (org.linlinjava.litemall.db.domain.LitemallOrderGoods)1 Transactional (org.springframework.transaction.annotation.Transactional)1