当前位置:   article > 正文

微信支付-前后端分离_vue+springboot 微信支付demo

vue+springboot 微信支付demo

目录

1.微信支付的流程

2.微信的接口文档

3.建一个spring boot工程

 4.导入该工程需要的依赖

5.配置application文件

6.创建一个Httpclient的工具类-默认浏览器进行远程调用

7.generator自动生成器--帮我们自动生成类,接口等

8. 前端代码

8.1.新建vue项目

8.1.1.打开cmd命令窗口,输入命令打开窗口

8.1.2. 新建工程

8.2.引入axios和设置axios基础路径


哥一定看到最后有详细的干货!!!

1.微信支付的流程

2.微信的接口文档

https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1

3.建一个spring boot工程

 

 4.导入该工程需要的依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.psh</groupId>
    <artifactId>weixin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>weixin</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!--微信需要的依赖-->
        <dependency>
            <groupId>com.github.wxpay</groupId>
            <artifactId>wxpay-sdk</artifactId>
            <version>0.0.3</version>
        </dependency>
        <!-- java端发送请求:在java端模拟浏览器远程访问微信端口-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

5.配置application文件

server.port=9000
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/wenxin?serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=pAssW0rd

logging.level.com.psh.weixinpay.dao=debug


//这个需要自己有营业执照才能申请的微信代码现在的这个是假的有需要的私信我给你发
weixin.appid=wx8087d814432d27c
weixin.mch_id=1532192432461144
weixin.api_key=wqesadasdasdasdasdass

6.创建一个Httpclient的工具类-默认浏览器进行远程调用

  1. public class HttpClient {
  2. private String url;
  3. private Map<String, String> param;
  4. private int statusCode;
  5. private String content;
  6. private String xmlParam;
  7. private boolean isHttps;
  8. public boolean isHttps() {
  9. return isHttps;
  10. }
  11. public void setHttps(boolean isHttps) {
  12. this.isHttps = isHttps;
  13. }
  14. public String getXmlParam() {
  15. return xmlParam;
  16. }
  17. public void setXmlParam(String xmlParam) {
  18. this.xmlParam = xmlParam;
  19. }
  20. public HttpClient(String url, Map<String, String> param) {
  21. this.url = url;
  22. this.param = param;
  23. }
  24. public HttpClient(String url) {
  25. this.url = url;
  26. }
  27. public void setParameter(Map<String, String> map) {
  28. param = map;
  29. }
  30. public void addParameter(String key, String value) {
  31. if (param == null)
  32. param = new HashMap<String, String>();
  33. param.put(key, value);
  34. }
  35. public void post() throws ClientProtocolException, IOException {
  36. HttpPost http = new HttpPost(url);
  37. setEntity(http);
  38. execute(http);
  39. }
  40. public void put() throws ClientProtocolException, IOException {
  41. HttpPut http = new HttpPut(url);
  42. setEntity(http);
  43. execute(http);
  44. }
  45. public void get() throws ClientProtocolException, IOException {
  46. if (param != null) {
  47. StringBuilder url = new StringBuilder(this.url);
  48. boolean isFirst = true;
  49. for (String key : param.keySet()) {
  50. if (isFirst)
  51. url.append("?");
  52. else
  53. url.append("&");
  54. url.append(key).append("=").append(param.get(key));
  55. }
  56. this.url = url.toString();
  57. }
  58. HttpGet http = new HttpGet(url);
  59. execute(http);
  60. }
  61. /**
  62. * set http post,put param
  63. */
  64. private void setEntity(HttpEntityEnclosingRequestBase http) {
  65. if (param != null) {
  66. List<NameValuePair> nvps = new LinkedList<NameValuePair>();
  67. for (String key : param.keySet())
  68. nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
  69. http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
  70. }
  71. if (xmlParam != null) {
  72. http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
  73. }
  74. }
  75. private void execute(HttpUriRequest http) throws ClientProtocolException,
  76. IOException {
  77. CloseableHttpClient httpClient = null;
  78. try {
  79. if (isHttps) {
  80. SSLContext sslContext = new SSLContextBuilder()
  81. .loadTrustMaterial(null, new TrustStrategy() {
  82. // 信任所有
  83. public boolean isTrusted(X509Certificate[] chain,
  84. String authType)
  85. throws CertificateException {
  86. return true;
  87. }
  88. }).build();
  89. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
  90. sslContext);
  91. httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
  92. .build();
  93. } else {
  94. httpClient = HttpClients.createDefault();
  95. }
  96. CloseableHttpResponse response = httpClient.execute(http);
  97. try {
  98. if (response != null) {
  99. if (response.getStatusLine() != null)
  100. statusCode = response.getStatusLine().getStatusCode();
  101. HttpEntity entity = response.getEntity();
  102. // 响应内容
  103. content = EntityUtils.toString(entity, Consts.UTF_8);
  104. }
  105. } finally {
  106. response.close();
  107. }
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. } finally {
  111. httpClient.close();
  112. }
  113. }
  114. public int getStatusCode() {
  115. return statusCode;
  116. }
  117. public String getContent() throws ParseException, IOException {
  118. return content;
  119. }
  120. }

