想要做支付宝网站支付,首先必须要有企业级商户号,并且成功签约,得到PID和key
controller就简单的写一下,异步回调就不写了,页面在后面
@RequestMapping(value = "/deposit", method = RequestMethod.POST,produces = "application/json")
public String deposit(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception {
Date date = new Date();
// 支付类型
// 必填,不能修改
String payment_type = "1";
//订单编号,必填
String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//订单名称,必填
String subject = new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
//付款金额,必填
String total_fee = new String(request.getParameter("WIDtotal_fee").getBytes("ISO-8859-1"),"UTF-8");
//收银台页面上,商品展示的超链接,必填
String show_url = new String(request.getParameter("WIDshow_url").getBytes("ISO-8859-1"),"UTF-8");
//商品描述,可空
String body = new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8");
Map<String, String> sParaTemp = new HashMap<String, String>();
sParaTemp.put("service", AlipayConfig.service);
sParaTemp.put("partner", AlipayConfig.partner);
sParaTemp.put("seller_id", AlipayConfig.seller_id);
sParaTemp.put("_input_charset", AlipayConfig.input_charset);
sParaTemp.put("payment_type", AlipayConfig.payment_type);
sParaTemp.put("notify_url", AlipayConfig.notify_url);
sParaTemp.put("return_url", AlipayConfig.return_url);
sParaTemp.put("out_trade_no", AlipayConfig.out_trade_no);
sParaTemp.put("subject", subject);
sParaTemp.put("total_fee", total_fee);
sParaTemp.put("show_url", show_url);
//sParaTemp.put("app_pay","Y");//启用此参数可唤起钱包APP支付。
sParaTemp.put("body", body);
//建立请求
try {
String sHtmlText = AlipaySubmit.buildRequest(sParaTemp,"get","确认");
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String s = gson.toJson(sHtmlText);
model.addAttribute("sHtmlText", s);
request.setAttribute("sHtmlText", s);
result = "{\"success\":true,\"msg\":\"跳转成功\"}";
writeToWeb(sHtmlText, "html", response);
return null;
} catch (Exception e) {
result = "{\"success\":false,\"msg\":\"跳转失败,请稍候再试!\"}";
writeToWeb(result, "html", response);
return null;
}
}
/**
* 同步通知的页面的Controller
* @param request
* @param response
* @return
* @author
*/
@RequestMapping(value="/return_url")
public String Return_url(HttpServletRequest request,HttpServletResponse response) throws Exception {
return "/success.jsp";
}
public static void writeToWeb(String message, String type, HttpServletResponse response) throws IOException{
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/" + type +"; charset=utf-8");
response.getWriter().write(message);
response.getWriter().close();
}
AlipayConfig.java ,MD5.java,AlipaySubmit.java等class文件可在蚂蚁金服demo里直接导下文件,至于AlipayConfig.java 需要PID和key配置
蚂蚁金服下载手机网站支付demo:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7386797.0.0.RzxgFl&treeId=60&articleId=103564&docType=1
这个demo是可以直接运行的,配置好pid和key的值是可以直接跑通的。
测试页面index.jsp
<!DOCTYPE html>
<html>
<head>
<title>支付宝手机网站支付接口</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
*{
margin:0;
padding:0;
}
ul,ol{
list-style:none;
}
body{
font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
}
.hidden{
display:none;
}
.new-btn-login-sp{
padding: 1px;
display: inline-block;
width: 75%;
}
.new-btn-login {
background-color: #02aaf1;
color: #FFFFFF;
font-weight: bold;
border: none;
width: 100%;
height: 30px;
border-radius: 5px;
font-size: 16px;
}
#main{
width:100%;
margin:0 auto;
font-size:14px;
}
.red-star{
color:#f00;
width:10px;
display:inline-block;
}
.null-star{
color:#fff;
}
.content{
margin-top:5px;
}
.content dt{
width:100px;
display:inline-block;
float: left;
margin-left: 20px;
color: #666;
font-size: 13px;
margin-top: 8px;
}
.content dd{
margin-left:120px;
margin-bottom:5px;
}
.content dd input {
width: 85%;
height: 28px;
border: 0;
-webkit-border-radius: 0;
-webkit-appearance: none;
}
#foot{
margin-top:10px;
position: absolute;
bottom: 15px;
width: 100%;
}
.foot-ul{
width: 100%;
}
.foot-ul li {
width: 100%;
text-align:center;
color: #666;
}
.note-help {
color: #999999;
font-size: 12px;
line-height: 130%;
margin-top: 5px;
width: 100%;
display: block;
}
#btn-dd{
margin: 20px;
text-align: center;
}
.foot-ul{
width: 100%;
}
.one_line{
display: block;
height: 1px;
border: 0;
border-top: 1px solid #eeeeee;
width: 100%;
margin-left: 20px;
}
.am-header {
display: -webkit-box;
display: -ms-flexbox;
display: box;
width: 100%;
position: relative;
padding: 7px 0;
-webkit-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
background: #1D222D;
height: 50px;
text-align: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
box-pack: center;
-webkit-box-align: center;
-ms-flex-align: center;
box-align: center;
}
.am-header h1 {
-webkit-box-flex: 1;
-ms-flex: 1;
box-flex: 1;
line-height: 18px;
text-align: center;
font-size: 18px;
font-weight: 300;
color: #fff;
}
</style>
</head>
<body text=#000000 bgColor="#ffffff" leftMargin=0 topMargin=4>
<header class="am-header">
<h1>支付宝手机网站支付接口快速通道</h1>
</header>
<div id="main">
<form name=alipayment action="/deposit" method=post target="_blank">
<div id="body" style="clear:left">
<dl class="content">
<dt>商户订单号
:</dt>
<dd>
<input name="WIDout_trade_no" id="WIDout_trade_no" />
</dd>
<hr class="one_line">
<dt>订单名称
:</dt>
<dd>
<input name="WIDsubject" id="WIDsubject" />
</dd>
<hr class="one_line">
<dt>付款金额
:</dt>
<dd>
<input name="WIDtotal_fee" id="WIDtotal_fee" />
</dd>
<hr class="one_line">
<dt>商品展示网址
:</dt>
<dd>
<input name="WIDshow_url" id="WIDshow_url" />
</dd>
<hr class="one_line">
<dt>商品描述:</dt>
<dd>
<input name="WIDbody" id="WIDbody" />
</dd>
<hr class="one_line">
<dt></dt>
<dd id="btn-dd">
<span class="new-btn-login-sp">
<button class="new-btn-login" type="submit" style="text-align:center;">确 认</button>
</span>
<span class="note-help">如果您点击“确认”按钮,即表示您同意该次的执行操作。</span>
</dd>
</dl>
</div>
</form>
<div id="foot">
<ul class="foot-ul">
<li>
支付宝版权所有 2015-2018 ALIPAY.COM
</li>
</ul>
</div>
</div>
</body>
<script language="javascript">
function GetDateNow() {
var vNow = new Date();
var sNow = "";
sNow += String(vNow.getFullYear());
sNow += String(vNow.getMonth() + 1);
sNow += String(vNow.getDate());
sNow += String(vNow.getHours());
sNow += String(vNow.getMinutes());
sNow += String(vNow.getSeconds());
sNow += String(vNow.getMilliseconds());
document.getElementById("WIDout_trade_no").value = sNow;
document.getElementById("WIDsubject").value = "测试";
document.getElementById("WIDtotal_fee").value = "0.01";
}
GetDateNow();
</script>
</html>
只要是pid和key是存在并且无误的,这个就可以运行,我本人也是刚刚测试完。