paypal在线支付的通信接口问题!

paypal_Charles 2013-02-20 02:28:56
paypal作为国际在线支付的一种比较常用的工具,在现在的国际电子商务中使用的非常多,这里将就paypal支付接口与企业自己的网上电子商务系统之间沟通作一详细描述。

一般电子商务系统实现的流程如下:
客户在系统内下订单 -> 将订单的金额信息提交到paypal网站 -> 客户在paypal上付款 -> paypal将客户的付款完成信息发送给电子商务系统 -> 系统收到paypal信息后确定客户订单已经付款 -> 进行发货等后续流程。

从这个流程中可以看到系统与paypal的通信尤其关键,可以实现订单的自动付款确认。




(1)如何调用paypal接口?(将订单的金额信息提交到paypal网站)
Html代码 :
<%@ page contentType="text/html;charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>goto paypal</title>
</head>
<body bgcolor="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<form name="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="paypal@xxx.com"><!--这里填写你的paypal账户email-->
<input type="hidden" name="item_name" value="order information"><!--这里填写客户订单的一些相关信息,当客户连到paypal网站付款的时候将看到这些信息-->
<input type="hidden" name="amount" value="220.00"><!--订单的总金额信息-->
<input type="hidden" name="currency_code" value="USD"><!--订单总金额对应的货币类型 ,客户可以用其他币种来付款,比如这里订单币种是美元USD,客户可以用欧元EUR来付款,由paypal根据当前汇率自动实现币种之间的换算-->
<input type="hidden" name="on0" value="customerId"><!-- 自定义的参数1 -->
<input type="hidden" name="os0" value="stephen"><!-- 对应上面自定义参数1对应的值 -->
<input type="hidden" name="on1" value="address"><!-- 自定义的参数2 -->
<input type="hidden" name="os1" value="shanghai china"><!-- 对应上面自定义参数2对应的值 -->
<input type="hidden" name="notify_url" value="http://www.xxx.com/notifyurl.jsp?order_id=23876412"><!--这里告诉paypal付款的通信url,即当客户付款后调用这个url通知系统-->
<input name="Paypal" type="button" value="Go to Paypal" onclick="javaScript:this.form.submit();"></td>
</form>
</body>
</html>
在生成上面的paypal表单时,其中的[cmd],[action],[business],[amount],[currency_code],[notify_url]参数设置绝对不能出错,[cmd]和[action]指定paypaly接口类型,[business]出错,你将收不到客户的付款,[amount]与[currency_code]关系到订单的金额,[notify_url]是系统与paypal的付款通知接口url。

当客户下单后看到上面的页面后,就可以通过设置的按钮[Go to Paypal]连接到paypal.com网站,然后登录并进行付款。

(2)paypay将付款信息返回给系统
当客户付款后,paypal就会自动调用上面表单提供的[notify_url],下面是一个[notifyurl.jsp]的一个例子:
Html代码
<%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.NotifyUrlMgr"%><%
String ret = NotifyUrlMgr.insert(request);
if(ret==null){out.print("200 OK");}else{out.print("fail");}
%>
<%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.NotifyUrlMgr"%><%
String ret = NotifyUrlMgr.insert(request);
if(ret==null){out.print("200 OK");}else{out.print("fail");}
%>

如果确认收到paypal发来的客户付款信息,则返回"200 OK",这样子paypal就知道系统已经收到信息了;否则返回"fail",这样paypal会过一段时间后再次发来。其实,只有当paypal收到"200 OK"的返回信息后才会停止发送付款信息,否则会自动的每隔一段时间就调用上面
的[notify_url]通信接口。

(3)系统处理paypay发来的付款信息
Java代码 :
/*
* 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;
/**
* paypal付款通知接口.
*
* @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();
//判断paypal付款账户是否正确
String business = httpRequest.getParameter("business");
if( !"paypal@xxx.com".equals(business) ) {
System.out.println("gu:Wrong receive paypal email:"+business);
return null;
}
try {
String orderId = httpRequest.getParameter("order_id");//订单号
if(orderId==null||"".equals(orderId)) orderId="-1";
parameterNames = httpRequest.getParameterNames();
boolean isPrint = false;
while (parameterNames.hasMoreElements()) {//循环收取paypal发来的所有参数信息
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;
/**
* paypal付款通知接口.
*
* @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();
//判断paypal付款账户是否正确
String business = httpRequest.getParameter("business");
if( !"paypal@xxx.com".equals(business) ) {
System.out.println("gu:Wrong receive paypal email:"+business);
return null;
}

try {
String orderId = httpRequest.getParameter("order_id");//订单号
if(orderId==null||"".equals(orderId)) orderId="-1";
parameterNames = httpRequest.getParameterNames();
boolean isPrint = false;
while (parameterNames.hasMoreElements()) {//循环收取paypal发来的所有参数信息
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 {
//
}
}
}


这样系统可以自动对客户订单的付款情况进行跟踪,更快捷的进行订单处理。

附录:
[1] paypay允许的币种

AUD Australian Dollar
CAD Canadian Dollar
CHF Swiss Franc
CZK Czech Koruna
DKK Danish Krone
EUR Euro
GBP Pound Sterling
HKD Hong Kong Dollar
HUF Hungarian Forint
JPY Japanese Yen
NOK Norwegian Krone
NZD New Zealand Dollar
PLN Polish Zloty
SEK Swedish Krona
SGD Singapore Dollar
USD U.S. Dollar

[2] paypal付款信息的参数含义:

https://www.paypal.com/IntegrationCenter/ic_ipn-pdt-variable-reference.html


[3] paypal提供的开发api的pdf文档

PP_OrderManagement_IntegrationGuide.pdf.zip [在附件中可以下载]

[4] paypal提交付款所支持的参数及其含义(也就是你提交给paypal中的那个Form表单中需要设置的参数)
https://www.paypal.com/IntegrationCenter/ic_std-variable-reference.html

[5] paypal显示页面的语言设置(也就是打开paypal网站的语言)。需要用参数locale.x进行设置,但这个参数在paypal提供的参数表(参见上面[4])中没有的。
Html代码
<form name="paypal" action="https://www.paypal.com/cgi-bin/webscr?locale.x=zh_HK" method="post">
...
</form>
<form name="paypal" action="https://www.paypal.com/cgi-bin/webscr?locale.x=zh_HK" method="post">
...
</form>

locale.x=zh_HK 繁体中文
locale.x=en_GB 英文

如果你的电子商务系统不是java环境的,也可以参考上面的内容。
...全文
256 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
smile__110 2013-06-02
  • 打赏
  • 举报
回复
thank you

1,170

社区成员

发帖
与我相关
我的任务
社区描述
移动支付相关内容讨论专区
社区管理员
  • 移动支付
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