1

一直试图让 IPN 响应调用通过 servlet 工作。我可以使用演示 jsp 来接收 IPN 请求,也可以发出和接收 IPN 响应。 https://www.paypal.com/us/cgi-bin/webscr?cmd=p/pdn/ipn-codesamples-pop-outside#java

但是当我在 servlet 中尝试相同的代码时它不起作用 - servlet 接收到初始 IPN 请求,我能够提取请求变量但是当我将它们返回到 paypal 时,我得到的响应基本上是一堆 HTML 和不是典型的 VERIFIED 消息。我已经冲浪了一段时间,也尝试过多种方式更改我的 servlet,但均无济于事。

谢谢

这是我的 servlet 代码


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+":");


    }

}

任何帮助都会很棒。谢谢

4

0 回答 0