张延森 2 years ago
parent
commit
f9e711da9c
26 changed files with 1099 additions and 145 deletions
  1. 10
    1
      pom.xml
  2. 5
    0
      src/main/java/com/yunzhi/nanyang/common/DateUtils.java
  3. 6
    0
      src/main/java/com/yunzhi/nanyang/common/MathUtils.java
  4. 75
    0
      src/main/java/com/yunzhi/nanyang/common/alipay/AlipayFactory.java
  5. 131
    0
      src/main/java/com/yunzhi/nanyang/common/alipay/AlipayService.java
  6. 26
    0
      src/main/java/com/yunzhi/nanyang/common/alipay/Utils.java
  7. 143
    0
      src/main/java/com/yunzhi/nanyang/controller/PayNotifyController.java
  8. 119
    0
      src/main/java/com/yunzhi/nanyang/controller/SysAliPayConfigController.java
  9. 119
    0
      src/main/java/com/yunzhi/nanyang/controller/SysWxPayConfigController.java
  10. 18
    3
      src/main/java/com/yunzhi/nanyang/controller/TaPayController.java
  11. 0
    71
      src/main/java/com/yunzhi/nanyang/controller/WxPayController.java
  12. 62
    0
      src/main/java/com/yunzhi/nanyang/entity/SysAliPayConfig.java
  13. 66
    0
      src/main/java/com/yunzhi/nanyang/entity/SysWxPayConfig.java
  14. 18
    0
      src/main/java/com/yunzhi/nanyang/mapper/SysAliPayConfigMapper.java
  15. 18
    0
      src/main/java/com/yunzhi/nanyang/mapper/SysWxPayConfigMapper.java
  16. 16
    0
      src/main/java/com/yunzhi/nanyang/service/ISysAliPayConfigService.java
  17. 16
    0
      src/main/java/com/yunzhi/nanyang/service/ISysWxPayConfigService.java
  18. 10
    1
      src/main/java/com/yunzhi/nanyang/service/ITaPayService.java
  19. 20
    0
      src/main/java/com/yunzhi/nanyang/service/impl/SysAliPayConfigServiceImpl.java
  20. 20
    0
      src/main/java/com/yunzhi/nanyang/service/impl/SysWxPayConfigServiceImpl.java
  21. 162
    66
      src/main/java/com/yunzhi/nanyang/service/impl/TaPayServiceImpl.java
  22. 10
    0
      src/main/resources/application-dev.yml
  23. 13
    3
      src/main/resources/application-prod.yml
  24. 6
    0
      src/main/resources/application.yml
  25. 5
    0
      src/main/resources/mapper/SysAliPayConfigMapper.xml
  26. 5
    0
      src/main/resources/mapper/SysWxPayConfigMapper.xml

+ 10
- 1
pom.xml View File

@@ -103,7 +103,16 @@
103 103
 			<artifactId>weixin-java-pay</artifactId>
104 104
 			<version>4.2.0</version>
105 105
 		</dependency>
106
-		<!--weixin-pay start-->
106
+		<!--weixin-pay end-->
107
+
108
+
109
+		<!--alipay-pay start-->
110
+		<dependency>
111
+			<groupId>com.alipayFactory.sdk</groupId>
112
+			<artifactId>alipayFactory-sdk-java</artifactId>
113
+			<version>4.31.12.ALL</version>
114
+		</dependency>
115
+		<!--alipay-pay end-->
107 116
 
108 117
 		<!--lombok start-->
109 118
 		<dependency>

+ 5
- 0
src/main/java/com/yunzhi/nanyang/common/DateUtils.java View File

@@ -4,6 +4,11 @@ import java.time.*;
4 4
 import java.time.format.DateTimeFormatter;
5 5
 
