张延森 2 年之前
父節點
當前提交
0d90c0f12d

+ 14
- 10
src/main/java/com/yunzhi/nanyang/common/WxPayUtils.java 查看文件

@@ -35,17 +35,16 @@ public class WxPayUtils {
35 35
     public WxPayUnifiedOrderV3Result createOrder(WxPayUnifiedOrderV3Request request, String client) throws WxPayException {
36 36
         // 北京时间
37 37
         LocalDateTime now = LocalDateTime.now(ZoneOffset.ofHours(8));
38
-        WxPayService payService = wxUtils.getPayService();
38
+        request.setNotifyUrl(servBase + "/wxpay/notify/"+client+"/order");
39 39
 
40
-        request.setNotifyUrl(servBase + "/wxpay/notify/order");
41
-
42
-        TradeTypeEnum tradeType = TradeTypeEnum.JSAPI;
43 40
         if ("ios".equals(client) || "android".equals(client)) {
44
-            tradeType = TradeTypeEnum.APP;
41
+            WxPayService payService = wxUtils.getAndroidPayService();
42
+            return payService.unifiedOrderV3(TradeTypeEnum.APP, request);
43
+        } else {
44
+            WxPayService payService = wxUtils.getMiniPayService();
45
+            return payService.unifiedOrderV3(TradeTypeEnum.JSAPI, request);
45 46
         }
46 47
 
47
-        return payService.unifiedOrderV3(tradeType, request);
48
-
49 48
 //        WxPayUnifiedOrderV3Result wxPayUnifiedOrderV3Result =
50 49
 //        return wxPayUnifiedOrderV3Result.getPayInfo(tradeType, request.getAppid(), request.getMchid(), payService.getConfig().getPrivateKey());
51 50
     }
@@ -56,10 +55,15 @@ public class WxPayUtils {
56 55
      * @return
57 56
      * @throws Exception
58 57
      */
59
-    public WxPayRefundV3Result refund(WxPayRefundV3Request request) throws Exception {
60
-        WxPayService payService = wxUtils.getPayService();
58
+    public WxPayRefundV3Result refund(WxPayRefundV3Request request, String client) throws Exception {
59
+        WxPayService payService = null;
60
+        if ("ios".equals(client) || "android".equals(client)) {
61
+            payService = wxUtils.getAndroidPayService();
62
+        } else {
63
+            payService = wxUtils.getMiniPayService();
64
+        }
61 65
 
62
-        request.setNotifyUrl(servBase + "/wxpay/notify/refund");
66
+        request.setNotifyUrl(servBase + "/wxpay/notify/"+client+"/refund");
63 67
         WxPayRefundV3Result result = payService.refundV3(request);
64 68
 
65 69
         if ("ABNORMAL".equals(result.getStatus())) {

+ 41
- 12
src/main/java/com/yunzhi/nanyang/common/WxUtils.java 查看文件

@@ -21,8 +21,10 @@ public class WxUtils {
21 21
     private static WxMaService workerService;
22 22
     // 农户小程序
23 23
     private static WxMaService farmerService;
24
-    // 微信支付
25
-    private static WxPayService payService;
24
+    // 小程序支付
25
+    private static WxPayService miniPayService;
26
+    // android 支付
27
+    private static WxPayService androidPayService;
26 28
 
27 29
     // 此处不能使用变量注入方式, 否则会是 null
28 30
     ISysMiniappService iSysMiniappService;
@@ -35,7 +37,8 @@ public class WxUtils {
35 37
         try {
36 38
             setWorkerService();
37 39
             setFarmerService();
38
-            setPayService();
40
+            setMiniPayService();
41
+            setAndroidPayService();
39 42
         } catch (Exception e) {
40 43
             e.printStackTrace();
41 44
         }
@@ -92,17 +95,17 @@ public class WxUtils {
92 95
         return service;
93 96
     }
94 97
 
95
-    public WxPayService getPayService() {
96
-        if (payService == null) {
97
-            setPayService();
98
+    public WxPayService getMiniPayService() {
99
+        if (miniPayService == null) {
100
+            setMiniPayService();
98 101
         }
99 102
 
100
-        return payService;
103
+        return miniPayService;
101 104
     }
102 105
 
103
-    private void setPayService() {
106
+    private void setMiniPayService() {
104 107
         WxPayConfig payConfig = new WxPayConfig();
105
-        payConfig.setAppId(config.getPay().getAppId());
108
+        payConfig.setAppId(config.getPay().getMiniAppid());
106 109
         payConfig.setMchId(config.getPay().getMchId());
107 110
         payConfig.setMchKey(config.getPay().getMchKey());
108 111
         payConfig.setSubAppId(config.getPay().getSubAppId());
@@ -111,12 +114,38 @@ public class WxUtils {
111 114
         payConfig.setApiV3Key(config.getPay().getApiV3Key());
112 115
         payConfig.setPrivateKeyPath(config.getPay().getPrivateKeyPath());
113 116
         payConfig.setPrivateCertPath(config.getPay().getPrivateCertPath());
114
-        // 固定小程序支付
115 117
         payConfig.setTradeType(WxPayConstants.TradeType.JSAPI);
116 118
         // 可以指定是否使用沙箱环境
117 119
         payConfig.setUseSandboxEnv(false);
118 120
 
119
-        payService = new WxPayServiceImpl();
120
-        payService.setConfig(payConfig);
121
+        miniPayService = new WxPayServiceImpl();
122
+        miniPayService.setConfig(payConfig);
123
+    }
124
+
125
+    public WxPayService getAndroidPayService() {
126
+        if (androidPayService == null) {
127
+            setAndroidPayService();
128
+        }
129
+
130
+        return androidPayService;
131
+    }
132
+
133
+    private void setAndroidPayService() {
134
+        WxPayConfig payConfig = new WxPayConfig();
135
+        payConfig.setAppId(config.getPay().getMiniAppid());
136
+        payConfig.setMchId(config.getPay().getMchId());
137
+        payConfig.setMchKey(config.getPay().getMchKey());
138
+        payConfig.setSubAppId(config.getPay().getSubAppId());
139
+        payConfig.setSubMchId(config.getPay().getSubMchId());
140
+        payConfig.setKeyPath(config.getPay().getKeyPath());
141
+        payConfig.setApiV3Key(config.getPay().getApiV3Key());
142
+        payConfig.setPrivateKeyPath(config.getPay().getPrivateKeyPath());
143
+        payConfig.setPrivateCertPath(config.getPay().getPrivateCertPath());
144
+        payConfig.setTradeType(WxPayConstants.TradeType.APP);
145
+        // 可以指定是否使用沙箱环境
146
+        payConfig.setUseSandboxEnv(false);
147
+
148
+        androidPayService = new WxPayServiceImpl();
149
+        androidPayService.setConfig(payConfig);
121 150
     }
122 151
 }

+ 2
- 1
src/main/java/com/yunzhi/nanyang/config/WeixinConfig.java 查看文件

@@ -27,7 +27,8 @@ public class WeixinConfig {
27 27
 
28 28
     @Data
29 29
     public static class Pay {
30
-        private String appId;
30
+        private String miniAppid;
31
+        private String androidAppid;
31 32
         private String mchId;
32 33
         private String mchKey;
33 34
         private String subAppId;

+ 2
- 3
src/main/java/com/yunzhi/nanyang/controller/TaPayController.java 查看文件

@@ -54,13 +54,12 @@ public class TaPayController extends BaseController {
54 54
         }
55 55
 
56 56
         WxPayUnifiedOrderV3Result payResult = iTaPayService.createPay(client, clientId, taOrder);
57
-
58
-        WxPayConfig payConfig = wxUtils.getPayService().getConfig();
59
-
60 57
         if ("wx".equals(client)) {
58
+            WxPayConfig payConfig = wxUtils.getMiniPayService().getConfig();
61 59
             WxPayUnifiedOrderV3Result.JsapiResult result = payResult.getPayInfo(TradeTypeEnum.JSAPI, payConfig.getAppId(), payConfig.getMchId(), payConfig.getPrivateKey());
62 60
             return ResponseBean.success(result);
63 61
         } else {
62
+            WxPayConfig payConfig = wxUtils.getAndroidPayService().getConfig();
64 63
             WxPayUnifiedOrderV3Result.AppResult result = payResult.getPayInfo(TradeTypeEnum.APP, payConfig.getAppId(), payConfig.getMchId(), payConfig.getPrivateKey());
65 64
             return ResponseBean.success(result);
66 65
         }

+ 21
- 10
src/main/java/com/yunzhi/nanyang/controller/WxPayController.java 查看文件

@@ -8,10 +8,7 @@ import com.yunzhi.nanyang.common.WxUtils;
8 8
 import com.yunzhi.nanyang.service.ITaPayService;
9 9
 import io.swagger.annotations.Api;
10 10
 import org.springframework.beans.factory.annotation.Autowired;
11
-import org.springframework.web.bind.annotation.RequestBody;
12
-import org.springframework.web.bind.annotation.RequestMapping;
13
-import org.springframework.web.bind.annotation.ResponseBody;
14
-import org.springframework.web.bind.annotation.RestController;
11
+import org.springframework.web.bind.annotation.*;
15 12
 
16 13
 @Api(tags = "微信小程序")
17 14
 @RestController
@@ -25,9 +22,16 @@ public class WxPayController extends BaseController {
25 22
     ITaPayService orderPayService;
26 23
 
27 24
     @ResponseBody
28
-    @RequestMapping("/notify/order")
29
-    public String payNotify(@RequestBody String jsonData) {
30
-        WxPayService payService = wxUtils.getPayService();
25
+    @RequestMapping("/notify/{client}/order")
26
+    public String payNotify(@PathVariable String client,
27
+                            @RequestBody String jsonData) {
28
+        WxPayService payService = null;
29
+        if ("ios".equals(client) || "android".equals(client)) {
30
+            payService = wxUtils.getAndroidPayService();
31
+        } else {
32
+            payService = wxUtils.getMiniPayService();
33
+        }
34
+
31 35
         WxPayOrderNotifyV3Result result = null;
32 36
         try {
33 37
             result = payService.parseOrderNotifyV3Result(jsonData, null);
@@ -42,9 +46,16 @@ public class WxPayController extends BaseController {
42 46
     }
43 47
 
44 48
     @ResponseBody
45
-    @RequestMapping("/notify/refund")
46
-    public String refundNotify(@RequestBody String jsonData) {
47
-        WxPayService payService = wxUtils.getPayService();
49
+    @RequestMapping("/notify/{client}/refund")
50
+    public String refundNotify(@PathVariable String client,
51
+                               @RequestBody String jsonData) {
52
+        WxPayService payService = null;
53
+        if ("ios".equals(client) || "android".equals(client)) {
54
+            payService = wxUtils.getAndroidPayService();
55
+        } else {
56
+            payService = wxUtils.getMiniPayService();
57
+        }
58
+
48 59
         WxPayRefundNotifyV3Result result = null;
49 60
         try {
50 61
             result = payService.parseRefundNotifyV3Result(jsonData, null);

+ 1
- 1
src/main/java/com/yunzhi/nanyang/service/impl/TaPayServiceImpl.java 查看文件

@@ -195,7 +195,7 @@ public class TaPayServiceImpl extends BaseServiceImpl<TaPayMapper, TaPay> implem
195 195
         amount.setCurrency("CNY");
196 196
         request.setAmount(amount);
197 197
 
198
-        wxPayUtils.refund(request);
198
+        wxPayUtils.refund(request, client);
199 199
         if (StringUtils.isEmpty(taPay.getPayId())) {
200 200
             // 退款申请中 ...
201 201
             taOrder.setPayStatus(Constants.PAY_REFUNDING);

+ 2
- 1
src/main/resources/application.yml 查看文件

@@ -80,7 +80,8 @@ weixin:
80 80
     aesKey:
81 81
     msgDataFormat: JSON
82 82
   pay:
83
-    appId: wx94f23e5b285c046d
83
+    miniAppid: wx94f23e5b285c046d
84
+    androidAppid: wx0969b05b5393ae17
84 85
     mchId: 1621038483
85 86
     mchKey: #商户密钥
86 87
     subAppId: #服务商模式下的子商户公众账号ID