7.generator自动生成器--帮我们自动生成类,接口等

  1. public class Generator {
  2. public static void main(String[] args) {
  3. FastAutoGenerator.create("jdbc:mysql://localhost:3306/wenxin?serverTimezone=Asia/Shanghai", "自己数据库的账号", "自己数据库的密码")
  4. .globalConfig(builder -> {
  5. builder.author("psh") // 设置作者
  6. .enableSwagger() // 开启 swagger 模式
  7. .fileOverride() // 覆盖已生成文件
  8. .outputDir(".\\src\\main\\java\\"); // 指定输出目录
  9. })
  10. .packageConfig(builder -> {
  11. builder.parent("com.psh") // 设置父包名
  12. .moduleName("system") // 设置父包模块名
  13. .pathInfo(Collections.singletonMap(OutputFile.xml, "src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
  14. })
  15. .strategyConfig(builder -> {
  16. builder.addInclude("t_pay_log")// 设置需要生成的表名
  17. .addTablePrefix("t_"); // 设置过滤表前缀
  18. })
  19. .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
  20. .execute();
  21. }
  22. }

8. 前端代码

8.1.新建vue项目

8.1.1.打开cmd命令窗口,输入命令打开窗口

vue ui

8.1.2. 新建工程

然后点回车才能保存自己的项目名字

 

点安装

 点完成安装

8.2.引入axios和设置axios基础路径

//引入axios
import axios from "axios";
Vue.config.productionTip = false
//设置axios基础路径
axios.defaults.baseURL="http://localhost:8888"

Vue.prototype.axios=axios;

 其余的前后端代码看这个远程仓库

java

 https://gitee.com/panshih/weixinpay.git

vue

https://gitee.com/panshih/weixinpayvue.git

哔哩哔哩视频有需要详细了解的看

java架构师的个人空间_哔哩哔哩_bilibili

数据库

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_order
-- ----------------------------
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE `t_order`  (
  `id` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  `order_no` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '订单号',
  `course_id` varchar(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '课程id',
  `course_title` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '课程名称',
  `course_cover` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '课程封面',
  `teacher_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '讲师名称',
  `member_id` varchar(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '会员id',
  `nickname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '会员昵称',
  `mobile` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '会员手机',
  `total_fee` decimal(10, 2) NULL DEFAULT 0.01 COMMENT '订单金额(分)',
  `pay_type` tinyint(0) NULL DEFAULT NULL COMMENT '支付类型(0:微信 1:支付宝)',
  `status` tinyint(0) NULL DEFAULT NULL COMMENT '订单状态(0:未支付 1:已支付)',
  `is_deleted` tinyint(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT '逻辑删除 1(true)已删除, 0(false)未删除',
  `gmt_create` datetime(0) NOT NULL COMMENT '创建时间',
  `gmt_modified` datetime(0) NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `ux_order_no`(`order_no`) USING BTREE,
  INDEX `idx_course_id`(`course_id`) USING BTREE,
  INDEX `idx_member_id`(`member_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '订单' ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_order
-- ----------------------------
INSERT INTO `t_order` VALUES ('0195f142a5824e0b88f', 'c60801fbd96355f8888', '1408424998648799234', 'Java初中级系统架构师组合套餐课(含12门课程)', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/44df362b-8d51-43a9-b823-090b02a3f2e8.jpg', '张飞', '1402269617551667201', '仲梦君', '15092182775', 32.00, 0, 0, 0, '2021-06-25 23:03:35', '2021-06-25 23:03:35');
INSERT INTO `t_order` VALUES ('21e04d165a324e59b96', '0322b37415384df0bbf', '1408421906918268930', '从零学习netty网络IO通讯开发视频教程', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/7b4a1bc6-b016-4afa-84c2-9ecfa0fae14d.jpg', '张飞', '1306495996639842305', '邱成相', '15092182541', 120.00, 1, 1, 0, '2020-06-25 23:02:27', '2020-06-25 23:02:27');
INSERT INTO `t_order` VALUES ('383714ba15e9474eb1a', 'e334ce2a6b1d4bc6938', '1408411851292532738', '姜维自传', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/8f731951-d606-4b86-8d4d-1cea38ee39f5.jpg', '姜维', '1408590478277685250', '李白', '15972331424', 0.01, 0, 0, 0, '2021-06-26 09:08:56', '2021-06-26 09:08:56');
INSERT INTO `t_order` VALUES ('39a3553e85bb4d69894', '9de332298489407fa81', '1408413427792994305', '成功的秘诀', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/03fa648e-a619-4500-a832-4750b3fa44ec.jpg', '张飞', '1402269617551667201', '仲梦君', '15092182775', 20.00, 0, 1, 0, '2021-06-25 23:09:34', '2021-06-25 23:09:50');
INSERT INTO `t_order` VALUES ('3fbd422bd60a4614a3b', '3d2e34a108174a67aba', '1408410177668767745', '水淹七军', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/74b7e90d-8463-4801-bdf8-52efd9cb6e41.jpg', '关羽', '1448200763102928897', NULL, '15660773278', 0.01, 0, 0, 0, '2021-10-13 16:17:05', '2021-10-13 16:17:05');
INSERT INTO `t_order` VALUES ('699aba8a2753439eb6a', '858c6dc73bb84c898a5', '1408409971229319170', '大白话领域驱动设计', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/5ae6a380-dbd6-4e74-8c99-a8c2ede79455.jpg', '张飞', '1408590478277685250', '李白', '15972331424', 0.01, 0, 1, 0, '2021-06-26 08:59:31', '2021-06-26 08:59:47');
INSERT INTO `t_order` VALUES ('7951387ce957484c80c', '2e4a952c118b4c47a79', '1408410177668767745', '水淹七军', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/74b7e90d-8463-4801-bdf8-52efd9cb6e41.jpg', '关羽', '1402268479037206530', '舒隆振', '15092182328', 0.01, 0, 1, 0, '2021-06-25 23:22:56', '2021-06-25 23:24:02');
INSERT INTO `t_order` VALUES ('9de56a01d7fb4a9895a', '7ff948d22cf04e61a06', '1408411851292532738', '姜维自传', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/8f731951-d606-4b86-8d4d-1cea38ee39f5.jpg', '姜维', '1448200763102928897', NULL, '15660773278', 0.01, 0, 1, 0, '2021-10-13 16:37:28', '2021-10-13 16:38:20');
INSERT INTO `t_order` VALUES ('b25d3987e7e94092b53', 'da27dfff1ae440c2aed', '1408411851292532738', '姜维自传', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/8f731951-d606-4b86-8d4d-1cea38ee39f5.jpg', '姜维', '1306495996639842305', '邱成相', '15092182541', 100.00, 0, 0, 0, '2019-06-25 21:54:13', '2019-06-25 21:54:13');
INSERT INTO `t_order` VALUES ('b9d4382ae2d84c568f9', '67164fb9916a4218a94', '1408410177668767745', '水淹七军', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/74b7e90d-8463-4801-bdf8-52efd9cb6e41.jpg', '关羽', '1406792661091491841', '景晨曦', '15007124873', 10.00, 0, 1, 0, '2021-06-25 23:09:51', '2021-06-25 23:10:13');
INSERT INTO `t_order` VALUES ('c1fddde4bdae4ad68a6', 'def097eeafe44d7b8dd', '1408425438857781250', '亿级电商微服务优惠劵系统全实现', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/08a1a97e-a059-4ed7-a026-131890a4700f.JPG', '张飞', '1402269617551667201', '仲梦君', '15092182775', 0.01, 0, 0, 0, '2021-06-25 23:11:04', '2021-06-25 23:11:04');
INSERT INTO `t_order` VALUES ('dbad9d7063ab47a9b8b', '821d7109fa6d4520982', '1408409971229319170', '大白话领域驱动设计', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/5ae6a380-dbd6-4e74-8c99-a8c2ede79455.jpg', '张飞', '1448200763102928897', NULL, '15660773278', 211.00, 1, 0, 0, '2021-10-13 16:16:42', '2021-10-13 16:16:42');
INSERT INTO `t_order` VALUES ('e5f80e6712894b8793c', '28d54a53bdf14c3c853', '1408409971229319170', '大白话领域驱动设计', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/5ae6a380-dbd6-4e74-8c99-a8c2ede79455.jpg', '张飞', '1402269617551667201', '仲梦君', '15092182775', 210.00, 0, 1, 0, '2018-06-25 21:54:13', '2018-06-25 21:54:13');
INSERT INTO `t_order` VALUES ('ea744617fe8d41128d7', '74c7a7bcf5f84c84a43', '1408411851292532738', '姜维自传', 'https://kk-books.oss-cn-hangzhou.aliyuncs.com/8f731951-d606-4b86-8d4d-1cea38ee39f5.jpg', '姜维', '1306147989314703362', '木白', '15700085997', 0.01, 0, 1, 0, '2021-10-27 17:54:32', '2021-10-27 17:54:49');

-- ----------------------------
-- Table structure for t_pay_log
-- ----------------------------
DROP TABLE IF EXISTS `t_pay_log`;
CREATE TABLE `t_pay_log`  (
  `id` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  `order_no` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '订单号',
  `pay_time` datetime(0) NULL DEFAULT NULL COMMENT '支付完成时间',
  `total_fee` decimal(10, 2) NULL DEFAULT 0.01 COMMENT '支付金额(分)',
  `transaction_id` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '交易流水号',
  `trade_state` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '交易状态',
  `pay_type` tinyint(0) NOT NULL DEFAULT 0 COMMENT '支付类型(0:微信 1:支付宝)',
  `attr` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '其他属性',
  `is_deleted` tinyint(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT '逻辑删除 1(true)已删除, 0(false)未删除',
  `gmt_create` datetime(0) NOT NULL COMMENT '创建时间',
  `gmt_modified` datetime(0) NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `uk_order_no`(`order_no`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '支付日志表' ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/308139
推荐阅读