|
@@ -1,102 +1,102 @@
|
1
|
|
-package com.yunzhi.shigongli.common;
|
2
|
|
-
|
3
|
|
-import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
4
|
|
-import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
|
5
|
|
-import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
6
|
|
-import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
|
7
|
|
-import com.github.binarywang.wxpay.constant.WxPayConstants;
|
8
|
|
-import com.github.binarywang.wxpay.service.WxPayService;
|
9
|
|
-import com.yunzhi.shigongli.config.WeixinConfig;
|
10
|
|
-import com.yunzhi.shigongli.entity.TaOrder;
|
11
|
|
-import com.yunzhi.shigongli.entity.TaOrderPay;
|
12
|
|
-import com.yunzhi.shigongli.entity.TaOrderSub;
|
13
|
|
-import com.yunzhi.shigongli.entity.TaPerson;
|
14
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
15
|
|
-import org.springframework.stereotype.Component;
|
16
|
|
-
|
17
|
|
-import java.time.LocalDateTime;
|
18
|
|
-import java.time.ZoneOffset;
|
19
|
|
-
|
20
|
|
-@Component
|
21
|
|
-public class WxPayUtils {
|
22
|
|
- @Autowired
|
23
|
|
- WeixinConfig config;
|
24
|
|
-
|
25
|
|
- @Autowired
|
26
|
|
- WxUtils wxUtils;
|
27
|
|
-
|
28
|
|
- /**
|
29
|
|
- * 下单
|
30
|
|
- * @param person
|
31
|
|
- * @param orderPay
|
32
|
|
- * @return
|
33
|
|
- * @throws Exception
|
34
|
|
- */
|
35
|
|
- public WxPayMpOrderResult createOrder(TaPerson person, TaOrderPay orderPay) throws Exception {
|
36
|
|
- // 北京时间
|
37
|
|
- LocalDateTime now = LocalDateTime.now(ZoneOffset.ofHours(8));
|
38
|
|
- WxPayService payService = wxUtils.getPayService();
|
39
|
|
-
|
40
|
|
- WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
|
41
|
|
- orderRequest.setBody(orderPay.getDescription());
|
42
|
|
- orderRequest.setOutTradeNo(orderPay.getPayId());
|
43
|
|
- orderRequest.setTotalFee(orderPay.getCharges());
|
44
|
|
- orderRequest.setOpenid(person.getOpenid());
|
45
|
|
- orderRequest.setNotifyUrl(config.getPay().getNotifyUrl());
|
46
|
|
- orderRequest.setSpbillCreateIp("218.91.253.51"); // 随便写个 IP
|
47
|
|
- orderRequest.setTimeStart(DateUtils.toString(now, "yyyyMMddHHmmss"));
|
48
|
|
- // 默认支付 30 分钟有效期
|
49
|
|
- orderRequest.setTimeExpire(DateUtils.toString(now.plusMinutes(30), "yyyyMMddHHmmss"));
|
50
|
|
-
|
51
|
|
- WxPayMpOrderResult payResult = payService.createOrder(orderRequest);
|
52
|
|
- return payResult;
|
53
|
|
- }
|
54
|
|
-
|
55
|
|
-// /**
|
56
|
|
-// * 订单退款
|
57
|
|
-// * @param taOrder
|
58
|
|
-// * @param cashback
|
59
|
|
-// * @return
|
60
|
|
-// * @throws Exception
|
61
|
|
-// */
|
62
|
|
-// public WxPayRefundResult refund(TaOrder taOrder, Integer cashback) throws Exception {
|
63
|
|
-// WxPayService payService = wxUtils.getPayService();
|
64
|
|
-// WxPayRefundRequest refundRequest = new WxPayRefundRequest();
|
65
|
|
-// refundRequest.setOutTradeNo(taOrder.getOrderId());
|
66
|
|
-// refundRequest.setOutRefundNo("R"+taOrder.getOrderId());
|
67
|
|
-// refundRequest.setTotalFee(taOrder.getCharges());
|
68
|
|
-// refundRequest.setRefundFee(taOrder.getCharges() - cashback);
|
69
|
|
-// refundRequest.setNotifyUrl(config.getPay().getRefundNotifyUrl());
|
70
|
|
-// WxPayRefundResult result = payService.refund(refundRequest);
|
71
|
|
-//
|
72
|
|
-// if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())) {
|
73
|
|
-// throw new Exception(result.getErrCodeDes());
|
74
|
|
-// }
|
75
|
|
-//
|
76
|
|
-// return result;
|
77
|
|
-// }
|
78
|
|
-
|
79
|
|
- /**
|
80
|
|
- * 订单退款
|
81
|
|
- * @param originOrderPay
|
82
|
|
- * @param orderPay
|
83
|
|
- * @return
|
84
|
|
- * @throws Exception
|
85
|
|
- */
|
86
|
|
- public WxPayRefundResult refund(TaOrderPay originOrderPay, TaOrderPay orderPay) throws Exception {
|
87
|
|
- WxPayService payService = wxUtils.getPayService();
|
88
|
|
- WxPayRefundRequest refundRequest = new WxPayRefundRequest();
|
89
|
|
- refundRequest.setOutTradeNo(originOrderPay.getPayId());
|
90
|
|
- refundRequest.setOutRefundNo(orderPay.getPayId());
|
91
|
|
- refundRequest.setTotalFee(originOrderPay.getCharges());
|
92
|
|
- refundRequest.setRefundFee(orderPay.getCharges());
|
93
|
|
- refundRequest.setNotifyUrl(config.getPay().getRefundNotifyUrl());
|
94
|
|
- WxPayRefundResult result = payService.refund(refundRequest);
|
95
|
|
-
|
96
|
|
- if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())) {
|
97
|
|
- throw new Exception(result.getErrCodeDes());
|
98
|
|
- }
|
99
|
|
-
|
100
|
|
- return result;
|
101
|
|
- }
|
102
|
|
-}
|
|
1
|
+package com.yunzhi.shigongli.common;
|
|
2
|
+
|
|
3
|
+import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
|
4
|
+import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
|
|
5
|
+import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
|
6
|
+import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
|
|
7
|
+import com.github.binarywang.wxpay.constant.WxPayConstants;
|
|
8
|
+import com.github.binarywang.wxpay.service.WxPayService;
|
|
9
|
+import com.yunzhi.shigongli.config.WeixinConfig;
|
|
10
|
+import com.yunzhi.shigongli.entity.TaOrder;
|
|
11
|
+import com.yunzhi.shigongli.entity.TaOrderPay;
|
|
12
|
+import com.yunzhi.shigongli.entity.TaOrderSub;
|
|
13
|
+import com.yunzhi.shigongli.entity.TaPerson;
|
|
14
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
15
|
+import org.springframework.stereotype.Component;
|
|
16
|
+
|
|
17
|
+import java.time.LocalDateTime;
|
|
18
|
+import java.time.ZoneOffset;
|
|
19
|
+
|
|
20
|
+@Component
|
|
21
|
+public class WxPayUtils {
|
|
22
|
+ @Autowired
|
|
23
|
+ WeixinConfig config;
|
|
24
|
+
|
|
25
|
+ @Autowired
|
|
26
|
+ WxUtils wxUtils;
|
|
27
|
+
|
|
28
|
+ /**
|
|
29
|
+ * 下单
|
|
30
|
+ * @param person
|
|
31
|
+ * @param orderPay
|
|
32
|
+ * @return
|
|
33
|
+ * @throws Exception
|
|
34
|
+ */
|
|
35
|
+ public WxPayMpOrderResult createOrder(TaPerson person, TaOrderPay orderPay) throws Exception {
|
|
36
|
+ // 北京时间
|
|
37
|
+ LocalDateTime now = LocalDateTime.now(ZoneOffset.ofHours(8));
|
|
38
|
+ WxPayService payService = wxUtils.getPayService();
|
|
39
|
+
|
|
40
|
+ WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
|
|
41
|
+ orderRequest.setBody(orderPay.getDescription());
|
|
42
|
+ orderRequest.setOutTradeNo(orderPay.getPayId());
|
|
43
|
+ orderRequest.setTotalFee(orderPay.getCharges());
|
|
44
|
+ orderRequest.setOpenid(person.getOpenid());
|
|
45
|
+ orderRequest.setNotifyUrl(config.getPay().getNotifyUrl());
|
|
46
|
+ orderRequest.setSpbillCreateIp("218.91.253.51"); // 随便写个 IP
|
|
47
|
+ orderRequest.setTimeStart(DateUtils.toString(now, "yyyyMMddHHmmss"));
|
|
48
|
+ // 默认支付 30 分钟有效期
|
|
49
|
+ orderRequest.setTimeExpire(DateUtils.toString(now.plusMinutes(30), "yyyyMMddHHmmss"));
|
|
50
|
+
|
|
51
|
+ WxPayMpOrderResult payResult = payService.createOrder(orderRequest);
|
|
52
|
+ return payResult;
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+// /**
|
|
56
|
+// * 订单退款
|
|
57
|
+// * @param taOrder
|
|
58
|
+// * @param cashback
|
|
59
|
+// * @return
|
|
60
|
+// * @throws Exception
|
|
61
|
+// */
|
|
62
|
+// public WxPayRefundResult refund(TaOrder taOrder, Integer cashback) throws Exception {
|
|
63
|
+// WxPayService payService = wxUtils.getPayService();
|
|
64
|
+// WxPayRefundRequest refundRequest = new WxPayRefundRequest();
|
|
65
|
+// refundRequest.setOutTradeNo(taOrder.getOrderId());
|
|
66
|
+// refundRequest.setOutRefundNo("R"+taOrder.getOrderId());
|
|
67
|
+// refundRequest.setTotalFee(taOrder.getCharges());
|
|
68
|
+// refundRequest.setRefundFee(taOrder.getCharges() - cashback);
|
|
69
|
+// refundRequest.setNotifyUrl(config.getPay().getRefundNotifyUrl());
|
|
70
|
+// WxPayRefundResult result = payService.refund(refundRequest);
|
|
71
|
+//
|
|
72
|
+// if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())) {
|
|
73
|
+// throw new Exception(result.getErrCodeDes());
|
|
74
|
+// }
|
|
75
|
+//
|
|
76
|
+// return result;
|
|
77
|
+// }
|
|
78
|
+
|
|
79
|
+ /**
|
|
80
|
+ * 订单退款
|
|
81
|
+ * @param originOrderPay
|
|
82
|
+ * @param orderPay
|
|
83
|
+ * @return
|
|
84
|
+ * @throws Exception
|
|
85
|
+ */
|
|
86
|
+ public WxPayRefundResult refund(TaOrderPay originOrderPay, TaOrderPay orderPay) throws Exception {
|
|
87
|
+ WxPayService payService = wxUtils.getPayService();
|
|
88
|
+ WxPayRefundRequest refundRequest = new WxPayRefundRequest();
|
|
89
|
+ refundRequest.setOutTradeNo(originOrderPay.getPayId());
|
|
90
|
+ refundRequest.setOutRefundNo(orderPay.getPayId());
|
|
91
|
+ refundRequest.setTotalFee(originOrderPay.getCharges());
|
|
92
|
+ refundRequest.setRefundFee(orderPay.getCharges());
|
|
93
|
+ refundRequest.setNotifyUrl(config.getPay().getRefundNotifyUrl());
|
|
94
|
+ WxPayRefundResult result = payService.refund(refundRequest);
|
|
95
|
+
|
|
96
|
+ if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())) {
|
|
97
|
+ throw new Exception(result.getErrCodeDes());
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ return result;
|
|
101
|
+ }
|
|
102
|
+}
|