SpringBoot实现用支付宝(支付、查询、退款(超详细))

SpringBoot继承支付宝(支付、查询、退款)

首先推荐个视频给各位先熟悉熟悉,怎么将支付宝的给你的老版demo运行起来

https://www.bilibili.com/video/BV1Ft41167fk?p=11&t=149

怎么从eclipse到IDEA中的SpringBoot工程?

前提:看看项目的工程结构和包名。
SpringBoot实现用支付宝(支付、查询、退款(超详细))_第1张图片
SpringBoot实现用支付宝(支付、查询、退款(超详细))_第2张图片
第一步:导入依赖

 
        
            com.alipay.sdk
            alipay-sdk-java
            3.1.0
        

如果你觉得我的依赖版本低了,那么你可以查看最新依赖查看地方(就是你是人类认证有点麻烦):

SpringBoot实现用支付宝(支付、查询、退款(超详细))_第3张图片

第二步:配置AlipayConfig文件

注意:如果这里的配置什么的,你不懂什么意思,请将最前面的视频链接看一遍。

public class AlipayConfig {
     

    // AppId
    public static String app_id = "2021000117611220";

    // 商户私钥
    public static  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCv7xoq96nGOq47YEGQ+R3B9hMszzpWjQvDy1tfyjBGKw1S4gN61mK5T8pBcLyPdSKZxMWCbx+rQLOOb5lhFkFOwqRAW4KxY891MXbc3Trx8RJeYSi/sR5pj0FKU3V9fLsCRylTXGqhlpV4N7HcKwCn+G6U9K8akEpd2+sfYN4TbLeF+Yh/G0TJDR+pTAM28xZa9+vhBP1Zfk4zl4Z6gniQm+gtxnGX20bm7gJvlVzwHVgFPOsWwelWM/iO+UzbCfSGgcVQZ3nvCv567gJlYfU++nfdTQzpFEduYCuyas2ZxDQb1GZ9ExBqFxnsJffTyPbUFJ2scW6/dPveF4BF5NdPAgMBAAECggEABKLZXq5+E9IyCzCDKc3Wwei5IqInWGaJG9S9CK4sTX0O8dMw4kjJ+zYmD0loPqOIkOLFjaVeAUZINQWt1ggfGnM4ntWKgSAvNFhkWHef+QYwRLpst3h0Y7W5s7q1SuRBjVLQvPifericIbnd/v5HGyYJy/UStDF/NC/6Zy5HS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ay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArMfU8PA29JmykhIQynJpx47fea7OgD048vvEF/0Gk2DC0WuqN5rNyQErxOG2UPwRkrjSr3na6VBz3dR+W40TFCNWw3n+H2d+VqD50wBYVYNRlhd8TpQYWRSvPsqFoZOvb3CxuHQCxy9W9ijC3jB/db26NxbyKDnf4YLzdvR17v2PcYmJGLRBL6yDSSyWTqaUzoaegBMqtL+qsPKDRtoDToMNnHTC6BveQJlg6HL5JMHUL43Jj62FYLczO57bPGmjceEW6x3vIk0CgAJcoupYwnAsYhY8QSeZkJL1UhoLtu7mKhnWwX0Bn6tfU8bj3Ea6wxRSTb+NyCYhZi5ilulT+QIDAQAB";

    // 服务器异步通知页面路径
    public static  String notify_url = "http://www.baidu.com";

    // 页面跳转同步通知页面路径,回调地址
    public static  String return_url = "http://www.baidu.com";

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

    // 数据格式
    public static  String format = "json";

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

    // 支付宝网关
    public static String gateway = "https://openapi.alipaydev.com/gateway.do";
}

其实这里的配置文件就是那个demo里面的,请看下面:

SpringBoot实现用支付宝(支付、查询、退款(超详细))_第4张图片

所以弄懂我这个先得把demo里面的东西弄懂即可。

第三步:写Service层中的PayService类(支付)

@Service
public class PayService {
     

    public Object pay() throws Exception {
     

        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.gateway,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key,
                "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type
        );
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号,商户网站订单系统中唯一订单号,必填
        String out_trade_no = new String("WIDout_trade_no");
        //付款金额,必填
        String total_amount = new String("11.5");
        //订单名称,必填
        String subject = new String("测试专用");
        //商品描述,可空
        String body = new String("专为测试而生");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        return result;
    }
}

然而这个PayService也就跟demo里面jsp中的代码一样,请看下面
SpringBoot实现用支付宝(支付、查询、退款(超详细))_第5张图片

其他几个查询、退款啥的都可以从支付宝提供的demo中找,所以下面就不详细阐述了。

其他几个类附代码:

QueryService类(查询订单)

@Service
public class QueryService {
     

    public Object query() throws AlipayApiException {
     
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.gateway,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key,
                "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type
        );

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        //商户订单号,后台可以写一个工具类生成一个订单号,必填
        String out_trade_no = new String("20210210210655");
        //支付宝交易号,后台可以写一个工具类生成一个订单号,必填
        String trade_no  = new String("2021021022001468560501398476");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","+"\"trade_no\":\""+ trade_no +"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();

        //输出
        System.out.println(result);
        System.out.println("查询成功");
        return result;
    }
}

RefundQueryService类(退款查询)

@Service
public class RefundQueryService {
     
    public Object refundQuery() throws AlipayApiException {
     
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.gateway,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key,
                "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type
        );

        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();

        //商户订单号,后台可以写一个工具类生成一个订单号,必填
        String out_trade_no = new String("20210210211025");
        //支付宝交易号,后台可以写一个工具类生成一个订单号,必填
        String trade_no  = new String("2021021022001468560501398642");

