Eclipse、Springboot支付宝接口的使用(沙箱环境)

在开放平台文档中心
https://docs.open.alipay.com/270/106291/
下载demo,我下载的是Java版的,一定要用eclipse打开,如果要用idea的话可以到时候再放过去
在这里插入图片描述
导入之前应该将eclipse中设置一下UTF-8

在这里插入图片描述在eclipse中创建新的项目
在这里插入图片描述
在这里插入图片描述
将如下文件直接复制进去
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

运行后如下
在这里插入图片描述
登录支付宝-开发者中心-研发服务-沙箱应用在这里插入图片描述
设置公钥(可能跟我的不太一样,因为是已经设置过了才截的图)在这里插入图片描述
下载生成公钥的工具(点击设置会有提示下载)
在这里插入图片描述
运行 RSA签名验签工具.bat
在这里插入图片描述
在这里插入图片描述
复制公钥到应用公钥处,会生成相应的支付宝公钥
在这里插入图片描述在这里插入图片描述
将1-4填到Demo中AlipayConfig的对应地方去
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
注意:代码中的签名方式 sign_type,是哪种加密方式就选哪种,我是用RSA2就写了RSA2在这里插入图片描述
在沙箱工具中下载这个工具

在这里插入图片描述
登录买家账号
在这里插入图片描述
运行修改过后的Demo,点击付款后扫码,会弹到支付页面,扫码支付成功后跳到return_url
在这里插入图片描述
在沙箱账号-商家信息中账户余额多了0.03,买家信息中账户余额少了0.03
在这里插入图片描述
在这里插入图片描述

导入源码
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

**

接下来是springboot版

**

页面代码:

<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <script th:src="@{http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js}"></script>
    <script th:src="@{http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js}"></script>
    <link rel="stylesheet" th:href="@{http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css}">
    <title>pay</title>
</head>
<body>
<form th:action="@{/pay}" method="post" accept-charset="UTF-8">
    订单号:<input type="text" name="bianhao" required><br/>
    订单名称:<input type="text" name="mingcheng" required><br/>
    付款金额:<input type="text" name="jiage" required><br/>
        商品描述:<input type="text" name="miaoshu"><br/>
    <input type="submit" value="下单"> <input type="reset" value="重置">
</form>

</body>
</html>

订单号和订单名称、付款金额都是必须要有的,商品描述选填

配置按照Demo里面来即可,除了个人信息外没有改动过

Controller里面的标黄色字段是对应页面中的
在这里插入图片描述



import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hstc.config.AlipayConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class PayController {

    @RequestMapping("/index")
    public ModelAndView toTest() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }

    @RequestMapping("/pay")
    public void payController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得初始化的AlipayClient

        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, 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(request.getParameter("bianhao").getBytes("ISO-8859-1"), "UTF-8");
        //付款金额,必填
        String total_amount = new String(request.getParameter("jiage").getBytes("ISO-8859-1"), "UTF-8");
        //订单名称,必填
        String subject = new String(request.getParameter("mingcheng").getBytes("ISO-8859-1"), "UTF-8");
        //商品描述,可空
        String body = new String(request.getParameter("miaoshu").getBytes("ISO-8859-1"), "UTF-8");

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + AlipayConfig.charset);
        response.getWriter().write(form);//直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }
}


如果不理解response.getWriter().write(form);的话可以看看下面的

前端项目和response.getWriter().write(xxxx)的理解 - 从小就很酷的博客 - CSDN博客 https://blog.csdn.net/weixin_39669410/article/details/89244169

如果不理解response.getWriter().flush();response.getWriter().close();的话可以看看下面的

JAVA中 write()方法后调用flush()方法的作用 - RainSwear的博客 - CSDN博客 https://blog.csdn.net/qq_35271409/article/details/82057096

运行后结果如下
在这里插入图片描述
填入数据点击下单后跳转
在这里插入图片描述
在这里插入图片描述

**

完成。

**

一些关于Demo里面notify_url.jsp的相关知识
在这里插入图片描述
TRADE_FINISHED是超过退款日期后,交易完成
TRADE_SUCCESS是支付成功但是有可能会退款,所以不代表交易完成

**

交易查询

**
html(来自Demo中的index.html)

  <form name=tradequery  th:action="@{/query}" method=post
          target="_blank">
        <div id="body2" class="tab-content" name="divcontent">
            <dl class="content">
                <dt>商户订单号 :</dt>
                <dd>
                    <input id="WIDTQout_trade_no" name="moid" />
                </dd>
                <hr class="one_line">
                <dt>支付宝交易号 :</dt>
                <dd>
                    <input id="WIDTQtrade_no" name="WIDTQtrade_no" />
                </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>

controller(来自Demo中的alipay.trade.query.jsp)

