当前位置: 代码迷 >> Paypal >> paypal IPN回来
  详细解决方案

paypal IPN回来

热度:289   发布时间:2016-05-21 02:41:41.0
paypal IPN返回

1.设定返回的地址

目标:登录paypal-->用户信息-->我的销售工具-->即时付款通知-->编辑并填写url

?

填写的URL必须为公网的,不能为局域网,要不就无法接收到paypal发送的信息

?


?2.编写IPN.jsp (此代码为官方代码)

Java代码??收藏代码
  1. //从?PayPal?出读取?POST?信息同时添加变量?cmd???
  2. ????Enumeration?en?=?request.getParameterNames();??
  3. ????String?str?=?"cmd=_notify-validate";??
  4. ????while?(en.hasMoreElements())?{??
  5. ????????String?paramName?=?(String)?en.nextElement();??
  6. ????????String?paramValue?=?request.getParameter(paramName);??
  7. ????????str?=?str?+?"&"?+?paramName?+?"="??
  8. ????????????????+?URLEncoder.encode(paramValue,?"utf-8");??
  9. ????????//此处的编码一定要和自己的网站编码一致,不然会出现乱码,paypal回复的通知为‘INVALID’??
  10. ????}??
  11. ????System.out.println("paypal传递过来的交易信息:"?+?str);??
  12. ????//建议在此将接受到的信息?str?记录到日志文件中以确认是否收到?IPN?信息??
  13. ????//将信息?POST?回给?PayPal?进行验证??
  14. ????//设置?HTTP?的头信息??
  15. ????//在?Sandbox?情况下,设置:??
  16. ????URL?u?=?new?URL("https://www.sandbox.paypal.com/cgi-bin/webscr");??
  17. ????//正式环境??
  18. ????//??????URL?u?=?new?URL("https://www.paypal.com/cgi-bin/webscr");??
  19. ????URLConnection?uc?=?u.openConnection();??
  20. ????uc.setDoOutput(true);??
  21. ????uc.setRequestProperty("Content-Type",??
  22. ????????????"application/x-www-form-urlencoded");??
  23. ????PrintWriter?pw?=?new?PrintWriter(uc.getOutputStream());??
  24. ????pw.println(str);??
  25. ????pw.close();??
  26. ????//接受?PayPal?对?IPN?回发的回复信息??
  27. ????BufferedReader?in?=?new?BufferedReader(new?InputStreamReader(??
  28. ????????????uc.getInputStream()));??
  29. ????String?res?=?in.readLine();??
  30. ????in.close();??
  31. ??
  32. ????//将?POST?信息分配给本地变量,可以根据您的需要添加??
  33. ????//该付款明细所有变量可参考:??
  34. ????//https://www.paypal.com/IntegrationCenter/ic_ipn-pdt-variable-reference.html??
  35. ????String?itemName?=?request.getParameter("item_name");//商品名??
  36. ????String?itemNumber?=?request.getParameter("item_number");//购买数量??
  37. ????String?paymentStatus?=?request.getParameter("payment_status");//交易状态??
  38. ????String?paymentDate?=?request.getParameter("payment_date");//交易时间??
  39. ????String?paymentAmount?=?request.getParameter("mc_gross");//交易钱数??
  40. ????String?paymentCurrency?=?request.getParameter("mc_currency");//货币种类??
  41. ????String?txnId?=?request.getParameter("txn_id");//交易id??
  42. ????String?receiverEmail?=?request.getParameter("receiver_email");//收款人email??
  43. ????String?payerEmail?=?request.getParameter("payer_email");//付款人email??
  44. ??
  45. ????if?(res?==?null?||?res?==?"")??
  46. ????????res?=?"0";??
  47. ????//…??
  48. ????//获取?PayPal?对回发信息的回复信息,判断刚才的通知是否为?PayPal?发出的??
  49. ????if?(res.equals("VERIFIED"))?{??
  50. ????????//检查付款状态??
  51. ????????//检查?txn_id?是否已经处理过??
  52. ????????//检查?receiver_email?是否是您的?PayPal?账户中的?EMAIL?地址??
  53. ????????//检查付款金额和货币单位是否正确??
  54. ????????//处理其他数据,包括写数据库??
  55. ??????????
  56. ????}?else?if?(res.equals("INVALID"))?{??
  57. ????????//非法信息,可以将此记录到您的日志文件中以备调查??
  58. ????}?else?{??
  59. ????????//处理其他错误??
  60. ??
  61. ????}??

?注意:paypal回复的通知为'VERIFIED',也不一定代表此次交易成功,要判断是否交易成功通过下面语句,判断交易状态是否成功。

Java代码??收藏代码
  1. String?paymentStatus?=?request.getParameter("payment_status");//交易状态?Completed?代表交易成功??
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Sun Nov 18 18:29:34 CST 2018
  • 最近登录:Sun Nov 18 18:29:34 CST 2018
  • 最近登录:Sun Nov 18 18:29:34 CST 2018
  • 最近登录:Sun Nov 18 18:29:34 CST 2018
  • 最近登录:Sun Nov 18 18:29:34 CST 2018