首页 > 编程语言 >java springboot+maven 对接支付宝生成支付二维码;查看订单;取消订单;退款

java springboot+maven 对接支付宝生成支付二维码;查看订单;取消订单;退款

时间:2024-10-25 11:18:31浏览次数:8  
标签:alipay java springboot request 订单 out com response String

支付宝提供了测试环境
支付宝沙箱:先登录开放平台 https://openhome.alipay.com/develop/manage

在这里插入图片描述
这边能拿到调用支付宝接口所需要的所有参数
在这里插入图片描述
代码部分:
首先引入依赖:

    <dependency>
        <groupId>com.alipay.sdk</groupId>
        <artifactId>alipay-sdk-java</artifactId>
        <version>4.35.37.ALL</version>
    </dependency>

这边统一整理到main方法里面

package org.example;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {

        getAliCodeurl();
    }
    //--------------------------支付宝-----------------------
    //需要的字段
    //支付宝请求的地址:https://openapi-sandbox.dl.alipaydev.com/gateway.do
    //(应用/小程序/公众号)appid:9021000140694217
    //商户mchid:2088721045400254
    //商户私钥:MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjVNfrCpJYS0h4aqsyVXth9q6IRa9sAPyPa5yBLZI7QtR57jfckjS01/NIIaCV6sW6JJw/sxqoHwRwl7D0mbaExSdHXgrYbO4FoshfSi07SVi3X2Yp8k7g1GZ5sfZhfy3bA835CiXbpjomQk+8DF30AdPsT8Sgdgz7dR0eqTuDmuEF/eLPG18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bWNpvj6x+JpyJwGDbeauuwrHfMRhRLAiXQgB8WpgMac4fyvPas42sx65sZwXHufrvQPYThyKNrLPEH4YhJ/3tvVOYpmfUhcXPxuwHvgswZMLh3nHMYvOULtWKj+qTYFZCQKBgHP7idZBGpSWE/0k1nNvzpN46gUOPKx836xnxKVmXUQOPrjfXihSi4WD7XG/9Q4ZS2M2vPIh9DT4qKXTwtY7Ji1JzwXBgAdkMcPGz6Ad1udveQi9QrXPAm21ykvEMapHW/q87oGg9vGJDPTujHbjazDBVwI1FGYp26xuqu5nxrwk
    //支付宝公钥:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuw+rYJnRhkyGPr/VE9Znp8tgeVw5EFRQ3/38O6+fm31G9Yb3ewj2bRQ2WAk2uitX0tvMTqT33DUsSm2++Q0BjTT03Vst5atCjjI7Un4opMyIy0qgkNq6BaG0FqkXHZpCP0Pk7w6quQGG6qk75skH7ByqKZIXzhLiZyZy7/hv7+s52NVrwxwxuK9WpJmOzvH+QLDr5ZKdTyqDQH4IQa4hZGFfQhZ/ZCAGCixGQrV3/Ypx7Ig1eUG1kSvy4cn1JDvHbomdDKRdj8LNMXxBu6BvTtTklQfkDorilETXVv+AWuUfjExp7r0psAAOk+7hGZLklqfZ6l8rGhqky3YB/LfJswIDAQAB

    //--------------------------支付宝获取二维码url-----------------------
    //获取签证请求
    public static AlipayClient createAlipayClient(){
        String gataliUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        String appid = "9021000140694217";
        String mchid = "2088721045400254";
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjVNfrCpJYS0h4aqsyVXth9q6IRa9sAPyPa5yBLZI7QtR57jfckjS01/NIIaCV6sW6JJw/sxqoHwRwl7D0mbaExSdHXgrYbO4FoshfSi07SVi3X2Yp8k7g1GZ5sfZhfy3bA835CiXbpjomQk+8DF30AdPsT8Sgdgz7dR0eqTuDmuEF/eLPG18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bWNpvj6x+JpyJwGDbeauuwrHfMRhRLAiXQgB8WpgMac4fyvPas42sx65sZwXHufrvQPYThyKNrLPEH4YhJ/3tvVOYpmfUhcXPxuwHvgswZMLh3nHMYvOULtWKj+qTYFZCQKBgHP7idZBGpSWE/0k1nNvzpN46gUOPKx836xnxKVmXUQOPrjfXihSi4WD7XG/9Q4ZS2M2vPIh9DT4qKXTwtY7Ji1JzwXBgAdkMcPGz6Ad1udveQi9QrXPAm21ykvEMapHW/q87oGg9vGJDPTujHbjazDBVwI1FGYp26xuqu5nxrwk";
        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuw+rYJnRhkyGPr/VE9Znp8tgeVw5EFRQ3/38O6+fm31G9Yb3ewj2bRQ2WAk2uitX0tvMTqT33DUsSm2++Q0BjTT03Vst5atCjjI7Un4opMyIy0qgkNq6BaG0FqkXHZpCP0Pk7w6quQGG6qk75skH7ByqKZIXzhLiZyZy7/hv7+s52NVrwxwxuK9WpJmOzvH+QLDr5ZKdTyqDQH4IQa4hZGFfQhZ/ZCAGCixGQrV3/Ypx7Ig1eUG1kSvy4cn1JDvHbomdDKRdj8LNMXxBu6BvTtTklQfkDorilETXVv+AWuUfjExp7r0psAAOk+7hGZLklqfZ6l8rGhqky3YB/LfJswIDAQAB";

        AlipayClient alipayClient = new DefaultAlipayClient(gataliUrl, appid,
                privateKey, "JSON", "UTF-8", publicKey
                , "RSA2");
        return alipayClient;
    }


    public static String getAliCodeurl(){
        AlipayClient alipayClient = createAlipayClient();
        //这段代码设置二维码url的失效时间,这边取的失效时间是  当前日期+2分钟
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime nowPlusTwoMinutes = now.plusMinutes(2);
        //失效时间
        String formattedNowPlusTwoMinutes = nowPlusTwoMinutes.format(formatter);
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        //设置回调接口,可以填自己的接口地址,这边我传的空值(回调接口不太稳定,这边我用下面的主动请求ali接口,获取订单信息)
        request.setNotifyUrl("");
        //订单号   自己生成的 uuid
        String out_trade_no = "qwertasdfg";
        //金额
        String total_amount = "10";
        //subject body 商品描述之类的
        String subject = "iphone";
        String body = "iphone";
        request.setBizContent("{\"out_trade_no\":\"" + out_trade_no+ "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"time_expire\":\"" + formattedNowPlusTwoMinutes + "\","
                + "\"product_code\":\"FACE_TO_FACE_PAYMENT\"}");
        //获取的二维码连接
        String qrCode = "";

        // 执行请求,拿到响应的结果,返回给浏览器
        try {
            AlipayTradePrecreateResponse result = alipayClient.execute(request); // 调用SDK生成表单
            if (result.isSuccess()) {
                qrCode = result.getQrCode();
                System.out.println("二维码连接:"+qrCode);
            } else {
                String resultdata = result.getBody();
                System.out.println("错误原因:"+resultdata);
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return qrCode;
    }

    //--------------------------支付宝主动获取订单信息-----------------------
    public static Boolean isPaySuccess() throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //自己生成订单号
        String out_trade_no = "qwertasdfg";
        request.setBizContent("{" +
                "\"out_trade_no\":\"" + out_trade_no + "\"" +
                "}");
        AlipayClient alipayClient = createAlipayClient();
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            System.out.println(response.getBody());
            //返回结果是已经支付
            if ("TRADE_SUCCESS".equals(response.getTradeStatus())) {
                return true;
            }else {
                return false;
            }
        } else {
            System.out.println("调用失败");
            return false;
        }
    }

    //--------------------------支付宝关闭订单,这边就是让上面生成的支付地址失效-----------------------
    public static String colsePay() throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //自己生成订单号
        String out_trade_no = "qwertasdfg";
        request.setBizContent("{" +
                "\"out_trade_no\":\"" + out_trade_no + "\"" +
                "}");
        AlipayClient alipayClient = createAlipayClient();
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            return "关闭订单成功";
        } else {
            return "关闭订单失败"+response.getSubCode();
        }
    }


    //--------------------------退款-----------------------
    public static String returnPay() throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //自己生成订单号
        String out_trade_no = "qwertasdfg";
        request.setBizContent("{" +
                "\"out_trade_no\":\"" + out_trade_no + "\"" +
                "}");
        AlipayClient alipayClient = createAlipayClient();
        AlipayTradeRefundRequest alipay_request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        //自己生成的订单号
        model.setOutTradeNo("qwertasdfg");
        //需要退款的金额
        model.setRefundAmount("10");
        model.setRefundReason("");
        alipay_request.setBizModel(model);
        AlipayTradeRefundResponse alipay_response = alipayClient.execute(alipay_request);
        if ("Y".equals(alipay_response.getFundChange())) {
            return "退款成功";
        } else {
            return "退款失败"+alipay_response.getSubMsg();
        }
    }

}

