1:在擼代碼之前 先去開通支付寶支付的賬戶 提交私鑰 公鑰一系列反鎖的 事情 下面簡單介紹一下我的支付過程
以下是整個項目的結構 只是做了個支付的測試 所有結構很簡單 大神勿噴:
上面的 lib裏面的 jar 大部分都可以在 支付寶的官方dome裏面下載
當然 在寫完文章 我會附上我的源碼地址: 在這裏 主要貼出 兩個主要類的 代碼 和 web.mlx 的配置 還index.html的按鈕
CsPay.Java
- public class CsPay extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- // 參數目前都是 寫死的 根據業務需求 寫活
- Map<String, String> maps = new HashMap<String, String>();
- maps.put("out_trade_no", UtilDate.getOrderNum());
- maps.put("total_amount", "0.01");
- maps.put("subject", "Iphone6 16G");
- maps.put("body", "Iphone6 16G");
- maps.put("product_code", "QUICK_WAP_PAY");
- // 下面兩個 參數的 KEY 不要亂寫 要和工具類裏面對應
- maps.put("ReturnUrl", "http://domain.com/CallBack/return_url.jsp");
- maps.put("NotifyUrl", "http://domain.com/CallBack/notify_url.jsp");
- try {
- AlipayClientFactory ali = new AlipayClientFactory();
- String form = ali.ydAndPc_Pay(maps);
- if (!form.equals("err")) {
- response.setContentType("text/html;charset=utf-8");
- response.getWriter().write(form);// 直接將完整的表單html輸出到頁面
- response.getWriter().flush();
- }
- } catch (AlipayApiException e) {
- e.printStackTrace();
- }
- }
- }
調用 支付工具類 AlipayClientFactory.java :裏面包含 支付 訂單查詢 訂單退款 掃描支付 等等 ...
我這裏只貼 支付那一塊代碼 想看全的 待會貼上下載地址:
- // 手機網頁支付 網站支付
- public String ydAndPc_Pay(Map<String, String> maps)
- throws AlipayApiException {
- AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
- String NotifyUrl = maps.get("NotifyUrl");
- String ReturnUrl = maps.get("ReturnUrl");
- // 後臺回調
- if (!StringUtils.isEmpty(NotifyUrl)) {
- alipayRequest.setNotifyUrl(NotifyUrl);
- // bizContent 中不需要 公共參數
- maps.remove("NotifyUrl");
- }
- // 頁面回調
- if (!StringUtils.isEmpty(ReturnUrl)) {
- alipayRequest.setReturnUrl(ReturnUrl);
- // bizContent 中不需要 公共參數
- maps.remove("ReturnUrl");
- }
- String bizCon = JSON.toJSONString(maps);
- alipayRequest.setBizContent(bizCon);
- String form = "";
- try {
- form = AlipayClientFactory.getAlipayClientInstance()
- .pageExecute(alipayRequest).getBody();
- } catch (AlipayApiException e) {
- form = "err";
- e.printStackTrace();
- } // 調用SDK生成表單
- return form;
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>ACPSample_WuTiaoZhuan</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>csPay</servlet-name>
- <servlet-class>com.cs.alipay.CsPay</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>csPay</servlet-name>
- <url-pattern>/csPay</url-pattern>
- </servlet-mapping>
- </web-app>
index.html:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <a href="http://localhost:8080/csalipay/csPay">AAAAA</a>
- </body>
下面是 點擊按鈕 手機端調支付的效果圖:
下面是 PC端 調用支付的效果:
一下 是點擊跳轉過後的頁面: 支付寶自定義組裝返回的支付頁面:
最後 附上源碼的下載地址:http://download.csdn.NET/detail/wangbo54979/9630419
新手發帖 大神勿噴