======================================================================================================
AlipayFunction.java
======================================================================================================
AlipayNotify.java
-
package com.test.util.alipay;
-
-
import java.io.BufferedReader;
-
import java.io.InputStreamReader;
-
import java.net.HttpURLConnection;
-
import java.net.URL;
-
import java.util.Map;
-
-
import com.test.constants.AlipayConfig;
-
-
-
public class AlipayNotify {
-
-
-
-
-
-
-
public static String GetMysign(Map Params, String key){
-
Map sParaNew = AlipayFunction.ParaFilter(Params);
-
String mysign = AlipayFunction.BuildMysign(sParaNew, key);
-
-
return mysign;
-
}
-
-
-
-
-
-
-
-
-
-
-
public static String Verify(String notify_id){
-
-
String transport = AlipayConfig.transport;
-
String partner = AlipayConfig.partner;
-
String veryfy_url = "";
-
if(transport.equalsIgnoreCase("https")){
-
veryfy_url = "https://www.alipay.com/cooperate/gateway.do?service=notify_verify";
-
} else{
-
veryfy_url = "http://notify.alipay.com/trade/notify_query.do?";
-
}
-
veryfy_url = veryfy_url + "&partner=" + partner + "?ify_id=" + notify_id;
-
-
String responseTxt = CheckUrl(veryfy_url);
-
-
return responseTxt;
-
}
-
-
-
-
-
-
-
-
-
-
-
public static String CheckUrl(String urlvalue){
-
String inputLine = "";
-
-
try {
-
URL url = new URL(urlvalue);
-
HttpURLConnection urlConnection = (HttpURLConnection) url
-
.openConnection();
-
BufferedReader in = new BufferedReader(new InputStreamReader(
-
urlConnection.getInputStream()));
-
inputLine = in.readLine().toString();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
-
return inputLine;
-
}
-
}
======================================================================================================
AlipayService.java
-
package com.test.util.alipay;
-
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
-
public class AlipayService {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static String BuildForm(String partner,
-
String seller_email,
-
String return_url,
-
String notify_url,
-
String show_url,
-
String out_trade_no,
-
String subject,
-
String body,
-
String total_fee,
-
String paymethod,
-
String defaultbank,
-
String anti_phishing_key,
-
String exter_invoke_ip,
-
String extra_common_param,
-
String buyer_email,
-
String royalty_type,
-
String royalty_parameters,
-
String input_charset,
-
String key,
-
String sign_type,
-
String it_b_pay){
-
Map sPara = new HashMap();
-
sPara.put("service","create_direct_pay_by_user");
-
sPara.put("payment_type","1");
-
sPara.put("partner", partner);
-
sPara.put("seller_email", seller_email);
-
sPara.put("return_url", return_url);
-
sPara.put("notify_url", notify_url);
-
sPara.put("_input_charset", input_charset);
-
sPara.put("show_url", show_url);
-
sPara.put("out_trade_no", out_trade_no);
-
sPara.put("subject", subject);
-
sPara.put("body", body);
-
sPara.put("total_fee", total_fee);
-
sPara.put("paymethod", paymethod);
-
sPara.put("defaultbank", defaultbank);
-
sPara.put("anti_phishing_key", anti_phishing_key);
-
sPara.put("exter_invoke_ip", exter_invoke_ip);
-
sPara.put("extra_common_param", extra_common_param);
-
sPara.put("buyer_email", buyer_email);
-
sPara.put("royalty_type", royalty_type);
-
sPara.put("royalty_parameters", royalty_parameters);
-
sPara.put("it_b_pay", it_b_pay);
-
-
Map sParaNew = AlipayFunction.ParaFilter(sPara);
-
String mysign = AlipayFunction.BuildMysign(sParaNew, key);
-
-
StringBuffer sbHtml = new StringBuffer();
-
List keys = new ArrayList(sParaNew.keySet());
-
String gateway = "https://www.alipay.com/cooperate/gateway.do?";
-
-
-
-
-
sbHtml.append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + gateway + "_input_charset=" + input_charset + "\" method=\"post\">");
-
-
for (int i = 0; i < keys.size(); i++) {
-
String name = (String) keys.get(i);
-
String value = (String) sParaNew.get(name);
-
-
sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
-
}
-
sbHtml.append("<input type=\"hidden\" name=\"sign\" value=\"" + mysign + "\"/>");
-
sbHtml.append("<input type=\"hidden\" name=\"sign_type\" value=\"" + sign_type + "\"/>");
-
-
-
sbHtml.append("<input type=\"submit\" value=\"支付宝确认付款\"></form>");
-
-
sbHtml.append("<script>document.forms[‘alipaysubmit‘].submit();</script>");
-
-
return sbHtml.toString();
-
}
-
}
======================================================================================================
Md5Encrypt.java
-
package com.test.util.alipay;
-
-
import java.io.UnsupportedEncodingException;
-
import java.security.MessageDigest;
-
import java.security.NoSuchAlgorithmException;
-
-
import com.test.constants.AlipayConfig;
-
-
-
-
-
-
-
-
-
-
-
public class Md5Encrypt {
-
-
-
-
private static final char[] DIGITS = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘,
-
‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ };
-
-
-
-
-
-
-
-
-
-
public static String md5(String text) {
-
MessageDigest msgDigest = null;
-
-
try {
-
msgDigest = MessageDigest.getInstance("MD5");
-
} catch (NoSuchAlgorithmException e) {
-
throw new IllegalStateException(
-
"System doesn‘t support MD5 algorithm.");
-
}
-
-
try {
-
msgDigest.update(text.getBytes(AlipayConfig.input_charset));
-
-
} catch (UnsupportedEncodingException e) {
-
-
throw new IllegalStateException(
-
"System doesn‘t support your EncodingException.");
-
-
}
-
-
byte[] bytes = msgDigest.digest();
-
-
String md5Str = new String(encodeHex(bytes));
-
-
return md5Str;
-
}
-
-
public static char[] encodeHex(byte[] data) {
-
-
int l = data.length;
-
-
char[] out = new char[l << 1];
-
-
-
for (int i = 0, j = 0; i < l; i++) {
-
out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
-
out[j++] = DIGITS[0x0F & data[i]];
-
}
-
-
return out;
-
}
-
-
}
======================================================================================================
AlipayConfig.java这里是对账号,Key,回调连接地址等一些设置
-
package com.test.constants;
-
-
import java.util.Properties;
-
-
import com.test.util.PropertiesUtil;
-
-
public class AlipayConfig {
-
-
private static AlipayConfig alconfig = null;
-
private AlipayConfig(){
-
-
}
-
public static AlipayConfig getInstance(){
-
-
if(alconfig==null){
-
alconfig = new AlipayConfig();
-
}
-
-
return alconfig;
-
}
-
-
-
-
-
-
-
-
-
public static String it_b_pay = "1h";
-
-
public static String partner = "2088601003079118";
-
-
public static String service = "create_direct_pay_by_user";
-
-
public static String key = "zxcdvxgksaam2zjrmv5cv0p4jqesaioh";
-
-
-
public static String seller_email = "test@yahoo.com.cn";
-
-
-
-
public static String notify_url ="http:www.xxx.com/projectName/alipayTrade.action";
-
-
-
-
-
-
-
-
public static String show_url = "http://www.alipay.com";
-
-
-
public static String mainname = "收款方名称";
-
-
-
-
public static String input_charset = "UTF-8";
-
-
-
public static String sign_type = "MD5";
-
-
-
public static String transport = "http";
-
}
===============================================================================================================
下面是一个简单的应用流程:
从JSP调用的就不说了,因为这也就只是传给后台你要买的东西是什么,价格多少等一些参数。
这里介绍一下后台处理的:
PaymentAction.java
-
-
-
-
-
public synchronized static String getOrderNum(){
-
Date date=new Date();
-
DateFormat df=new SimpleDateFormat("yyyyMMddHHmmssSSS");
-
return df.format(date);
-
}
-
-
-
protected HttpServletRequest getRequest() {
-
return ServletActionContext.getRequest();
-
}
-
-
-
-
-
String orderNum = getOrderNum();
-
-
-
getRequest().setAttribute("totalMoney","0.01");
-
-
getRequest().setAttribute("out_trade_no", orderNum);
-
-
getRequest().setAttribute("subject", "商品名称");
-
-
-
===============================================================================================================
alipay.jsp
-
<%@page import="com.test.constants.AlipayConfig"%>
-
<%@page import="com.test.util.alipay.UtilDate"%>
-
<%@page import="com.test.util.alipay.AlipayService"%>
-
<%
-
/*
-
功能:设置商品有关信息(入口页)
-
*详细:该页面是接口入口页面,生成支付时的URL
-
*版本:3.1
-
*日期:2010-11-01
-
*说明:
-
*以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
-
*该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
-
-
*************************注意*****************
-
如果您在接口集成过程中遇到问题,
-
您可以到商户服务中心(https://b.alipay.com/support/helperApply.htm?action=consultationApply),提交申请集成协助,我们会有专业的技术工程师主动联系您协助解决,
-
您也可以到支付宝论坛(http://club.alipay.com/read-htm-tid-8681712.html)寻找相关解决方案
-
要传递的参数要么不允许为空,要么就不要出现在数组与隐藏控件或URL链接里。
-
**********************************************
-
*/
-
%>
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
-
pageEncoding="UTF-8"%>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
<title>test</title>
-
<link rel="SHORTCUT ICON" href="favicon.ico">
-
<meta name="keywords" content="" />
-
<meta name="description" content="" />
-
<style type="text/css">
-
.font_content{
-
font-family:"宋体";
-
font-size:14px;
-
color:#FF6600;
-
}
-
.font_title{
-
font-family:"宋体";
-
font-size:16px;
-
color:#FF0000;
-
font-weight:bold;
-
}
-
table{
-
border: 1px solid #CCCCCC;
-
}
-
</style>
-
<script type="text/javascript">
-
var _gaq = _gaq || [];
-
_gaq.push([‘_setAccount‘, ‘UA-25469955-1‘]);
-
_gaq.push([‘_trackPageview‘]);
-
-
-
(function() {
-
var ga = document.createElement(‘script‘); ga.type = ‘text/javascript‘; ga.async = true;
-
ga.src = (‘https:‘ == document.location.protocol ? ‘https://ssl‘ : ‘http://www‘) + ‘.google-analytics.com/ga.js‘;
-
var s = document.getElementsByTagName(‘script‘)[0]; s.parentNode.insertBefore(ga, s);
-
})();
-
</script>
-
</head>
-
<%
-
//request.setCharacterEncoding("UTF-8");
-
//AlipyConfig.java中配置信息(不可以修改)
-
String input_charset = AlipayConfig.getInstance().input_charset;
-
String sign_type = AlipayConfig.getInstance().sign_type;
-
String seller_email = AlipayConfig.getInstance().seller_email;
-
String partner = AlipayConfig.getInstance().partner;
-
String key = AlipayConfig.getInstance().key;
-
-
String show_url = AlipayConfig.getInstance().show_url;
-
String notify_url = AlipayConfig.getInstance().notify_url;
-
String return_url = AlipayConfig.getInstance().return_url;
-
String it_b_pay = AlipayConfig.getInstance().it_b_pay;
-
-
///////////////////////////////////////////////////////////////////////////////////
-
-
//以下参数是需要通过下单时的订单数据传入进来获得
-
//必填参数
-
String out_trade_no = (String)request.getAttribute("out_trade_no");//请与贵网站订单系统中的唯一订单号匹配
-
//订单名称,显示在支付宝收银台里的“商品名称”里,显示在支付宝的交易管理的“商品名称”的列表里。
-
String subject = (String)request.getAttribute("subject");
-
//订单描述、订单详细、订单备注,显示在支付宝收银台里的“商品描述”里
-
String body = (String)request.getAttribute("body");
-
//订单总金额,显示在支付宝收银台里的“应付总额”里
-
String total_fee = (String)request.getAttribute("totalMoney");
-
-
//扩展功能参数——默认支付方式
-
// String pay_mode = request.getParameter("pay_bank");
-
String paymethod = ""; //默认支付方式,四个值可选:bankPay(网银); cartoon(卡通); directPay(余额); CASH(网点支付)
-
String defaultbank = ""; //默认网银代号,代号列表见http://club.alipay.com/read.php?tid=8681379
-
/*if(pay_mode.equals("directPay")){
-
paymethod = "directPay";
-
}
-
else{
-
paymethod = "bankPay";
-
defaultbank = pay_mode;
-
}*/
-
-
//扩展功能参数——防钓鱼
-
//请慎重选择是否开启防钓鱼功能
-
//exter_invoke_ip、anti_phishing_key一旦被设置过,那么它们就会成为必填参数
-
//开启防钓鱼功能后,服务器、本机电脑必须支持远程XML解析,请配置好该环境。
-
//建议使用POST方式请求数据
-
String anti_phishing_key = ""; //防钓鱼时间戳
-
String exter_invoke_ip= ""; //获取客户端的IP地址,建议:编写获取客户端IP地址的程序
-
//如:
-
//anti_phishing_key = AlipayFunction.query_timestamp(partner); //获取防钓鱼时间戳函数
-
//exter_invoke_ip = "202.1.1.1";
-
-
//扩展功能参数——其他
-
String extra_common_param = ""; //自定义参数,可存放任何内容(除=、&等特殊字符外),不会显示在页面上
-
String buyer_email = "137672927"; //默认买家支付宝账号
-
String extend_param = "";
-
-
-
//扩展功能参数——分润(若要使用,请按照注释要求的格式赋值)
-
String royalty_type = ""; //提成类型,该值为固定值:10,不需要修改
-
String royalty_parameters ="";
-
//提成信息集,与需要结合商户网站自身情况动态获取每笔交易的各分润收款账号、各分润金额、各分润说明。最多只能设置10条
-
//各分润金额的总和须小于等于total_fee
-
//提成信息集格式为:收款方Email_1^金额1^备注1|收款方Email_2^金额2^备注2
-
//如:
-
//royalty_type = "10"
-
//royalty_parameters = "111@126.com^0.01^分润备注一|222@126.com^0.01^分润备注二"
-
-
-
//之前设置的1h将返回
-
//错误描述: 抱歉,商户没有开通自定义超时权限,请联系您的商家。
-
//错误代码: SELF_TIMEOUT_NOT_SUPPORT
-
-
it_b_pay="";
-
-
//构造函数,生成请求URL
-
String sHtmlText = AlipayService.BuildForm(partner,seller_email,return_url,notify_url,show_url,out_trade_no,
-
subject,body,total_fee,paymethod,defaultbank,anti_phishing_key,exter_invoke_ip,extra_common_param,buyer_email,
-
royalty_type,royalty_parameters,input_charset,key,sign_type,it_b_pay);
-
%>
-
-
<body>
-
<table align="center" width="350" cellpadding="5" cellspacing="0">
-
<tr>
-
<td align="center" class="font_title" colspan="2">订单确认</td>
-
</tr>
-
<tr>
-
<td class="font_content" align="right">订单号:</td>
-
<td class="font_content" align="left"><%=out_trade_no%></td>
-
</tr>
-
<tr>
-
<td class="font_content" align="right">付款总金额:</td>
-
<td class="font_content" align="left"><%=total_fee%></td>
-
</tr>
-
<tr>
-
<td align="center" colspan="2"><%=sHtmlText%></td>
-
</tr>
-
</table>
-
</body>
-
</html>
===============================================================================================================
支付宝回调的接口:AlipayNotify.java
-
package com.test.action.payment;
-
-
import java.util.Date;
-
import java.util.List;
-
-
import com.test.action.base.BaseAction;
-
import com.test.dao.model.paymentcenter.OrderForm;
-
import com.test.dao.model.paymentcenter.OrderList;
-
import com.test.dao.model.paymentcenter.UserPurview;
-
-
public class AlipayNotify extends BaseAction {
-
-
private static final long serialVersionUID = 1L;
-
private String buyer_email;
-
private long buyer_id;
-
private String exterface;
-
private String is_success;
-
private String notify_id;
-
private String notify_time;
-
private String notify_type;
-
private String out_trade_no;
-
private String payment_type;
-
private String seller_email;
-
private long seller_id;
-
private String subject;
-
private float total_fee;
-
private String trade_no;
-
private String trade_status;
-
private String sign;
-
private String sign_type;
-
private OrderForm of;
-
-
-
………………………………………………………………
-
-
-
public String alipayTrade() throws Exception {
-
-
if (this.is_success.equalsIgnoreCase("T")
-
&& "TRADE_SUCCESS".equalsIgnoreCase(this.trade_status)) {
-
-
of = (OrderForm) orderFormService
-
.findOrderFormsByOut_trade_no(this.out_trade_no);
-
-
if (of != null) {
-
of.setTradeStatus(1);
-
of.setTradeNo(this.trade_no);
-
of.setNotifyTime(new Date());
-
orderFormService.updateOldModel(of);
-
-
List<OrderList> orderList = orderListService
-
.findOrderFormsByOutTradeNo(this.out_trade_no);
-
for (OrderList ol : orderList) {
-
-
}
-
}
-
-
return SUCCESS;
-
-
}
-
-
return "failure";
-
-
}
-
}
============================================================================================
就这样,支付宝的第三方即时到账接口就算实现了。