Springboot对接支付宝SDK案例

导入Maven依赖

        <!-- 支付功能SDK -->
        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.10.124.ALL</version>
        </dependency>

创建config配置类

@Configuration
public class Config_alipay {

    //创建支付宝所需要的对象
    @Bean
    public AlipayClient alipayClient(){
        return new DefaultAlipayClient(AppUtil.gatewayUrl,AppUtil.app_id,AppUtil.merchant_private_key,"json",AppUtil.charset,
                AppUtil.alipay_public_key,AppUtil.sign_type);
    }

    //支付信息的配置
    @Bean  
    public AlipayTradePagePayRequest alipayTradePagePayRequest(){
        return  new AlipayTradePagePayRequest();
    }
}

创建支付配置类

主要是公钥私钥以及appi等,还有异步通知以及页面跳转

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

    // 商户私钥,您的PKCS8格式RSA2私钥
    public static String merchant_private_key="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCk+PWvsQ9fhYzJd5b7qxpyEEVYYW2/B6Uxk7wDH4g7dOzPXstebK87anwMqISlRyT7Nc7AGAaYJnBMdNArEh5YQOXoUAtj3bZtR12PXtigCy/WllB4gtT5O3nf5LR4xmGz2lMvzkIoOalLvtVqei9Fq8dXwVJpS9gRzfAWou9pnanNcrxpVuALUvH+uHkT5xZAcWTQC9nGnfN6r45d70x8LoSHuTXpMezDsaZQgNoXVLkSAr3XnDwk6CpXcAG2Oq1ex1v0BEgI7AbgFTJOEQg8ZEBGDOERDYeA/FScS8J10SHb35nLUcRbqaDERsrXGLgJc/zxg2uNdF5l5KLzkSkpAgMBAAECggEAUZQrKi5Mg9fuvtG8f8ck1LLnF8VWF9kX/2Qm3QSCwaQYVpNxzcfA/4iwvzG2JmUADJmXG7bFzAd2ENjS9/mFt+6fwjmS6FeDFpbr1TH5vWrKxTqfoHhSCM04f1cBR5gSSHX2HPXKEUoyRZqgvn6dUb6YXPTkY911g5hq2Ub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://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApPj1r7EPX4WMyXeW+6sachBFWGFtvwelMZO8Ax+IO3Tsz17LXmyvO2p8DKiEpUck+zXOwBgGmCZwTHTQKxIeWEDl6FALY922bUddj17YoAsv1pZQeILU+Tt53+S0eMZhs9pTL85CKDmpS77VanovRavHV8FSaUvYEc3wFqLvaZ2pzXK8aVbgC1Lx/rh5E+cWQHFk0AvZxp3zeq+OXe9MfC6Eh7k16THsw7GmUIDaF1S5EgK915w8JOgqV3ABtjqtXsdb9ARICOwG4BUyThEIPGRARgzhEQ2HgPxUnEvCddEh29+Zy1HEW6mgxEbK1xi4CXP88YNrjXReZeSi85EpKQIDAQAB";

    // 服务器异步通知页面路径
    //需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static String notify_url = "http://120.27.94.243:8955/getnotify";

    // 页面跳转同步通知页面路径
    //需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static String return_url = "http://120.27.94.243:8955/getreturn";

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

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

    // 支付宝网关,注意这些使用的是沙箱的支付宝网关,与正常网关的区别是多了dev
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

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

创建Controller

这里面的getnotify和getreturn只是用于反馈信息的,如果接入自己的业务,需要根据自己的实际业务需求进行重构。

@RestController
public class alipayController {

    @Resource
    private AlipayClient alipayClient;
    @Resource
    private AlipayTradePagePayRequest alipayTradePagePayRequest;