@RequestMapping("/query")
    public void queryController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, 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(request.getParameter("moid").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号
        String trade_no = new String(request.getParameter("WIDTQtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置

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

        //请求
        String form = "";
        try {
            form =alipayClient.execute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
//        response.setContentType("text/html;charset=" + AlipayConfig.charset);
//        response.getWriter().write(form);//直接将完整的表单html输出到页面
//        response.getWriter().flush();
//        response.getWriter().close();

        System.out.println(form);

    }

输入订单号或者交易号后在这里插入图片描述
控制台会输出信息(这里用了HiJson格式化Json字符串)
在这里插入图片描述
**

退款

**
html

		<form name=traderefund action=alipay.trade.refund.jsp method=post
			target="_blank">
			<div id="body3" class="tab-content" name="divcontent">
				<dl class="content">
					<dt>商户订单号 :</dt>
					<dd>
						<input id="WIDTRout_trade_no" name="WIDTRout_trade_no" />
					</dd>
					<hr class="one_line">
					<dt>支付宝交易号 :</dt>
					<dd>
						<input id="WIDTRtrade_no" name="WIDTRtrade_no" />
					</dd>
					<hr class="one_line">
					<dt>退款金额 :</dt>
					<dd>
						<input id="WIDTRrefund_amount" name="WIDTRrefund_amount" />
					</dd>
					<hr class="one_line">
					<dt>退款原因 :</dt>
					<dd>
						<input id="WIDTRrefund_reason" name="WIDTRrefund_reason" />
					</dd>
					<hr class="one_line">
					<dt>退款请求号 :</dt>
					<dd>
						<input id="WIDTRout_request_no" name="WIDTRout_request_no" />
					</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>

controller(来自alipay.trade.refund.jsp)

 @RequestMapping("/refund")
    public void refundController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, 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(request.getParameter("WIDTRout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号
        String trade_no = new String(request.getParameter("WIDTRtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置
        //需要退款的金额,该金额不能大于订单金额,必填
        String refund_amount = new String(request.getParameter("WIDTRrefund_amount").getBytes("ISO-8859-1"),"UTF-8");
        //退款的原因说明
        String refund_reason = new String(request.getParameter("WIDTRrefund_reason").getBytes("ISO-8859-1"),"UTF-8");
        //标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
        String out_request_no = new String(request.getParameter("WIDTRout_request_no").getBytes("ISO-8859-1"),"UTF-8");

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

        //请求
        String form = "";
        try {
            form = alipayClient.execute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
//        response.setContentType("text/html;charset=" + AlipayConfig.charset);
//        response.getWriter().write(form);//直接将完整的表单html输出到页面
//        response.getWriter().flush();
//        response.getWriter().close();

        System.out.println(form);

    }

输入订单号退款后在这里插入图片描述
控制台会输出信息

在这里插入图片描述
**

退款查询

**
html

    <form name=traderefundquery
          action=th:action="@{/refundQuery}" method=post
          target="_blank">
        <div id="body4" class="tab-content" name="divcontent">
            <dl class="content">
                <dt>商户订单号 :</dt>
                <dd>
                    <input id="WIDRQout_trade_no" name="WIDRQout_trade_no" />
                </dd>
                <hr class="one_line">
                <dt>支付宝交易号 :</dt>
                <dd>
                    <input id="WIDRQtrade_no" name="WIDRQtrade_no" />
                </dd>
                <hr class="one_line">
                <dt>退款请求号 :</dt>
                <dd>
                    <input id="WIDRQout_request_no" name="WIDRQout_request_no" />
                </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>

controller

    @RequestMapping("/refundQuery")
    public void refundQueryController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, 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(request.getParameter("WIDRQout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号
        String trade_no = new String(request.getParameter("WIDRQtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置
        //请求退款接口时,传入的退款请求号,如果在退款请求时未传入,则该值为创建交易时的外部交易号,必填
        String out_request_no = new String(request.getParameter("WIDRQout_request_no").getBytes("ISO-8859-1"),"UTF-8");

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

        //请求
        String form = "";
        try {
            form = alipayClient.execute(alipayRequest).getBody();//调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
//        response.setContentType("text/html;charset=" + AlipayConfig.charset);
//        response.getWriter().write(form);//直接将完整的表单html输出到页面
//        response.getWriter().flush();
//        response.getWriter().close();

        System.out.println(form);

    }

输入订单号后
在这里插入图片描述
控制台会输出信息(这是错的!!!)
在这里插入图片描述
然后发现一定要包含退款请求号才能查询成功,如下(我的退款请求号是123,是在退款的时候就要写上,查询的时候才能查成功)
在这里插入图片描述
注意:退款和退款查询所用的订单号等是从手机沙箱版支付宝里面-我的-账单-里面查到的,退款后也有相应的提示
在这里插入图片描述

还有一个交易关闭功能也是类似,以后有空再整理

以上就是支付宝接口的教程,代码支付宝的Demo里面全部都有,如果没有贴完全的话可以自行去下载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值