1
1
package org .linlinjava .litemall .admin .web ;
2
2
3
+ import com .github .binarywang .wxpay .bean .request .WxPayRefundRequest ;
4
+ import com .github .binarywang .wxpay .bean .result .WxPayRefundResult ;
5
+ import com .github .binarywang .wxpay .exception .WxPayException ;
6
+ import com .github .binarywang .wxpay .service .WxPayService ;
3
7
import org .apache .commons .logging .Log ;
4
8
import org .apache .commons .logging .LogFactory ;
5
9
import org .linlinjava .litemall .admin .annotation .LoginAdmin ;
6
10
import org .linlinjava .litemall .core .notify .NotifyService ;
7
11
import org .linlinjava .litemall .core .notify .NotifyType ;
12
+ import org .linlinjava .litemall .core .util .CharUtil ;
8
13
import org .linlinjava .litemall .core .util .JacksonUtil ;
9
14
import org .linlinjava .litemall .core .util .ResponseUtil ;
10
15
import org .linlinjava .litemall .core .validator .Order ;
@@ -50,7 +55,8 @@ public class AdminOrderController {
50
55
private LitemallUserService userService ;
51
56
@ Autowired
52
57
private LitemallCommentService commentService ;
53
-
58
+ @ Autowired
59
+ private WxPayService wxPayService ;
54
60
@ Autowired
55
61
private NotifyService notifyService ;
56
62
@@ -93,16 +99,21 @@ public Object detail(@LoginAdmin Integer adminId, @NotNull Integer id) {
93
99
}
94
100
95
101
/**
96
- * 订单退款确认
97
- * 1. 检测当前订单是否能够退款确认
98
- * 2. 设置订单退款确认状态
99
- * 3. 订单商品恢复
102
+ * 订单退款
103
+ * <p>
104
+ * 1. 检测当前订单是否能够退款;
105
+ * 2. 微信退款操作;
106
+ * 3. 设置订单退款确认状态;
107
+ * 4. 订单商品库存回库。
108
+ * <p>
109
+ * TODO
110
+ * 虽然接入了微信退款API,但是从安全角度考虑,建议开发者删除这里微信退款代码,采用以下两步走步骤:
111
+ * 1. 管理员登录微信官方支付平台点击退款操作进行退款
112
+ * 2. 管理员登录litemall管理后台点击退款操作进行订单状态修改和商品库存回库
100
113
*
101
114
* @param adminId 管理员ID
102
115
* @param body 订单信息,{ orderId:xxx }
103
- * @return 订单操作结果
104
- * 成功则 { errno: 0, errmsg: '成功' }
105
- * 失败则 { errno: XXX, errmsg: XXX }
116
+ * @return 订单退款操作结果
106
117
*/
107
118
@ PostMapping ("refund" )
108
119
public Object refund (@ LoginAdmin Integer adminId , @ RequestBody String body ) {
@@ -114,6 +125,9 @@ public Object refund(@LoginAdmin Integer adminId, @RequestBody String body) {
114
125
if (orderId == null ) {
115
126
return ResponseUtil .badArgument ();
116
127
}
128
+ if (StringUtils .isEmpty (refundMoney )){
129
+ return ResponseUtil .badArgument ();
130
+ }
117
131
118
132
LitemallOrder order = orderService .findById (orderId );
119
133
if (order == null ) {
@@ -129,6 +143,31 @@ public Object refund(@LoginAdmin Integer adminId, @RequestBody String body) {
129
143
return ResponseUtil .fail (ORDER_CONFIRM_NOT_ALLOWED , "订单不能确认收货" );
130
144
}
131
145
146
+ // 微信退款
147
+ WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest ();
148
+ wxPayRefundRequest .setOutTradeNo (order .getOrderSn ());
149
+ wxPayRefundRequest .setOutRefundNo ("refund_" + order .getOrderSn ());
150
+ // 元转成分
151
+ Integer totalFee = order .getActualPrice ().multiply (new BigDecimal (100 )).intValue ();
152
+ wxPayRefundRequest .setTotalFee (totalFee );
153
+ wxPayRefundRequest .setRefundFee (totalFee );
154
+
155
+ WxPayRefundResult wxPayRefundResult = null ;
156
+ try {
157
+ wxPayRefundResult = wxPayService .refund (wxPayRefundRequest );
158
+ } catch (WxPayException e ) {
159
+ e .printStackTrace ();
160
+ return ResponseUtil .fail (ORDER_REFUND_FAILED , "订单退款失败" );
161
+ }
162
+ if (!wxPayRefundResult .getReturnCode ().equals ("SUCCESS" )){
163
+ logger .warn ("refund fail: " + wxPayRefundResult .getReturnMsg ());
164
+ return ResponseUtil .fail (ORDER_REFUND_FAILED , "订单退款失败" );
165
+ }
166
+ if (!wxPayRefundResult .getResultCode ().equals ("SUCCESS" )){
167
+ logger .warn ("refund fail: " + wxPayRefundResult .getReturnMsg ());
168
+ return ResponseUtil .fail (ORDER_REFUND_FAILED , "订单退款失败" );
169
+ }
170
+
132
171
// 开启事务管理
133
172
DefaultTransactionDefinition def = new DefaultTransactionDefinition ();
134
173
def .setPropagationBehavior (TransactionDefinition .PROPAGATION_REQUIRED );
@@ -156,16 +195,10 @@ public Object refund(@LoginAdmin Integer adminId, @RequestBody String body) {
156
195
}
157
196
158
197
//TODO 发送邮件和短信通知,这里采用异步发送
159
- // 退款成功通知用户
160
- /**
161
- *
162
- * 您申请的订单退款 [ 单号:{1} ] 已成功,请耐心等待到账。
163
- * 注意订单号只发后6位
164
- *
165
- */
198
+ // 退款成功通知用户, 例如“您申请的订单退款 [ 单号:{1} ] 已成功,请耐心等待到账。”
199
+ // 注意订单号只发后6位
166
200
notifyService .notifySmsTemplate (order .getMobile (), NotifyType .REFUND , new String []{order .getOrderSn ().substring (8 , 14 )});
167
201
168
-
169
202
txManager .commit (status );
170
203
171
204
return ResponseUtil .ok ();
0 commit comments