0

Hi,

1. I send such form to the testing Sandbox Paypal server

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Start page</title>
</head>
<body>
<form method=post action=https://api-3t.sandbox.paypal.com/nvp>
        <input type=hidden name=USER value=my_sandbox_account>
        <input type=hidden name=PWD value=my_pass>
        <input type=hidden name=SIGNATURE value=my_sign_key>
        <input type=hidden name=VERSION value=72.0>
        <input type=hidden name=PAYMENTREQUEST_0_PAYMENTACTION
            value=Sale>
        <input name=PAYMENTREQUEST_0_AMT value=6.00>
        <input type=hidden name=RETURNURL
            value=http://www.my_return_url.com>
        <input type=hidden name=CANCELURL
            value=http://www.my_return_url.com>
        <input type=submit name=METHOD value=SetExpressCheckout>
</form>
</body>
</html>

2. Once I submit that form I recieve back string in my browser like this:

TOKEN=EC%23409823094JKK&TIMESTAMP=2013%2d04%2d25T10%3a30%3a54Z&CORRELATIONID=345803985njkk3&ACK=Success&VERSION=72%2e0&BUILD=5709304

3. Than I want that my servlet gets this returned string in my variable that I could manage it further

    import java.io.IOException;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     * Servlet implementation class SetExpressCheckoutServlet
     */
    @WebServlet("/SetExpressCheckoutServlet")
    public class SetExpressCheckoutServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public SetExpressCheckoutServlet() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String token = request.getQueryString();
            System.out.println(token.toString());
        }

    }

When I launch the project it just passes only steps 1 and 2, but 3 doesn't pass.

Help me please to create servlet correctly.

UPDATE

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String token = request.getParameter("TOKEN");
    PrintWriter pw = response.getWriter();
    if(token == null){
        System.out.println("It is null");
    } else {
        pw.print("<html><body>" + token + "<body></html>");
    }

}

my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" metadata-complete="true">

  <display-name>Ppconnector</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>


  <servlet>
    <servlet-name>SetExpressCheckoutServlet</servlet-name>
    <servlet-class>ua.pp.connector.SetExpressCheckoutServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>SetExpressCheckoutServlet</servlet-name>
    <url-pattern>/SetExpressCheckoutServlet</url-pattern>
  </servlet-mapping>


</web-app>

devger
  • 703
  • 4
  • 12
  • 26
  • Is your return url correct? Does it point to your server where you have deployed your servlet? – NilsH Apr 25 '13 at 11:40
  • No, I just put my web-site in return url that doesn't have any Java services. Is there need to point url to servlet that can handle a response from PayPal? – devger May 02 '13 at 05:20

1 Answers1

0

What you want is to get a parameter value by a known parameter name, using ServletRequest#getParameter(String name). Parameters are those name=value pairs in get requests, and they can be retrieved in a servlet by calling getParameter method. So, in your case, it'd be:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String token = request.getParameter("TOKEN");
    //retrieve other parameters in a same way
    //and handle parameters accordingly
}

For the servlet to be called your request URL must path the url-mappling of servlet that in your case was declared via @WebServlet anotation.

Also, if you don't need a get method - do not use it. Otherwise, a better practice is for both get and post methods to call a common function like processRequest(...).

skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Unfortunatelly, that didn't help. I got NULL. Updated my first post with code. – devger Apr 25 '13 at 14:39
  • What exactly is null? Is your servlet method called? How do you treat a callback from paypal? – skuntsel Apr 25 '13 at 14:42
  • First of all I submit the form setExpressCheckout and getting string with TOKEN and url in browser https://api-3t.sandbox.paypal.com/nvp. After that I force the browser go to http://localhost:8080/Ppconnector/SetExpressCheckoutServlet and get Null, but not sure that it is correct. In this case I don't understand how should I catch the TOKEN from returned string from https://api-3t.sandbox.paypal.com/nvp? – devger Apr 25 '13 at 17:59
  • There must be some callback URL/function with the parameters you describe. There you need to point to/call in callback your servlet to catch parameters and do the processing. For the latter, use my answer; for the former consult Paypal documentation/developer's guide for such an example. Also, try to inspect what's happening in browser tools like Firebug. – skuntsel Apr 26 '13 at 05:24