张延森 3 years ago
parent
commit
0d90c0f12d

+ 14
- 10
src/main/java/com/yunzhi/nanyang/common/WxPayUtils.java View File

35
     public WxPayUnifiedOrderV3Result createOrder(WxPayUnifiedOrderV3Request request, String client) throws WxPayException {
35
     public WxPayUnifiedOrderV3Result createOrder(WxPayUnifiedOrderV3Request request, String client) throws WxPayException {
36
         // 北京时间
36
         // 北京时间
37
         LocalDateTime now = LocalDateTime.now(ZoneOffset.ofHours(8));
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
         if ("ios".equals(client) || "android".equals(client)) {
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
 //        WxPayUnifiedOrderV3Result wxPayUnifiedOrderV3Result =
48
 //        WxPayUnifiedOrderV3Result wxPayUnifiedOrderV3Result =
50
 //        return wxPayUnifiedOrderV3Result.getPayInfo(tradeType, request.getAppid(), request.getMchid(), payService.getConfig().getPrivateKey());
49
 //        return wxPayUnifiedOrderV3Result.getPayInfo(tradeType, request.getAppid(), request.getMchid(), payService.getConfig().getPrivateKey());
51
     }
50
     }
56
      * @return
55
      * @return
57
      * @throws Exception
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
         WxPayRefundV3Result result = payService.refundV3(request);
67
         WxPayRefundV3Result result = payService.refundV3(request);
64
 
68
 
65
         if ("ABNORMAL".equals(result.getStatus())) {
69
         if ("ABNORMAL".equals(result.getStatus())) {

+ 41
- 12
src/main/java/com/yunzhi/nanyang/common/WxUtils.java View File

21
     private static WxMaService workerService;
21
     private static WxMaService workerService;
22
     // 农户小程序
22
     // 农户小程序
23
     private static WxMaService farmerService;
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
     // 此处不能使用变量注入方式, 否则会是 null
29
     // 此处不能使用变量注入方式, 否则会是 null
28
     ISysMiniappService iSysMiniappService;
30
     ISysMiniappService iSysMiniappService;
35
         try {
37
         try {
36
             setWorkerService();
38
             setWorkerService();
37
             setFarmerService();
39
             setFarmerService();
38
-            setPayService();
40
+            setMiniPayService();
41
+            setAndroidPayService();
39
         } catch (Exception e) {
42
         } catch (Exception e) {
40
             e.printStackTrace();
43
             e.printStackTrace();
41
         }
44
         }
92
         return service;
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
         WxPayConfig payConfig = new WxPayConfig();
107
         WxPayConfig payConfig = new WxPayConfig();
105
-        payConfig.setAppId(config.getPay().getAppId());
108
+        payConfig.setAppId(config.getPay().getMiniAppid());
106
         payConfig.setMchId(config.getPay().getMchId());
109
         payConfig.setMchId(config.getPay().getMchId());
107
         payConfig.setMchKey(config.getPay().getMchKey());
110
         payConfig.setMchKey(config.getPay().getMchKey());
108
         payConfig.setSubAppId(config.getPay().getSubAppId());
111
         payConfig.setSubAppId(config.getPay().getSubAppId());
111
         payConfig.setApiV3Key(config.getPay().getApiV3Key());
114
         payConfig.setApiV3Key(config.getPay().getApiV3Key());
112
         payConfig.setPrivateKeyPath(config.getPay().getPrivateKeyPath());
115
         payConfig.setPrivateKeyPath(config.getPay().getPrivateKeyPath());
113
         payConfig.setPrivateCertPath(config.getPay().getPrivateCertPath());
116
         payConfig.setPrivateCertPath(config.getPay().getPrivateCertPath());
114
-        // 固定小程序支付
115
         payConfig.setTradeType(WxPayConstants.TradeType.JSAPI);
117
         payConfig.setTradeType(WxPayConstants.TradeType.JSAPI);
116
         // 可以指定是否使用沙箱环境
118
         // 可以指定是否使用沙箱环境
117
         payConfig.setUseSandboxEnv(false);
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 View File

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

+ 2
- 3
src/main/java/com/yunzhi/nanyang/controller/TaPayController.java View File

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

+ 21
- 10
src/main/java/com/yunzhi/nanyang/controller/WxPayController.java View File

8
 import com.yunzhi.nanyang.service.ITaPayService;
8
 import com.yunzhi.nanyang.service.ITaPayService;
9
 import io.swagger.annotations.Api;
9
 import io.swagger.annotations.Api;
10
 import org.springframework.beans.factory.annotation.Autowired;
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
 @Api(tags = "微信小程序")
13
 @Api(tags = "微信小程序")
17
 @RestController
14
 @RestController
25
     ITaPayService orderPayService;
22
     ITaPayService orderPayService;
26
 
23
 
27
     @ResponseBody
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
         WxPayOrderNotifyV3Result result = null;
35
         WxPayOrderNotifyV3Result result = null;
32
         try {
36
         try {
33
             result = payService.parseOrderNotifyV3Result(jsonData, null);
37
             result = payService.parseOrderNotifyV3Result(jsonData, null);
42
     }
46
     }
43
 
47
 
44
     @ResponseBody
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
         WxPayRefundNotifyV3Result result = null;
59
         WxPayRefundNotifyV3Result result = null;
49
         try {
60
         try {
50
             result = payService.parseRefundNotifyV3Result(jsonData, null);
61
             result = payService.parseRefundNotifyV3Result(jsonData, null);

+ 1
- 1
src/main/java/com/yunzhi/nanyang/service/impl/TaPayServiceImpl.java View File

195
         amount.setCurrency("CNY");
195
         amount.setCurrency("CNY");
196
         request.setAmount(amount);
196
         request.setAmount(amount);
197
 
197
 
198
-        wxPayUtils.refund(request);
198
+        wxPayUtils.refund(request, client);
199
         if (StringUtils.isEmpty(taPay.getPayId())) {
199
         if (StringUtils.isEmpty(taPay.getPayId())) {
200
             // 退款申请中 ...
200
             // 退款申请中 ...
201
             taOrder.setPayStatus(Constants.PAY_REFUNDING);
201
             taOrder.setPayStatus(Constants.PAY_REFUNDING);

+ 2
- 1
src/main/resources/application.yml View File

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