Paypal with JSF – Making payments from your Enterprise Application
If you have an enterprise application based on J2EE, Seam, Spring or just pure JSF and you want to start charging people for the services or products you provide you can use Paypal’s services to make credit card or bank transfer payments. The Paypal BuyNow button is a an easy approach to do this.
New! A complete Enteprise Sales Paypal Package is available from us for a small fee of 80EUR or 90USD. If you are interested please contact us in order to buy this package. Or simply let us know if this information was useful or interesting for you.
You can use the BuyNow JSF component which wraps the functionality of the Paypal button. The original implementation of the Paypal BuyNow button uses an HTTP form with POST method that sends predefined POST variables to Paypal.
The JSF Paypal BuyNow component
Download the bpcatalog-ee5-ea-0.8-installer.jar and see the reference documentation on Java Sun Blueprints.
The component can be linked to a backing bean and you can easily send additional post data to Paypal.
First you have to extract the downloaded bpcatalog. The installer should do this automatically; if it doesn’t just run the jar with the java -jar switch.
In the bpcatalog-ee5-ea-0.8/lib folder you’ll find the bp-ui-14.jar and shalem-remoting.jar libraries. You will need to include these two libraries into your project. The bp-ui-14.jar is the actual wrapper for the button.
Fix the tag library
The JSF component is missing its tag library. In order to include and use it as a JSF component you must insert a taglib.xml file into the bp-ui-14.jar/META-INF folder.
The taglib.xml file should contain the following:
<?xml version="1.0"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"> <facelet-taglib xmlns="http://java.sun.com/JSF/Facelet"> <namespace>http://java.sun.com/blueprints/ui</namespace> <tag> <tag-name>buyNow</tag-name> <component> <component-type>com.sun.j2ee.blueprints.ui.shopping.BuyNow</component-type> <renderer-type>com.sun.j2ee.blueprints.ui.shopping.BuyNow</renderer-type> </component> </tag> </facelet-taglib>
Include it into your JSF page
Now you’re ready to use the Paypal button as a JSF component. Include the tag into the namespace of your page template:
<ui:composition .... xmlns:bp="http://java.sun.com/blueprints/ui" ..>
Include the component on your page:
<bp:buyNow amount="10.00" itemName="My Product" business="email@yourapp.com" quantity="1" returnUrl="http://yourapp.com/thankyou.page" paymentCancelledUrl="http://yourapp.com/billing.page" notifyUrl="http://yourapp.com/ipnscript.page" type="BuyNow">
There are a couple of smaller mistakes in the documentation:
- notify_url is wrapped as notifyUrl instead of postbackUrl as stated (use notifyUrl=”http://yourserver/YourIPNProcessingScript”)
- item_number does not send any value to Paypal
Another Paypal related common difficulty is parsing the date sent by Paypal. They are formatting it based on US PST/PDT time. The recommended formatting for the date parsing is:
String PAYPAL_DATE_FORMAT = "HH:mm:ss MMM dd, yyyy z";
Creating the pages
With the help of the return, paymentCancelled and notify URLs Paypal can send messages to your server about events. It can also redirect your customers to the pages you want them to view when an event occurs. When a payment occurs Paypal will invoke the notifyUrl page and send back some POST variables. You will have to process and validate these variables manually as the above component will not do this for you. A method in your backing bean should be able to process the Paypal message. This will be your so called IPN script.
Processing the Paypal response
The notifyUrl parameter tells Paypal where to send a message which notifies you of the event initiated by a paying customer. The PaypalProcessBean will take care of processing this message by checking whether the data of the transaction is correct. You can then proceed with providing the service to your customer.
You can process the POST variables sent by Paypal by reading them from the FacesContext:
FacesContext facesContext = FacesContext.getCurrentInstance(); HashMap<String, String> IPNPaypalVariables = new HashMap<String, String>(); IPNPaypalVariables.putAll(facesContext.getExternalContext() .getRequestParameterMap());
Validating the transaction
Paypal suggests that in order to verify the data which is being posted back you need to send back all these POST variables in the exact order starting with and additional cmd=_notify-validate parameter.
Paypal responds with a VERIFIED or INVALID message depending on the correctness of the information you have posted earlier. If you received the VERIFIED message the transaction was correct. Now you can rest assured it wasn’t a hacking attempt.
Conclusion
If you want to start earning money with your hard work Paypal is an easy to use service from the perspective of your customers. You can easily integrate the Sun Blueprints Library with your favorite enterprise application framework.
- Tech
Comments are closed!