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;
}
web.xml 配置:
<?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
新手发帖 大神勿喷