6 6
 public class DateUtils {
7
+    public static LocalDateTime now() {
8
+        // 北京时间
9
+        return LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
10
+    }
11
+
7 12
     public static LocalDateTime from(String str, DateTimeFormatter fmt) {
8 13
         return LocalDateTime.parse(str, fmt);
9 14
     }

+ 6
- 0
src/main/java/com/yunzhi/nanyang/common/MathUtils.java View File

@@ -1,5 +1,6 @@
1 1
 package com.yunzhi.nanyang.common;
2 2
 
3
+import java.text.DecimalFormat;
3 4
 import java.util.Random;
4 5
 
5 6
 public class MathUtils {
@@ -8,4 +9,9 @@ public class MathUtils {
8 9
         Random random = new Random();
9 10
         return random.nextInt(max) % (max - min + 1) + min;
10 11
     }
12
+
13
+    public static String formatFloat(float f, String formater) {
14
+        DecimalFormat df = new DecimalFormat(formater);
15
+        return  df.format(f);
16
+    }
11 17
 }

+ 75
- 0
src/main/java/com/yunzhi/nanyang/common/alipay/AlipayFactory.java View File

@@ -0,0 +1,75 @@
1
+package com.yunzhi.nanyang.common.alipay;
2
+
3
+import com.alipay.api.AlipayApiException;
4
+import com.alipay.api.AlipayClient;
5
+import com.alipay.api.CertAlipayRequest;
6
+import com.alipay.api.DefaultAlipayClient;
7
+import lombok.Data;
8
+
9
+/**
10
+ * 主要用来构造 支付宝支付客户端
11
+ */
12
+public class AlipayFactory {
13
+
14
+    Config config;
15
+    AlipayClient alipayClient;
16
+
17
+    public AlipayFactory(Config config) {
18
+        this.config = config;
19
+
20
+        try {
21
+            this.initAlipayClient();
22
+        } catch (AlipayApiException e) {
23
+            e.printStackTrace();
24
+        }
25
+    }
26
+
27
+    public AlipayClient getAlipayClient() throws AlipayApiException {
28
+        if (alipayClient == null) {
29
+            this.initAlipayClient();
30
+        }
31
+
32
+        return alipayClient;
33
+    }
34
+
35
+    private void initAlipayClient() throws AlipayApiException {
36
+        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
37
+        //设置网关地址
38
+        certAlipayRequest.setServerUrl(config.getServerUrl());
39
+        //设置应用Id
40
+        certAlipayRequest.setAppId(config.getAppid());
41
+        //设置应用私钥
42
+        certAlipayRequest.setPrivateKey(config.getPrivateKey());
43
+        //设置请求格式,固定值json
44
+        certAlipayRequest.setFormat(config.getFormat());
45
+        //设置字符集
46
+        certAlipayRequest.setCharset(config.getCharset());
47
+        //设置签名类型
48
+        certAlipayRequest.setSignType(config.getSignType());
49
+        //设置应用公钥证书路径
50
+        certAlipayRequest.setCertPath(config.getCertPath());
51
+        //设置支付宝公钥证书路径
52
+        certAlipayRequest.setAlipayPublicCertPath(config.getAlipayPublicCertPath());
53
+        //设置支付宝根证书路径
54
+        certAlipayRequest.setRootCertPath(config.getRootCertPath());
55
+        //构造client
56
+        alipayClient = new DefaultAlipayClient(certAlipayRequest);
57
+    }
58
+
59
+    @Data
60
+    public static class Config {
61
+        String appid;
62
+        String privateKey;
63
+        String certPath;
64
+        String alipayPublicCertPath;
65
+        String rootCertPath;
66
+        String format;
67
+        String charset;
68
+        String signType;
69
+        String serverUrl;
70
+        String notifyUrl;
71
+        String authUrl;
72
+        String authCallback;
73
+    }
74
+
75
+}

+ 131
- 0
src/main/java/com/yunzhi/nanyang/common/alipay/AlipayService.java View File

@@ -0,0 +1,131 @@
1
+package com.yunzhi.nanyang.common.alipay;
2
+
3
+import com.alipay.api.AlipayApiException;
4
+import com.alipay.api.AlipayClient;
5
+import com.alipay.api.domain.AlipayTradeAppPayModel;
6
+import com.alipay.api.domain.AlipayTradeRefundModel;
7
+import com.alipay.api.internal.util.AlipaySignature;
8
+import com.alipay.api.request.AlipayTradeAppPayRequest;
9
+import com.alipay.api.request.AlipayTradeRefundRequest;
10
+import com.alipay.api.response.AlipayTradeAppPayResponse;
11
+import com.alipay.api.response.AlipayTradeRefundResponse;
12
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
13
+import com.yunzhi.nanyang.entity.SysAliPayConfig;
14
+import com.yunzhi.nanyang.service.ISysAliPayConfigService;
15
+import org.springframework.beans.factory.annotation.Autowired;
16
+import org.springframework.stereotype.Component;
17
+
18
+import java.util.Map;
19
+
20
+/**
21
+ * 主要用来实现支付等相关功能
22
+ */
23
+@Component
24
+public class AlipayService {
25
+
26
+    ISysAliPayConfigService iSysAliPayConfigService;
27
+    AlipayFactory alipayFactory = null;
28
+    AlipayFactory.Config config = null;
29
+    AlipayClient alipayClient = null;
30
+
31
+    @Autowired
32
+    public AlipayService(ISysAliPayConfigService iSysAliPayConfigService) {
33
+        this.iSysAliPayConfigService = iSysAliPayConfigService;
34
+
35
+        try {
36
+            config = getConfig();
37
+            alipayFactory = new AlipayFactory(config);
38
+            alipayClient = alipayFactory.getAlipayClient();
39
+        } catch (Exception e) {
40
+            e.printStackTrace();
41
+        }
42
+    }
43
+
44
+    /**
45
+     * 支付下单
46
+     * @param model
47
+     * @param client
48
+     * @return
49
+     * @throws AlipayApiException
50
+     */
51
+    public AlipayTradeAppPayResponse createOrder(AlipayTradeAppPayModel model, String client) throws AlipayApiException {
52
+        if (null == alipayClient) {
53
+            try {
54
+                config = getConfig();
55
+                alipayFactory = new AlipayFactory(config);
56
+                alipayClient = alipayFactory.getAlipayClient();
57
+            } catch (Exception e) {
58
+                e.printStackTrace();
59
+                return null;
60
+            }
61
+        }
62
+
63
+        String notifyUrl = String.format("%s/alipay/%s/order", config.getNotifyUrl(), client);
64
+
65
+        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
66
+        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
67
+        request.setBizModel (model);
68
+        request.setNotifyUrl(notifyUrl);
69
+        return alipayClient.sdkExecute( request );
70
+    }
71
+
72
+    /**
73
+     * 退款
74
+     * @param model
75
+     * @param client
76
+     * @return
77
+     * @throws AlipayApiException
78
+     */
79
+    public AlipayTradeRefundResponse refund(AlipayTradeRefundModel model, String client) throws AlipayApiException {
80
+        if (null == alipayClient) {
81
+            try {
82
+                config = getConfig();
83
+                alipayFactory = new AlipayFactory(config);
84
+                alipayClient = alipayFactory.getAlipayClient();
85
+            } catch (Exception e) {
86
+                e.printStackTrace();
87
+                return null;
88
+            }
89
+        }
90
+
91
+        String notifyUrl = String.format("%s/alipay/%s/refund", config.getNotifyUrl(), client);
92
+
93
+        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
94
+        request.setBizModel(model);
95
+        request.setNotifyUrl(notifyUrl);
96
+        return alipayClient.execute(request);
97
+    }
98
+
99
+    /**
100
+     * 获取 支付宝配置
101
+     * @return
102
+     */
103
+    public boolean rsaCheckV1(Map<String, String> params) throws AlipayApiException {
104
+        if (null == config) return false;
105
+
106
+        return AlipaySignature.rsaCheckV1 (params, config.getAlipayPublicCertPath(), config.getCharset(),config.getSignType());
107
+    }
108
+
109
+    private AlipayFactory.Config getConfig() throws Exception {
110
+        SysAliPayConfig sysAliPayConfig = iSysAliPayConfigService.getOne(new QueryWrapper<SysAliPayConfig>().eq("status", 1).last("limit 1"));
111
+        if (null == sysAliPayConfig) {
112
+            throw new Exception("未找到支付宝支付相关配置");
113
+        }
114
+
115
+        AlipayFactory.Config config = new AlipayFactory.Config();
116
+        config.setAppid(sysAliPayConfig.getAppid());
117
+        config.setPrivateKey(sysAliPayConfig.getPrivateKey());
118
+        config.setCertPath(sysAliPayConfig.getCertPath());
119
+        config.setAlipayPublicCertPath(sysAliPayConfig.getAlipayPublicCertPath());
120
+        config.setRootCertPath(sysAliPayConfig.getRootCertPath());
121
+        config.setServerUrl(sysAliPayConfig.getServerUrl());
122
+        config.setNotifyUrl(sysAliPayConfig.getNotifyUrl());
123
+        config.setAuthUrl(sysAliPayConfig.getAuthUrl());
124
+        config.setAuthCallback(sysAliPayConfig.getAuthCallback());
125
+        config.setFormat("json");
126
+        config.setCharset("UTF-8");
127
+        config.setSignType("RSA2");
128
+
129
+        return config;
130
+    }
131
+}

+ 26
- 0
src/main/java/com/yunzhi/nanyang/common/alipay/Utils.java View File

@@ -0,0 +1,26 @@
1
+package com.yunzhi.nanyang.common.alipay;
2
+
3
+import java.io.IOException;
4
+import java.nio.ByteBuffer;
5
+import java.nio.channels.FileChannel;
6
+import java.nio.charset.StandardCharsets;
7
+import java.nio.file.Paths;
8
+
9
+public class Utils {
10
+    /**
11
+     * 小文件读取, 文件大小不能超过 2M
12
+     * @param f
13
+     * @return
14
+     * @throws IOException
15
+     */
16
+    public static String readFile(String f) throws IOException {
17
+        FileChannel file = FileChannel.open(Paths.get(f));
18
+        int fileSize = (int) file.size();
19
+        ByteBuffer buff = ByteBuffer.allocate(fileSize);
20
+        file.read(buff);
21
+        buff.flip();
22
+        file.close();
23
+        return StandardCharsets.UTF_8.decode(buff).toString();
24
+    }
25
+
26
+}

+ 143
- 0
src/main/java/com/yunzhi/nanyang/controller/PayNotifyController.java View File

@@ -0,0 +1,143 @@
1
+package com.yunzhi.nanyang.controller;
2
+
3
+import com.alipay.api.AlipayApiException;
4
+import com.alipay.api.internal.util.AlipaySignature;
5
+import com.github.binarywang.wxpay.bean.notify.*;
6
+import com.github.binarywang.wxpay.service.WxPayService;
7
+import com.yunzhi.nanyang.common.BaseController;
8
+import com.yunzhi.nanyang.common.WxUtils;
9
+import com.yunzhi.nanyang.common.alipay.AlipayFactory;
10
+import com.yunzhi.nanyang.common.alipay.AlipayService;
11
+import com.yunzhi.nanyang.service.ITaPayService;
12
+import io.swagger.annotations.Api;
13
+import org.springframework.beans.factory.annotation.Autowired;
14
+import org.springframework.web.bind.annotation.*;
15
+
16
+import javax.servlet.http.HttpServletRequest;
17
+import java.util.HashMap;
18
+import java.util.Iterator;
19
+import java.util.Map;
20
+
21
+@Api(tags = "支付回调")
22
+@RestController
23
+@RequestMapping("/")
24
+public class PayNotifyController extends BaseController {
25
+
26
+    @Autowired
27
+    WxUtils wxUtils;
28
+
29
+    @Autowired
30
+    AlipayService alipayService;
31
+
32
+    @Autowired
33
+    ITaPayService orderPayService;
34
+
35
+    @ResponseBody
36
+    @RequestMapping("/wxpay/notify/{client}/order")
37
+    public String wxNotify(@PathVariable String client,
38
+                            @RequestBody String jsonData) {
39
+        WxPayService payService = null;
40
+        if ("ios".equals(client) || "android".equals(client)) {
41
+            payService = wxUtils.getAndroidPayService();
42
+        } else {
43
+            payService = wxUtils.getMiniPayService();
44
+        }
45
+
46
+        WxPayOrderNotifyV3Result result = null;
47
+        try {
48
+            result = payService.parseOrderNotifyV3Result(jsonData, null);
49
+            orderPayService.notifyOrder(result.getResult());
50
+            return WxPayNotifyResponse.success("OK");
51
+        } catch (Exception e) {
52
+            e.printStackTrace();
53
+            return WxPayNotifyResponse.fail(e.getMessage());
54
+        }
55
+    }
56
+
57
+    @ResponseBody
58
+    @RequestMapping("/wxpay/notify/{client}/refund")
59
+    public String wxRefundNotify(@PathVariable String client,
60
+                               @RequestBody String jsonData) {
61
+        WxPayService payService = null;
62
+        if ("ios".equals(client) || "android".equals(client)) {
63
+            payService = wxUtils.getAndroidPayService();
64
+        } else {
65
+            payService = wxUtils.getMiniPayService();
66
+        }
67
+
68
+        WxPayRefundNotifyV3Result result = null;
69
+        try {
70
+            result = payService.parseRefundNotifyV3Result(jsonData, null);
71
+            orderPayService.refundNotifyOrder(result.getResult());
72
+            return WxPayNotifyResponse.success("OK");
73
+        } catch (Exception e) {
74
+            e.printStackTrace();
75
+            return WxPayNotifyResponse.fail(e.getMessage());
76
+        }
77
+
78
+    }
79
+
80
+
81
+    @ResponseBody
82
+    @RequestMapping("/alipay/notify/{client}/order")
83
+    public String aliNotify(@PathVariable String client,
84
+                            HttpServletRequest request) {
85
+        Map<String, String> params = getRequestParams(request);
86
+
87
+        // 支付等待中
88
+        if ("WAIT_BUYER_PAY".equals(params.get("trade_status"))) {
89
+            return "success";
90
+        }
91
+
92
+        //切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
93
+        //boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
94
+        try {
95
+            boolean isOk = alipayService.rsaCheckV1(params);
96
+            if (!isOk) return "failure";
97
+
98
+            orderPayService.notifyOrder(params);
99
+            return "success";
100
+        } catch (Exception e) {
101
+            e.printStackTrace();
102
+            return "failure";
103
+        }
104
+    }
105
+
106
+
107
+    @ResponseBody
108
+    @RequestMapping("/alipay/notify/{client}/refund")
109
+    public String aliRefundNotify(@PathVariable String client,
110
+                                  HttpServletRequest request) {
111
+        Map<String, String> params = getRequestParams(request);
112
+
113
+        try {
114
+            boolean isOk = alipayService.rsaCheckV1(params);
115
+            if (!isOk) return "failure";
116
+
117
+            orderPayService.refundNotifyOrder(params);
118
+            return "success";
119
+        } catch (Exception e) {
120
+            e.printStackTrace();
121
+            return "failure";
122
+        }
123
+
124
+    }
125
+
126
+    private Map<String, String> getRequestParams(HttpServletRequest request) {
127
+        Map<String, String> params = new HashMap<>();
128
+        Map requestParams = request.getParameterMap();
129
+        for(Iterator iter = requestParams.keySet().iterator(); iter.hasNext();){
130
+            String name = (String)iter.next();
131
+            String[] values = (String [])requestParams.get(name);
132
+            String valueStr = "";
133
+            for(int i = 0;i < values.length;i ++ ){
134
+                valueStr =  (i==values.length-1)?valueStr + values [i]:valueStr + values[i] + ",";
135
+            }
136
+            //乱码解决,这段代码在出现乱码时使用。
137
+            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
138
+            params.put (name,valueStr);
139
+        }
140
+        return params;
141
+    }
142
+
143
+}

+ 119
- 0
src/main/java/com/yunzhi/nanyang/controller/SysAliPayConfigController.java View File

@@ -0,0 +1,119 @@
1
+package com.yunzhi.nanyang.controller;
2
+
3
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4
+import com.baomidou.mybatisplus.core.metadata.IPage;
5
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6
+import com.yunzhi.nanyang.common.BaseController;
7
+import com.yunzhi.nanyang.common.ResponseBean;
8
+import io.swagger.annotations.Api;
9
+import io.swagger.annotations.ApiOperation;
10
+import io.swagger.annotations.ApiParam;
11
+import org.slf4j.Logger;
12
+import org.slf4j.LoggerFactory;
13
+import org.springframework.beans.factory.annotation.Autowired;
14
+import org.springframework.web.bind.annotation.PathVariable;
15
+import org.springframework.web.bind.annotation.RequestBody;
16
+import org.springframework.web.bind.annotation.RequestMapping;
17
+import org.springframework.web.bind.annotation.RequestMethod;
18
+import org.springframework.web.bind.annotation.RequestParam;
19
+import com.yunzhi.nanyang.service.ISysAliPayConfigService;
20
+import com.yunzhi.nanyang.entity.SysAliPayConfig;
21
+import org.springframework.web.bind.annotation.RestController;
22
+
23
+/**
24
+ * <p>
25
+    * 阿里支付配置 前端控制器
26
+    * </p>
27
+ *
28
+ * @author yansen
29
+ * @since 2022-06-22
30
+ */
31
+
32
+@Api(tags = "阿里支付配置")
33
+@RestController
34
+@RequestMapping("/")
35
+public class SysAliPayConfigController extends BaseController {
36
+
37
+    private final Logger logger = LoggerFactory.getLogger(SysAliPayConfigController.class);
38
+
39
+    @Autowired
40
+    public ISysAliPayConfigService iSysAliPayConfigService;
41
+
42
+
43
+    /**
44
+     * 分页查询列表
45
+     * @param pageNum
46
+     * @param pageSize
47
+     * @return
48
+     */
49
+    @RequestMapping(value="/sysAliPayConfig",method= RequestMethod.GET)
50
+    @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
51
+    public ResponseBean sysAliPayConfigList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
52
+									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
53
+
54
+		    IPage<SysAliPayConfig> pg = new Page<>(pageNum, pageSize);
55
+            QueryWrapper<SysAliPayConfig> queryWrapper = new QueryWrapper<>();
56
+            queryWrapper.orderByDesc("create_date");
57
+
58
+            IPage<SysAliPayConfig> result = iSysAliPayConfigService.page(pg, queryWrapper);
59
+            return ResponseBean.success(result);
60
+    }
61
+
62
+    /**
63
+     * 保存对象
64
+     * @param sysAliPayConfig 实体对象
65
+     * @return
66
+     */
67
+    @RequestMapping(value="/sysAliPayConfig",method= RequestMethod.POST)
68
+    @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
69
+    public ResponseBean sysAliPayConfigAdd(@ApiParam("保存内容") @RequestBody SysAliPayConfig sysAliPayConfig) throws Exception{
70
+
71
+        if (iSysAliPayConfigService.save(sysAliPayConfig)){
72
+            return ResponseBean.success(sysAliPayConfig);
73
+        }else {
74
+            return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
75
+        }
76
+    }
77
+
78
+    /**
79
+     * 根据id删除对象
80
+     * @param id  实体ID
81
+     */
82
+    @RequestMapping(value="/sysAliPayConfig/{id}", method= RequestMethod.DELETE)
83
+    @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
84
+    public ResponseBean sysAliPayConfigDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
85
+        if(iSysAliPayConfigService.removeById(id)){
86
+            return ResponseBean.success("success");
87
+        }else {
88
+            return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
89
+        }
90
+    }
91
+
92
+    /**
93
+     * 修改对象
94
+     * @param id  实体ID
95
+     * @param sysAliPayConfig 实体对象
96
+     * @return
97
+     */
98
+    @RequestMapping(value="/sysAliPayConfig/{id}",method= RequestMethod.PUT)
99
+    @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
100
+    public ResponseBean sysAliPayConfigUpdate(@ApiParam("对象ID") @PathVariable Integer id,
101
+                                        @ApiParam("更新内容") @RequestBody SysAliPayConfig sysAliPayConfig) throws Exception{
102
+
103
+        if (iSysAliPayConfigService.updateById(sysAliPayConfig)){
104
+            return ResponseBean.success(iSysAliPayConfigService.getById(id));
105
+        }else {
106
+            return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
107
+        }
108
+    }
109
+
110
+    /**
111
+     * 根据id查询对象
112
+     * @param id  实体ID
113
+     */
114
+    @RequestMapping(value="/sysAliPayConfig/{id}",method= RequestMethod.GET)
115
+    @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
116
+    public ResponseBean sysAliPayConfigGet(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
117
+        return ResponseBean.success(iSysAliPayConfigService.getById(id));
118
+    }
119
+}

