java实现网站paypal支付功能并且异步修改订单的状态:步骤如下
第一步:去paypal的官网https://www.paypal.com注册一个个人账号,在创建沙箱测试账号时需要用到
第二步:paypal提供了模拟的测试环境,我们需要在https://www.sandbox.paypal.com/去创建一个虚拟卖家账号和买家账号
(必须用第一步注册的真实账号才能登录进去,沙箱账号是登录不进去的)
第三步:登录成功后创建卖家、买家账号,如果不知道在哪创建账号可以直接点击这个链接https://developer.paypal.com/developer/accounts/
如下图:
上面几个就是我创建的卖家和买家测试账号,如果创建不了的可以私信我
第四步:编写java代码,把必要的数据传给paypal就行,比如金额、产品描述等等
第五步:编写异步回调路径的java代码
/** * pay pal支付返回信息 * @param request * @param response */@RequestMapping("payPal/recharge")public void payPal(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException, ParseException { String itemNumber=""; Enumeration en = request.getParameterNames(); while (en.hasMoreElements()) { String paramName = (String) en.nextElement(); String paramValue = request.getParameter(paramName); if(paramName.equals("item_number")){ itemNumber=paramValue; } } String id[]=itemNumber.split(","); PrintWriter out=response.getWriter(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sDate = sdf.format(new Date()); Date date = sdf.parse(sDate); String str1 = request.getParameter("tx"); /*正式环境下 String str2 = "&at=sTvmKEM1YR2EmQXW3VyBrqYWiX-8_wr0Sj5w2DQ5uqGoakHYOCKcFsaAAU4"; */ String str2 = "&at=VmjfBuVl1vbSC6bMV7xvROqisIsrMpKftSx_bLbAnNr-UO2JsLnAR2wfzK8"; String str = "?tx=" + str1 + "&cmd=_notify-synch" + str2; /* String str = "?tx=" + str1 + "&cmd=_notify-validate" + str2; */ /* 正式环境下 String payPalUrl = "https://www.paypal.com/cgi-bin/webscr"; */ String payPalUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr"; payPalUrl = payPalUrl + str; URL u = new URL(payPalUrl); 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 line = ""; String txn_id = ""; //paypal的号码 String item_name = "";//本地订单号 String contact_phone = ""; int i = 0; String res = ""; String msg = ""; while ((line = in.readLine()) != null) { i = i + 1; if (i == 1) { res = line; } if (res.equals("SUCCESS")) { if (line.indexOf("txn_id=") != -1) { txn_id = line.replace("txn_id=", ""); } else if (line.indexOf("item_name=") != -1) { item_name = line.replace("item_name=", ""); } else if (line.indexOf("contact_phone=") != -1) { contact_phone = line.replace("contact_phone=", ""); } } } if (!txn_id.equals("") && !item_name.equals("")) { UserRecord userRecord=userRecordService.findById(Integer.parseInt(id[1])); userRecord.setCommitDate(date); userRecord.setHandler(id[2]); userRecord.setState(0); userRecordService.updateUserRecord(userRecord);//修改数据库的字段信息 msg = "Pay for success! Please wait for delivery! Your Order Number: " + txn_id + " !"; } else { msg = "Sorry ! Your operating error! Please contact website administrator !!"; } out.print("<script>alert(‘" + msg + "‘);location.href=‘" + request.getContextPath() + "/goto/back‘</script>");//支付完毕返回 用户信息页 !} 注意)本地是无法进行异步调试,要在外网才行,也可以使用nat123将本地映射到外网。另外,如果异步返回时接收不到相关的数据,有可能是你没开通PDT数据传输功能,在网站付款习惯里面可以设定,如下图:
时间: 2024-12-10 21:23:03