You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by jayeshps <ja...@gmail.com> on 2015/04/16 11:11:02 UTC

Paypal Integration in wicket

Hello, 

I am trying to add paypal as a payment option in wicket, I am using a
straight forward method using a BuyNow button with the <form> and hidden
fields in html. But I am not quite sure how to implement the IPN, without
which it would be very unsafe as the html can be changed during just before
clicking the buyNow button. Could any one share with me an example code for
the IPN implementation or a different example where OAuth and the restAPI
have been used for paypal integration. I would be very grateful

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Paypal-Integration-in-wicket-tp4670325.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Paypal Integration in wicket

Posted by lucast <lu...@hotmail.com>.
Hi Jayeshps,
I have implemented a wicket pay with paypal button a few weeks ago.

The beauty of working with wicket is that you do not have to hard-code and
hide the html parameters you pass to paypal on your html.
It can all be done on the AjaxButton.onSubmit();

I am assuming you're familiar with the parameter's passed to paypal, if not,
here is a good example:
http://www.onlineinteract.com/wiki/viewpage.php?pageId=179
<http://www.onlineinteract.com/wiki/viewpage.php?pageId=179>  

Also, an explanation of each parameter can be  found here
<https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/#id08A6HF009YW> 
.


Ok, onto the code:

Inside the AjaxButton.onSubmit() button, you need to construct the URL with
parameters to pass to paypal:
An example url is this:
https://www.paypal.com/cgi-bin/webscr?cmd=_cart&upload=1&no_shipping=1&no_note=1&custom=0A566EC9F6AC27E9&currency_code=GBP&lc=GB&bn=business_BuyNow_WPS_GB&return=https://localhost:8443/paymentpage/sucessParameters&cancel_return=https://localhost:8443/paymentpage/cancelParameters&notify_url=https://localhost:8443/paypalipnprocess&business=admin@businessowner.com&item_name_1=entry_test_test&item_number_1=DB3BE2BD0BFC8D03&amount_1=0.30


Notice how all parameters are separated by '&'. 

You can produce the above URL from the server side, avoiding posting all of
the above info in hidden fields on the HTML form post parameters. 

The way I have implemented is as follow:
AjaxButton.onSubmit() 
{
       setResponsePage(new PayPalPaymentGatewayExampleClass(parameters to
construct the url));
}


I've attached the PayPalPaymentGatewayExampleClass which extends
RedirectPage2 on this post.
PayPalPaymentGatewayExampleClass.java
<http://apache-wicket.1842946.n4.nabble.com/file/n4670405/PayPalPaymentGatewayExampleClass.java>  
RedirectPage2.html
<http://apache-wicket.1842946.n4.nabble.com/file/n4670405/RedirectPage2.html>  
RedirectPage2.java
<http://apache-wicket.1842946.n4.nabble.com/file/n4670405/RedirectPage2.java>  

PayPalPaymentGatewayExampleClass builds the URL with the necessary
parameters before posting the request to Paypal.


Notice that on RedirectPage2 I throw new RedirectToUrlException(
url.toString() );
That's because I was having trouble to properly rediderct a page running
wicket on production mode. I spent days trying to find a solution. Finally,
I had to settle for the workaround but functional RedirectToUrlException.



Part 2, processing the IPN:

Once the payment has taken place, Paypal will send the IPN request to you.
If you haven't played with Paypal's IPN simulator, you can do so  here
<https://developer.paypal.com/developer/ipnSimulator>  . Remember to provide
a valid url to IPN handler URL since IPN is a 3rd party trying to contact
your application. localhost will not do.

More on IPN
<https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNIntro/> 
. Although, based on your post, you are already familiar with it.

As explained on this picture:
<http://apache-wicket.1842946.n4.nabble.com/file/n4670405/IPN_auth_flow.gif> 

and on this
link:http://codeoftheday.blogspot.co.uk/2013/07/paypal-button-and-instant-payment_6.html,
Paypal sends an IPN message, which if the URL is valid, automatically sends
a http 200 response back. Then you need to send the exact IPN message back
to Paypal with the code "cmd=_notify-validate" (valid at time of writing).

When Paypal replies to you with a valid code, only then is advisable to do
your back end processing of the payments and everything else you need to do.

I've attached the code for PayPalIPNProcessingPortal class which is an empty
page and should be a bookmarkable page.

If you read through the code and try to understand it, it might be better
than me trying to explain step by step.

PayPalIPNProcessingPortal.java
<http://apache-wicket.1842946.n4.nabble.com/file/n4670405/PayPalIPNProcessingPortal.java>  
PayPalIPNProcessingPortal.html
<http://apache-wicket.1842946.n4.nabble.com/file/n4670405/PayPalIPNProcessingPortal.html>  
IPN_Parameter.java
<http://apache-wicket.1842946.n4.nabble.com/file/n4670405/IPN_Parameter.java>  


Let me know if you have any questions. I am sorry if my code doesn't seem
clear enough. I work from home and although I try to make the code as
readable as possible for my future self, I don't have a team member to
code-review with.

All the best =0)
Lucas


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Paypal-Integration-in-wicket-tp4670325p4670405.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Paypal Integration in wicket

Posted by jayeshps <ja...@gmail.com>.
Hello, 

IPN is short for Instant Paypal Notification, it is a notification service,
which runs while a user is trying to make a payment after clicking on the
facebook button on my web application. unless a confirmation is sent from my
application to paypal the payment will not be confirmed, this is done to
hinder any man in the middle attack.


I also tried using the implementation like shown in
https://github.com/paypal/PayPal-Java-SDK, but it does not seem to redirect
to the paypal's website.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Paypal-Integration-in-wicket-tp4670325p4670351.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Paypal Integration in wicket