+ 119
- 0
src/main/java/com/yunzhi/nanyang/controller/SysWxPayConfigController.java View File

@@ -0,0 +1,119 @@
1
+package com.yunzhi.nanyang.controller;
2
+
3
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4
+import com.baomidou.mybatisplus.core.metadata.IPage;
5
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6
+import com.yunzhi.nanyang.common.BaseController;
7
+import com.yunzhi.nanyang.common.ResponseBean;
8
+import io.swagger.annotations.Api;
9
+import io.swagger.annotations.ApiOperation;
10
+import io.swagger.annotations.ApiParam;
11
+import org.slf4j.Logger;
12
+import org.slf4j.LoggerFactory;
13
+import org.springframework.beans.factory.annotation.Autowired;
14
+import org.springframework.web.bind.annotation.PathVariable;
15
+import org.springframework.web.bind.annotation.RequestBody;
16
+import org.springframework.web.bind.annotation.RequestMapping;
17
+import org.springframework.web.bind.annotation.RequestMethod;
18
+import org.springframework.web.bind.annotation.RequestParam;
19
+import com.yunzhi.nanyang.service.ISysWxPayConfigService;
20
+import com.yunzhi.nanyang.entity.SysWxPayConfig;
21
+import org.springframework.web.bind.annotation.RestController;
22
+
23
+/**
24
+ * <p>
25
+    * 微信支付配置 前端控制器
26
+    * </p>
27
+ *
28
+ * @author yansen
29
+ * @since 2022-06-22
30
+ */
31
+
32
+@Api(tags = "微信支付配置")
33
+@RestController
34
+@RequestMapping("/")
35
+public class SysWxPayConfigController extends BaseController {
36
+
37
+    private final Logger logger = LoggerFactory.getLogger(SysWxPayConfigController.class);
38
+
39
+    @Autowired
40
+    public ISysWxPayConfigService iSysWxPayConfigService;
41
+
42
+
43
+    /**
44
+     * 分页查询列表
45
+     * @param pageNum
46
+     * @param pageSize
47
+     * @return
48
+     */
49
+    @RequestMapping(value="/sysWxPayConfig",method= RequestMethod.GET)
50
+    @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
51
+    public ResponseBean sysWxPayConfigList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
52
+									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
53
+
54
+		    IPage<SysWxPayConfig> pg = new Page<>(pageNum, pageSize);
55
+            QueryWrapper<SysWxPayConfig> queryWrapper = new QueryWrapper<>();
56
+            queryWrapper.orderByDesc("create_date");
57
+
58
+            IPage<SysWxPayConfig> result = iSysWxPayConfigService.page(pg, queryWrapper);
59
+            return ResponseBean.success(result);
60
+    }
61
+
62
+    /**
63
+     * 保存对象
64
+     * @param sysWxPayConfig 实体对象
65
+     * @return
66
+     */
67
+    @RequestMapping(value="/sysWxPayConfig",method= RequestMethod.POST)
68
+    @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
69
+    public ResponseBean sysWxPayConfigAdd(@ApiParam("保存内容") @RequestBody SysWxPayConfig sysWxPayConfig) throws Exception{
70
+
71
+        if (iSysWxPayConfigService.save(sysWxPayConfig)){
72
+            return ResponseBean.success(sysWxPayConfig);
73
+        }else {
74
+            return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
75
+        }
76
+    }
77
+
78
+    /**
79
+     * 根据id删除对象
80
+     * @param id  实体ID
81
+     */
82
+    @RequestMapping(value="/sysWxPayConfig/{id}", method= RequestMethod.DELETE)
83
+    @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
84
+    public ResponseBean sysWxPayConfigDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
85
+        if(iSysWxPayConfigService.removeById(id)){
86
+            return ResponseBean.success("success");
87
+        }else {
88
+            return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
89
+        }
90
+    }
91
+
92
+    /**
93
+     * 修改对象
94
+     * @param id  实体ID
95
+     * @param sysWxPayConfig 实体对象
96
+     * @return
97
+     */
98
+    @RequestMapping(value="/sysWxPayConfig/{id}",method= RequestMethod.PUT)
99
+    @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
100
+    public ResponseBean sysWxPayConfigUpdate(@ApiParam("对象ID") @PathVariable Integer id,
101
+                                        @ApiParam("更新内容") @RequestBody SysWxPayConfig sysWxPayConfig) throws Exception{
102
+
103
+        if (iSysWxPayConfigService.updateById(sysWxPayConfig)){
104
+            return ResponseBean.success(iSysWxPayConfigService.getById(id));
105
+        }else {
106
+            return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
107
+        }
108
+    }
109
+
110
+    /**
111
+     * 根据id查询对象
112
+     * @param id  实体ID
113
+     */
114
+    @RequestMapping(value="/sysWxPayConfig/{id}",method= RequestMethod.GET)
115
+    @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
116
+    public ResponseBean sysWxPayConfigGet(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
117
+        return ResponseBean.success(iSysWxPayConfigService.getById(id));
118
+    }
119
+}

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

