You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Baofeng Yu <ba...@moremagic.com> on 2008/03/13 15:59:17 UTC

T5 Paypal IPN handler

Hi,

I would like to integrate Paypal with a T5 application. Previously with 
JSP, I had a paypal IPN handler jsp page. After user makes a payment, 
Paypal posts the payment data to my jsp page and my script checks the 
data and process it. The jsp script runs in the background and is 
separate  from the web flow.  Now in T5, what's the best way to 
implement this? Should this be done by adding an empty page with an 
onActive event and put the code there?

The following is the sample code for the ipn handler:

|<%@ page import="java.util.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>

<%
// read post from PayPal system and add 'cmd'
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);
}

// post back to PayPal system to validate
// NOTE: change http: to https: in the following URL to verify using SSL 
(for increased security).
// using HTTPS requires either Java 1.4 or greater, or Java Secure 
Socket Extension (JSSE)
// and configured for older versions.
URL u = new URL("http://www.paypal.com/cgi-bin/webscr");
URLConnection uc = u.openConnection();
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");

check notification validation
if(res.equals("VERIFIED")) {
// check that paymentStatus=Completed
// check that txnId has not been previously processed
// check that receiverEmail is your Primary PayPal email
// check that paymentAmount/paymentCurrency are correct
// process payment
}
else if(res.equals("INVALID")) {
// log for investigation
}
else {
// error
}
%>|

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 Paypal IPN handler

Posted by Baofeng Yu <ba...@moremagic.com>.
Thanks for your help. I'll look into this.

Hugo Palma wrote:
> I implemented the exact same integration a few weeks ago, also using T5.
> My approach was to use the Dispatcher concept.
>
> All you have to do is implement a class that implements the Dispatcher
> interface. That dispatcher will get executed in the Tapestry request
> handling lifecycle.
> My implementation is something like this:
>
> public class PayPalOrderNotificationDispatcher implements Dispatcher {
>     public static final String PATH_PREFIX = "/notifyorder";
>
>     public PayPalOrderNotificationDispatcher(/* you can inject any
> Tapestry IOC service here that implements the validation logic */) {
>     }
>
>     public boolean dispatch(Request request, Response response) throws
> IOException {
>         String path = request.getPath();
>
>         if (!path.startsWith(PATH_PREFIX))
>             return false;
>
>         // validate order
>         // send confirmation back to paypal
>
>        return true;
>     }
>
> So now you have a Dispatcher that gets executed when someone access the
> url http://myserver/mycontext/notifyorder. So just give that URL to
> paypal and your set.
>
> Baofeng Yu wrote:
>   
>> Hi,
>>
>> I would like to integrate Paypal with a T5 application. Previously
>> with JSP, I had a paypal IPN handler jsp page. After user makes a
>> payment, Paypal posts the payment data to my jsp page and my script
>> checks the data and process it. The jsp script runs in the background
>> and is separate  from the web flow.  Now in T5, what's the best way to
>> implement this? Should this be done by adding an empty page with an
>> onActive event and put the code there?
>>
>> The following is the sample code for the ipn handler:
>>
>> |<%@ page import="java.util.*" %>
>> <%@ page import="java.net.*" %>
>> <%@ page import="java.io.*" %>
>>
>> <%
>> // read post from PayPal system and add 'cmd'
>> 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);
>> }
>>
>> // post back to PayPal system to validate
>> // NOTE: change http: to https: in the following URL to verify using
>> SSL (for increased security).
>> // using HTTPS requires either Java 1.4 or greater, or Java Secure
>> Socket Extension (JSSE)
>> // and configured for older versions.
>> URL u = new URL("http://www.paypal.com/cgi-bin/webscr");
>> URLConnection uc = u.openConnection();
>> 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");
>>
>> check notification validation
>> if(res.equals("VERIFIED")) {
>> // check that paymentStatus=Completed
>> // check that txnId has not been previously processed
>> // check that receiverEmail is your Primary PayPal email
>> // check that paymentAmount/paymentCurrency are correct
>> // process payment
>> }
>> else if(res.equals("INVALID")) {
>> // log for investigation
>> }
>> else {
>> // error
>> }
>> %>|
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>>     
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 Paypal IPN handler

Posted by Hugo Palma <hu...@gmail.com>.
I implemented the exact same integration a few weeks ago, also using T5.
My approach was to use the Dispatcher concept.

All you have to do is implement a class that implements the Dispatcher
interface. That dispatcher will get executed in the Tapestry request
handling lifecycle.
My implementation is something like this:

public class PayPalOrderNotificationDispatcher implements Dispatcher {
    public static final String PATH_PREFIX = "/notifyorder";

    public PayPalOrderNotificationDispatcher(/* you can inject any
Tapestry IOC service here that implements the validation logic */) {
    }

    public boolean dispatch(Request request, Response response) throws
IOException {
        String path = request.getPath();

        if (!path.startsWith(PATH_PREFIX))
            return false;

        // validate order
        // send confirmation back to paypal

       return true;
    }

So now you have a Dispatcher that gets executed when someone access the
url http://myserver/mycontext/notifyorder. So just give that URL to
paypal and your set.

Baofeng Yu wrote:
> Hi,
>
> I would like to integrate Paypal with a T5 application. Previously
> with JSP, I had a paypal IPN handler jsp page. After user makes a
> payment, Paypal posts the payment data to my jsp page and my script
> checks the data and process it. The jsp script runs in the background
> and is separate  from the web flow.  Now in T5, what's the best way to
> implement this? Should this be done by adding an empty page with an
> onActive event and put the code there?
>
> The following is the sample code for the ipn handler:
>
> |<%@ page import="java.util.*" %>
> <%@ page import="java.net.*" %>
> <%@ page import="java.io.*" %>
>
> <%
> // read post from PayPal system and add 'cmd'
> 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);
> }
>
> // post back to PayPal system to validate
> // NOTE: change http: to https: in the following URL to verify using
> SSL (for increased security).
> // using HTTPS requires either Java 1.4 or greater, or Java Secure
> Socket Extension (JSSE)
> // and configured for older versions.
> URL u = new URL("http://www.paypal.com/cgi-bin/webscr");
> URLConnection uc = u.openConnection();
> 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");
>
> check notification validation
> if(res.equals("VERIFIED")) {
> // check that paymentStatus=Completed
> // check that txnId has not been previously processed
> // check that receiverEmail is your Primary PayPal email
> // check that paymentAmount/paymentCurrency are correct
> // process payment
> }
> else if(res.equals("INVALID")) {
> // log for investigation
> }
> else {
> // error
> }
> %>|
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org