Posted by lucast <lu...@hotmail.com>.
Hi Jayesh,
For IPN whether it is the the sandbox or live, you have to provide an URL
that is accessible from outside your local network.

IPN is a third party system, so if you tell IPN to send its requests to
localhost, it will send requests to IPN's localhost.

You need to open up a port on your network: 80, 443, 8080 or 8443, look up
your public IP address and provide it to IPN simulator.

Since I'm working from home, on my router, I opened up port 80 directed all
traffic to my development machine.
You can tell your router to redirect all traffic from port 80, to send it to
your machine on port 8080. 

Don't forget to open up the above ports on your development machine as well.

If you are at an office then you can ask your sys admin to open up the above
ports and redirect them to your machine, but you still have to open up the
ports on your development machine.

I hope this helps and not make things more confusing for you.
Any questions, just ask.

Regards,
Lucas

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Paypal-Integration-in-wicket-tp4670325p4670495.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Paypal Integration in wicket

Posted by jayeshps <ja...@gmail.com>.
Hello lucas and vishal, 

Thanks for your replies they were of great help. The payment seems to be
working great, and I checked the IPN by creating a local <form> and adding
values like those paypal would send, however, is there a way to check it
with the paypal's ipn_simulator, as when ever I make a payment I cannot see
anything from the ipn class happening, is it because of the localhost ? and
if it is, is there a way to test the ipn on localhost ? I tried by providing
my ip address in the ipn_simulator but it gives an error stating that,
paypal was unable to connect to the url.

Thanks and Regards,
Jayesh

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Paypal-Integration-in-wicket-tp4670325p4670494.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Paypal Integration in wicket

Posted by vp143 <vi...@cipriati.co.uk>.
Hi jayeshps,

I have implemented PayPal IPN in the following way:

In the html form you submit, you set the notifyUrl to a path i.e.
www.example.com/payment/ipn

In my Application class I have the following: mountPage("/payment/ipn",
IPNPaymentHandler.class);

Then my IPNPaymentHandler.class is as follows:

public class IPNPaymentHandler extends WebPage {
        private static final Log log =
LogFactory.getLog(IPNPaymentHandler.class);
	public IPNPaymentHandler() {
		super();
		log.debug("IPN Payment being received");
                Request request = getRequest();

		try {
			Set<String> paramSet =
request.getRequestParameters().getParameterNames();
			Iterator<String> iterator = paramSet.iterator();

			StringBuffer sb = new StringBuffer();
			sb.append("cmd=_notify-validate");
			while(iterator.hasNext()){
				String paramName = (String)iterator.next();
				String paramValue =
request.getRequestParameters().getParameterValue(paramName).toString();
				sb.append("&" + paramName + "=" + URLEncoder.encode(paramValue,
"UTF-8"));
			}

			URL u = new URL(AppUtil.getSettings().getPayPalWebsiteStandardUrl());
			URLConnection uc = u.openConnection();
			uc.setDoOutput(true);
		
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			PrintWriter pw = new PrintWriter(uc.getOutputStream());
			pw.println(sb.toString());
			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.getRequestParameters().getParameterValue("item_name").toString();
			String itemNumber =
request.getRequestParameters().getParameterValue("item_number").toString();
			String paymentStatus =
request.getRequestParameters().getParameterValue("payment_status").toString();
			String paymentAmount =
request.getRequestParameters().getParameterValue("mc_gross").toString();
			String paymentCurrency =
request.getRequestParameters().getParameterValue("mc_currency").toString();
			String txnId =
request.getRequestParameters().getParameterValue("txn_id").toString();
			String receiverEmail =
request.getRequestParameters().getParameterValue("receiver_email").toString();
			String payerEmail =
request.getRequestParameters().getParameterValue("payer_email").toString();

			//check notification validation
			if(res != null && res.equals("VERIFIED")) {
				if(paymentStatus.equals("Completed")) {
                                    //Do business logic
				}
				else {
					log.debug("paymentStatus = " + paymentStatus);
				}
			}
			else if(res != null && res.equals("INVALID")) {
				log.info("result = " + res);
				log.info("itemName = " + itemName + " itemNumber = " + itemNumber + "
paymentStatus = " + paymentStatus + " paymentAmount = " + paymentAmount + "
" + paymentCurrency);
				log.info(" txnId = " + txnId + " receiverEmail = " + receiverEmail + "
payerEmail = " + payerEmail);
			}
			else {
				log.info("result = " + res);
				log.info("itemName = " + itemName + " itemNumber = " + itemNumber + "
paymentStatus = " + paymentStatus + " paymentAmount = " + paymentAmount + "
" + paymentCurrency);
				log.info(" txnId = " + txnId + " receiverEmail = " + receiverEmail + "
payerEmail = " + payerEmail);
			}
		}
		catch(MalformedURLException mue) {
			mue.printStackTrace();
		}
		catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
}

The above is just the skeleton where you can fine tune to your needs... 
Please do let me know if you see any flaws.... payment needs to be done
right especially with security etc.

Regards
Vishal

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Paypal-Integration-in-wicket-tp4670325p4670415.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Paypal Integration in wicket

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

Please describe what is IPN and what is the technical issue.

Martin Grigorov
Freelancer, available for hire!
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Thu, Apr 16, 2015 at 12:11 PM, jayeshps <ja...@gmail.com> wrote:

> Hello,
>
> I am trying to add paypal as a payment option in wicket, I am using a
> straight forward method using a BuyNow button with the <form> and hidden
> fields in html. But I am not quite sure how to implement the IPN, without
> which it would be very unsafe as the html can be changed during just before
> clicking the buyNow button. Could any one share with me an example code for
> the IPN implementation or a different example where OAuth and the restAPI
> have been used for paypal integration. I would be very grateful
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Paypal-Integration-in-wicket-tp4670325.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>