From 788deca1b4a24f8a24e49c24f7d89975a1d74bbe Mon Sep 17 00:00:00 2001
From: admin <2780501319@qq.com>
Date: 星期日, 03 十一月 2019 23:31:25 +0800
Subject: [PATCH] 商城订单退款处理

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java |   59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 58 insertions(+), 1 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java
index 4db3d5b..14fc3b4 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java
+++ b/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, "閲嶅鏀粯");
 		}
 
@@ -145,4 +149,57 @@
 		}
 	}
 
+	@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, "寰俊鏀粯閫�娆惧け璐�");
+			}
+		}
+	}
+
 }

--
Gitblit v1.8.0