    @RequestMapping("pay.api")
    public String alipay1(String WIDout_trade_no, String WIDsubject, String WIDtotal_amount, String WIDbody){

        System.out.println("pay");

        //设置响应的地址(支付宝返回给商户的响应地址)
        alipayTradePagePayRequest.setNotifyUrl(AppUtil.notify_url);
        alipayTradePagePayRequest.setReturnUrl(AppUtil.return_url);
        //4.设置请求的参数(传递给支付宝的数据)
        alipayTradePagePayRequest.setBizContent(
                "{\"out_trade_no\":\""+ WIDout_trade_no +"\","
                        + "\"total_amount\":\""+ WIDtotal_amount +"\","
                        + "\"subject\":\""+ WIDsubject +"\","
                        + "\"body\":\""+ WIDbody +"\","
                        + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //发送请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 接收支付宝返回的异步通知的信息
     * @param request
     * @return
     */
    @RequestMapping("getnotify")
    public String getnotify(HttpServletRequest request){
        //商户订单号
        String out_trade_no =request.getParameter("out_trade_no");
        //支付宝交易号
        String trade_no = request.getParameter("trade_no");
        //交易状态
        String trade_status = request.getParameter("trade_status");
        StringBuffer sb = new StringBuffer();
        sb.append("接收支付宝返回的异步通知的信息=");
        sb.append("商户订单号:");
        sb.append(out_trade_no);
        sb.append("支付宝交易号:");
        sb.append(trade_no);
        sb.append("交易状态:");
        sb.append(trade_status);
        System.out.println(sb);
        return sb.toString();
    }

    /**
     * 获取支付宝GET过来反馈信息
     * @param request
     * @return
     */
    @RequestMapping("getreturn")
    public String getreturn(HttpServletRequest request){
        //商户订单号
        String out_trade_no =request.getParameter("out_trade_no");
        //支付宝交易号
        String trade_no = request.getParameter("trade_no");
        //交易状态
        String total_amount = request.getParameter("total_amount");
        StringBuffer sb = new StringBuffer();
        sb.append("获取支付宝GET过来反馈信息=");
        sb.append("商户订单号:");
        sb.append(out_trade_no);
        sb.append("支付宝交易号:");
        sb.append(trade_no);
        sb.append("交易金额:");
        sb.append(total_amount);
        return sb.toString();
    }
}

编写前端页面

然后需要一个前端html页面调用这个api

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>支付宝网站支付</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul, ol {
            list-style: none;
        }

        body {
            font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande",
            sans-serif;
        }

        .tab-head {
            margin-left: 120px;
            margin-bottom: 10px;
        }

        .tab-content {
            clear: left;
            display: none;
        }

        h2 {
            border-bottom: solid #02aaf1 2px;
            width: 200px;
            height: 25px;
            margin: 0;
            float: left;
            text-align: center;
            font-size: 16px;
        }

        .selected {
            color: #FFFFFF;
            background-color: #02aaf1;
        }

        .show {
            clear: left;
            display: block;
        }

        .hidden {
            display: none;
        }

        .new-btn-login-sp {
            padding: 1px;
            display: inline-block;
            width: 75%;
        }

        .new-btn-login {
            background-color: #02aaf1;
            color: #FFFFFF;
            font-weight: bold;
            border: none;
            width: 100%;
            height: 30px;
            border-radius: 5px;
            font-size: 16px;
        }

        #main {
            width: 100%;
            margin: 0 auto;
            font-size: 14px;
        }

        .red-star {
            color: #f00;
            width: 10px;
            display: inline-block;
        }

        .null-star {
            color: #fff;
        }

        .content {
            margin-top: 5px;
        }

        .content dt {
            width: 100px;
            display: inline-block;
            float: left;
            margin-left: 20px;
            color: #666;
            font-size: 13px;
            margin-top: 8px;
        }

        .content dd {
            margin-left: 120px;
            margin-bottom: 5px;
        }

        .content dd input {
            width: 85%;
            height: 28px;
            border: 0;
            -webkit-border-radius: 0;
            -webkit-appearance: none;
        }

