SpringBoot整合支付宝沙盒

1、 统一下单并支付页面窗口

1、秘钥配置

  1. 访问支付宝开放平台,并登录: 支付宝开放平台 (alipay.com)

  2. 进入以后,点击沙箱

    在这里插入图片描述

  3. 选择自定义秘钥

    在这里插入图片描述

  4. 选择左上角更换公钥

    在这里插入图片描述

  5. 打开支付宝秘钥生成器

    在这里插入图片描述

  6. 下载工具

    在这里插入图片描述

  7. 打开工具,生成公钥

    在这里插入图片描述

  8. 复制应用公钥,粘贴回来

    在这里插入图片描述

  9. 这样秘钥就配置完毕了,可以点击左边的沙箱账号查看信息。

2、代码编写

  1. 在idea中创建springBoot项目,导入web、thymeleaf依赖

  2. 点击网页导航栏中的文档

    在这里插入图片描述

  3. 在文档导航栏中点击工具

    在这里插入图片描述

  4. 这里选择java

    在这里插入图片描述

  5. 这里点击Maven就行了

    在这里插入图片描述

  6. 这里点击最新版的就可以了。

    在这里插入图片描述

  7. 复制这个依赖,到pom.xml中导入

    在这里插入图片描述

  8. 回到文档,点击支付能力

    在这里插入图片描述

  9. 进入如下目录,并找到这块代码

    在这里插入图片描述

  10. 在idea中创建alipay类放入util包下,类名随意。

    在这里插入图片描述

  11. 创建pay方法,并把页面上的那块代码粘贴过来进行修改

    package com.tcc.com.tcc.util;
    
    import com.alibaba.fastjson.JSONObject;
    import com.alipay.api.AlipayApiException;
    import com.alipay.api.AlipayClient;
    import com.alipay.api.DefaultAlipayClient;
    import com.alipay.api.request.AlipayTradePagePayRequest;
    import com.alipay.api.request.AlipayTradeQueryRequest;
    import com.alipay.api.response.AlipayTradePagePayResponse;
    import com.alipay.api.response.AlipayTradeQueryResponse;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    /**
     * @author 宇辰
     * 2022/4/12 - 1:45
     */
    @Component
    public class MyalipayUtil {
    
        //这里引入了配置文件里自己配置的值,下面会进行配置
        @Value("${alipay.url}")
        private String url;
        @Value("${alipay.appid}")
        private String appid;
        @Value("${alipay.privateKey}")
        private String privateKey;
        @Value("${alipay.publicKey}")
        private String publicKey;
        @Value("${alipay.notifyUrl}")
        private String notifyUrl;
        @Value("${alipay.returnUrl}")
        private String returnUrl;
    
        //参数是用来接收前台传来的商品的数据的,编号,价格,标题
        public String pay(String id, String price, String title) {
            AlipayClient alipayClient = new DefaultAlipayClient(url, appid , privateKey, "json", "UTF-8", publicKey, "RSA2");
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            request.setNotifyUrl(notifyUrl);
            request.setReturnUrl(returnUrl);
            JSONObject bizContent = new JSONObject();
            //存放编号
            bizContent.put("out_trade_no", id);
            //存放价格
            bizContent.put("total_amount", price);
            //存放标题
            bizContent.put("subject", title);
            bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
    
            request.setBizContent(bizContent.toString());
            AlipayTradePagePayResponse response = null;
            //创建一个变量存放响应的数据
            String from = null;
            try {
                response = alipayClient.pageExecute(request);
                //获得响应的数据
                from = response.getBody();
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            if (response.isSuccess()) {
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
            return from;
        }
    
    }
    
    
  12. 编写application.yml配置文件

    # 配置服务端口
    server:
      port: 8800
    
    # 配置模板缓存、前缀、后缀
    spring:
      thymeleaf:
        cache: false
        prefix: classpath:/templates/
        suffix: .html
    
    # 配置路径(页面上沙箱应用里的网关地址)、商户编号、私钥(太长省略)、公钥(太长省略)、异步通知、支付成功以后自动访问的请求
    alipay:
      url: https://openapi.alipaydev.com/gateway.do
      appid: 2021000118691681
      privateKey: MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDZIJJGe4b...
      publicKey: MIIBIjANBgkqhkiG9...
      notifyUrl: http://localhost:8800/notify
      returnUrl: http://localhost:8800/return
    
    • 解释:

      1. 路径:

        在这里插入图片描述

      2. 商户编号:

        在这里插入图片描述

      3. 秘钥,软件生成的应用秘钥,复制过来。

      4. 公钥,页面生成的支付宝公钥

        在这里插入图片描述

  13. 编写前台支付按钮提交商品信的页面(模拟购物)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1>支付宝沙箱支付</h1>
        <form action="create" method="post">
            订单编号:<input type="text" name="id"><br>
            订单金额:<input type="text" name="price"><br>
            订单标题:<input type="text" name="title"><br>
            <input type="submit" value="创建订单"><br>
        </form>
    
    </body>
    </html>
    
  14. 编写controller跳转这个页面

    @GetMapping("/")
    public String index(){
        return "index";
    }
    
  15. 启动测试,查看是否可以正常访问这个页面

    在这里插入图片描述

  16. 前台表单action中访问的是create请求,controller编写这个请求,并调用创建的pay方法

    package com.tcc.controller;
    
    import com.tcc.com.tcc.util.MyalipayUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    
    /**
     * @author 宇辰
     * 2022/4/12 - 1:31
     */
    @Controller
    public class controller {
    
        @Autowired
        private MyalipayUtil myalipayUtil;
    
        @GetMapping("/")
        public String index(){
            return "index";
        }
    
        /**
         * 点击前台表单提交按钮,访问的请求
         * @param id 前台传的商品编号
         * @param price 前台传的商品价格
         * @param title 前台传的商品标题
         * @param model 存放pay方法的返回值(响应数据)
         * @return
         */
        @PostMapping("/create")
        public String create(String id, String price, String title, Model model){
            String pay = myalipayUtil.pay(id,price,title);
            model.addAttribute("form",pay);
            return "pay";
        }
    
    }
    
    
  17. 编写支付成功以后跳转的请求

    /**
    * 支付成功以后跳转的页面
    * @return
    */
    @GetMapping("/return")
        public String returnNotice(){
            return "index";
        }
    
  18. 运行,测试,查看是否可以正常支付,跳转页面

  19. 支付流程如下:

    1. 在这里插入图片描述

    2. 在这里插入图片描述

    3. 在这里插入图片描述

    4. 在这里插入图片描述

    5. 在这里插入图片描述

  20. 支付成功并跳转以后,复制网页路径,打开url键值对解析网址: 数据格式化 (mumuwei.xyz)

  21. 粘贴进去进行解析可以看到响应的数据

    在这里插入图片描述

2、 统一收单线下交易查询接口

  1. 打开网页,并找到代码

    在这里插入图片描述

  2. 在Util中创建query方法,并粘贴进去,进行修改

    package com.tcc.com.tcc.util;
    
    import com.alibaba.fastjson.JSONObject;
    import com.alipay.api.AlipayApiException;
    import com.alipay.api.AlipayClient;
    import com.alipay.api.DefaultAlipayClient;
    import com.alipay.api.request.AlipayTradePagePayRequest;
    import com.alipay.api.request.AlipayTradeQueryRequest;
    import com.alipay.api.response.AlipayTradePagePayResponse;
    import com.alipay.api.response.AlipayTradeQueryResponse;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    /**
     * @author 宇辰
     * 2022/4/12 - 1:45
     */
    @Component
    //@ConfigurationProperties(prefix = "alipay")
    public class MyalipayUtil {
    
        @Value("${alipay.url}")
        private String url;
        @Value("${alipay.appid}")
        private String appid;
        @Value("${alipay.privateKey}")
        private String privateKey;
        @Value("${alipay.publicKey}")
        private String publicKey;
        @Value("${alipay.notifyUrl}")
        private String notifyUrl;
        @Value("${alipay.returnUrl}")
        private String returnUrl;
    
        public String pay(String id, String price, String title) {
            AlipayClient alipayClient = new DefaultAlipayClient(url, appid , privateKey, "json", "UTF-8", publicKey, "RSA2");
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            request.setNotifyUrl(notifyUrl);
            request.setReturnUrl(returnUrl);
            JSONObject bizContent = new JSONObject();
            bizContent.put("out_trade_no", id);
            bizContent.put("total_amount", price);
            bizContent.put("subject", title);
            bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
    
            request.setBizContent(bizContent.toString());
            AlipayTradePagePayResponse response = null;
            String from = null;
            try {
                response = alipayClient.pageExecute(request);
                from = response.getBody();
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            if (response.isSuccess()) {
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
            return from;
        }
    
        /**
         * 查询订单是否成功
         * @param id
         * @return
         */
        public String query(String id){
            //粘贴上面的
            AlipayClient alipayClient = new DefaultAlipayClient(url, appid , privateKey, "json", "UTF-8", publicKey, "RSA2");
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            JSONObject bizContent = new JSONObject();
            //订单id
            bizContent.put("out_trade_no", id);
            request.setBizContent(bizContent.toString());
            AlipayTradeQueryResponse response = null;
            //和上面方法一样,定义变量存放响应的数据
            String from = null;
            try {
                response = alipayClient.execute(request);
                //获取响应的数据
                from = response.getBody();
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            if(response.isSuccess()){
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
            //返回数据
            return from;
        }
    }
    
  3. 编写controller方法,修改原有的return请求

    package com.tcc.controller;
    
    import com.tcc.com.tcc.util.MyalipayUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    
    /**
     * @author 宇辰
     * 2022/4/12 - 1:31
     */
    @Controller
    public class controller {
    
        @Autowired
        private MyalipayUtil myalipayUtil;
    
        @GetMapping("/")
        public String index(){
            return "index";
        }
    
        /**
         * 点击前台表单提交按钮,访问的请求
         * @param id
         * @param price
         * @param title
         * @param model
         * @return
         */
        @PostMapping("/create")
        public String create(String id, String price, String title, Model model){
            String pay = myalipayUtil.pay(id,price,title);
            model.addAttribute("form",pay);
            return "pay";
        }
    
        /**
         * 支付成功以后跳转的页面
         * @return
         */
        @GetMapping("/return")
        //传入商品的编号
        public String returnNotice(String out_trade_no,Model model){
            String query = myalipayUtil.query(out_trade_no);
            model.addAttribute("form",query);
            return "query";
        }
    }
    
  4. 编写query页面,打印form变量

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <div th:text="${form}"></div>
    </body>
    </html>
    
  5. 运行,测试。

  6. 支付成功以后,等待跳转到query页面,复制页面上的数据,到转化页面中转换查看

    在这里插入图片描述

  7. 查看响应

    在这里插入图片描述

  8. 参数查看

    在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值