1.设定返回的地址
目标:登录paypal-->用户信息-->我的销售工具-->即时付款通知-->编辑并填写url
填写的URL必须为公网的,不能为局域网,要不就无法接收到paypal发送的信息
2.编写IPN.jsp (此代码为官方代码)
//从 PayPal 出读取 POST 信息同时添加变量„cmd‟ Enumeration en = request.getParameterNames(); String str = "cmd=_notify-validate"; while (en.hasMoreElements()) { String paramName = (String) en.nextElement(); String paramValue = request.getParameter(paramName); str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue, "utf-8"); //此处的编码一定要和自己的网站编码一致,不然会出现乱码,paypal回复的通知为‘INVALID’ } System.out.println("paypal传递过来的交易信息:" + str); //建议在此将接受到的信息 str 记录到日志文件中以确认是否收到 IPN 信息 //将信息 POST 回给 PayPal 进行验证 //设置 HTTP 的头信息 //在 Sandbox 情况下,设置: URL u = new URL("https://www.sandbox.paypal.com/cgi-bin/webscr"); //正式环境 // URL u = new URL("https://www.paypal.com/cgi-bin/webscr"); URLConnection uc = u.openConnection(); uc.setDoOutput(true); uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); PrintWriter pw = new PrintWriter(uc.getOutputStream()); pw.println(str); pw.close(); //接受 PayPal 对 IPN 回发的回复信息 BufferedReader in = new BufferedReader(new InputStreamReader( uc.getInputStream())); String res = in.readLine(); in.close(); //将 POST 信息分配给本地变量,可以根据您的需要添加 //该付款明细所有变量可参考: //https://www.paypal.com/IntegrationCenter/ic_ipn-pdt-variable-reference.html String itemName = request.getParameter("item_name");//商品名 String itemNumber = request.getParameter("item_number");//购买数量 String paymentStatus = request.getParameter("payment_status");//交易状态 String paymentDate = request.getParameter("payment_date");//交易时间 String paymentAmount = request.getParameter("mc_gross");//交易钱数 String paymentCurrency = request.getParameter("mc_currency");//货币种类 String txnId = request.getParameter("txn_id");//交易id String receiverEmail = request.getParameter("receiver_email");//收款人email String payerEmail = request.getParameter("payer_email");//付款人email if (res == null || res == "") res = "0"; //… //获取 PayPal 对回发信息的回复信息,判断刚才的通知是否为 PayPal 发出的 if (res.equals("VERIFIED")) { //检查付款状态 //检查 txn_id 是否已经处理过 //检查 receiver_email 是否是您的 PayPal 账户中的 EMAIL 地址 //检查付款金额和货币单位是否正确 //处理其他数据,包括写数据库 } else if (res.equals("INVALID")) { //非法信息,可以将此记录到您的日志文件中以备调查 } else { //处理其他错误 }
注意:paypal回复的通知为'VERIFIED',也不一定代表此次交易成功,要判断是否交易成功通过下面语句,判断交易状态是否成功。
String paymentStatus = request.getParameter("payment_status");//交易状态 Completed 代表交易成功