注意:主动获取订单状态的接口,只能获取到支付成功的状态,其他状态拿不到;我这边的getAliCodeurl的接口是获取了支付的链接,需要前端自己将这个链接转成二维码
正式环境对接注意!!!
1.申请商户号
2.申请appid
3.商户号和appid进行绑定
4.商户号开通当面付功能
5.设置对应的appid的密钥
如果是企业商户,登录开放平台的时候不要扫码登录,要不然拿不到权限

标签:alipay,java,springboot,request,订单,out,com,response,String
From: https://blog.csdn.net/weixin_54143030/article/details/143115656

相关文章

  • Java复习16(PTA)
    快递计价器分数20全屏浏览切换布局作者 大数据2021单位 山东科技大学现需要编写一个简易快递计价程序。具体来说:1、抽象快递类Express,其包含一个属性intweight表示快递重量(单位为kg),一个方法getWeight()用于返回快递重量和一个抽象方法getTotal()用于计算快递运费......
  • 基于SpringBoot+Vue旅行推广网站的设计与实现
    博主主页:一季春秋博主简介:专注Java技术领域和毕业设计项目实战、Java、微信小程序、安卓等技术开发,远程调试部署、代码讲解、文档指导、ppt制作等技术指导。主要内容:SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、小程序、安卓app、大数据等设计与开发。感兴趣的可......
  • 基于SpringBoot + Vue的高校大学生竞赛项目管理系统(角色:学生、评委、管理员)
    文章目录前言一、详细操作演示视频二、具体实现截图三、技术栈1.前端-Vue.js2.后端-SpringBoot3.数据库-MySQL4.系统架构-B/S四、系统测试1.系统测试概述2.系统功能测试3.系统测试结论五、项目代码参考六、数据库代码参考七、项目论文示例结语前言......
  • 基于SpringBoot + Vue的《计算机基础》网上考试系统
    文章目录前言一、详细操作演示视频二、具体实现截图三、技术栈1.前端-Vue.js2.后端-SpringBoot3.数据库-MySQL4.系统架构-B/S四、系统测试1.系统测试概述2.系统功能测试3.系统测试结论五、项目代码参考六、数据库代码参考七、项目论文示例结语前言......
  • LeetCode_70. 爬楼梯_java
    1、题目70.爬楼梯https://leetcode.cn/problems/climbing-stairs/假设你正在爬楼梯。需要n阶你才能到达楼顶。每次你可以爬1或2个台阶。你有多少种不同的方法可以爬到楼顶呢?示例1:输入:n=2输出:2解释:有两种方法可以爬到楼顶。1.1阶+1阶2.2阶示例2:输......
  • LeetCode_509. 斐波那契数_java
    1、题目509.斐波那契数https://leetcode.cn/problems/fibonacci-number/斐波那契数(通常用F(n)表示)形成的序列称为斐波那契数列。该数列由0和1开始,后面的每一项数字都是前面两项数字的和。也就是:F(0)=0,F(1)=1F(n)=F(n-1)+F(n-2),其中n>1给定n,请......
  • 谈一谈 Netty 的内存管理 —— 且看 Netty 如何实现 Java 版的 Jemalloc
    本文基于Netty4.1.112.Final版本进行讨论在之前的Netty系列中,笔者是以4.1.56.Final版本为基础和大家讨论的,那么从本文开始,笔者将用最新版本4.1.112.Final对Netty的相关设计展开解析,之所以这么做的原因是Netty的内存池设计一直在不断地演进优化。在4.1.52.Final......
  • LeetCode_2119. 反转两次的数字_java
    1、题目2119.反转两次的数字https://leetcode.cn/problems/a-number-after-a-double-reversal/反转一个整数意味着倒置它的所有位。   例如,反转2021得到1202。反转12300得到321,不保留前导零。给你一个整数num,反转num得到reversed1,接着反转reversed1......
  • 实战篇:(二十一)Java 开发指南:避免 18个常见错误,提升你的编码效率
    实战篇:(二十一)Java开发指南:避免18个常见错误,提升你的编码效率Java作为一门成熟的编程语言,拥有丰富的生态系统与广泛的应用。然而,即使是经验丰富的开发者,也时常会在日常开发中犯一些常见的错误。这些错误不仅会影响代码的可读性,还可能造成性能问题甚至难以调试的Bug。本......
  • JavaScript 与 Rust 和 WebAssembly 集成
    偶然一次机会,接触了Rust的代码。当时想给团队小伙伴做演示,发现自己并不能在移动端按照文档生成演示demo。我就想,要是Rust代码能转化成JavaScript就好了。结果一搜,还真有。下面整理成文档,分享给大家。为大家解决问题,多提供一种思路、方式、方法。 一、分享的目的:▪由Rust、We......