A member of the development team nicknamed Zeo, living in south California sent me an email asking help to solve an issue. In his spare time he was migrating a web store application in Java/Spring 3.1 to the Google App Engine. One of the requirements was to integrate Express Checkout API to streamline the checkout process for buyers of digital goods. PayPal X delivers an open source java toolkit for Google App Engine available on Google Code named paypalx-gae-toolkit, which provides support for the Adaptive Payments APIs. Unfortunatly using the Adaptive Payments API, PayPal redirect page is not designed for mobile, so it creates a bad user experience. Googling the internet Zeo found an entry in the x.commerce forum which talk about this issue.
Currently PayPal don't have a mobile version of Adaptive Checkout, we only provide a full web flow or the embedded checkout flow (lightbox). At the moment the only mobile web solution PayPal offers is Mobile Express Checkout. One alternative is to have the customers sign up to a preApproval, once they set that up you can charge their PayPal account without the need for a redirect to PayPal for each payment. The main issue again with this is that there is no mobile friendly PreApproval flow.
In addition, after reviewing the source code developed by Zeo I realized that most of the application use the Paypal NVP HTTP API to interface the Express Checkout and to adopt the Paypalx GAE toolkit would require major changes to the application and would provide limited support for mobile devices.
The first issue Zeo faced, was an exception raised during the application start up.The problem was related to the xerces.jar included with the Paypal SDK. A solution was to remove the dependency changing the original code :
APICallerBase.java
- protected static void setupHandler(final String message, DefaultHandler handler)
- throws SAXException, IOException {
- ClassLoader cl = APICallerBase.class.getClassLoader();
- URL url = cl.getResource(message);
- SAXParserFactory factory = SAXParserFactory.newInstance();
- try {
- SAXParser saxParser = factory.newSAXParser();
- XMLReader xr = saxParser.getXMLReader();
- xr.setContentHandler(handler);
- xr.parse(new InputSource(url.openStream()));
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- }
- }
EndpointsReader.java
- public void startElement(String uri, String xname, String name, Attributes atts) {
- /* Do not change the original code */
- }
- public void endElement(String uri, String xname, String name) {
- /* Do not change the original code */
- }
Basically it replaces the xerces parser with the SAX Parser included with the Java SDK, which removes the dependency from the library delivered with the Paypal SDK. One less library that needs to be included in the application WEB-INF/lib.
The second issue related to the Google app engine “Black List” , in other words, Java APIs that did not work on GAE. In this case the issue was related to the Apache Http Client dependency, because of the NVPClientSocket class. .
Also in this case, a solution was to remove the dependency from the Apache Http Library (what a shame) and use the Google App Engine's URL fetch service.
Below is the code:
NVPAPICaller.java- public final String call(String payload) throws PayPalException {
- String nvpResponse = "";
- URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
- try {
- HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
- StringBuffer payloadData = new StringBuffer(payload).append("&").append(header);
- request.setPayload( payloadData.toString().getBytes() );
- HTTPResponse response = fetcher.fetch(request);
- byte[] content = response.getContent();
- // 200, 404, 500, etc
- int responseCode = response.getResponseCode();
- if ( responseCode == HttpURLConnection.HTTP_OK) {
- nvpResponse = new String(content);
- } else {
- throw new FatalException("HTTP Error code " + responseCode + " received, transaction not submitted");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return nvpResponse;
- }
Another change, was to remove the NVPSSLSocketFactory.java and all the references to the org.apache.commons.httpclient.* in the code.
I packed the changes in a new paypal-base-gae.jar and sent to Zeo to test on the web store: it worked. I explained to Zeo this solution is a workaround, and does not pretend to be "the solution" to run the Paypal API on GAE. He promised me next time I will be in South California we will grab a burger at one of my favorite places Hodads.
Cheers !
I packed the changes in a new paypal-base-gae.jar and sent to Zeo to test on the web store: it worked. I explained to Zeo this solution is a workaround, and does not pretend to be "the solution" to run the Paypal API on GAE. He promised me next time I will be in South California we will grab a burger at one of my favorite places Hodads.
Cheers !