java spring中的ipn沙箱不起作用
Posted
技术标签:
【中文标题】java spring中的ipn沙箱不起作用【英文标题】:ipn sandbox in java spring not working 【发布时间】:2015-10-18 04:21:17 【问题描述】:我创建了一个 ipn 沙箱(paypal)并在下面添加了一个代码,但我没有得到服务器的响应。请问可能是什么问题? 以及如何从我的服务器获取日志? PS:我正在使用java spring。 谢谢。
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");
String url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setRequestMethod("POST");
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();
//assign posted variables to local variables
String itemName = request.getParameter("item_name");
String itemNumber = request.getParameter("item_number");
String paymentStatus = request.getParameter("payment_status");
String paymentAmount = request.getParameter("mc_gross");
String paymentCurrency = request.getParameter("mc_currency");
String txnId = request.getParameter("txn_id");
String receiverEmail = request.getParameter("receiver_email");
String payerEmail = request.getParameter("payer_email");
if (res.equals("VERIFIED"))
//---
else if (res.equals("INVALID"))
//---
else
// error
System.out.println("error");
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
【问题讨论】:
你能分享你的错误跟踪吗 【参考方案1】:这里是我的服务方法供您查看可能会有所帮助,它是一种更简洁的方法,而不是在您的代码中对我来说不太可疑的较低级别创建 url,但错误代码可以帮助更准确地找到原因
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
configLogging();
System.out.println("Inside service method");
@SuppressWarnings("deprecation")
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Constants.IPN_SANDBOX_ENDPOINT);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("cmd", "_notify-validate")); //You need to add this parameter to tell PayPal to verify
for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();)
String name = e.nextElement();
String value = request.getParameter(name);
System.out.println("<"+name+","+value+">");
params.add(new BasicNameValuePair(name, value));
post.setEntity(new UrlEncodedFormEntity(params));
String rc = getRC(client.execute(post)).trim();
// IPNMessageHandler ipnMessageHandler = new IPNMessageHandler(request);
// ipnMessageHandler.handleMessage();
if ("VERIFIED".equals(rc))
//Your business code comes here
System.out.println("IPN is fine and working");
IPNMessageHandler ipnMessageHandler = new IPNMessageHandler(request);
ipnMessageHandler.handleMessage();
private String getRC(HttpResponse response) throws IOException, IllegalStateException
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String result = "";
String line = null;
while ((line = br.readLine()) != null)
result += line;
// System.out.println("Response after: "+result);
return result;
【讨论】:
以上是关于java spring中的ipn沙箱不起作用的主要内容,如果未能解决你的问题,请参考以下文章