        //请求退款接口时,传入的退款请求号,如果在退款请求时未传入,则该值为创建交易时的外部交易号,必填
        String out_request_no = new String("WIDRQout_request_no");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                +"\"trade_no\":\""+ trade_no +"\","
                +"\"out_request_no\":\""+ out_request_no +"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        //输出
        System.out.println(result);
        System.out.println("查询退余额成功");
        return result;
    }
}

RefundService类(退款)

@Service
public class RefundService {
     

    public Object refund() throws AlipayApiException {
     
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.gateway,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key,
                "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type
        );
        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        //商户订单号,后台可以写一个工具类生成一个订单号,必填
        String out_trade_no = new String("20210210211025");
        //支付宝交易号,后台可以写一个工具类生成一个订单号,必填
        String trade_no  = new String("2021021022001468560501398642");
        //付款金额,从前台获取,必填
        String refund_amount = new String("10.50");
        //订单名称/标题,可自定义
        String subject = new String("支付宝沙箱测试");
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"trade_no\":\""+ trade_no +"\","
                + "\"refund_amount\":\""+ refund_amount +"\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //输出
        System.out.println("result:"+result);
        //
        System.out.println("退款成功");
        return result;
    }
}

Controller层

@RestController
@RequestMapping(value = "/pay")
@AllArgsConstructor
@CrossOrigin
public class PayController {
     

    @Autowired
    private PayService payService;

    @Autowired
    private RefundService refundService;

    @Autowired
    private QueryService queryService;

    @Autowired
    private RefundQueryService refundQueryService;

    /**
     * 下单支付
     */
    @GetMapping(value = "/confirm", produces = {
     "text/html;charset=UTF-8"})
    public Object pay() throws Exception {
     
        //这个接口其实应该是post方式的,但是我这里图方便,直接以get方式访问,
        //且返回格式是text/html,这样前端页面就能直接显示支付宝返回的html片段
        //真实场景下由post方式请求,返回code、msg、data那种格式的标准结构,让前端拿到data里的
        //html片段之后自行加载
        System.out.println("执行了支付");
        return payService.pay();
    }

    /**
     * 下单退款
     */
    @GetMapping(value = "/refund", produces = {
     "text/html;charset=UTF-8"})
    public Object refund() throws Exception {
     
        //这个接口其实应该是post方式的,但是我这里图方便,直接以get方式访问,
        //且返回格式是text/html,这样前端页面就能直接显示支付宝返回的html片段
        //真实场景下由post方式请求,返回code、msg、data那种格式的标准结构,让前端拿到data里的
        //html片段之后自行加载
        System.out.println("执行了退款");
        return refundService.refund();
    }

    /**
     * 下单查询
     */
    @GetMapping(value = "/query", produces = {
     "text/html;charset=UTF-8"})
    public Object query() throws Exception {
     
        //这个接口其实应该是post方式的,但是我这里图方便,直接以get方式访问,
        //且返回格式是text/html,这样前端页面就能直接显示支付宝返回的html片段
        //真实场景下由post方式请求,返回code、msg、data那种格式的标准结构,让前端拿到data里的
        //html片段之后自行加载
        System.out.println("执行了查询");
        return queryService.query();
    }

    /**
     * 退款查询
     */
    @GetMapping(value = "/refundquery", produces = {
     "text/html;charset=UTF-8"})
    public Object refundQuery() throws Exception {
     
        //这个接口其实应该是post方式的,但是我这里图方便,直接以get方式访问,
        //且返回格式是text/html,这样前端页面就能直接显示支付宝返回的html片段
        //真实场景下由post方式请求,返回code、msg、data那种格式的标准结构,让前端拿到data里的
        //html片段之后自行加载
        System.out.println("执行了退款查询");
        return refundQueryService.refundQuery();
    }
}

静态资源里面的html为:

index.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>确认订单title>
head>
<body>
<div>
    您的订单信息如下,请确认无误后支付:<br />
    xxxxxxxxxx省略订单信息xxxxxxxxxx

    <form enctype="multipart/form-data" action="/pay/confirm" method="get">
        <button type="submit">确认支付button>
    form>
div>
body>
html>

indexQuery.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>确认查询title>
head>
<body>
<div>
    您的订单信息如下,请确认无误后查询:<br />
    xxxxxxxxxx省略订单信息xxxxxxxxxx

    <form enctype="multipart/form-data" action="/pay/query" method="get">
        <button type="submit">确认查询button>
    form>
div>
body>
html>

indexRefund.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>确认订单title>
head>
<body>
<div>
    您的订单信息如下,请确认无误后退款:<br />
    xxxxxxxxxx省略订单信息xxxxxxxxxx

    <form enctype="multipart/form-data" action="/pay/refund" method="get">
        <button type="submit">确认退款button>
    form>
div>
body>
html>

indexRefundQuery.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>确认查询title>
head>
<body>
<div>
    您的订单信息如下,请确认无误后查询:<br />
    xxxxxxxxxx省略订单信息xxxxxxxxxx

    <form enctype="multipart/form-data" action="/pay/refundquery" method="get">
        <button type="submit">确认退款信息查询button>
    form>
div>
body>
html>

切记:因为我的数据都是写死的,所以界面就没怎么写了,大家可以根据自己的项目需求,对前端进行优化。

专栏里面还有一些支付宝踩坑集像什么:网站被钓鱼、界面显示不出等问题的解释,大家如果出现类似问题可以去看看。

你可能感兴趣的:(支付宝网站支付,java)