一、文檔說明
該方案雖然沒有用到PAYPAL提供的高階特性,也考慮了安全驗(yàn)證以及郵件消息通知的實(shí)現(xiàn)。由于只是基本功能,只用到三個(gè)頁面(JSP實(shí)現(xiàn))。以下將詳細(xì)說明各個(gè)頁面的關(guān)系和職責(zé)。
二、頁面說明
1.提交頁面(test.jsp) 該頁面的代碼可以嵌入到購買頁面里,主要功能是按照PAYPAL的接口規(guī)則提交相應(yīng)的參數(shù),供PAYPAL處理訂單的支付請求。
下面是該頁面的參數(shù)配置
參數(shù)含義 | 參數(shù)名稱 | 參數(shù)示例 | 備注 |
action | 真實(shí)用 | | |
提交模式 | cmd | _xclick | 可以寫死 |
商務(wù)網(wǎng)站的郵箱 | business | Seller_1210746445_biz@21cn.com | 可以寫死 |
*商品名稱 | item_name | Productor/商品一 | 如果用中文要設(shè)置charset參數(shù) |
*商品編號 | item_number | 88888111 | 也可含英文字母 |
商品數(shù)量 | quantity | 10 | |
*自定義用于加密 | custom | sessionID | 建議將該ID用MD5加密,為以后驗(yàn)證做準(zhǔn)備 |
*商品價(jià)格 | amount | 119.00 | 為double型 |
商品運(yùn)費(fèi) | shipping | 10.00 | 為double型 |
貨幣類型 | currency_code | USD | CNY:人民幣,USD:美元 |
字符編碼 | charset | gb2312 | 不寫為默認(rèn) 默認(rèn)為ISO8859-1 |
1.驗(yàn)證頁面(back.jsp)
用于接有PAYPAL傳過來的信息,通過里面的字段CUSTOM來驗(yàn)證是否剛才提交的支付信息是否一致。該驗(yàn)證頁的配置在補(bǔ)充說明里參看。
2.取消頁面(cancel.jsp)
客戶可能在支付時(shí)臨時(shí)決定取消這次支付,取消后將返回的頁面。該取消頁的配置在補(bǔ)充說明里參看。
二、補(bǔ)充說明
1.驗(yàn)證和取消頁面的配置在提交頁加入以下兩個(gè)參數(shù),參數(shù)為實(shí)際驗(yàn)證頁和取消頁。
參數(shù)含義 | 參數(shù)名稱 | 參數(shù)示例 | 備注 |
取消后返回到的頁面 | cancel_return | http://localhost/howie/fail.jsp | |
確認(rèn)以及驗(yàn)證頁面 | return | http://localhost/howie/back.jsp | |
1.驗(yàn)證頁在PAYPAL里的設(shè)置
在Profile的Selling Preferences一欄,進(jìn)入“Instant Payment Notification Preferences”,然后Edit,選中checkbox,填入notify_url地網(wǎng)址。
二、各頁面源碼
1.test.jsp
<%@ page language="java" import="java.util.*,cn.howie.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%
//對sessionID進(jìn)行加密和驗(yàn)證傳回來的加密碼比較
MD5Code md5 = new MD5Code();
String custom_Encrypt = md5.getMD5ofStr(session.getId());
%>
<body>
<form action=" <!-- 真實(shí)用 -->
<!-- <form action=" <!-- 提交模式 -->
<input type="hidden" name="cmd" value="_xclick">
<!-- 商務(wù)網(wǎng)站的郵箱 -->
<input type="hidden" name="business" value="Seller_1210746445_biz@21cn.com">
<!-- 商品名稱 -->
<input type="hidden" name="item_name" value="productor-1">
<!-- 商品編號 -->
<input type="hidden" name="item_number" value="productor-number">
<!-- 商品數(shù)量 -->
<input type="hidden" name="quantity" value="1">
<!-- 自定義用于加密-->
<input type="hidden" name="custom" value="<%=custom_Encrypt %>">
<!-- 商品價(jià)格 -->
<input type="hidden" name="amount" value="119.00">
<!-- 商品運(yùn)費(fèi) -->
<input type="hidden" name="shipping" value="2">
<!-- 貨幣類型 CNY:人民幣,USD:美元 -->
<input type="hidden" name="currency_code" value="USD">
<!-- 字符編碼 -->
<input type="hidden" name="charset" value="gb2312">
<!-- 取消后返回到的頁面 -->
<input type="hidden" name="cancel_return"
value="http://localhost/howie/fail.jsp">
<!-- 確認(rèn)以及驗(yàn)證頁面 -->
<input type="hidden" name="return"
value="http://localhost/howie/back.jsp">
<input type="submit" value="buy now">
</form>
</body>
</html>
2.back.jsp
<%@ page language="java" import="java.util.*,cn.howie.util.*" pageEncoding="gbk"%>
<%@ page import="java.net.*"%>
<%@ page import="java.io.*"%>
<%
//對sessionID進(jìn)行加密和驗(yàn)證傳回來的加密碼比較
MD5Code md5 = new MD5Code();
String custom_Encrypt = md5.getMD5ofStr(session.getId());
%>
<%
String str = "cmd=_notify-validate";
String validate = "";
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
String paramValue = request.getParameter(paramName);
if("custom".equals(paramName)) {
validate = paramValue;
}
str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue);
}
URL url = new URL("測試用URL
//URL url = new URL("真是用URL
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(str);
pw.close();
BufferedReader in = new BufferedReader(new InputStreamReader(uc
.getInputStream()));
String res = in.readLine();
in.close();
//out.println(str+"<br/>");
//out.println(res);
//out.println(session.getId());
if ("VERIFIED".equals(res) && custom_Encrypt.equals(validate) ) {
// 支付成功
out.println("successful");
// 加入數(shù)據(jù)庫的操作
}else if ("INVALID".equals(res)) {
// 支付失敗
out.println("fail");
} else {
// 支付錯(cuò)誤
out.println("error");
}
%>
3.cancel.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Cancel !
</body>
</html>