快速java实现支付宝二维码支付(基于spring boot构建工程)

本文档详细介绍了如何使用Java和Spring Boot构建一个支付宝二维码支付项目。首先,通过支付宝官网进入沙箱环境并下载秘钥生成器工具。接着配置商户私钥和应用公钥,下载沙箱APP,并在Spring Boot工程中添加依赖,逐步构建ApplicationRun、AlipayConfig、AlipayService、ALiPayController等类。最后,通过运行项目并访问页面完成支付流程。
摘要由CSDN通过智能技术生成

java实现支付宝支付项目(基于spring boot构建)

由于我们没有企业和公号使用啥用支付宝提供的沙箱模拟真实支付操作!

标题通过支付宝的官网进行登录点击到沙箱:https://openhome.alipay.com

登录过后下拉选择研发服务:
在这里插入图片描述

沙箱操作流程与下载秘钥生成器工具:https://opendocs.alipay.com/open/291/105971/

1 通过工具生成密钥后,这里的秘钥即是商户私钥:
在这里插入图片描述
2 接着生成后 再将商户应用公钥复制填写在沙箱环境应用公钥处:
在这里插入图片描述
在这里插入图片描述
3 下载沙箱app:
在这里插入图片描述
4 在APP登录前提就在图中提示按流程登录:
在这里插入图片描述

以下操作Demo流程到Test环节

一springboot工程添加依赖:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cn</groupId>
    <artifactId>alipay</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <packaging>war</packaging>
    <dependencies>
        <!--springBoot动态的引入springMVC全部的配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--servlet依赖 注意与eureka整合时的问题 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

        <!--jstl依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--使jsp页面生效 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!--servlet-end-->
        <!--添加httpClient jar包 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <!-- alipay 支付宝 -->
        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

</project>

构建代码

1 配置application.yml:

server:
  port: 9999

spring: #定义springmvc视图解析器
  mvc:
    view:
      prefix: /WEB-INF/views/
      suffix: .jsp

2 看图与Demo操作:
在这里插入图片描述

  1. 构建父目录com.cn 新建一个启动类 ApplicationRun 粘贴以下code:
package com.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @version 1.0
 * @class: ApplicationRun
 * @author: Ang
 * @mail: 116
 * @date: 2020/7/5 19:48
 * @description:
 */
@SpringBootApplication
public class ApplicationRun {
   
    public static void main(String[] args) {
   
        SpringApplication.run(ApplicationRun.class,args);
    }

}

  1. 构建一个config子目录 新建配置支付宝类 AlipayConfig:
package com.cn.config;

import java.io.FileWriter;
import java.io.IOException;

/* *
 *类名:AlipayConfig
 *功能:基础配置类
 *详细:设置帐户有关信息及返回路径
 *说明:
 *该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
 */

public class AlipayConfig {
   

//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
    public static String app_id = "123";

    // 之前生成的私钥内容复制此位置
    public static String merchant_private_key ="把生成的私钥内容复制此位置";
    
    // 公钥,对应APPID下的支付宝公钥。
    public static String alipay_public_key ="公钥,对应APPID下的支付宝公钥复制此位置";
    
    // 服务器异步通知业务路径  需http://格式的完整路径,不能加?id=xxx这类自定义参数
    public static String notify_url = "http://localhost:9999/notifyUrl";//当前服务器内的方法路径

    // 页面跳转 同步通知支付状态路径 需http://格式的完整路径,不能加?id=123这类自定义参数
    public static String return_url = "http://localhost:9999/returnUrl";//当前服务器内的方法路径

    // 签名方式
    public static String sign_type = "RSA2";

    // 字符编码格式
    public static String charset = "utf-8";

    // 支付宝网关
    public static String gatewayUrl = "对应APPID下的那个http地址";

    // 日志目录 可以不写此操作环节
    public static String log_path = "D:\\xjj";


//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    /**
     * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
     *
     * @param sWord 要写入日志里的文本内容
     */
    public static void logResult(String sWord) {
   
        FileWriter writer = null;
        try {
   
            writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis() + ".txt");
            writer.write(sWord);
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            if (writer != null) {
   
                try {
   
                    writer.close();
                } catch (IOException e) {
   
                    e.printStackTrace();
                }
            }
        }
    }
}


  1. 构建 servic
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值