1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Eclipse Springboot支付宝接口的使用(沙箱环境)

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

时间:2022-12-30 16:27:05

相关推荐

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

在开放平台文档中心

https://docs./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=""><head><meta charset="UTF-8"><script th:src="@{http://cdn./libs/jquery/2.1.1/jquery.min.js}"></script><script th:src="@{http://cdn./libs/bootstrap/3.3.7/js/bootstrap.min.js}"></script><link rel="stylesheet" th:href="@{http://cdn./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;@RestControllerpublic 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 {//获得初始化的AlipayClientAlipayClient 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博客 /weixin_39669410/article/details/89244169

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

JAVA中 write()方法后调用flush()方法的作用 - RainSwear的博客 - CSDN博客 /qq_35271409/article/details/82057096

运行后结果如下

填入数据点击下单后跳转

**

完成。

**

一些关于Demo里面notify_url.jsp的相关知识

TRADE_FINISHED是超过退款日期后,交易完成

TRADE_SUCCESS是支付成功但是有可能会退款,所以不代表交易完成

**

交易查询

**

html(来自Demo中的index.html)

<form name=tradequery th:action="@{/query}" method=posttarget="_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 {//获得初始化的AlipayClientAlipayClient 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=posttarget="_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 {//获得初始化的AlipayClientAlipayClient 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=traderefundqueryaction=th:action="@{/refundQuery}" method=posttarget="_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 {//获得初始化的AlipayClientAlipayClient 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里面全部都有,如果没有贴完全的话可以自行去下载。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。