關於支付寶的一些信息可以直接訪問支付寶網站https://www.alipay.com/。
國內電子商務系統實現的基本流程如下:
客戶在系統內下訂單 -> 系統根據訂單生成支付寶接口url -> 客戶通過url使用支付寶(網上銀行)付款 -> 支付寶將客戶的付款完成信息發送給電子商務系統 -> 系統收到支付寶信息後確定客戶訂單已經付款 -> 進行發貨等後續流程。
其實這個流程與以前講paypal接口的基本類似,都是爲了實現訂單的自動付款確認。
paypal與支付寶在使用時候有一點區別:paypal接口是直接通過一個form提交給paypal網站;而支付寶是通過一個url跳轉到支付寶網站的。
在開始下面的內容之前,你要先有一個支付寶賬戶,如果要集成支付寶接口,你還必須申請開通服務(關於如何開通,可以直接到支付寶網站上申請).在服務開通後,支付寶會給你2個字符串編號:1個partnerId(合作伙伴ID),還有1個securityCode(安全碼).當你拿到這2個碼的時候就可以開始下面的內容了.
(1)如何調用支付寶接口?(將客戶的訂單信息按照既定的規則生成一個url跳轉到支付寶網站)
通過下面方法[makeOrderAlipayUrl(HttpServletRequest httpRequest,Order order)]的調用得到支付寶的url,然後進行跳轉(response.sendRedirect(url);).
- /**
- * 根據訂單生成支付寶接口URL.
- * @param httpRequest
- * @param order 訂單實例
- * @return
- * @throws Exception
- */
- public static String makeOrderAlipayUrl(HttpServletRequest httpRequest,Order order) throws Exception {
- HashMap hm = new HashMap();
- hm.put("_input_charset",httpRequest.getCharacterEncoding());//採用相同的編碼方式
- hm.put("body","您在www.xxx.com上的訂單");//填寫在跳到支付寶頁面上顯示的付款內容信息
- hm.put("discount","-5");//填寫折扣信息 -5表示抵扣5元
- hm.put("logistics_fee","10");//物流費用
- hm.put("logistics_payment","BUYER_PAY");//物流費用支付人 BUYER_PAY=買家支付物流費用
- hm.put("logistics_type","EXPRESS");//物流方式
- hm.put("notify_url","http://www.xxx.com/notifyurl.jsp");//客戶付款後,支付寶調用的頁面
- hm.put("out_trade_no",order.getId());//外部交易號,最好具有唯一性,在獲取支付寶發來的付款信息時使用.
- hm.put("partner",partnerId);//partnerId(合作伙伴ID)
- hm.put("agent",partnerId);//partnerId(合作伙伴ID)
- hm.put("payment_type","1");//支付類型 1=商品購買,2=服務購買,...
- hm.put("price","105.30");//訂單金額信息
- hm.put("quantity","1");//訂單商品數量,一般都是寫1,它是按照整個訂單包來計算
- hm.put("return_url","http://www.xxx.com/ReturnUrl.jsp");//客戶付款成功後,顯示給客戶的頁面
- hm.put("seller_email","[email protected]");//你的支付寶賬戶email
- hm.put("service","create_direct_pay_by_user");//create_direct_pay_by_user=直接付款,trade_create_by_buyer 擔保付款
- hm.put("subject","www.xxx.com的訂單");//填寫在跳到支付寶頁面上顯示的付款標題信息
- String payGateway = "https://www.alipay.com/cooperate/gateway.do?";//跳轉到支付寶的url頭
- return makeUrl(hm,securityCode,httpRequest.getCharacterEncoding(),payGateway);//securityCode(安全碼)
- }
- /**
- * 根據傳入的參數生成alipay的支付URL
- * @param hm 參數值
- * @param securityCode 安全碼
- * @param charset 編碼
- * @param payGateway 支付寶gateway
- * @return
- */
- public static String makeUrl(HashMap hm,String securityCode,String charset,String payGateway) throws Exception{
- List keys = new ArrayList(hm.keySet());
- Collections.sort(keys);//支付寶要求參數必須按字母排序
- StringBuffer content = new StringBuffer();
- for (int i = 0; i < keys.size(); i++) {
- content.append((String) keys.get(i));
- content.append("=");
- content.append((String) hm.get((String) keys.get(i)));
- if (i != keys.size() - 1) {
- content.append("&");
- }
- }
- content.append(securityCode);
- String sign = md5(content.toString(),charset);
- content.delete(0,content.length());
- content.append(payGateway);
- for (int i = 0; i < keys.size(); i++) {
- content.append(keys.get(i));
- content.append("=");
- content.append(URLEncoder.encode((String) hm.get(keys.get(i)), charset));
- content.append("&");
- }
- content.append("sign=");
- content.append(sign);
- content.append("&sign_type=MD5");
- keys.clear();
- keys = null;
- return content.toString();
- }
- /**
- * 生成md5編碼字符串.
- * @param str 源字符串
- * @param charset 編碼方式
- * @return
- *
- */
- public static String md5(String str,String charset) {
- if (str == null)
- return null;
- char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'a', 'b', 'c', 'd', 'e', 'f' };
- MessageDigest md5MessageDigest = null;
- byte[] md5Bytes = null;
- char md5Chars[] = null;
- byte[] strBytes = null;
- try {
- strBytes = str.getBytes(charset);
- md5MessageDigest = MessageDigest.getInstance("MD5");
- md5MessageDigest.update(strBytes);
- md5Bytes = md5MessageDigest.digest();
- int j = md5Bytes.length;
- md5Chars = new char[j * 2];
- int k = 0;
- for (int i = 0; i < j; i++) {
- byte md5Byte = md5Bytes[i];
- md5Chars[k++] = hexDigits[md5Byte >>> 4 & 0xf];
- md5Chars[k++] = hexDigits[md5Byte & 0xf];
- }
- return new String(md5Chars);
- } catch (NoSuchAlgorithmException e) {
- //Log.output(e.toString(), Log.STD_ERR);
- return null;
- } catch (UnsupportedEncodingException e) {
- //Log.output(e.toString(), Log.STD_ERR);
- return null;
- } finally {
- md5MessageDigest = null;
- strBytes = null;
- md5Bytes = null;
- }
- }
/**
* 根據訂單生成支付寶接口URL.
* @param httpRequest
* @param order 訂單實例
* @return
* @throws Exception
*/
public static String makeOrderAlipayUrl(HttpServletRequest httpRequest,Order order) throws Exception {
HashMap hm = new HashMap();
hm.put("_input_charset",httpRequest.getCharacterEncoding());//採用相同的編碼方式
hm.put("body","您在www.xxx.com上的訂單");//填寫在跳到支付寶頁面上顯示的付款內容信息
hm.put("discount","-5");//填寫折扣信息 -5表示抵扣5元
hm.put("logistics_fee","10");//物流費用
hm.put("logistics_payment","BUYER_PAY");//物流費用支付人 BUYER_PAY=買家支付物流費用
hm.put("logistics_type","EXPRESS");//物流方式
hm.put("notify_url","http://www.xxx.com/notifyurl.jsp");//客戶付款後,支付寶調用的頁面
hm.put("out_trade_no",order.getId());//外部交易號,最好具有唯一性,在獲取支付寶發來的付款信息時使用.
hm.put("partner",partnerId);//partnerId(合作伙伴ID)
hm.put("agent",partnerId);//partnerId(合作伙伴ID)
hm.put("payment_type","1");//支付類型 1=商品購買,2=服務購買,...
hm.put("price","105.30");//訂單金額信息
hm.put("quantity","1");//訂單商品數量,一般都是寫1,它是按照整個訂單包來計算
hm.put("return_url","http://www.xxx.com/ReturnUrl.jsp");//客戶付款成功後,顯示給客戶的頁面
hm.put("seller_email","[email protected]");//你的支付寶賬戶email
hm.put("service","create_direct_pay_by_user");//create_direct_pay_by_user=直接付款,trade_create_by_buyer 擔保付款
hm.put("subject","www.xxx.com的訂單");//填寫在跳到支付寶頁面上顯示的付款標題信息
String payGateway = "https://www.alipay.com/cooperate/gateway.do?";//跳轉到支付寶的url頭
return makeUrl(hm,securityCode,httpRequest.getCharacterEncoding(),payGateway);//securityCode(安全碼)
}
/**
* 根據傳入的參數生成alipay的支付URL
* @param hm 參數值
* @param securityCode 安全碼
* @param charset 編碼
* @param payGateway 支付寶gateway
* @return
*/
public static String makeUrl(HashMap hm,String securityCode,String charset,String payGateway) throws Exception{
List keys = new ArrayList(hm.keySet());
Collections.sort(keys);//支付寶要求參數必須按字母排序
StringBuffer content = new StringBuffer();
for (int i = 0; i < keys.size(); i++) {
content.append((String) keys.get(i));
content.append("=");
content.append((String) hm.get((String) keys.get(i)));
if (i != keys.size() - 1) {
content.append("&");
}
}
content.append(securityCode);
String sign = md5(content.toString(),charset);
content.delete(0,content.length());
content.append(payGateway);
for (int i = 0; i < keys.size(); i++) {
content.append(keys.get(i));
content.append("=");
content.append(URLEncoder.encode((String) hm.get(keys.get(i)), charset));
content.append("&");
}
content.append("sign=");
content.append(sign);
content.append("&sign_type=MD5");
keys.clear();
keys = null;
return content.toString();
}
/**
* 生成md5編碼字符串.
* @param str 源字符串
* @param charset 編碼方式
* @return
*
*/
public static String md5(String str,String charset) {
if (str == null)
return null;
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
MessageDigest md5MessageDigest = null;
byte[] md5Bytes = null;
char md5Chars[] = null;
byte[] strBytes = null;
try {
strBytes = str.getBytes(charset);
md5MessageDigest = MessageDigest.getInstance("MD5");
md5MessageDigest.update(strBytes);
md5Bytes = md5MessageDigest.digest();
int j = md5Bytes.length;
md5Chars = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte md5Byte = md5Bytes[i];
md5Chars[k++] = hexDigits[md5Byte >>> 4 & 0xf];
md5Chars[k++] = hexDigits[md5Byte & 0xf];
}
return new String(md5Chars);
} catch (NoSuchAlgorithmException e) {
//Log.output(e.toString(), Log.STD_ERR);
return null;
} catch (UnsupportedEncodingException e) {
//Log.output(e.toString(), Log.STD_ERR);
return null;
} finally {
md5MessageDigest = null;
strBytes = null;
md5Bytes = null;
}
}
當客戶通過接口url付款後,支付寶會自動的去調用前面提供的[notify_url]參數中的url.
(2)支付寶將付款信息返回給系統
當客戶付款後,支付寶就會自動調用上面表單提供的[notify_url],下面是一個[notifyurl.jsp]的一個例子:
- <%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.AlipayMgr"%><%
- String ret = AlipayMgr.insert(request);
- if(ret==null){
- out.print("success");//成功接收支付寶發來的付款信息
- }else{
- out.print("fail");//出錯
- }
- %>
<%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.AlipayMgr"%><%
String ret = AlipayMgr.insert(request);
if(ret==null){
out.print("success");//成功接收支付寶發來的付款信息
}else{
out.print("fail");//出錯
}
%>
如果確認收到支付寶發來的客戶付款信息,則返回"success",這樣子支付寶就知道系統已經收到信息了;否則返回"fail",這樣支付寶會過一段時間後再次發來。其實,只有當支付寶收到"success"的返回信息後纔會停止發送付款信息,否則會自動的每隔一段時間就調用上面
的[notify_url]通信接口。
(3)系統處理支付寶發來的付款信息
- /*
- * Created on 2005-6-12
- * Author stephen
- * Email zhoujianqiang AT gmail DOT com
- * CopyRight(C)2005-2008 , All rights reserved.
- */
- package com.soft4j;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Enumeration;
- import java.util.Vector;
- import javax.servlet.http.HttpServletRequest;
- /**
- * 支付寶付款通知接口.
- *
- * @author stephen
- * @version 1.0.0
- */
- public final class NotifyUrlMgr {
- public static String insert(HttpServletRequest httpRequest) {
- //定義變量和進行必要的初始化工作
- Enumeration parameterNames = null;
- String parameterName = null;
- String parameterValue = null;
- int count = 0;
- Vector[] params = null;
- Vector vParameterName = new Vector();
- Vector vParameterValue = new Vector();
- try {
- String orderId = httpRequest.getParameter("out_trade_no");//訂單號
- if(orderId==null||"".equals(orderId)) orderId="-1";
- parameterNames = httpRequest.getParameterNames();
- boolean isPrint = false;
- while (parameterNames.hasMoreElements()) {//循環收取支付寶發來的所有參數信息
- parameterName = (String) parameterNames.nextElement();
- parameterValue = httpRequest.getParameter(parameterName);
- if(parameterValue==null) parameterValue="";
- vParameterName.add(parameterName);
- vParameterValue.add(parameterValue);
- count++;
- }
- //這裏添加對收到信息的處理:一般是將這些信息存入數據庫,然後對客戶的訂單進行處理.
- return null;
- } catch (Exception e) {
- return e.toString();
- } finally {
- //
- }
- }
- }
/*
* Created on 2005-6-12
* Author stephen
* Email zhoujianqiang AT gmail DOT com
* CopyRight(C)2005-2008 , All rights reserved.
*/
package com.soft4j;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
/**
* 支付寶付款通知接口.
*
* @author stephen
* @version 1.0.0
*/
public final class NotifyUrlMgr {
public static String insert(HttpServletRequest httpRequest) {
//定義變量和進行必要的初始化工作
Enumeration parameterNames = null;
String parameterName = null;
String parameterValue = null;
int count = 0;
Vector[] params = null;
Vector vParameterName = new Vector();
Vector vParameterValue = new Vector();
try {
String orderId = httpRequest.getParameter("out_trade_no");//訂單號
if(orderId==null||"".equals(orderId)) orderId="-1";
parameterNames = httpRequest.getParameterNames();
boolean isPrint = false;
while (parameterNames.hasMoreElements()) {//循環收取支付寶發來的所有參數信息
parameterName = (String) parameterNames.nextElement();
parameterValue = httpRequest.getParameter(parameterName);
if(parameterValue==null) parameterValue="";
vParameterName.add(parameterName);
vParameterValue.add(parameterValue);
count++;
}
//這裏添加對收到信息的處理:一般是將這些信息存入數據庫,然後對客戶的訂單進行處理.
return null;
} catch (Exception e) {
return e.toString();
} finally {
//
}
}
}
http://stephen830.javaeye.com/blog/254827
這樣系統可以在客戶使用支付寶付款後,自動的根據支付寶發來的付款信息確認客戶的付款情況,並進行相應的後續操作