java支付宝支付,支付手机支付,pc网站支付

1:在撸代码之前 先去开通支付宝支付的账户  提交私钥 公钥一系列反锁的 事情 下面简单介绍一下我的支付过程

以下是整个项目的结构 只是做了个支付的测试 所有结构很简单 大神勿喷:


上面的 lib里面的 jar 大部分都可以在 支付宝的官方dome里面下载  

当然 在写完文章 我会附上我的源码地址:  在这里 主要贴出  两个主要类的 代码 和  web.mlx 的配置 还index.html的按钮

CsPay.Java  

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class CsPay extends HttpServlet {  
  2.     private static final long serialVersionUID = 1L;  
  3.     protected void doGet(HttpServletRequest request,  
  4.             HttpServletResponse response) throws ServletException, IOException {  
  5.         doPost(request, response);  
  6.     }  
  7.     protected void doPost(HttpServletRequest request,  
  8.             HttpServletResponse response) throws ServletException, IOException {  
  9.         // 参数目前都是 写死的 根据业务需求 写活  
  10.         Map<String, String> maps = new HashMap<String, String>();  
  11.         maps.put("out_trade_no", UtilDate.getOrderNum());  
  12.         maps.put("total_amount""0.01");  
  13.         maps.put("subject""Iphone6 16G");  
  14.         maps.put("body""Iphone6 16G");  
  15.         maps.put("product_code""QUICK_WAP_PAY");  
  16.         // 下面两个 参数的 KEY 不要乱写 要和工具类里面对应  
  17.         maps.put("ReturnUrl""http://domain.com/CallBack/return_url.jsp");  
  18.         maps.put("NotifyUrl""http://domain.com/CallBack/notify_url.jsp");  
  19.         try {  
  20.             AlipayClientFactory ali = new AlipayClientFactory();  
  21.             String form = ali.ydAndPc_Pay(maps);  
  22.             if (!form.equals("err")) {  
  23.                 response.setContentType("text/html;charset=utf-8");  
  24.                 response.getWriter().write(form);// 直接将完整的表单html输出到页面  
  25.                 response.getWriter().flush();  
  26.             }  
  27.         } catch (AlipayApiException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31. }  

调用 支付工具类 AlipayClientFactory.java :里面包含 支付 订单查询  订单退款  扫描支付 等等 ...

我这里只贴 支付那一块代码  想看全的 待会贴上下载地址:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. // 手机网页支付 网站支付  
  2. public String ydAndPc_Pay(Map<String, String> maps)  
  3.         throws AlipayApiException {  
  4.     AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();  
  5.     String NotifyUrl = maps.get("NotifyUrl");  
  6.     String ReturnUrl = maps.get("ReturnUrl");  
  7.     // 后台回调  
  8.     if (!StringUtils.isEmpty(NotifyUrl)) {  
  9.         alipayRequest.setNotifyUrl(NotifyUrl);  
  10.         // bizContent 中不需要 公共参数  
  11.         maps.remove("NotifyUrl");  
  12.     }  
  13.     // 页面回调  
  14.     if (!StringUtils.isEmpty(ReturnUrl)) {  
  15.         alipayRequest.setReturnUrl(ReturnUrl);  
  16.         // bizContent 中不需要 公共参数  
  17.         maps.remove("ReturnUrl");  
  18.     }  
  19.     String bizCon = JSON.toJSONString(maps);  
  20.     alipayRequest.setBizContent(bizCon);  
  21.     String form = "";  
  22.     try {  
  23.         form = AlipayClientFactory.getAlipayClientInstance()  
  24.                 .pageExecute(alipayRequest).getBody();  
  25.     } catch (AlipayApiException e) {  
  26.         form = "err";  
  27.         e.printStackTrace();  
  28.     } // 调用SDK生成表单  
  29.     return form;  
  30. }  


web.xml 配置:

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     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"  
  4.     id="WebApp_ID" version="2.5">  
  5.     <display-name>ACPSample_WuTiaoZhuan</display-name>  
  6.     <welcome-file-list>  
  7.         <welcome-file>index.html</welcome-file>  
  8.     </welcome-file-list>  
  9.     <servlet>  
  10.         <servlet-name>csPay</servlet-name>  
  11.         <servlet-class>com.cs.alipay.CsPay</servlet-class>  
  12.     </servlet>  
  13.     <servlet-mapping>  
  14.         <servlet-name>csPay</servlet-name>  
  15.         <url-pattern>/csPay</url-pattern>  
  16.     </servlet-mapping>  
  17. </web-app>  

index.html:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>Insert title here</title>  
  6. </head>  
  7. <body>  
  8.     <a href="http://localhost:8080/csalipay/csPay">AAAAA</a>  
  9. </body>  


下面是 点击按钮 手机端调支付的效果图:

下面是 PC端 调用支付的效果:

一下 是点击跳转过后的页面:  支付宝自定义组装返回的支付页面:


最后 附上源码的下载地址:http://download.csdn.NET/detail/wangbo54979/9630419

新手发帖 大神勿喷


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章