1.首先要到 螞蟻金服開發者中心 https://openhome.alipay.com/platform/home.htm 註冊商家帳戶。html
2.開發者認證,並利用RSARSA簽名驗籤工具生成公鑰、私鑰。java
3.pom配置文件com.alipay.sdk依賴引入。web
4.沙箱環境的接入引導(因爲是沙箱測試環境,用手機掃碼支付還須要下載沙箱版支付寶APP)。spring
5.Alipay配置類與支付接口開發。數據庫
商家帳戶註冊和開發者認證在這就很少說了,你們也能夠百度下,就是用你的支付寶帳號掃碼或者帳號登陸,完善我的信息,可選擇服務類型(本人選的是自研),註冊後可能須要等一個小時進行審覈,express
但不影響使用沙箱環境進行測試,圖片下方有生成RSA生成密鑰官方連接。json
生成密鑰的官網文檔:https://docs.open.alipay.com/291/105971
官方文檔講得已經很詳細,按照步驟來便可,記得保存好公鑰與私鑰,下面須要用到。api
<dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>3.0.0</version> </dependency>
1.登陸後點擊開發中心選擇研發服務。服務器
2.下圖爲沙箱應用的信息app
沙箱版APP下載
3.沙箱測試帳號登陸信息,下面是虛擬帳號餘額可隨意充值。
1.在config文件下建立AlipayConfig配置類。
package com.app.config; import java.io.FileWriter; import java.io.IOException; /** * Created on 2019-9-09 */ public class AlipayConfig { // 應用ID,您的APPID,收款帳號既是您的APPID對應支付寶帳號,開發時使用沙箱提供的APPID,生產環境改爲本身的APPID public static String APP_ID = "你的沙箱APPID"; //測試 // 商戶私鑰,您的PKCS8格式RSA2私鑰 public static String APP_PRIVATE_KEY = "在上面第一步生成的RSA私鑰"; // 支付寶公鑰,查看地址:https://openhome.alipay.com/platform/keyManage.htm 對應APPID下的支付寶公鑰。 public static String ALIPAY_PUBLIC_KEY = "在沙箱應用中對應的支付寶公鑰"; // 服務器異步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義參數,必須外網能夠正常訪問 public static String notify_url = "http://localhost:8080/Alipay/notifyUrl"; // 頁面跳轉同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義參數,必須外網能夠正常訪問(其實就是支付成功後返回的頁面) public static String return_url = "http://localhost:8080/Alipay/returnUrl"; // 簽名方式 public static String sign_type = "RSA2"; // 字符編碼格式 public static String CHARSET = "UTF-8"; // 支付寶網關,這是沙箱的網關 public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do"; //測試 // 支付寶網關 public static String log_path = "E:\\"; /** * 寫日誌,方便測試(看網站需求,也能夠改爲把記錄存入數據庫) * @param sWord 要寫入日誌裏的文本內容 */ public static void logResult(String sWord) { FileWriter writer = null; try { writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis() + ".txt"); writer.write(sWord); } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
2.後臺代碼
package com.app.controller; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.internal.util.AlipaySignature; import com.alipay.api.request.AlipayTradePagePayRequest; import com.app.config.AlipayConfig; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * Created by on 2019-9-09. */ @Controller @RequestMapping(value="/Alipay") public class AlipayController { /** * 購物車 * * @return */ @RequestMapping(value = "/Shopcart") public String Shopcart() { return "pay"; } /** * 結算界面 * @return */ @ResponseBody @RequestMapping(value = "/PayPage") public String payController (HttpServletRequest request, HttpServletResponse response) throws IOException { //得到初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_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 = request.getParameter("Order"); //付款金額,必填 ShopName String total_amount = request.getParameter("Money"); //訂單名稱,必填 String subject = request.getParameter("Name"); //商品描述,可空 String body =request.getParameter("購物測試"); // 該筆訂單容許的最晚付款時間,逾期將關閉交易。取值範圍:1m~15d。m-分鐘,h-小時,d-天,1c-當天(1c-當天的狀況下,不管交易什麼時候建立,都在0點關閉)。 該參數數值不接受小數點, 如 1.5h,可轉換爲 90m。 String timeout_express = "1c"; alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\"," + "\"timeout_express\":\""+ timeout_express +"\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); //請求 String url = ""; try { url = alipayClient.pageExecute(alipayRequest).getBody(); //調用SDK生成表單 /* response.setContentType("text/html;charset=" + AlipayConfig.CHARSET); response.getWriter().write(url); // 直接將完整的表單html輸出到頁面 response.getWriter().flush(); response.getWriter().close();*/ } catch (AlipayApiException e) { e.printStackTrace(); } System.out.println(url); return url; } /** * 同步跳轉 * * @param request * @throws Exception */ @ResponseBody @RequestMapping("/returnUrl") public ModelAndView returnUrl(HttpServletRequest request) throws Exception { ModelAndView mav = new ModelAndView(); // 獲取支付寶GET過來反饋信息(官方固定代碼) Map<String, String> params = new HashMap<String, String>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String[] values = (String[]) requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } params.put(name, valueStr); } boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.sign_type); // 調用SDK驗證簽名 // 返回界面 if (signVerified) { System.out.println("前往支付成功頁面"); mav.setViewName("Shopcart"); } else { System.out.println("前往支付失敗頁面"); mav.setViewName("failReturn"); } return mav; } /** * 支付寶服務器異步通知 * * @param request * @throws Exception */ @ResponseBody @RequestMapping("/notifyUrl") public void notifyUrl(HttpServletRequest request) throws Exception { // 獲取支付寶GET過來反饋信息 Map<String, String> params = new HashMap<String, String>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String[] values = (String[]) requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } params.put(name, valueStr); } boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.sign_type); // 調用SDK驗證簽名 if (signVerified) { // 驗證成功 更新訂單信息 System.out.println("異步通知成功"); // 商戶訂單號 String out_trade_no = request.getParameter("out_trade_no"); // 交易狀態 String trade_status = request.getParameter("trade_status"); // 修改數據庫 } else { System.out.println("異步通知失敗"); } } }
3.前臺頁面html(頁面比較lo,你們測試作個參考)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>沙箱下單測試</title> </head> <body> <div> <form action="http://localhost:8080/Alipay/PayPage" method="post"> <div> 訂單號:<input name="Order" value="20592155879901" ></input> </div> <div> 商品名稱:<input name="Name" value="筆記本電腦"></input> </div> <div> 金額:<input name="Money" value="99.99"></input>元 </div> <input type="submit" value="提交訂單"></input> </form> </div> </body> </html>
4.測試效果
網頁輸入:locahost:8080/Alipay/Shopcart
點擊提交訂單後會自動跳轉到支付界面,可用沙箱版支付寶APP掃描付款,也能夠直接登陸付款
沙箱帳戶支付效果
若是須要使用正式環境,只須要更換下正式APPID,和正式支付寶網關就好了.......
可是正式簽約好像還須要經營註冊和上傳營業執照,你們感興趣能夠試下...