Java Springboot整合支付寶接口的教程詳解
一、創(chuàng)建支付寶沙箱
跳轉(zhuǎn) : 支付寶沙箱平臺
1、進入控制臺
2、創(chuàng)建小程序,編寫名稱和綁定商家即可
3、返回第一個頁面,往下滑進入沙箱
4、進行相關(guān)的配置,拿到AppID、應(yīng)用公鑰、應(yīng)用私鑰、支付寶公鑰
5、進入沙箱賬號先提前給虛擬賬號充值一些錢款(商家賬號和普通賬號)
二、使用內(nèi)網(wǎng)穿透 nat app
跳轉(zhuǎn) : nat app
1、注冊并登陸
這里不做展示
2、申請一個免費隧道
3、下載一個最新的客戶端
下載后解壓到不需要管理員權(quán)限的盤符,比如說E盤
4、打開所在目錄
5、在nat app中進行復制然后在創(chuàng)建的start.txt文件中進行修改
復制:
修改:
運行:
將start.txt修改為bat后運行得到網(wǎng)址
三、編寫java程序
1、引入依賴
<!-- 阿里支付--> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.22.110.ALL</version> </dependency> <!-- 糊涂工具類--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.11</version> </dependency>
2、編寫配置文件
## 支付寶配置 alipay: appId: 2021000122615995 appPrivateKey: 這是在第一步中的應(yīng)用私鑰,在查看里面,特別長的一串 alipayPublicKey: 這是第一步中的支付寶公鑰 notifyUrl: 這是第二步中運行start.bat后得到的網(wǎng)址 + /alipay/notify
3、編寫實體類(支付需要的參數(shù))
@Data public class AliPay { private String traceNo; // 訂單編號 private double totalAmount; // 總金額 private String subject; // 商品名稱 private String alipayTraceNo; }
4、編寫支付的配置類
@Data @Component @ConfigurationProperties(prefix = "alipay") public class AlipayConfig { // 對應(yīng)配置文件中的內(nèi)容 private String appId; private String appPrivateKey; private String alipayPublicKey; private String notifyUrl; }
5、編寫支付的Controller
@RestController @RequestMapping("/alipay") public class AliPayController { private static final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do"; private static final String FORMAT = "JSON"; private static final String CHARSET = "UTF-8"; //簽名方式 private static final String SIGN_TYPE = "RSA2"; @Resource private AlipayConfig aliPayConfig; @GetMapping("/pay") // &subject=xxx&traceNo=xxx&totalAmount=xxx public void pay(AliPay aliPay, HttpServletResponse httpResponse) throws Exception { // 1. 創(chuàng)建Client,通用SDK提供的Client,負責調(diào)用支付寶的API AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, aliPayConfig.getAppId(), aliPayConfig.getAppPrivateKey(), FORMAT, CHARSET, aliPayConfig.getAlipayPublicKey(), SIGN_TYPE); // 2. 創(chuàng)建 Request并設(shè)置Request參數(shù) AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); // 發(fā)送請求的 Request類 request.setNotifyUrl(aliPayConfig.getNotifyUrl()); JSONObject bizContent = new JSONObject(); bizContent.set("out_trade_no", aliPay.getTraceNo()); // 我們自己生成的訂單編號 bizContent.set("total_amount", aliPay.getTotalAmount()); // 訂單的總金額 bizContent.set("subject", aliPay.getSubject()); // 支付的名稱 bizContent.set("product_code", "FAST_INSTANT_TRADE_PAY"); // 固定配置 request.setBizContent(bizContent.toString()); // 執(zhí)行請求,拿到響應(yīng)的結(jié)果,返回給瀏覽器 String form = ""; try { form = alipayClient.pageExecute(request).getBody(); // 調(diào)用SDK生成表單 } catch (AlipayApiException e) { e.printStackTrace(); } httpResponse.setContentType("text/html;charset=" + CHARSET); httpResponse.getWriter().write(form);// 直接將完整的表單html輸出到頁面 httpResponse.getWriter().flush(); httpResponse.getWriter().close(); } @PostMapping("/notify") // 注意這里必須是POST接口 public String payNotify(HttpServletRequest request) throws Exception { if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) { System.out.println("=========支付寶異步回調(diào)========"); Map<String, String> params = new HashMap<>(); Map<String, String[]> requestParams = request.getParameterMap(); for (String name : requestParams.keySet()) { params.put(name, request.getParameter(name)); // System.out.println(name + " = " + request.getParameter(name)); } String outTradeNo = params.get("out_trade_no"); String gmtPayment = params.get("gmt_payment"); String alipayTradeNo = params.get("trade_no"); String sign = params.get("sign"); String content = AlipaySignature.getSignCheckContentV1(params); boolean checkSignature = AlipaySignature.rsa256CheckContent(content, sign, aliPayConfig.getAlipayPublicKey(), "UTF-8"); // 驗證簽名 // 支付寶驗簽 if (checkSignature) { // 驗簽通過 System.out.println("交易名稱: " + params.get("subject")); System.out.println("交易狀態(tài): " + params.get("trade_status")); System.out.println("支付寶交易憑證號: " + params.get("trade_no")); System.out.println("商戶訂單號: " + params.get("out_trade_no")); System.out.println("交易金額: " + params.get("total_amount")); System.out.println("買家在支付寶唯一id: " + params.get("buyer_id")); System.out.println("買家付款時間: " + params.get("gmt_payment")); System.out.println("買家付款金額: " + params.get("buyer_pay_amount")); } } return "success"; } }
四、訪問
http://localhost:8080/alipay/pay?subject=%E7%9B%B8%E6%9C%BA&traceNo=11111111&totalAmount=10
到此這篇關(guān)于Java Springboot整合支付寶接口的教程詳解的文章就介紹到這了,更多相關(guān)Java Springboot整合支付寶接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot項目中定制PropertyEditors方法
在本篇文章里小編給大家分享的是一篇關(guān)于Spring Boot定制PropertyEditors的知識點內(nèi)容,有需要的朋友們可以參考學習下。2019-11-11Mybatis中SqlSession下的四大對象之執(zhí)行器(executor)
mybatis中sqlsession下的四大對象是指:executor, statementHandler,parameterHandler,resultHandler對象。這篇文章主要介紹了Mybatis中SqlSession下的四大對象之執(zhí)行器(executor),需要的朋友可以參考下2019-04-04java發(fā)起http請求調(diào)用post與get接口的方法實例
在實際開發(fā)過程中,我們經(jīng)常需要調(diào)用對方提供的接口或測試自己寫的接口是否合適,下面這篇文章主要給大家介紹了關(guān)于java發(fā)起http請求調(diào)用post與get接口的相關(guān)資料,需要的朋友可以參考下2022-08-08使用apache 的FileUtils處理文件的復制等操作方式
這篇文章主要介紹了使用apache 的FileUtils處理文件的復制等操作方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07