Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 97011e1

Browse files
committedNov 29, 2018
feat[litemall-admin]: 支持微信退款
1 parent 88c5a81 commit 97011e1

File tree

5 files changed

+65
-16
lines changed

5 files changed

+65
-16
lines changed
 

‎deploy/litemall/application-core.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ litemall:
66
mch-id: 111111
77
mch-key: xxxxxx
88
notify-url: http://122.152.206.172:8080/wx/order/pay-notify
9+
# 商户证书文件路径
10+
# 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
11+
key-path: xxxxx
912

1013
#通知相关配置
1114
notify:

‎litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java

+49-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.linlinjava.litemall.admin.web;
22

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;
37
import org.apache.commons.logging.Log;
48
import org.apache.commons.logging.LogFactory;
59
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
610
import org.linlinjava.litemall.core.notify.NotifyService;
711
import org.linlinjava.litemall.core.notify.NotifyType;
12+
import org.linlinjava.litemall.core.util.CharUtil;
813
import org.linlinjava.litemall.core.util.JacksonUtil;
914
import org.linlinjava.litemall.core.util.ResponseUtil;
1015
import org.linlinjava.litemall.core.validator.Order;
@@ -50,7 +55,8 @@ public class AdminOrderController {
5055
private LitemallUserService userService;
5156
@Autowired
5257
private LitemallCommentService commentService;
53-
58+
@Autowired
59+
private WxPayService wxPayService;
5460
@Autowired
5561
private NotifyService notifyService;
5662

@@ -93,16 +99,21 @@ public Object detail(@LoginAdmin Integer adminId, @NotNull Integer id) {
9399
}
94100

95101
/**
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管理后台点击退款操作进行订单状态修改和商品库存回库
100113
*
101114
* @param adminId 管理员ID
102115
* @param body 订单信息,{ orderId:xxx }
103-
* @return 订单操作结果
104-
* 成功则 { errno: 0, errmsg: '成功' }
105-
* 失败则 { errno: XXX, errmsg: XXX }
116+
* @return 订单退款操作结果
106117
*/
107118
@PostMapping("refund")
108119
public Object refund(@LoginAdmin Integer adminId, @RequestBody String body) {
@@ -114,6 +125,9 @@ public Object refund(@LoginAdmin Integer adminId, @RequestBody String body) {
114125
if (orderId == null) {
115126
return ResponseUtil.badArgument();
116127
}
128+
if(StringUtils.isEmpty(refundMoney)){
129+
return ResponseUtil.badArgument();
130+
}
117131

118132
LitemallOrder order = orderService.findById(orderId);
119133
if (order == null) {
@@ -129,6 +143,31 @@ public Object refund(@LoginAdmin Integer adminId, @RequestBody String body) {
129143
return ResponseUtil.fail(ORDER_CONFIRM_NOT_ALLOWED, "订单不能确认收货");
130144
}
131145

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+
132171
// 开启事务管理
133172
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
134173
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
@@ -156,16 +195,10 @@ public Object refund(@LoginAdmin Integer adminId, @RequestBody String body) {
156195
}
157196

158197
//TODO 发送邮件和短信通知,这里采用异步发送
159-
// 退款成功通知用户
160-
/**
161-
*
162-
* 您申请的订单退款 [ 单号:{1} ] 已成功,请耐心等待到账。
163-
* 注意订单号只发后6位
164-
*
165-
*/
198+
// 退款成功通知用户, 例如“您申请的订单退款 [ 单号:{1} ] 已成功,请耐心等待到账。”
199+
// 注意订单号只发后6位
166200
notifyService.notifySmsTemplate(order.getMobile(), NotifyType.REFUND, new String[]{order.getOrderSn().substring(8, 14)});
167201

168-
169202
txManager.commit(status);
170203

171204
return ResponseUtil.ok();

‎litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxConfig.java

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public WxPayConfig wxPayConfig() {
3939
payConfig.setMchId(properties.getMchId());
4040
payConfig.setMchKey(properties.getMchKey());
4141
payConfig.setNotifyUrl(properties.getNotifyUrl());
42+
payConfig.setKeyPath(properties.getKeyPath());
4243
payConfig.setTradeType("JSAPI");
4344
payConfig.setSignType("MD5");
4445
return payConfig;

‎litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxProperties.java

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class WxProperties {
1717

1818
private String notifyUrl;
1919

20+
private String keyPath;
21+
2022
public String getNotifyUrl() {
2123
return notifyUrl;
2224
}
@@ -57,4 +59,11 @@ public void setMchId(String mchId) {
5759
this.mchId = mchId;
5860
}
5961

62+
public String getKeyPath() {
63+
return keyPath;
64+
}
65+
66+
public void setKeyPath(String keyPath) {
67+
this.keyPath = keyPath;
68+
}
6069
}

‎litemall-core/src/main/resources/application-core.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ litemall:
66
mch-id: 111111
77
mch-key: xxxxxx
88
notify-url: http://www.example.com/wx/order/pay-notify
9+
# 商户证书文件路径
10+
# 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
11+
key-path: xxxxx
912

1013
#通知相关配置
1114
notify:

0 commit comments

Comments
 (0)
Please sign in to comment.