admin
2019-11-03 788deca1b4a24f8a24e49c24f7d89975a1d74bbe
fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java
@@ -7,6 +7,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.entity.wx.WXAPPInfo;
import org.yeshi.utils.exception.WXOrderException;
import org.yeshi.utils.wx.WXPayUtil;
import com.yeshi.fanli.entity.redpack.RedPackDetail;
import com.yeshi.fanli.entity.shop.BanLiShopGoods;
@@ -23,6 +26,7 @@
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
@Service
@@ -101,7 +105,7 @@
      if (order.getMoneyPayment() == null)
         throw new BanLiShopOrderException(2, "不需要采用现金支付");
      if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
      if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() != BanLiShopOrder.PAY_STATE_NOPAY) {
         throw new BanLiShopOrderException(3, "重复支付");
      }
@@ -141,15 +145,61 @@
      }
      if (detail != null) {
         RedPackDetail oldDetail = redPackDetailService.getByIdentifyCode(detail.getIdentifyCode());
         if (oldDetail != null) {// 外显红包详情
            RedPackDetail update = new RedPackDetail();
            update.setId(oldDetail.getId());
            update.setDisplay(true);
            redPackDetailService.updateByPrimaryKeySelective(update);
         redPackDetailService.changeDisplayByIdentifyCode(detail.getIdentifyCode(), true);
      }
   }
   @Transactional
   @Override
   public void refund(Long orderId) throws BanLiShopOrderException {
      // 订单退款
      // 查询订单是否已经被拒绝
      BanLiShopOrder order = banLiShopOrderService.selectByPrimaryKeyForUpdate(orderId);
      if (order == null)
         throw new BanLiShopOrderException(1, "订单不存在");
      if (order.getState() != BanLiShopOrder.STATE_REJECT)
         throw new BanLiShopOrderException(2, "订单未被拒绝/订单已退款");
      BanLiShopOrder update = new BanLiShopOrder();
      update.setId(order.getId());
      if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
         BanLiShopGoods goods = banLiShopGoodsService.selectByPrimaryKey(order.getGoods().getId());
         BanLiShopGoodsClass goodsClass = banLiShopGoodsClassService
               .selectByPrimaryKey(goods.getGoodsClass().getId());
         BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(order.getGoodsSet().getId());
         // 红包退款
         RedPackDetail detail = null;
         try {
            detail = RedPackDetailFactory.createShopOrderDrawBack(orderId, order.getUid(), goodsClass.getName(),
                  set.getName(), order.getHongBaoPayment());
         } catch (RedPackDetailException e) {
            e.printStackTrace();
         }
         if (detail == null)
            throw new BanLiShopOrderException(4, "红包详情失败");
         try {
            redPackBalanceService.addRedPack(order.getUid(), order.getHongBaoPayment(), detail);
         } catch (RedPackBalanceException e) {
            throw new BanLiShopOrderException(5, "红包退款失败");
         }
         update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
      }
      // TODO 微信支付退款
      if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
         try {
            boolean success = WXPayUtil.refund(order.getOrderNo(), order.getMoneyPayment(), order.getMoneyPayment(),
                  null, new WXAPPInfo(), null, null);
            if (success) {
               update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
            } else
               throw new BanLiShopOrderException(6, "微信支付退款失败");
         } catch (WXOrderException e) {
            throw new BanLiShopOrderException(6, "微信支付退款失败");
         }
      }
   }
}