@@ -1,5 +1,6 @@
1 1
 package com.yunzhi.nanyang.controller;
2 2
 
3
+import com.alipay.api.response.AlipayTradeAppPayResponse;
3 4
 import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
4 5
 import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
5 6
 import com.github.binarywang.wxpay.config.WxPayConfig;
@@ -53,15 +54,29 @@ public class TaPayController extends BaseController {
53 54
             return ResponseBean.error("订单已支付");
54 55
         }
55 56
 
56
-        WxPayUnifiedOrderV3Result payResult = iTaPayService.createPay(client, clientId, taOrder);
57
-        if ("wx".equals(client)) {
57
+        iTaOrderService.updateById(taOrder);
58
+
59
+        // 是否微信小程序
60
+        boolean isMiniapp = "wx".equals(client);
61
+        // 是否微信支付
62
+        boolean isWxpay = "wx".equals(payOrder.getPayType());
63
+
64
+        // 如果是微信小程序支付
65
+        if (isMiniapp) {
66
+            WxPayUnifiedOrderV3Result payResult = iTaPayService.createWxPay(client, clientId, taOrder);
58 67
             WxPayConfig payConfig = wxUtils.getMiniPayService().getConfig();
59 68
             WxPayUnifiedOrderV3Result.JsapiResult result = payResult.getPayInfo(TradeTypeEnum.JSAPI, payConfig.getAppId(), payConfig.getMchId(), payConfig.getPrivateKey());
60 69
             return ResponseBean.success(result);
61
-        } else {
70
+        } else if (isWxpay) {
71
+            // 如果是 APP 微信支付
72
+            WxPayUnifiedOrderV3Result payResult = iTaPayService.createWxPay(client, clientId, taOrder);
62 73
             WxPayConfig payConfig = wxUtils.getAndroidPayService().getConfig();
63 74
             WxPayUnifiedOrderV3Result.AppResult result = payResult.getPayInfo(TradeTypeEnum.APP, payConfig.getAppId(), payConfig.getMchId(), payConfig.getPrivateKey());
64 75
             return ResponseBean.success(result);
76
+        } else {
77
+            // 如果是 APP 支付宝支付
78
+            AlipayTradeAppPayResponse aliPayResponse = iTaPayService.createAliPay(client, clientId, taOrder);
79
+            return ResponseBean.success(aliPayResponse.getBody());
65 80
         }
66 81
     }
67 82
 

+ 0
- 71
src/main/java/com/yunzhi/nanyang/controller/WxPayController.java View File

