Have been trying to get the IPN response call to work via a servlet. I can use the demo jsp to receive the IPN request and also issue and receive the IPN response. https://www.paypal.com/us/cgi-bin/webscr?cmd=p/pdn/ipn-codesamples-pop-outside#java
But when I try the same code in a servlet it does not work - the servlet receives the initial IPN request, I am able to pull the request variables but when I shoot them back to paypal the response I get is basically a bunch of HTML and not the typical VERIFIED message. I have surfed for a while and also tried changing my servlet any number of ways to no avail.
Thanks
Here is My servlet Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class PPListen extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 8669468768750366974L;
static Logger logger = Logger.getLogger(PPListen.class.getName());
private static final String PAYPAL_TEST_URL = "https://www.sandbox.paypal.com/cgi-bin/webscr";
private static final String PAYPAL_PROD_URL ="https://www.paypal.com/cgi-bin/webscr";
//private HttpClient httpClient;
public PPListen() {
// TODO Auto-generated constructor stub
}
public void service(HttpServletRequest request, HttpServletResponse respose) throws ServletException, IOException {
if (logger.isInfoEnabled()) {logger.info("Into Service ");}
PropertyConfigurator.configure("c:\\D-Drive\\EclipseProjects\\gcms\\WEB-INF\\log4j.conf");
String uri = request.getRequestURI();
String rh = request.getRemoteHost();
System.out.println("URI:"+uri+":");
System.out.println("Remote Host:"+rh+":");
// This is required by PayPal
Enumeration<String> e = request.getParameterNames();
String outStr = "cmd=_notify_validate";
logger.debug("******* Output Data");
while (e.hasMoreElements()) {
String name = e.nextElement();
String val = request.getParameter(name);
outStr = outStr + "&"+name+"="+URLEncoder.encode(val);
if (logger.isDebugEnabled()) {
logger.debug("Received Value Named:"+name+": Value:"+val+":");
}
}
URL u = new URL(PAYPAL_TEST_URL);
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(outStr);
pw.close();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
logger.debug("******* Output Data");
while (true) {
String result = in.readLine();
if (result == null) {
break;
}
logger.debug(result);
}
logger.debug("******* END");
in.close();
}
/*
public void service2(HttpServletRequest request, HttpServletResponse respose) throws ServletException, IOException {
if (logger.isInfoEnabled()) {logger.info("Into Service ");}
PropertyConfigurator.configure("c:\\D-Drive\\EclipseProjects\\gcms\\WEB-INF\\log4j.conf");
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
String uri = request.getRequestURI();
String rh = request.getRemoteHost();
System.out.println("URI:"+uri+":");
System.out.println("Remote Host:"+rh+":");
// This is required by PayPal
params.add(new NameValuePair("cmd","_notify_validate"));
Enumeration<String> e = request.getParameterNames();
while (e.hasMoreElements()) {
String name = e.nextElement();
String val = request.getParameter(name);
params.add(new NameValuePair(name,val));
if (logger.isDebugEnabled()) {
logger.debug("Received Value Named:"+name+": Value:"+val+":");
}
}
NameValuePair[] uu = (NameValuePair[])params.toArray(new NameValuePair[params.size()]);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(PAYPAL_PROD_URL);
post.setRequestBody(uu);
if (logger.isDebugEnabled()) {
logger.debug("--- calling parameters out to PayPal are ---");
for (NameValuePair nvp: params) {
logger.debug(" Being Sent - Name:"+nvp.getName()+": Value:"+nvp.getValue());
}
logger.debug("--- end of calling parameters out to PayPal ---");
}
logger.debug("--- Prior to Connect Specific");
logger.debug("--- after Connect Specific");
client.executeMethod(post);
logger.debug("--- after POST");
// String resp = post.getResponseBodyAsString();
int status = post.getStatusCode();
byte[] responseBody = post.getResponseBody();
logger.debug("Value Returned From PayPal is :"+new String(responseBody));
logger.debug("Query String :"+post.getQueryString()+":");
logger.debug("Status Code of call is "+status+":");
}
}
Any help will be great. Thanks