Java 实现微信和支付宝支付功能

一、前期准备
1、申请好微信商户号appid,拿到商户id和商户秘钥,退款的话需要商户证书
2、申请好支付宝商户号appid,商户公钥和秘钥(需要用支付宝工具自己生成),支付宝退款不需要证书

二、数据库表设计
1、微信支付配置表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE `py_wx_config` (
 `wx_config_id` varchar(18) NOT NULL COMMENT '微信支付配置ID',
 `appid` varchar(128) DEFAULT NULL COMMENT '微信公众号ID',
 `mch_id` varchar(128) DEFAULT NULL COMMENT '商户号ID',
 `mch_key_secret` varchar(2200) DEFAULT NULL COMMENT '商户安全密钥',
 `crt_path` varchar(200) DEFAULT NULL COMMENT '商户证书存放路径',
 `app_secret` varchar(2200) DEFAULT NULL COMMENT '公众号安全密钥',
 `create_id` varchar(18) DEFAULT NULL COMMENT '创建人',
 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
 `upd_id` varchar(18) DEFAULT NULL COMMENT '修改人',
 `upd_time` datetime DEFAULT NULL COMMENT '修改时间',
 `delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志',
 `app_code` varchar(18) NOT NULL COMMENT '商家编码',
 `bm_code` varchar(20) DEFAULT NULL COMMENT '部门编码',
 PRIMARY KEY (`wx_config_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='微信支付配置';
2、微信小程序支付配置表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE `py_wx_min_config` (
 `wx_min_config_id` varchar(18) NOT NULL COMMENT '微信支付配置ID',
 `appid` varchar(128) DEFAULT NULL COMMENT '微信公众号ID',
 `mch_id` varchar(128) DEFAULT NULL COMMENT '商户号ID',
 `mch_key_secret` varchar(2200) DEFAULT NULL COMMENT '商户安全密钥',
 `crt_path` varchar(200) DEFAULT NULL COMMENT '商户证书存放路径',
 `app_secret` varchar(2200) DEFAULT NULL COMMENT '公众号安全密钥',
 `create_id` varchar(18) DEFAULT NULL COMMENT '创建人',
 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
 `upd_id` varchar(18) DEFAULT NULL COMMENT '修改人',
 `upd_time` datetime DEFAULT NULL COMMENT '修改时间',
 `delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志',
 `app_code` varchar(18) NOT NULL COMMENT '商家编码',
 `sys_software_code` varchar(18) DEFAULT NULL COMMENT '系统编码',
 PRIMARY KEY (`wx_min_config_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='微信支付配置';
3、支付宝支付配置表

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE `py_alipay_config` (
 `alipay_config_id` varchar(18) NOT NULL COMMENT '支付宝支付配置ID',
 `ali_private_key` varchar(2200) DEFAULT NULL COMMENT '支付宝密钥',
 `ali_public_key` varchar(2200) DEFAULT NULL COMMENT '支付宝公钥',
 `ali_appid` varchar(128) DEFAULT NULL COMMENT '支付宝appid',
 `create_id` varchar(18) DEFAULT NULL COMMENT '创建人',
 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
 `upd_id` varchar(18) DEFAULT NULL COMMENT '修改人',
 `upd_time` datetime DEFAULT NULL COMMENT '修改时间',
 `delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志',
 `app_code` varchar(18) NOT NULL COMMENT '商家编码',
 PRIMARY KEY (`alipay_config_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='支付宝支付配置';
4、支付流水记录表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CREATE TABLE `py_pay_log` (
 `pay_log_id` varchar(18) NOT NULL COMMENT '支付流水ID',
 `pay_no` varchar(20) DEFAULT NULL COMMENT '支付订单号',
 `third_id` varchar(64) DEFAULT NULL COMMENT '微信支付宝订单ID',
 `money_type` varchar(20) DEFAULT NULL COMMENT '支付类型 0-微信 1-支付宝 2-会员卡 3-银联',
 `mq_topic` varchar(100) DEFAULT NULL COMMENT 'MQ主题',
 `mq_status` tinyint(3) DEFAULT NULL COMMENT 'MQ发送状态 0-待发送 1-已发送 2-发送失败',
 `mq_remark` varchar(100) DEFAULT NULL COMMENT 'MQ发送备注',
 `request` text COMMENT '发送数据',
 `response` text COMMENT '返回数据',
 `fee` decimal(14,2) DEFAULT NULL COMMENT '订单金额',
 `create_id` varchar(18) DEFAULT NULL COMMENT '创建人',
 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
 `upd_id` varchar(18) DEFAULT NULL COMMENT '修改人',
 `upd_time` datetime DEFAULT NULL COMMENT '修改时间',
 `delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志',
 `app_code` varchar(18) NOT NULL COMMENT '商家编码',
 `pay_config_json_str` longtext COMMENT '支付配置',
 PRIMARY KEY (`pay_log_id`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='支付流水';
5、支付失败记录表

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE `pay_fail_log` (
 `pay_id` varchar(18) NOT NULL,
 `json_date` varchar(500) DEFAULT NULL,
 `create_id` varchar(18) DEFAULT NULL,
 `create_time` datetime DEFAULT NULL,
 `update_id` varchar(18) DEFAULT NULL,
 `update_time` datetime DEFAULT NULL,
 `app_code` varchar(18) NOT NULL,
 `delete_flag` tinyint(1) DEFAULT '0' COMMENT '是否删除:0-否;1-是',
 PRIMARY KEY (`pay_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;
三、依赖引入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?xml version="1.0" encoding="UTF-8"?>
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.3.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.pay</groupId>
 <artifactId>qc-payment</artifactId>
 <version>2.3</version>
 <name>qc-payment</name>
 <!-- FIXME change it to the project's website -->
 <url>http://www.example.com</url>
 
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  </dependency>
 
 
  <!--z支付宝-->
  <dependency>
   <groupId>com.alipay.sdk</groupId>
   <artifactId>alipay-sdk-java</artifactId>
   <version>3.3.4.ALL</version>
  </dependency>
 
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 
  <!--导入重试机制的坐标-->
  <dependency>
   <groupId>org.springframework.retry</groupId>
   <artifactId>spring-retry</artifactId>
  </dependency>
 
  <!--引入Hystrix依赖-->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>
 
  <!--导入jwt相关依赖-->
  <dependency>
   <groupId>com.auth0</groupId>
   <artifactId>java-jwt</artifactId>
   <version>3.4.1</version>
  </dependency>
 
  <!-- StringUtil 类-->
  <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.3.2</version>
  </dependency>
 
  <!-- ali 连接池 -->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.12</version>
  </dependency>
 
  <!-- mybatis -->
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>2.0.0</version>
  </dependency>
 
  <!-- mysql -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.persistence</groupId>
   <artifactId>persistence-api</artifactId>
   <version>1.0.2</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
  <dependency>
   <groupId>tk.mybatis</groupId>
   <artifactId>mapper</artifactId>
   <version>4.0.3</version>
  </dependency>
 
  <!-- import lombok 可以省去getter setter toString等方法 -->
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <scope>provided</scope>
  </dependency>
 
  <!-- commons-io -->
  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.4</version>
  </dependency>
 
  <!-- 引入easyqinyu-tools项目 -->
  <dependency>
   <groupId>com.pay</groupId>
   <artifactId>easyqinyu-tools</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
 
  <!-- pagehelper分页插件 -->
  <dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper</artifactId>
   <version>5.1.2</version>
  </dependency>
 
  <dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
   <version>1.2.3</version>
  </dependency>
  <dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-starter</artifactId>
   <version>1.2.3</version>
  </dependency>
  <dependency>
   <groupId>com.qcsoft</groupId>
   <artifactId>qc-swagger-ui</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.3.0</version>
  </dependency>
 
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>RELEASE</version>
   <scope>compile</scope>
  </dependency>
 
  <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>${httpclient.version}</version>
  </dependency>
 
 
  <dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.8.2</version>
  </dependency>
 
 
  <!--阿里云信息发送-->
  <dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <version>3.3.1</version>
  </dependency>
  <dependency>
   <groupId>com.github.1991wangliang</groupId>
   <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
   <version>1.0.0</version>
  </dependency>
 
  <dependency>
   <groupId>org.springframework.kafka</groupId>
   <artifactId>spring-kafka</artifactId>
  </dependency>
 
  <!-- xml解析-->
  <dependency>
   <groupId>com.thoughtworks.xstream</groupId>
   <artifactId>xstream</artifactId>
   <version>1.4.10</version>
  </dependency>
 
  <!-- dom4j -->
  <dependency>
   <groupId>org.dom4j</groupId>
   <artifactId>dom4j</artifactId>
   <version>2.1.1</version>
  </dependency>
 
   <dependency>
   <groupId>com.github.binarywang</groupId>
   <artifactId>weixin-java-common</artifactId>
   <version>3.3.4.B</version>
  </dependency>
 
 
  <dependency>
   <groupId>org.jodd</groupId>
   <artifactId>jodd-http</artifactId>
   <scope>compile</scope>
  </dependency>
 
  <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
  </dependency>
 
 
  <!-- xml解析-->
  <dependency>
   <groupId>commons-beanutils</groupId>
   <artifactId>commons-beanutils</artifactId>
   <version>1.9.3</version>
  </dependency>
 
  <dependency>
   <groupId>jdom</groupId>
   <artifactId>jdom</artifactId>
   <version>1.0</version>
  </dependency>
 
 
  <dependency>
   <groupId>org.bouncycastle</groupId>
   <artifactId>bcpkix-jdk15on</artifactId>
   <version>1.59</version>
  </dependency>
  <dependency>
   <groupId>org.jodd</groupId>
   <artifactId>jodd-http</artifactId>
   <version>3.7.1</version>
  </dependency>
  <!--二维码工具-->
  <dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.2.1</version>
  </dependency>
 
  <dependency>
   <groupId>com.qcsoft</groupId>
   <artifactId>qc-commonbean</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
 
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <!-- xxl-job-core -->
  <dependency>
   <groupId>com.xuxueli</groupId>
   <artifactId>xxl-job-core</artifactId>
   <version>2.0.1</version>
  </dependency>
  <!--线路跟踪-->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  </dependency>
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
 </dependencies>
 
 
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 <build>
  <finalName>qc-payment</finalName>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>8</source>
     <target>8</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>
四、程序实现
1、Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.qcsoft.payment.controller;
 
import com.alibaba.fastjson.JSON;
import com.qcsoft.commonbean.bean.common.SwaggerConstant;
import com.qcsoft.commonbean.bean.payment.common.PrePayResult;
import com.qcsoft.commonbean.bean.payment.wx.bean.PreOrderParam;
import com.qcsoft.commonbean.bean.payment.wx.exception.WxPayException;
import com.qcsoft.commonbean.bean.payment.wx.util.QrcodeUtils;
import com.qcsoft.easyqinyutools.message.ReturnMsg;
import com.qcsoft.payment.controller.commom.BaseController;
import com.qcsoft.payment.service.PayService;
import com.qcsoft.swaggerui.anno.ApiTag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
 
 
/**
 * @Description: 支付控制中心
 * @Author: 
 * @Date: 
 */
@RestController
@RequestMapping("/v2")
public class PayController extends BaseController {
 private static final Logger logger = LoggerFactory.getLogger(PayController.class);
 
 @Autowired
 private PayService payService;
 
 //private final AtomicInteger tick = new AtomicInteger();
 
 /**
  * 统一预支付接口(统一下单)
  *
  * @param orderParam {dh:订单号,payType:支付方式(WXPAY、ALIPAY、MEMBER_CARD、ACCOUNT),appCode:商家编码,
  *     tradeType:支付方式类型(NATIVE、JSAPI、APP、MWEB),openId:支付方式为JSAPI必传,reqId:请求的真实IP地址
  *     ,redirectUrl:支付成功返回地址,appCode:商家编码,outTradeNo:商务订单号,timeExpire,订单到期时间
  *     ,totalAmount:订单金额,body:商品简单描述,subject:订单标题,quitUrl:中途退出}
  * @param token  token
  * @return (微信公众号 / 小程序返回一串json字符串 : { appId :, timeStamp :, nonceStr : , info_package :, paySign :, signType : }, 前端使用微信sdk调用微信支付)
  * (微信扫码支付返回一个连接,前端需要把连接生成一个二维码使用,连接如:weixin://123.123.com/***)
  * (微信H5--返回一个连接,前端需要把连接生成一个连接前端直接跳转该连接使用,使用如:location.href='weixin://123.123.com/***')
  * (支付宝扫码支付--返回一个连接,前端直接跳转到该连接使用,使用如:location.href='weixin://123.123.com/***')
  * (支付宝扫码H5--返回一个页面form表单,前端需要把这个form表单渲染到h5页面上,使用如:$("#view).html(alipayForm)')
  * (支付宝app支付--返回一个参数包,如sdk-version=**&service=**&&service=**&&body=**&&out_trade_no=**&,前端使用支付宝sdk调用app支付')
  * {
  * wxNativeUrl:微信扫码地址
  * wxAppPackage:app支付sdk参数包
  * wxJsapiPackage:公众号支付包
  * wxH5Url:h5支付url
  * wxScanUrl:wxScanUrl
  * alipayScanUrl:支付宝扫码支付url
  * alipayH5Form:支付宝h5支付表单
  * alipayAppParamStr:支付宝app支付参数支付串
  * }
  * @throws WxPayException
  */
 @RequestMapping(value = "/prePayOrder")
 @ApiTag(SwaggerConstant.PAYMENT_001)
 public ReturnMsg prePayOrder(@RequestBody PreOrderParam orderParam) {
  String metadata = JSON.toJSONString(orderParam);
  logger.info("PayController.prePayOrder----\n正在请求支付....,参数为>>>>{}", metadata);
  ReturnMsg ret = new ReturnMsg();
  //预下单
  PrePayResult prePayResult = this.payService.payPreOrder(orderParam);
  ret.setData(prePayResult);
  return ret.success("下单成功,订单有效期为" + orderParam.getTimeExpire());
 }
 
 /**
  * 生成支付二维码
  *
  * @param url 二维码地址
  * @throws IOException
  */
 @RequestMapping(value = "/createQrCode/url")
 @ApiTag(SwaggerConstant.PAYMENT_001)
 public void createQrCode(@PathVariable String url, HttpServletRequest request, HttpServletResponse response) throws IOException {
  String queryString = request.getQueryString();
  BufferedImage bufferedImage = QrcodeUtils.encode(url+queryString, 500, 500, null, null);
  BufferedImage grayImage = QrcodeUtils.grayImage(bufferedImage);
  ImageIO.write(grayImage, "png", response.getOutputStream());
 }
 
 
}
2、PayService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.qcsoft.payment.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.qcsoft.commonbean.bean.common.KafkaConstant;
import com.qcsoft.commonbean.bean.payment.ChinaumsConfig;
import com.qcsoft.commonbean.bean.payment.PayLog;
import com.qcsoft.commonbean.bean.payment.common.PayConstant;
import com.qcsoft.commonbean.bean.payment.common.PrePayResult;
import com.qcsoft.commonbean.bean.payment.common.WxPrePayResultPackage;
import com.qcsoft.commonbean.bean.payment.wx.bean.PreOrderParam;
import com.qcsoft.commonbean.bean.payment.wx.bean.request.WxPayUnifiedOrderRequest;
import com.qcsoft.commonbean.bean.payment.wx.bean.result.WxPayUnifiedOrderResult;
import com.qcsoft.commonbean.bean.payment.wx.config.WxPayConfig;
import com.qcsoft.easyqinyutools.utils.JsonUtil;
import com.qcsoft.payment.commns.exception.BaseException;
import com.qcsoft.payment.commns.exception.QYError;
import com.qcsoft.payment.commns.utils.UserUtil;
import com.qcsoft.payment.commns.utils.weixin.PackageUtil;
import com.qcsoft.payment.service.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.SortedMap;
import java.util.TreeMap;
 
@Service
public class PayServiceImpl implements PayService {
 public Logger logger = LoggerFactory.getLogger(getClass());
 @Autowired
 private AliPayService aliPayService;
 @Autowired
 private WxPayService wxPayService;
 @Autowired
 private ChinaumsPayService chinaumsPayService;
 
 @Autowired
 private UserUtil userUtil;
 @Autowired
 private PayLogService payLogService;
 
 
 @Override
 public PrePayResult payPreOrder(PreOrderParam orderParam) throws BaseException {
  logger.info("PayServiceImpl.payPreOrder---->>>>\n准备下单.....,当前用户订单号[{}]", orderParam.getDh());
  String temp = "";
  PrePayResult payResult = new PrePayResult();
  //校验支付方式
  String payType = orderParam.getPayType();
  if (!StringUtils.isNotBlank(payType))
   throw new BaseException(QYError.msg("请选择支付方式!"));
  if (PayConstant.WXPAY.toString().equals(payType)) {
   //查询商家配置信息
   WxPayConfig config;
   if (orderParam.getTradeType().equals(PayConstant.WECHATAPP.toString())) {
    config = wxPayService.getWxMinPayConfigByAppCode(orderParam.getAppCode(), orderParam.getSysSoftwareCode());
   } else {
    config = wxPayService.getWxPayConfigByAppCode(orderParam.getAppCode(), orderParam.getBmCode());
   }
   //设置预支付请求参数
   if (orderParam.getTradeType().equals(PayConstant.WECHATAPP.toString())) {
    orderParam.setTradeType(PayConstant.JSAPI.toString());
   }
   WxPayUnifiedOrderRequest requestParam = wxPayService.packRequestParam(orderParam);
   //开始请求预支付接口
   WxPayUnifiedOrderResult result = wxPayService.wxPrePay(config, requestParam);
   //根据支付类型设置值
   WxPrePayResultPackage resultPackage = null;
   if (orderParam.getTradeType().equals(PayConstant.APP.toString()) || orderParam.getTradeType().equals(PayConstant.JSAPI.toString()) || orderParam.getTradeType().equals(PayConstant.WECHATAPP.toString())) {
    resultPackage = new WxPrePayResultPackage();
    String timeMillis = PackageUtil.getTimeStamp();
    String nonceString = PackageUtil.getNonceStr();
    resultPackage.setAppId(result.getAppid());
    resultPackage.setNonceStr(nonceString);
    resultPackage.setTimeStamp(timeMillis);
    resultPackage.setPrePayId(result.getPrepayId());
    resultPackage.setInfoPackage("prepay_id=" + result.getPrepayId());
    resultPackage.setSignType(requestParam.getSignType());
    if (orderParam.getTradeType().equals(PayConstant.APP.toString())) {
     SortedMap<String, String> returnData = PackageUtil.getReturnData(resultPackage, config.getMchId());
     String paysign = PackageUtil.createSign(returnData, config.getMchKey());
     resultPackage.setPaySign(paysign);
     resultPackage.setInfoPackage(returnData.get("package"));
     payResult.setWxAppPackage(resultPackage);
    } else {
     SortedMap<String, String> paypackageParams = new TreeMap<String, String>();
     paypackageParams.put("appId", result.getAppid()); // appid
     paypackageParams.put("timeStamp", timeMillis); //
     paypackageParams.put("nonceStr", nonceString); //
     paypackageParams.put("package", resultPackage.getInfoPackage()); //
     paypackageParams.put("signType", resultPackage.getSignType());
     String paysign = PackageUtil.createSign(paypackageParams, config.getMchKey());
     resultPackage.setPaySign(paysign);
     payResult.setWxJsapiPackage(resultPackage);
    }
   } else if (orderParam.getTradeType().equals(PayConstant.MWEB.toString())) {
    payResult.setWxH5Url(result.getMwebUrl() + " &redirect_url=" + orderParam.getRedirectUrl());
   } else if (orderParam.getTradeType().equals(PayConstant.NATIVE.toString())) {
    payResult.setWxNativeUrl(result.getCodeURL());
   }
   //添加支付日志
   insertPayLog(orderParam, KafkaConstant.PAYMENT_NOTIFY_001.getTopic(), "0", resultPackage, JSON.toJSONString(config));
   return payResult;
  } else if (PayConstant.ALIPAY.toString().equals(payType)) {
   AlipayClient client = aliPayService.getAlipayClientByAppCode(orderParam.getAppCode());
   if (orderParam.getTradeType().equals(PayConstant.APP.toString())) {
    temp = aliPayService.aliAppPay(client, orderParam);
    payResult.setAlipayAppParamStr(temp);
   } else if (orderParam.getTradeType().equals(PayConstant.MWEB.toString())) {
    temp = aliPayService.aliH5Pay(client, orderParam);
    payResult.setAlipayH5Form(temp);
   } else if (orderParam.getTradeType().equals(PayConstant.NATIVE.toString())) {
    temp = aliPayService.aliScanPay(client, orderParam);
    payResult.setAlipayScanUrl(temp);
   }
   //添加支付日志
   insertPayLog(orderParam, KafkaConstant.PAYMENT_NOTIFY_001.getTopic(), "1", orderParam, JSON.toJSONString(aliPayService.getConfigByAppCode(orderParam.getAppCode())));
   return payResult;
  } else if (PayConstant.WECHATAPP.toString().equals(payType)) {
 
  } else if (PayConstant.CHINAUMS.toString().equals(payType)) { //银联pos支付
   //查询商家配置信息
   ChinaumsConfig config;
   config = chinaumsPayService.getPayConfigByAppCode(orderParam.getAppCode(), orderParam.getBmCode());
   if (orderParam.getTradeType().equals(PayConstant.CODE_SCAN.toString())) {
    chinaumsPayService.codeScanPay(config, orderParam);
   }
   //添加支付日志
   insertPayLog(orderParam, KafkaConstant.PAYMENT_NOTIFY_001.getTopic(), "3", orderParam, JSON.toJSONString(config));
   return payResult;
  }
  logger.warn("用户未选择正确的支付方式");
  throw new BaseException(QYError.msg("请选择正确的支付方式!"));
 
 }
 
 public void insertPayLog(PreOrderParam orderParam, String topic, String moneyType, Object obj, String payConfigJsonStr) {
  PayLog payLog = new PayLog();
  payLog.setAppCode(orderParam.getAppCode());
  payLog.setPayLogId(orderParam.getDh());
  payLog.setMoneyType(moneyType);
  payLog.setMqTopic(topic);
  payLog.setMqStatus(0);
  payLog.setPayNo(orderParam.getDh());
  payLog.setRequest(JsonUtil.toJSon(obj));
  payLog.setFee(orderParam.getTotalAmount());
  payLog.setPayConfigJsonStr(payConfigJsonStr);
  payLogService.saveOrUpdate(payLog);
 }
}

  • 0
    点赞
  • 1
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值