1.注冊一個沙箱号 網站https://open.alipay.com/platform/home.htm
使用真的支付寶掃描 進去 注冊 沙箱支付寶
2.使用支付寶的秘鑰生成工具生成個人的私鑰,附位址:https://opendocs.alipay.com/open/291/sign
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN2XjlGcjAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL4NGVPFTRU10dRpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLxYTN4UTMyETM0ATMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
3.将秘鑰填入支付寶沙箱中,附位址:https://openhome.alipay.com/platform/appDaily.htm?tab=info
4.程式設計實作,使用ssm,若需要找maven依賴可在https://mvnrepository.com/找不同版本的依賴
5.pom.xml中引入支付寶依賴(使用ssm也要引入spring、spring與web整合的maven依賴)
6.controller
@Controller
public class PayController {
private final String APP_ID = "應用的APPID";
private final String APP_PRIVATE_KEY = "生成的應用私鑰";
private final String CHARSET = "UTF-8";
private final String ALIPAY_PUBLIC_KEY = "應用私鑰";
//這是沙箱接口路徑,正式路徑為https://openapi.alipay.com/gateway.do
private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
private final String FORMAT = "JSON";
//簽名方式
private final String SIGN_TYPE = "RSA2";
//支付寶異步通知路徑,付款完畢後會異步調用本項目的方法,必須為公網位址
private final String NOTIFY_URL = "http://1270..0.1/notifyUrl";
//支付寶同步通知路徑,也就是當付款完畢後跳轉本項目的頁面,可以不是公網位址
private final String RETURN_URL = "http://1270..0.1/returnUrl";
@RequestMapping("/pay")
public void alipay(HttpServletResponse httpResponse) throws IOException {
System.out.println("進入了pay");
Random r=new Random();
//執行個體化用戶端,填入所需參數
AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//在公共參數中設定回跳和通知位址
request.setReturnUrl(RETURN_URL);
request.setNotifyUrl(NOTIFY_URL);
//商戶訂單号,商戶網站訂單系統中唯一訂單号,必填
//生成随機Id
String out_trade_no = UUID.randomUUID().toString();
//付款金額,必填
String total_amount =Integer.toString(r.nextInt(100)+100);
//訂單名稱,必填
String subject ="奧迪A8 2016款 A8L 60 TFSl quattro豪華型";
//商品描述,可空
String body = "尊敬的會員歡迎購買奧迪A8 2016款 A8L 60 TFSl quattro豪華型";
request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
String form = "";
try {
form = alipayClient.pageExecute(request).getBody(); // 調用SDK生成表單
} catch (AlipayApiException e) {
e.printStackTrace();
}
httpResponse.setContentType("text/html;charset=" + CHARSET);
httpResponse.getWriter().write(form);// 直接将完整的表單html輸出到頁面
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
}
@RequestMapping(value = "/returnUrl", method = RequestMethod.GET)
public String returnUrl(HttpServletRequest request, HttpServletResponse response)
throws IOException, AlipayApiException {
System.out.println("=================================同步回調=====================================");
// 擷取支付寶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] + ",";
}
// 亂碼解決,這段代碼在出現亂碼時使用
valueStr = new String(valueStr.getBytes("utf-8"), "utf-8");
params.put(name, valueStr);
}
System.out.println(params);//檢視參數都有哪些
boolean signVerified = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET, SIGN_TYPE); // 調用SDK驗證簽名
//驗證簽名通過
if (signVerified) {
// 商戶訂單号
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
// 支付寶交易号
String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
// 付款金額
String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
System.out.println("商戶訂單号=" + out_trade_no);
System.out.println("支付寶交易号=" + trade_no);
System.out.println("付款金額=" + total_amount);
//支付成功,修複支付狀态
// payService.updateById(Integer.valueOf(out_trade_no));
return "ok";//跳轉付款成功頁面
} else {
return "no";//跳轉付款失敗頁面
}
}
}
7.使用ssm需要配置前端控制器,在web.xml中
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置全局參數:spring配置檔案的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
8.配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--使用注解:開啟注解掃描包-->
<context:component-scan base-package="com.igeek.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
</beans>
8.完成後可直接開啟tomcat,網頁中進行使用http://localhost:8888/alipayDemo/pay,可作為頁面跳轉寫到項目中