        #foot {
            margin-top: 10px;
            position: absolute;
            bottom: 15px;
            width: 100%;
        }

        .foot-ul {
            width: 100%;
        }

        .foot-ul li {
            width: 100%;
            text-align: center;
            color: #666;
        }

        .note-help {
            color: #999999;
            font-size: 12px;
            line-height: 130%;
            margin-top: 5px;
            width: 100%;
            display: block;
        }

        #btn-dd {
            margin: 20px;
            text-align: center;
        }

        .foot-ul {
            width: 100%;
        }

        .one_line {
            display: block;
            height: 1px;
            border: 0;
            border-top: 1px solid #eeeeee;
            width: 100%;
            margin-left: 20px;
        }

        .am-header {
            display: -webkit-box;
            display: -ms-flexbox;
            display: box;
            width: 100%;
            position: relative;
            padding: 7px 0;
            -webkit-box-sizing: border-box;
            -ms-box-sizing: border-box;
            box-sizing: border-box;
            background: #1D222D;
            height: 50px;
            text-align: center;
            -webkit-box-pack: center;
            -ms-flex-pack: center;
            box-pack: center;
            -webkit-box-align: center;
            -ms-flex-align: center;
            box-align: center;
        }

        .am-header h1 {
            -webkit-box-flex: 1;
            -ms-flex: 1;
            box-flex: 1;
            line-height: 18px;
            text-align: center;
            font-size: 18px;
            font-weight: 300;
            color: #fff;
        }
    </style>
</head>
<body text=#000000 bgColor="#ffffff" leftMargin=0 topMargin=4>
<header class="am-header">
    <h1>支付宝体验入口页</h1>
</header>
<div id="main">
    <div id="tabhead" class="tab-head">
        <h2 id="tab1" class="selected" name="tab">付 款</h2>
    </div>
    <form name="alipayment" action="http://192.168.5.10:89/pay.api" method="post" target="_blank">
        <div id="body1" class="show" name="divcontent">
            <dl class="content">
                <dt>商户订单号 :</dt>
                <dd>
                    <input id="WIDout_trade_no" name="WIDout_trade_no" />
                </dd>
                <hr class="one_line">
                <dt>订单名称 :</dt>
                <dd>
                    <input id="WIDsubject" name="WIDsubject" />
                </dd>
                <hr class="one_line">
                <dt>付款金额 :</dt>
                <dd>
                    <input id="WIDtotal_amount" name="WIDtotal_amount" />
                </dd>
                <hr class="one_line">
                <dt>商品描述:</dt>
                <dd>
                    <input id="WIDbody" name="WIDbody" />
                </dd>
                <hr class="one_line">
                <dt></dt>
                <dd id="btn-dd">
                        <span class="new-btn-login-sp">
                            <button class="new-btn-login" type="submit"
                                    style="text-align: center;">付 款</button>
                        </span> <span class="note-help">如果您点击“付款”按钮,即表示您同意该次的执行操作。</span>
                </dd>
            </dl>
        </div>
    </form>

    <div id="foot">
        <ul class="foot-ul">
            <li>版权所有 2015-2018</li>
        </ul>
    </div>
</div>
</body>
<script language="javascript">
    function GetDateNow() {
        var vNow = new Date();
        var sNow = "";
        sNow += String(vNow.getFullYear());
        sNow += String(vNow.getMonth() + 1);
        sNow += String(vNow.getDate());
        sNow += String(vNow.getHours());
        sNow += String(vNow.getMinutes());
        sNow += String(vNow.getSeconds());
        sNow += String(vNow.getMilliseconds());
        document.getElementById("WIDout_trade_no").value =  sNow;
        document.getElementById("WIDsubject").value = "测试商品";
        document.getElementById("WIDtotal_amount").value = "2499";
    }
    GetDateNow();
</script>
</html>

然后就可以通过支付宝进行付款操作了 ,非常的方便,非常的银杏。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