@@ -1,71 +0,0 @@
1
-package com.yunzhi.nanyang.controller;
2
-
3
-import com.github.binarywang.wxpay.bean.notify.*;
4
-import com.github.binarywang.wxpay.exception.WxPayException;
5
-import com.github.binarywang.wxpay.service.WxPayService;
6
-import com.yunzhi.nanyang.common.BaseController;
7
-import com.yunzhi.nanyang.common.WxUtils;
8
-import com.yunzhi.nanyang.service.ITaPayService;
9
-import io.swagger.annotations.Api;
10
-import org.springframework.beans.factory.annotation.Autowired;
11
-import org.springframework.web.bind.annotation.*;
12
-
13
-@Api(tags = "微信小程序")
14
-@RestController
15
-@RequestMapping("/wxpay")
16
-public class WxPayController extends BaseController {
17
-
18
-    @Autowired
19
-    WxUtils wxUtils;
20
-
21
-    @Autowired
22
-    ITaPayService orderPayService;
23
-
24
-    @ResponseBody
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
-
35
-        WxPayOrderNotifyV3Result result = null;
36
-        try {
37
-            result = payService.parseOrderNotifyV3Result(jsonData, null);
38
-            orderPayService.notifyOrder(result.getResult());
39
-            return WxPayNotifyResponse.success("OK");
40
-        } catch (Exception e) {
41
-            e.printStackTrace();
42
-            return WxPayNotifyResponse.fail(e.getMessage());
43
-        }
44
-
45
-
46
-    }
47
-
48
-    @ResponseBody
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
-
59
-        WxPayRefundNotifyV3Result result = null;
60
-        try {
61
-            result = payService.parseRefundNotifyV3Result(jsonData, null);
62
-            orderPayService.refundNotifyOrder(result.getResult());
63
-            return WxPayNotifyResponse.success("OK");
64
-        } catch (Exception e) {
65
-            e.printStackTrace();
66
-            return WxPayNotifyResponse.fail(e.getMessage());
67
-        }
68
-
69
-    }
70
-
71
-}

+ 62
- 0
src/main/java/com/yunzhi/nanyang/entity/SysAliPayConfig.java View File

@@ -0,0 +1,62 @@
1
+package com.yunzhi.nanyang.entity;
2
+
3
+import com.baomidou.mybatisplus.annotation.IdType;
4
+import com.baomidou.mybatisplus.annotation.TableId;
5
+import java.io.Serializable;
6
+import io.swagger.annotations.ApiModel;
7
+import io.swagger.annotations.ApiModelProperty;
8
+import lombok.Data;
9
+import lombok.EqualsAndHashCode;
10
+import lombok.experimental.Accessors;
11
+
12
+/**
13
+ * <p>
14
+ * 阿里支付配置
15
+ * </p>
16
+ *
17
+ * @author yansen
18
+ * @since 2022-06-22
19
+ */
20
+@Data
21
+@EqualsAndHashCode(callSuper = false)
22
+@Accessors(chain = true)
23
+@ApiModel(value="SysAliPayConfig对象", description="阿里支付配置")
24
+public class SysAliPayConfig implements Serializable {
25
+
26
+    private static final long serialVersionUID = 1L;
27
+
28
+    @ApiModelProperty(value = "应用ID")
29
+    @TableId(value = "appid", type = IdType.INPUT)
30
+    private String appid;
31
+
32
+    @ApiModelProperty(value = "应用类型")
33
+    private String appType;
34
+
35
+    @ApiModelProperty(value = "应用私钥")
36
+    private String privateKey;
37
+
38
+    @ApiModelProperty(value = "应用公钥证书路径")
39
+    private String certPath;
40
+
41
+    @ApiModelProperty(value = "支付宝公钥证书路径")
42
+    private String alipayPublicCertPath;
43
+
44
+    @ApiModelProperty(value = "支付宝根证书路径")
45
+    private String rootCertPath;
46
+
47
+    @ApiModelProperty(value = "网关地址")
48
+    private String serverUrl;
49
+
50
+    @ApiModelProperty(value = "授权地址")
51
+    private String authUrl;
52
+
53
+    @ApiModelProperty(value = "授权回调地址")
54
+    private String authCallback;
55
+
56
+    @ApiModelProperty(value = "支付回调地址")
57
+    private String notifyUrl;
58
+
59
+    @ApiModelProperty(value = "状态")
60
+    private Integer status;
61
+
62
+}

+ 66
- 0
src/main/java/com/yunzhi/nanyang/entity/SysWxPayConfig.java View File

@@ -0,0 +1,66 @@
1
+package com.yunzhi.nanyang.entity;
2
+
3
+import com.baomidou.mybatisplus.annotation.IdType;
4
+import com.baomidou.mybatisplus.annotation.TableId;
5
+import java.io.Serializable;
6
+import io.swagger.annotations.ApiModel;
7
+import io.swagger.annotations.ApiModelProperty;
8
+import lombok.Data;
9
+import lombok.EqualsAndHashCode;
10
+import lombok.experimental.Accessors;
11
+
12
+/**
13
+ * <p>
14
+ * 微信支付配置
15
+ * </p>
16
+ *
17
+ * @author yansen
18
+ * @since 2022-06-22
19
+ */
20
+@Data
21
+@EqualsAndHashCode(callSuper = false)
22
+@Accessors(chain = true)
23
+@ApiModel(value="SysWxPayConfig对象", description="微信支付配置")
24
+public class SysWxPayConfig implements Serializable {
25
+
26
+    private static final long serialVersionUID = 1L;
27
+
28
+    @ApiModelProperty(value = "应用ID")
29
+    @TableId(value = "appid", type = IdType.INPUT)
30
+    private String appid;
31
+
32
+    @ApiModelProperty(value = "应用类型")
33
+    private String appType;
34
+
35
+    @ApiModelProperty(value = "商户ID")
36
+    private String mchId;
37
+
38
+    @ApiModelProperty(value = "商户密钥")
39
+    private String mchKey;
40
+
41
+    @ApiModelProperty(value = "子商户应用ID")
42
+    private String subAppId;
43
+
44
+    @ApiModelProperty(value = "子商户ID")
45
+    private String subMchId;
46
+
47
+    @ApiModelProperty(value = "微信V3版本KEY")
48
+    private String apiV3Key;
49
+
50
+    @ApiModelProperty(value = "密钥路径")
51
+    private String keyPath;
52
+
53
+    @ApiModelProperty(value = "私钥路径")
54
+    private String privateKeyPath;
55
+
56
+    @ApiModelProperty(value = "私钥证书路径")
57
+    private String privateCertPath;
58
+
59
+    @ApiModelProperty(value = "支付回调")
60
+    private String notifyUrl;
61
+
62
+    @ApiModelProperty(value = "状态")
63
+    private Integer status;
64
+
65
+
66
+}

+ 18
- 0
src/main/java/com/yunzhi/nanyang/mapper/SysAliPayConfigMapper.java View File

@@ -0,0 +1,18 @@
1
+package com.yunzhi.nanyang.mapper;
2
+
3
+import com.yunzhi.nanyang.entity.SysAliPayConfig;
4
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5
+import org.apache.ibatis.annotations.Mapper;
6
+
7
+/**
8
+ * <p>
9
+ * 阿里支付配置 Mapper 接口
10
+ * </p>
11
+ *
12
+ * @author yansen
13
+ * @since 2022-06-22
14
+ */
15
+@Mapper
16
+public interface SysAliPayConfigMapper extends BaseMapper<SysAliPayConfig> {
17
+
18
+}

+ 18
- 0
src/main/java/com/yunzhi/nanyang/mapper/SysWxPayConfigMapper.java View File

@@ -0,0 +1,18 @@
1
+package com.yunzhi.nanyang.mapper;
2
+
3
+import com.yunzhi.nanyang.entity.SysWxPayConfig;
4
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5
+import org.apache.ibatis.annotations.Mapper;
6
+
7
+/**
8
+ * <p>
9
+ * 微信支付配置 Mapper 接口
10
+ * </p>
11
+ *
12
+ * @author yansen
13
+ * @since 2022-06-22
14
+ */
15
+@Mapper
16
+public interface SysWxPayConfigMapper extends BaseMapper<SysWxPayConfig> {
17
+
18
+}

+ 16
- 0
src/main/java/com/yunzhi/nanyang/service/ISysAliPayConfigService.java View File

@@ -0,0 +1,16 @@
1
+package com.yunzhi.nanyang.service;
2
+
3
+import com.yunzhi.nanyang.entity.SysAliPayConfig;
4
+import com.baomidou.mybatisplus.extension.service.IService;
5
+
6
+/**
7
+ * <p>
8
+ * 阿里支付配置 服务类
9
+ * </p>
10
+ *
11
+ * @author yansen
12
+ * @since 2022-06-22
13
+ */
14
+public interface ISysAliPayConfigService extends IService<SysAliPayConfig> {
15
+
16
+}

