• 【精品】Spring2.7 采用easysdk方式 整合 aplipay


    准备工作

    1. 注册真正的支付宝账号

    2. 在百度搜索支付宝开放平台
      搜索到的网址:https://open.alipay.com/dev/workspace/
      在这里插入图片描述

    3. 用真正的支付宝账号登录
      在这里插入图片描述

    4. 选择沙箱环境
      在这里插入图片描述

    5. 打开沙箱应用页面
      在控制台中查看到APPID:
      在这里插入图片描述

    6. 沙箱账号
      这个账号一会项目启动起来,支付时会使用到
      在这里插入图片描述

    7. 启用系统默认密钥
      在这里插入图片描述
      在这里插入图片描述

    8. 查看
      在这里插入图片描述

    具体编码工作

    第一步:创建SpringBoot项目,添加依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    
    <!--阿里支付easy版-->
    <dependency>
        <groupId>com.alipay.sdk</groupId>
        <artifactId>alipay-easysdk</artifactId>
        <version>2.2.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    第二步:修改application.yml

    server:
      port: 80
      servlet:
        context-path: /wego
    alipay:
      #阿里支付
      appId: 2021000118674150
      #应用私钥
      appPrivateKey: 
      #支付宝公钥
      alipayPublicKey: 
      notifyUrl: 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其中:
    在这里插入图片描述
    notifyUrl是成功之后,阿里巴巴将支付结果回传到我们自己项目中的url的地址,此时需要网络穿透,请参看博客:内网穿透工具 netapp

    第三步:读取alipay配置文件的配置类:AlipayConfig

    @Getter
    @Setter
    @Component
    @ConfigurationProperties(prefix = "alipay")
    public class AlipayConfig {
       private String appId;
       private String appPrivateKey;
       private String alipayPublicKey;
       private String notifyUrl;
    
       @PostConstruct
       public void init(){
          Config options = getOptions();
          options.appId = this.appId;
          options.merchantPrivateKey=this.appPrivateKey;
          options.alipayPublicKey = this.alipayPublicKey;
          options.notifyUrl = this.notifyUrl;
          Factory.setOptions(options);
          System.out.println("支付宝初始化成功!");
       }
    
       private Config getOptions(){
          Config config = new Config();
          config.protocol="https";
          config.gatewayHost="openapi.alipaydev.com";
          config.signType="RSA2";
          return config;
       }
    }
    
    • 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

    第四步:定义支付相关的控制器:AlipayController

    @RestController
    @RequestMapping("/v1/alipay")
    public class AlipayController {
        //http://localhost/wego/v1/alipay/pay?subject=aabbcc&tradeNo=1001&totalAmount=8888
        @GetMapping(value = "/pay", produces = "text/html;charset=utf-8")
        String pay(String subject, String tradeNo, String totalAmount) {
            AlipayTradePagePayResponse response = null;
            //发起API调用
            try {
                response = Factory.Payment.Page().pay(subject, tradeNo, totalAmount, "");
            } catch (Exception e) {
                System.err.println("支付时遇到异常:");
                e.printStackTrace();
            }
            System.out.println(response.getBody());
            return response.getBody();
        }
    
    
        @PostMapping("/notify")
        String payNotify(HttpServletRequest request) throws Exception {
            System.out.println("pay notify");
            String tradeStatus = request.getParameter("trade_status");
            System.out.println(tradeStatus);
            if ("TRADE_SUCCESS".equals(tradeStatus)) {
                System.out.println("支付宝异步回调");
                Map<String, String> params = new HashMap<>();
                Map<String, String[]> requestParams = request.getParameterMap();
                for (String name : requestParams.keySet()) {
                    params.put(name, request.getParameter(name));
                }
                String tradeNo = params.get("out_trade_no");
                String gmtPayment = params.get("gmt_payment");
    
                //支付宝验签
                if (Factory.Payment.Common().verifyNotify(params)) {
                    System.out.println("交易名称:" + params.get("subject"));
                    System.out.println("交易状态:" + params.get("trade_status"));
                    System.out.println("支付宝交易凭证号:" + params.get("trade_no"));
                    System.out.println("商户订单号:" + params.get("out_trade_no"));
                    System.out.println("交易金额:" + params.get("total_amount"));
                    System.out.println("买家在支付宝唯一ID:" + params.get("buyer_id"));
                    System.out.println("买家付款时间:" + params.get("gmt_payment"));
                    System.out.println("买家付款金额:" + params.get("buyer_pay_amount"));
                    //更新订单为已支付
                    //....
                }
            }
            return "success";
        }
    }
    
    • 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

    第五步:运行程序测试

    • 打开edge浏览器(不要使用chrome),在其中输入:http://localhost/wego/v1/alipay/pay?subject=aabbcc&tradeNo=1021&totalAmount=8888,之后自支打开下图右边的网页

    在这里插入图片描述
    按图所示输入用户名和密码,单击提交:
    在这里插入图片描述

    • 输入支付密码
      在这里插入图片描述
      单击确认支付:
      在这里插入图片描述
    • 自动跳转到页面:
      在这里插入图片描述
      此时在IDEA控制台中会看到:
      在这里插入图片描述

    拓展

    上述实现在准备工作中,采用的是系统默认密钥,也可以采用自定义密钥的方式:
    在这里插入图片描述

    第一步:下载支付宝开放平台开发助手

    下载地址:https://opendocs.alipay.com/open/02kipk
    在这里插入图片描述
    下载之后默认安装,之后打开,生成密钥:
    在这里插入图片描述

    改成自定义密钥的方式

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    麻省理工人工智能实验室新研究!有远见的机器学习方法:能预知未来行为的AI智能体
    Springboot聚合工程打包:代码正确,但打包出错找不到类
    一篇文章教你自动化测试如何解析excel文件?
    阿里云/腾讯云国际站代理:阿里云服务器介绍
    聚焦光量子应用开发!Quandela 发布新版量子计算云服务
    两轮平衡电动车原理简单叙述
    【Java线程池】 java.util.concurrent.ThreadPoolExecutor 源码分析
    图文结合纯c手写内存池
    【面试题精讲】JavaSe和JavaEE的区别
    神经网络图像处理实例图,神经网络图像识别算法
  • 原文地址:https://blog.csdn.net/lianghecai52171314/article/details/125602504