Skip to content
August 24, 2010 / charlie

Paypal adaptive API and Spring 3 rest template — Part 1

Recently I have totally rewrote how we use paypal. Switching to use the newish adaptive payment API, and IPN. My serverside setup is using spring and recently switch to version 3 (if you haven’t heard about it for some reason try this link). One of the new things available for spring 3 is rest templates, very useful bit of API. Allow you to use a range of binding API, I use JAXB2 and it comes working out of the box ;)

I am going to give a full example of how to use spring 3 rest template with adaptive payment API. This example will be split into 3 parts. First part is setting up your project. Second is making the request and finally unmarshalling the response back to a JAXB2 annotated object.

So without further deplay, lets get on with it!

Setting up paypal:

So I would expect you have a active paypal account. If not you will need one to carryon. Once you have that you will need to do the following to setup a sandbox envioment for you to test your implementation:

  • Login to developer.paypal.com and set up some test accounts. You will need at lest one Seller account (so you can take payments) and one Buyer account (so you can make payments). I personally just use the preconfigured options as works well for me.
  • Once you have these accounts you can test them by login into sandbox.paypal.com using your test accounts.
  • To set up to use the adaptive payment API you will need a API key.
  • Login to x.com using your normal paypal account.
  • Go to this link and follow the instructions
  • You do need to submit a application request that paypal will approve, but you can carry on testing while this is happening.

Setting up the project:

I use eclipse helios with the IAM maven plugin. Here is my pom.xml

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>paypalAdaptive</groupId>
<artifactId>paypalAdaptive</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>paypalAdaptive</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>
I have added OXM as a dependancy tho is not strictly requires as you can define HTTP converts that will marshall/unmarshall JAXB2 bean for you. However for this particular use we will have our own which will utilise oxm internally.
The other things you will need is your spring file. To start off with, this can be very simple, we will build more on it as we move into part 2/3. Here is what you should start with:
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:oxm=”http://www.springframework.org/schema/oxm”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd”>
<context:annotation-config/>
</beans>
Notice that I have added some namespaces here for oxm and context to support the good old @Autowired  annotation.
The last thing for this tutorial is to set up your Main application so you can run your settings. Something like this will be fine:

package rest;

public class MainPost {

	public String makeRequest(){
		System.out.println("Hello Paypal");
	}

	public static void main(String[] arg) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-rest-template.xml");
		MainPost mainPost = (MainPost) ctx.getBean("rest");
		mainPost.makeRequest();
	}
}

The only thing to note that this class is manage by spring. It has to else nothing will get injected otherwise.

Thats it for this part. Let continue to part 2

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.