一、文档说明
该方案虽然没有用到PAYPAL提供的高阶特性,也考虑了安全验证以及邮件消息通知的实现。由于只是基本功能,只用到三个页面(JSP实现)。以下将详细说明各个页面的关系和职责。
二、页面说明
1.提交页面(test.jsp) 该页面的代码可以嵌入到购买页面里,主要功能是按照PAYPAL的接口规则提交相应的参数,供PAYPAL处理订单的支付请求。
下面是该页面的参数配置
参数含义
|
参数名称
|
参数示例
|
备注
|
action
|
真实用
https://www.
|
|
测试用https://www.
|
提交模式
|
cmd
|
_xclick
|
可以写死
|
商务网站的邮箱
|
business
|
Seller_1210746445_biz@21cn.com
|
可以写死
|
*商品名称
|
item_name
|
Productor/商品一
|
如果用中文要设置charset参数
|
*商品编号
|
item_number
|
88888111
|
也可含英文字母
|
商品数量
|
quantity
|
10
|
|
*自定义用于加密
|
custom
|
sessionID
|
建议将该ID用MD5加密,为以后验证做准备
|
*商品价格
|
amount
|
119.00
|
为double型
|
商品运费
|
shipping
|
10.00
|
为double型
|
货币类型
|
currency_code
|
USD
|
CNY:人民币,USD:美元
|
字符编码
|
charset
|
gb2312
|
不写为默认 默认为ISO8859-1
|
1.验证页面(back.jsp)
用于接有PAYPAL传过来的信息,通过里面的字段CUSTOM来验证是否刚才提交的支付信息是否一致。该验证页的配置在补充说明里参看。
2.取消页面(cancel.jsp)
客户可能在支付时临时决定取消这次支付,取消后将返回的页面。该取消页的配置在补充说明里参看。
二、补充说明
1.验证和取消页面的配置在提交页加入以下两个参数,参数为实际验证页和取消页。
参数含义
|
参数名称
|
参数示例
|
备注
|
取消后返回到的页面
|
cancel_return
|
http://localhost/howie/fail.jsp
|
|
确认以及验证页面
|
return
|
http://localhost/howie/back.jsp
|
|
1.验证页在PAYPAL里的设置
在Profile的Selling Preferences一栏,进入“Instant Payment Notification Preferences”,然后Edit,选中checkbox,填入notify_url地网址。
二、各页面源码
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进行加密和验证传回来的加密码比较
MD5Code md5 = new MD5Code();
String custom_Encrypt = md5.getMD5ofStr(session.getId());
%>
<body>
<form action="https://www." method="post">
<!-- 真实用 -->
<!-- <form action="https://www." method="post"> -->
<!-- 提交模式 -->
<input type="hidden" name="cmd" value="_xclick">
<!-- 商务网站的邮箱 -->
<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">
<!-- 商品数量 -->
<input type="hidden" name="quantity" value="1">
<!-- 自定义用于加密-->
<input type="hidden" name="custom" value="<%=custom_Encrypt %>">
<!-- 商品价格 -->
<input type="hidden" name="amount" value="119.00">
<!-- 商品运费 -->
<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">
<!-- 确认以及验证页面 -->
<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进行加密和验证传回来的加密码比较
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("https://www.");//测试用URL
//URL url = new URL("https://www.");//真是用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");
// 加入数据库的操作
}else if ("INVALID".equals(res)) {
// 支付失败
out.println("fail");
} else {
// 支付错误
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>