+ 16
- 0
src/main/java/com/yunzhi/nanyang/service/ISysWxPayConfigService.java View File

@@ -0,0 +1,16 @@
1
+package com.yunzhi.nanyang.service;
2
+
3
+import com.yunzhi.nanyang.entity.SysWxPayConfig;
4
+import com.baomidou.mybatisplus.extension.service.IService;
5
+
6
+/**
7
+ * <p>
8
+ * 微信支付配置 服务类
9
+ * </p>
10
+ *
11
+ * @author yansen
12
+ * @since 2022-06-22
13
+ */
14
+public interface ISysWxPayConfigService extends IService<SysWxPayConfig> {
15
+
16
+}

+ 10
- 1
src/main/java/com/yunzhi/nanyang/service/ITaPayService.java View File

@@ -1,14 +1,17 @@
1 1
 package com.yunzhi.nanyang.service;
2 2
 
3
+import com.alipay.api.response.AlipayTradeAppPayResponse;
3 4
 import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result;
4 5
 import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyV3Result;
5 6
 import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
6 7
 import com.yunzhi.nanyang.entity.TaOrder;
7 8
 import com.yunzhi.nanyang.entity.TaPay;
8 9
 
10
+import java.util.Map;
11
+
9 12
 public interface ITaPayService extends IBaseService<TaPay> {
10 13
 
11
-    WxPayUnifiedOrderV3Result createPay(String client, String clientId, TaOrder taOrder) throws Exception;
14
+    WxPayUnifiedOrderV3Result createWxPay(String client, String clientId, TaOrder taOrder) throws Exception;
12 15
 
13 16
     WxPayUnifiedOrderV3Result prePay(TaPay taPay, String client) throws Exception;
14 17
 
@@ -19,4 +22,10 @@ public interface ITaPayService extends IBaseService<TaPay> {
19 22
     void refund(String client, String clientId, TaOrder taOrder) throws Exception;
20 23
 
21 24
     void refundNotifyOrder(WxPayRefundNotifyV3Result.DecryptNotifyResult result) throws Exception;
25
+
26
+    AlipayTradeAppPayResponse createAliPay(String client, String clientId, TaOrder taOrder) throws Exception;
27
+
28
+    void notifyOrder(Map<String, String> params) throws Exception;
29
+
30
+    void refundNotifyOrder(Map<String, String> params) throws Exception;
22 31
 }

+ 20
- 0
src/main/java/com/yunzhi/nanyang/service/impl/SysAliPayConfigServiceImpl.java View File

@@ -0,0 +1,20 @@
1
+package com.yunzhi.nanyang.service.impl;
2
+
3
+import com.yunzhi.nanyang.entity.SysAliPayConfig;
4
+import com.yunzhi.nanyang.mapper.SysAliPayConfigMapper;
5
+import com.yunzhi.nanyang.service.ISysAliPayConfigService;
6
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
7
+import org.springframework.stereotype.Service;
8
+
9
+/**
10
+ * <p>
11
+ * 阿里支付配置 服务实现类
12
+ * </p>
13
+ *
14
+ * @author yansen
15
+ * @since 2022-06-22
16
+ */
17
+@Service
18
+public class SysAliPayConfigServiceImpl extends ServiceImpl<SysAliPayConfigMapper, SysAliPayConfig> implements ISysAliPayConfigService {
19
+
20
+}

+ 20
- 0
src/main/java/com/yunzhi/nanyang/service/impl/SysWxPayConfigServiceImpl.java View File

@@ -0,0 +1,20 @@
1
+package com.yunzhi.nanyang.service.impl;
2
+
3
+import com.yunzhi.nanyang.entity.SysWxPayConfig;
4
+import com.yunzhi.nanyang.mapper.SysWxPayConfigMapper;
5
+import com.yunzhi.nanyang.service.ISysWxPayConfigService;
6
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
7
+import org.springframework.stereotype.Service;
8
+
9
+/**
10
+ * <p>
11
+ * 微信支付配置 服务实现类
12
+ * </p>
13
+ *
14
+ * @author yansen
15
+ * @since 2022-06-22
16
+ */
17
+@Service
18
+public class SysWxPayConfigServiceImpl extends ServiceImpl<SysWxPayConfigMapper, SysWxPayConfig> implements ISysWxPayConfigService {
19
+
20
+}

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

@@ -1,5 +1,10 @@
1 1
 package com.yunzhi.nanyang.service.impl;
2 2
 
3
+import com.alipay.api.AlipayApiException;
4
+import com.alipay.api.domain.AlipayTradeAppPayModel;
5
+import com.alipay.api.domain.AlipayTradeRefundModel;
6
+import com.alipay.api.response.AlipayTradeAppPayResponse;
7
+import com.alipay.api.response.AlipayTradeRefundResponse;
3 8
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 9
 import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result;
5 10
 import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyV3Result;
@@ -9,10 +14,8 @@ import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
9 14
 import com.github.binarywang.wxpay.constant.WxPayConstants;
10 15
 import com.github.binarywang.wxpay.constant.WxPayErrorCode;
11 16
 import com.github.binarywang.wxpay.exception.WxPayException;
12
-import com.yunzhi.nanyang.common.Constants;
13
-import com.yunzhi.nanyang.common.DateUtils;
14
-import com.yunzhi.nanyang.common.StringUtils;
15
-import com.yunzhi.nanyang.common.WxPayUtils;
17
+import com.yunzhi.nanyang.common.*;
18
+import com.yunzhi.nanyang.common.alipay.AlipayService;
16 19
 import com.yunzhi.nanyang.entity.TaAccount;
17 20
 import com.yunzhi.nanyang.entity.TaAccountLog;
18 21
 import com.yunzhi.nanyang.entity.TaOrder;
@@ -26,6 +29,7 @@ import org.springframework.stereotype.Service;
26 29
 
27 30
 import java.time.LocalDateTime;
28 31
 import java.time.format.DateTimeFormatter;
32
+import java.util.Map;
29 33
 
30 34
 @Slf4j
31 35
 @Service
@@ -49,24 +53,14 @@ public class TaPayServiceImpl extends BaseServiceImpl<TaPayMapper, TaPay> implem
49 53
     @Autowired
50 54
     TaAccountMapper taAccountMapper;
51 55
 
56
+    @Autowired
57
+    AlipayService alipayService;
58
+
52 59
     @Override
53
-    public WxPayUnifiedOrderV3Result createPay(String client, String clientId, TaOrder taOrder) throws Exception {
60
+    public WxPayUnifiedOrderV3Result createWxPay(String client, String clientId, TaOrder taOrder) throws Exception {
54 61
         TaPay taPay = getByOrderNo(taOrder.getOrderNo());
55 62
         if (null == taPay) {
56
-            taPay = new TaPay();
57
-            taPay.setOrderId(taOrder.getOrderId());
58
-            taPay.setOrderNo(taOrder.getOrderNo());
59
-            taPay.setPersonId(taOrder.getPersonId());
60
-            taPay.setCharges(taOrder.getCharges());
61
-
62
-            if (taOrder.getTypeName().length() > 127) {
63
-                taPay.setDescription(taOrder.getTypeName().substring(0, 127));
64
-            } else {
65
-                taPay.setDescription(taOrder.getTypeName());
66
-            }
67
-
68
-            taPay.setPayType(taOrder.getPayType());
69
-            taPay.setStatus(Constants.PAY_READY);
63
+            taPay = initPayByOrder(taOrder);
70 64
         }
71 65
 
72 66
         try {
@@ -83,20 +77,7 @@ public class TaPayServiceImpl extends BaseServiceImpl<TaPayMapper, TaPay> implem
83 77
 
84 78
             boolean isPaid = WxPayErrorCode.UnifiedOrder.ORDERPAID.equals(e.getErrCode());
85 79
             if (isPaid) {
86
-                // 如果订单已支付
87
-                taPay.setStatus(Constants.PAY_PAID);
88
-                taPay.setPayDate(LocalDateTime.now());
89
-                if (StringUtils.isEmpty(taPay.getPayId())) {
90
-                    taPay.setPayId(StringUtils.uuid());
91
-                    if (!save(taPay)) {
92
-                        throw new Exception("系统出错请重试");
93
-                    }
94
-                } else {
95
-                    updateById(taPay);
96
-                }
97
-                taOrderMapper.updatePayStatus(taPay.getOrderId(), Constants.PAY_PAID);
98
-                // 插入账户流水
99
-                insertAccount(taPay, taOrder.getOrgId(), Constants.ACCOUNT_INCOME, Constants.ACCOUNT_ORDER_PAY);
80
+                updateOrderPay(taPay, taOrder, DateUtils.now(), true, null);
100 81
             }
101 82
 
102 83
             throw e;
@@ -139,26 +120,30 @@ public class TaPayServiceImpl extends BaseServiceImpl<TaPayMapper, TaPay> implem
139 120
 
140 121
     @Override
141 122
     public void notifyOrder(WxPayOrderNotifyV3Result.DecryptNotifyResult result) throws Exception {
142
-        TaPay orderPay = getByOrderNo(result.getOutTradeNo());
143
-        if (orderPay.getStatus() != Constants.PAY_READY) {
144
-            return;
145
-        }
146
-
123
+        String orderNo = result.getOutTradeNo();
147 124
         boolean isSuccess = WxPayConstants.ResultCode.SUCCESS.equals(result.getTradeState());
125
+        LocalDateTime payTime = DateUtils.from(result.getSuccessTime(), DateTimeFormatter.ISO_DATE_TIME);
126
+        String message = result.getTradeStateDesc();
127
+        notifyOrder(orderNo, isSuccess, payTime, message);
128
+    }
148 129
 
149
-        orderPay.setPayDate(DateUtils.from(result.getSuccessTime(), DateTimeFormatter.ISO_DATE_TIME));
150
-        orderPay.setStatus(isSuccess ? Constants.PAY_PAID : Constants.PAY_FAIL);
151
-        orderPay.setMessage(isSuccess ? null : result.getTradeStateDesc());
152
-        baseMapper.updateById(orderPay);
153
-
154
-        // 更新订单状态
155
-        if (isSuccess) {
156
-            TaOrder taOrder = taOrderMapper.selectById(orderPay.getOrderId());
157
-            taOrderMapper.updatePayStatus(orderPay.getOrderId(), Constants.PAY_PAID);
130
+    @Override
131
+    public void notifyOrder(Map<String, String> params) throws Exception {
132
+        String orderNo = params.get("out_trade_no");
133
+        LocalDateTime payTime = DateUtils.from(params.get("gmt_payment"), "yyyy-MM- dd HH:mm:ss");
134
+        boolean isSuccess = "TRADE_SUCCESS".equals(params.get("trade_status"));
135
+        String message = params.get("trade_status");
136
+        notifyOrder(orderNo, isSuccess, payTime, message);
137
+    }
158 138
 
159
-            // 插入账户流水
160
-            insertAccount(orderPay, taOrder.getOrgId(), Constants.ACCOUNT_INCOME, Constants.ACCOUNT_ORDER_PAY);
139
+    private void notifyOrder(String orderNo, boolean isSuccess, LocalDateTime payTime, String message) throws Exception {
140
+        TaPay orderPay = getByOrderNo(orderNo);
141
+        if (orderPay.getStatus() != Constants.PAY_READY) {
142
+            return;
161 143
         }
144
+        TaOrder taOrder = taOrderMapper.selectById(orderPay.getOrderId());
145
+
146
+        updateOrderPay(orderPay, taOrder, payTime, isSuccess, message);
162 147
     }
163 148
 
164 149
     @Override
@@ -186,16 +171,33 @@ public class TaPayServiceImpl extends BaseServiceImpl<TaPayMapper, TaPay> implem
186 171
             taPay.setStatus(Constants.PAY_READY);
187 172
         }
188 173
 
189
-        WxPayRefundV3Request request = new WxPayRefundV3Request();
190
-        request.setOutTradeNo(origin.getOrderNo()); // 原单号
191
-        request.setOutRefundNo(taPay.getOrderNo()); // 退款单号
192
-        WxPayRefundV3Request.Amount amount = new  WxPayRefundV3Request.Amount();
193
-        amount.setTotal(taPay.getCharges());
194
-        amount.setRefund(taPay.getCharges());
195
-        amount.setCurrency("CNY");
196
-        request.setAmount(amount);
174
+        // 是否微信支付
175
+        boolean isWxpay = "wx".equals(taOrder.getPayType());
176
+        if (isWxpay) {
177
+            WxPayRefundV3Request request = new WxPayRefundV3Request();
178
+            request.setOutTradeNo(origin.getOrderNo()); // 原单号
179
+            request.setOutRefundNo(taPay.getOrderNo()); // 退款单号
180
+            WxPayRefundV3Request.Amount amount = new  WxPayRefundV3Request.Amount();
181
+            amount.setTotal(taPay.getCharges());
182
+            amount.setRefund(taPay.getCharges());
183
+            amount.setCurrency("CNY");
184
+            request.setAmount(amount);
185
+
186
+            wxPayUtils.refund(request, client);
187
+        } else {
188
+            AlipayTradeRefundModel model = new AlipayTradeRefundModel();
189
+            model.setOutTradeNo(origin.getOrderNo());
190
+            model.setOutRequestNo(taPay.getOrderNo());
191
+            model.setRefundAmount(MathUtils.formatFloat(taPay.getCharges() / 100f, "0.00"));
192
+            AlipayTradeRefundResponse refundResponse = alipayService.refund(model, client);
193
+            if (null == refundResponse) {
194
+                throw new Exception("初始化支付宝客户端错误");
195
+            }
196
+            if (!refundResponse.isSuccess()) {
197
+                throw new Exception(refundResponse.getSubMsg());
198
+            }
199
+        }
197 200
 
198
-        wxPayUtils.refund(request, client);
199 201
         if (StringUtils.isEmpty(taPay.getPayId())) {
200 202
             // 退款申请中 ...
201 203
             taOrder.setPayStatus(Constants.PAY_REFUNDING);
@@ -210,10 +212,28 @@ public class TaPayServiceImpl extends BaseServiceImpl<TaPayMapper, TaPay> implem
210 212
 
211 213
     @Override
212 214
     public void refundNotifyOrder(WxPayRefundNotifyV3Result.DecryptNotifyResult result) throws Exception {
213
-        TaPay taPay = getByOrderNo(result.getOutRefundNo());
215
+        String refundNo = result.getOutRefundNo();
216
+        LocalDateTime refundTime = DateUtils.from(result.getSuccessTime(), DateTimeFormatter.ISO_DATE_TIME);
217
+        boolean isSuccess = WxPayConstants.RefundStatus.SUCCESS.equals(result.getRefundStatus());
218
+        String message = result.getRefundStatus();
219
+        refundNotifyOrder(refundNo, isSuccess, refundTime, message);
220
+    }
221
+
222
+
223
+    @Override
224
+    public void refundNotifyOrder(Map<String, String> params) throws Exception {
225
+        String orderNo = params.get("out_trade_no");
226
+        String refundNo = "R-"+ orderNo;
227
+        LocalDateTime refundTime = DateUtils.from(params.get("gmt_refund"), "yyyy-MM- dd HH:mm:ss");
228
+        boolean isSuccess = "TRADE_SUCCESS".equals(params.get("trade_status"));
229
+        String message = params.get("trade_status");
230
+        refundNotifyOrder(refundNo, isSuccess, refundTime, message);
231
+    }
232
+
233
+    private void refundNotifyOrder(String refundNo, boolean isSuccess, LocalDateTime refundTime, String message) throws Exception {
234
+        TaPay taPay = getByOrderNo(refundNo);
214 235
         if (null == taPay) {
215
-            log.error("退款通知处理失败: 单号 " + result.getOutRefundNo());
216
-            log.error(result.toString());
236
+            log.error("退款通知处理失败: 单号 " + refundNo);
217 237
             return;
218 238
         }
219 239
 
@@ -221,12 +241,18 @@ public class TaPayServiceImpl extends BaseServiceImpl<TaPayMapper, TaPay> implem
221 241
             return;
222 242
         }
223 243
 
224
-        boolean isSuccess = WxPayConstants.RefundStatus.SUCCESS.equals(result.getRefundStatus());
225
-
226
-        taPay.setPayDate(DateUtils.from(result.getSuccessTime(), DateTimeFormatter.ISO_DATE_TIME));
244
+        taPay.setPayDate(refundTime);
227 245
         taPay.setStatus(isSuccess ? Constants.PAY_PAID : Constants.PAY_FAIL);
228
-        taPay.setMessage(isSuccess ? null : result.getRefundStatus());
229
-        baseMapper.updateById(taPay);
246
+        taPay.setMessage(message);
247
+
248
+        if (StringUtils.isEmpty(taPay.getPayId())) {
249
+            taPay.setPayId(StringUtils.uuid());
250
+            if (!save(taPay)) {
251
+                throw new Exception("系统出错请重试");
252
+            }
253
+        } else {
254
+            updateById(taPay);
255
+        }
230 256
 
231 257
         // 更新订单状态
232 258
         if (isSuccess) {
@@ -241,6 +267,76 @@ public class TaPayServiceImpl extends BaseServiceImpl<TaPayMapper, TaPay> implem
241 267
         }
242 268
     }
243 269
 
270
+    @Override
271
+    public AlipayTradeAppPayResponse createAliPay(String client, String clientId, TaOrder taOrder) throws Exception {
272
+        TaPay taPay = getByOrderNo(taOrder.getOrderNo());
273
+        if (null == taPay) {
274
+            taPay = initPayByOrder(taOrder);
275
+        }
276
+
277
+        try {
278
+            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
279
+            model.setSubject (taPay.getDescription());
280
+            model.setOutTradeNo (taPay.getOrderNo());
281
+            model.setTotalAmount (MathUtils.formatFloat(taPay.getCharges() / 100f, "0.00"));
282
+            AlipayTradeAppPayResponse response = alipayService.createOrder(model, client);
283
+            if (null == response) {
284
+                throw new Exception("初始化支付宝客户端错误");
285
+            }
286
+
287
+            if (!response.isSuccess()) {
288
+                if ("ACQ.TRADE_HAS_SUCCESS".equals(response.getSubCode())) {
289
+                    // 如果是已经支付成功的
290
+                    updateOrderPay(taPay, taOrder, DateUtils.now(), true, null);
291
+                    throw new Exception("当前订单已支付");
292
+                } else {
293
+                    throw new Exception(response.getSubMsg());
294
+                }
295
+            }
296
+
297
+            return response;
298
+        } catch (AlipayApiException e) {
299
+            e.printStackTrace();
300
+            throw e;
301
+        }
302
+    }
303
+
304
+    private void updateOrderPay(TaPay taPay, TaOrder taOrder, LocalDateTime payDate, boolean isSuccess, String errorMessage) throws Exception {
305
+        taPay.setPayDate(payDate);
306
+        taPay.setStatus(isSuccess ? Constants.PAY_PAID : Constants.PAY_FAIL);
307
+        taPay.setMessage(errorMessage);
308
+        baseMapper.updateById(taPay);
309
+
310
+        // 更新订单状态
311
+        if (isSuccess) {
312
+            taOrderMapper.updatePayStatus(taPay.getOrderId(), Constants.PAY_PAID);
313
+
314
+            // 插入账户流水
315
+            insertAccount(taPay, taOrder.getOrgId(), Constants.ACCOUNT_INCOME, Constants.ACCOUNT_ORDER_PAY);
316
+        } else {
317
+            taOrderMapper.updatePayStatus(taPay.getOrderId(), Constants.PAY_FAIL);
318
+        }
319
+    }
320
+
321
+    private TaPay initPayByOrder(TaOrder taOrder) {
322
+        TaPay taPay = new TaPay();
323
+        taPay.setOrderId(taOrder.getOrderId());
324
+        taPay.setOrderNo(taOrder.getOrderNo());
325
+        taPay.setPersonId(taOrder.getPersonId());
326
+        taPay.setCharges(taOrder.getCharges());
327
+
328
+        if (taOrder.getTypeName().length() > 127) {
329
+            taPay.setDescription(taOrder.getTypeName().substring(0, 127));
330
+        } else {
331
+            taPay.setDescription(taOrder.getTypeName());
332
+        }
333
+
334
+        taPay.setPayType(taOrder.getPayType());
335
+        taPay.setStatus(Constants.PAY_READY);
336
+
337
+        return taPay;
338
+    }
339
+
244 340
     private void insertAccount(TaPay taPay, String orgId, Integer feeType, String chargeType) throws Exception {
245 341
         TaAccount taAccount = iTaAccountService.getUserAccount(orgId);
246 342
         String accountId = taAccount.getAccountId();

+ 10
- 0
src/main/resources/application-dev.yml View File

@@ -15,3 +15,13 @@ logging:
15 15
     root: info
16 16
     springfox: info
17 17
 
18
+aliPay:
19
+  appid: 2019053065353839
20
+  private-key: E:/work/nanyang/sevice/src/main/resources/cert/rsa-private.txt
21
+  cert-path: E:/work/nanyang/sevice/src/main/resources/cert/appCertPublicKey_2019053065353839.crt
22
+  public-cert-path: E:/work/nanyang/sevice/src/main/resources/cert/alipayCertPublicKey_RSA2.crt
23
+  root-cert-path: E:/work/nanyang/sevice/src/main/resources/cert/alipayRootCert.crt
24
+  api-url: https://openapi.alipaydev.com/gateway.do
25
+  notify-url: http://127.0.0.1:8080/api/callback/alipay/pay
26
+  auth-url: https://openauth.alipaydev.com/oauth2/appToAppAuth.htm
27
+  auth-callback: https://app.xnsport.cn/callback

+ 13
- 3
src/main/resources/application-prod.yml View File

@@ -5,7 +5,17 @@ spring:
5 5
       max-file-size: 10MB
6 6
       max-request-size: 50MB
7 7
   datasource:
8
-    url: jdbc:mysql://rm-uf6z3z6jq11x653d77o.mysql.rds.aliyuncs.com:3306/dianyang_colmo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
9
-    username: dianyang_colmo
10
-    password: dianyang_colmo@ABC123
8
+    url: jdbc:mysql://110.40.183.156:3306/nanyang_machinery?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
9
+    username: nanyang_machinery
10
+    password: nanyang_machinery@ABCD1234
11 11
 
12
+aliPay:
13
+  appid: 2021001190647352
14
+  private-key: /opt/niucai/cert/rsa-private.txt
15
+  cert-path: /opt/niucai/cert/appCertPublicKey_2021001196673227.crt
16
+  public-cert-path: /opt/niucai/cert/alipayCertPublicKey_RSA2.crt
17
+  root-cert-path: /opt/niucai/cert/alipayRootCert.crt
18
+  api-url: https://openapi.alipay.com/gateway.do
19
+  notify-url: https://app.xnsport.cn/api/callback/alipay/pay
20
+  auth-url: https://openauth.alipay.com/oauth2/appToAppAuth.htm
21
+  auth-callback: https://app.xnsport.cn/callback

+ 6
- 0
src/main/resources/application.yml View File

@@ -69,6 +69,12 @@ aliyun:
69 69
     endpoint: oss-accelerate.aliyuncs.com
70 70
     bucketName: yz-shigongli
71 71
     bucketURL: https://yz-shigongli.oss-accelerate.aliyuncs.com
72
+###
73
+alipay:
74
+  publicKey:
75
+  format: json
76
+  charset: UTF-8
77
+  sign-type: RSA2
72 78
 
73 79
 
74 80
 ###

+ 5
- 0
src/main/resources/mapper/SysAliPayConfigMapper.xml View File

@@ -0,0 +1,5 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
+<mapper namespace="com.yunzhi.nanyang.mapper.SysAliPayConfigMapper">
4
+
5
+</mapper>

+ 5
- 0
src/main/resources/mapper/SysWxPayConfigMapper.xml View File

@@ -0,0 +1,5 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
+<mapper namespace="com.yunzhi.nanyang.mapper.SysWxPayConfigMapper">
4
+
5
+</mapper>