Paypal IPN for recurring cancellation payment

Asked

Viewed 144 times

1

I got the following code from Paypal’s git hub. I am making recurring payments and would like to receive notification in the system when there is cancellation. Thus, I could block the provision of my services if the user cancels. I have Servlet code, but I don’t know how to get it to respond on my JSF system. I’ve already peed on web.xml.

package br.com.spacnet.util;

import java.io.IOException;
import java.util.Map;

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

import com.paypal.core.LoggingManager;
import com.paypal.ipn.IPNMessage;
import br.com.spacnet.util.Configuration;

public class IPNListenerServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;
 /* 
 * receiver for PayPal ipn call back.
 */
protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // For a full list of configuration parameters refer in wiki page. 
        // (https://github.com/paypal/sdk-core-java/wiki/SDK-Configuration-Parameters)
        Map<String,String> configurationMap =  Configuration.getConfig();
        IPNMessage  ipnlistener = new IPNMessage(request,configurationMap);
        boolean isIpnVerified = ipnlistener.validate();
        String transactionType = ipnlistener.getTransactionType();
        Map<String,String> map = ipnlistener.getIpnMap();

        LoggingManager.info(IPNListenerServlet.class, "******* IPN (name:value) pair : "+ map + "  " +
                "######### TransactionType : "+transactionType+"  ======== IPN verified : "+ isIpnVerified);
    }
}

My web.xml is like this:

 <servlet>
    <description></description>
    <display-name>IPNListenerServlet</display-name>
    <servlet-name>IPNListenerServlet</servlet-name>
    <servlet-class>com.sample.ipn.IPNListenerServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>IPNListenerServlet</servlet-name>
    <url-pattern>/IPNListener</url-pattern>
</servlet-mapping>
  • You should map your Servlet with the correct package, in your case br.com.spacnet.util and not com.sample.ipn.

  • Thank you, Cesarmiguel, thank you. But even if I map correctly now, when I call http://localhost:8080/meusite/admin/IPN/ipnMensagem.jsp, nothing is printed in the Tomcat output window...

1 answer

2

I believe there are two things wrong:

  1. The mapping of Servlet is wrong. The <servlet-class> must be <servlet-class>br.com.spacnet.util.IPNListenerServlet</servlet-class>, since the package of the Servlet is that way.

  2. Access is wrong. If you are using your browser to access IPNListenerServlet, is making a request GET for a Servlet that only meets requisitions POST (method doPost was implemented), and in addition the url is wrong.

    If the context-root of its application for meusite (may be /), I believe you must make a request POST, or using the curl or a HttpClient, to the address:

    localhost:8080/_seu_context-root_/IPNListener

    Because your mapping of Servlet in the web.xml is with:

    <url-pattern>/IPNListener</url-pattern>
    

    Another alternative would be to use the annotation @WebServlet, to set up your Servlet, but for this need to rotate in a Servlet Container implement version 3.0. If this is the case, your Servlet would be:

    @WebServlet("/IPNListener")
    public class IPNListenerServlet extends HttpServlet
    

    No need to configure it on web.xml.

For more details about the Servlets take a look at Java Servlet Technology 7 or Java Servlet Technology 6


I don’t quite understand why you use the page:

localhost:8080/meusite/admin/IPN/ipnMensagem.jsp

Because in general the registered endpoint for the IPN of Paypal just need to respond with a status code. And this can be done directly by Servlet using the HttpServletResponse.setStatus, it is not necessary to give forward to a new page.

In relation to status:

  1. Use status 200 to inform that the request has been processed correctly.
  2. Any other value is considered to be error and the notification IPN is done again at another time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.