Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit d6761169c0a481e9c27565e77e9f1b9e53ad998d 0 parents
@letronje letronje authored
Showing with 1,045 additions and 0 deletions.
  1. +24 −0 README.md
  2. +38 −0 build/build.xml
  3. BIN  dist/recurly-client-java-SNAPSHOT.jar
  4. BIN  libs/commons-lang-2.5.jar
  5. BIN  libs/jersey-client-1.4.jar
  6. BIN  libs/jersey-core-1.4.jar
  7. BIN  libs/junit-4.8.2.jar
  8. +72 −0 src/com/kwanzoo/recurly/Account.java
  9. +173 −0 src/com/kwanzoo/recurly/Base.java
  10. +95 −0 src/com/kwanzoo/recurly/BillingInfo.java
  11. +25 −0 src/com/kwanzoo/recurly/CreditCard.java
  12. +16 −0 src/com/kwanzoo/recurly/Plan.java
  13. +92 −0 src/com/kwanzoo/recurly/Subscription.java
  14. +10 −0 src/com/kwanzoo/recurly/exception/AccountNotFoundException.java
  15. +11 −0 src/com/kwanzoo/recurly/exception/BadRequestException.java
  16. +22 −0 src/com/kwanzoo/recurly/exception/Base.java
  17. +11 −0 src/com/kwanzoo/recurly/exception/ForbiddenAccessException.java
  18. +11 −0 src/com/kwanzoo/recurly/exception/InternalServerErrorException.java
  19. +11 −0 src/com/kwanzoo/recurly/exception/PaymentRequiredException.java
  20. +11 −0 src/com/kwanzoo/recurly/exception/PreconditionFailedException.java
  21. +11 −0 src/com/kwanzoo/recurly/exception/ResourceNotFoundException.java
  22. +11 −0 src/com/kwanzoo/recurly/exception/ServiceUnavailableException.java
  23. +11 −0 src/com/kwanzoo/recurly/exception/UnauthorizedAccessException.java
  24. +11 −0 src/com/kwanzoo/recurly/exception/UnknownRecurlyException.java
  25. +11 −0 src/com/kwanzoo/recurly/exception/UnprocessableEntityException.java
  26. +368 −0 src/com/kwanzoo/recurly/test/RecurlyTest.java
24 README.md
@@ -0,0 +1,24 @@
+Recurly Java Client
+===================
+
+The Recurly Java Client library is an open source wrapper library to talk to Recurly's subscription management from your Java app/website. The library interacts with Recurly's [REST API](http://support.recurly.com/faqs/api).
+
+
+Usage
+-----
+
+Please refer to the JUnit test cases in [RecurlyTest](http://github.com/gslab/recurly-client-java/blob/master/src/com/kwanzoo/recurly/test/RecurlyTest.java)
+
+
+Installation
+------------
+
+Just add the latest jar(under [dist/](http://github.com/gslab/recurly-client-java/blob/master/dist/recurly-client-java-SNAPSHOT.jar)) to your classpath.
+
+
+Dependencies
+------------
+
+This wrapper relies on the excellent [Jersey REST client API](https://jersey.dev.java.net/)
+
+
38 build/build.xml
@@ -0,0 +1,38 @@
+<project name="recurly-client-java" default="all" basedir="../">
+ <path id="build.classpath">
+ <fileset dir="${basedir}/libs">
+ <include name="*.jar" />
+ </fileset>
+ </path>
+
+ <target name="all" depends="build.jar" />
+
+ <target name="build.jar" depends="init, clean, compile" >
+ <jar basedir="${dist.dir}/classes" destfile="${dist.dir}/recurly-client-java-${version}.jar" />
+ <delete dir="${dist.dir}/classes" includeemptydirs="true" failonerror="false" />
+ </target>
+
+ <target name="init">
+ <property name="dist.dir" value="${basedir}/dist" />
+ <property name="src.dir" value="${basedir}/src" />
+ <property name="version" value="SNAPSHOT" />
+ </target>
+
+ <target name="clean">
+ <delete includeemptydirs="true" failonerror="false">
+ <fileset dir="${dist.dir}" includes="**/*" />
+ </delete>
+ <mkdir dir="${dist.dir}" />
+ <mkdir dir="${dist.dir}/classes" />
+ </target>
+
+ <target name="compile">
+ <javac srcdir="${src.dir}"
+ destdir="${dist.dir}/classes"
+ debug="true"
+ includes="**/*.java"
+ excludes="com/kwanzoo/recurly/test/**"
+ classpathref="build.classpath"
+ verbose="yes" />
+ </target>
+</project>
BIN  dist/recurly-client-java-SNAPSHOT.jar
Binary file not shown
BIN  libs/commons-lang-2.5.jar
Binary file not shown
BIN  libs/jersey-client-1.4.jar
Binary file not shown
BIN  libs/jersey-core-1.4.jar
Binary file not shown
BIN  libs/junit-4.8.2.jar
Binary file not shown
72 src/com/kwanzoo/recurly/Account.java
@@ -0,0 +1,72 @@
+package com.kwanzoo.recurly;
+
+import java.util.Date;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import com.sun.jersey.api.client.GenericType;
+import com.sun.jersey.api.client.UniformInterfaceException;
+
+@XmlRootElement(name="account")
+public class Account extends Base{
+ public static String pluralResourceName = "accounts";
+
+ //Required
+ @XmlElement(name="account_code")
+ public String accountCode;
+
+ @XmlElement(name="username")
+ public String username;
+
+ @XmlElement(name="first_name")
+ public String firstName;
+
+ @XmlElement(name="last_name")
+ public String lastName;
+
+ @XmlElement(name="email")
+ public String email;
+
+ @XmlElement(name="company_name")
+ public String companyName;
+
+ @XmlElement(name="balance_in_cents")
+ public Integer balanceInCents;
+
+ @XmlElement(name="created_at")
+ public Date createdAt;
+
+ @XmlElement(name="billing_info")
+ public BillingInfo billingInfo;
+
+ private static String getResourcePath(String accountCode){
+ return pluralResourceName + "/" + accountCode;
+ }
+
+ public static Account get(final String accountCode) throws Exception{
+ try{
+ return getWebResourceBuilder(getResourcePath(accountCode)).get(new GenericType<Account>(){});
+ }
+ catch(final UniformInterfaceException uie){
+ throwStatusBasedException(uie.getResponse());
+ return null;
+ }
+ }
+
+ @Override
+ protected String getResourcePath() {
+ return getResourcePath(accountCode);
+ }
+
+ @Override
+ protected String getResourceCreationPath() {
+ return pluralResourceName;
+ }
+
+ public Account(){}
+
+ public Account(final String accountCode){
+ this.accountCode = accountCode;
+ }
+}
173 src/com/kwanzoo/recurly/Base.java
@@ -0,0 +1,173 @@
+package com.kwanzoo.recurly;
+
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import javax.ws.rs.core.MediaType;
+
+import com.kwanzoo.recurly.exception.BadRequestException;
+import com.kwanzoo.recurly.exception.ForbiddenAccessException;
+import com.kwanzoo.recurly.exception.InternalServerErrorException;
+import com.kwanzoo.recurly.exception.PaymentRequiredException;
+import com.kwanzoo.recurly.exception.PreconditionFailedException;
+import com.kwanzoo.recurly.exception.ResourceNotFoundException;
+import com.kwanzoo.recurly.exception.ServiceUnavailableException;
+import com.kwanzoo.recurly.exception.UnauthorizedAccessException;
+import com.kwanzoo.recurly.exception.UnknownRecurlyException;
+import com.kwanzoo.recurly.exception.UnprocessableEntityException;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+import com.sun.jersey.client.urlconnection.HTTPSProperties;
+import com.sun.jersey.core.util.Base64;
+
+public abstract class Base{
+ private static final String BaseURI = "https://app.recurly.com";
+ private static final WebResource webResource;
+ private static String base64AuthStr = "";
+ private static final int UNPROCESSABLE_ENTITY_HTTP_CODE = 422;
+
+ static{
+ webResource = getNewWebResource();
+ }
+
+ private static TrustManager[] getTrustManager(){
+ final X509TrustManager trustManager = new X509TrustManager() {
+ @Override
+ public void checkClientTrusted(final X509Certificate[] arg0, final String arg1) throws CertificateException {
+ return;
+ }
+
+ @Override
+ public void checkServerTrusted(final X509Certificate[] arg0, final String arg1) throws CertificateException {
+ return;
+ }
+
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+ };
+ return new TrustManager[] {trustManager};
+ }
+
+ private static SSLContext getSSLContext(){
+ SSLContext context = null;
+
+ try {
+ context = SSLContext.getInstance("SSL");
+ context.init(null, getTrustManager(), null);
+ }
+ catch (final Exception e) {
+ context = null;
+ e.printStackTrace();
+ }
+ return context;
+ }
+
+ private static HostnameVerifier getHostNameVerifier(){
+ return new HostnameVerifier() {
+ @Override
+ public boolean verify(final String hostname, final SSLSession sslSession) {
+ return true;
+ }
+ };
+ }
+
+ private static WebResource getNewWebResource(){
+ final ClientConfig config = new DefaultClientConfig();
+ config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(getHostNameVerifier(), getSSLContext()));
+ return Client.create(config).resource(BaseURI);
+ }
+
+ public static WebResource.Builder getWebResourceBuilder(final String path){
+ return webResource.path(path).header("Authorization", base64AuthStr).accept(MediaType.APPLICATION_XML_TYPE);
+ }
+
+ //This method needs to be invoked only once, just before performing the first recurly operation
+ public static void setAuth(final String recurlyUsername, final String recurlyPassword){
+ base64AuthStr = new String(Base64.encode(recurlyUsername + ":" + recurlyPassword));
+ }
+
+ //Translates a recurly response to an appropriate recurly exception object.
+ public static void throwStatusBasedException(final ClientResponse response) throws Exception{
+ final ClientResponse.Status status = response.getClientResponseStatus();
+ if(status == null){
+ final int statusCode = response.getStatus();
+ if(UNPROCESSABLE_ENTITY_HTTP_CODE == statusCode){
+ System.out.println(response.getEntity(String.class));
+ throw new UnprocessableEntityException(response);
+ }
+ else{
+ System.out.println("ClientResponseStatus is null, but found status Code = " + UNPROCESSABLE_ENTITY_HTTP_CODE);
+ throw new UnknownRecurlyException(response);
+ }
+ }
+ else{
+ if(status.equals(ClientResponse.Status.BAD_REQUEST)){
+ throw new BadRequestException(response);
+ }
+ else if(status.equals(ClientResponse.Status.UNAUTHORIZED)){
+ throw new UnauthorizedAccessException(response);
+ }
+ else if(status.equals(ClientResponse.Status.PAYMENT_REQUIRED)){
+ throw new PaymentRequiredException(response);
+ }
+ else if(status.equals(ClientResponse.Status.FORBIDDEN)){
+ throw new ForbiddenAccessException(response);
+ }
+ else if(status.equals(ClientResponse.Status.NOT_FOUND)){
+ throw new ResourceNotFoundException(response);
+ }
+ else if(status.equals(ClientResponse.Status.PRECONDITION_FAILED)){
+ throw new PreconditionFailedException(response);
+ }
+ else if(status.equals(ClientResponse.Status.INTERNAL_SERVER_ERROR)){
+ throw new InternalServerErrorException(response);
+ }
+ else if(status.equals(ClientResponse.Status.SERVICE_UNAVAILABLE)){
+ throw new ServiceUnavailableException(response);
+ }
+ }
+ }
+
+ protected abstract String getResourcePath();
+ protected abstract String getResourceCreationPath();
+
+ //default implementations for create, update and delete operation on a resource.
+ //read operations are static methods within respective resource classes.
+ public void create() throws Exception{
+ try{
+ getWebResourceBuilder(getResourceCreationPath()).post(this);
+ }
+ catch(final UniformInterfaceException uie){
+ throwStatusBasedException(uie.getResponse());
+ }
+ }
+
+ public void update() throws Exception{
+ try{
+ getWebResourceBuilder(getResourcePath()).put(this);
+ }
+ catch(final UniformInterfaceException uie){
+ throwStatusBasedException(uie.getResponse());
+ }
+ }
+
+ public void delete() throws Exception{
+ try{
+ getWebResourceBuilder(getResourcePath()).delete(this);
+ }
+ catch(final UniformInterfaceException uie){
+ throwStatusBasedException(uie.getResponse());
+ }
+ }
+}
95 src/com/kwanzoo/recurly/BillingInfo.java
@@ -0,0 +1,95 @@
+package com.kwanzoo.recurly;
+
+import javax.security.auth.login.AccountNotFoundException;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+
+import com.kwanzoo.recurly.exception.ResourceNotFoundException;
+import com.sun.jersey.api.client.GenericType;
+import com.sun.jersey.api.client.UniformInterfaceException;
+
+@XmlRootElement(name="billing_info")
+public class BillingInfo extends Base{
+ private static String resourceName = "billing_info";
+
+ public String accountCode;
+
+ public BillingInfo(){}
+
+ public BillingInfo(final String accountCode){
+ this.accountCode = accountCode;
+ }
+
+ //Required
+ @XmlElement(name="first_name")
+ public String firstName;
+
+ //Required
+ @XmlElement(name="last_name")
+ public String lastName;
+
+ //Required
+ @XmlElement(name="address1")
+ public String address1;
+
+ @XmlElement(name="address2")
+ public String address2;
+
+ @XmlElement(name="city")
+ public String city;
+
+ @XmlElement(name="state")
+ public String state;
+
+ //Required
+ @XmlElement(name="zip")
+ public String zip;
+
+ @XmlElement(name="country")
+ public String country;
+
+ @XmlElement(name="ip_address")
+ public String ipAddress;
+
+ //Required
+ @XmlElement(name="credit_card")
+ public CreditCard creditCard;
+
+ private static String getResourcePath(String accountCode){
+ return Account.pluralResourceName + "/" + accountCode + "/" + resourceName;
+ }
+
+ public static BillingInfo get(final String accountCode) throws Exception{
+ try{
+ return getWebResourceBuilder(getResourcePath(accountCode)).get(new GenericType<BillingInfo>(){});
+ }
+ catch(final UniformInterfaceException uie){
+ throwStatusBasedException(uie.getResponse());
+ return null;
+ }
+ }
+
+ @Override
+ protected String getResourcePath() {
+ return getResourcePath(accountCode);
+ }
+
+ @Override
+ protected String getResourceCreationPath(){
+ //A BillingInfo resource is contained within an Account resource
+ //No way to create a stand alone BillingInfo resource.
+ return null;
+ }
+
+ //checks if the account exists before updating the subscription.
+ public void checkedUpdate() throws Exception{
+ try{
+ Account.get(accountCode);
+ }
+ catch(final ResourceNotFoundException e){
+ throw new AccountNotFoundException();
+ }
+ update();
+ }
+}
25 src/com/kwanzoo/recurly/CreditCard.java
@@ -0,0 +1,25 @@
+package com.kwanzoo.recurly;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name="credit_card")
+public class CreditCard {
+ @XmlElement(name="number")
+ public String number;
+
+ @XmlElement(name="last_four")
+ public String lastFour;
+
+ @XmlElement(name="type")
+ public String type;
+
+ @XmlElement(name="verification_value")
+ public String verificationValue;
+
+ @XmlElement(name="month")
+ public Integer expirationMonth;
+
+ @XmlElement(name="year")
+ public Integer expirationYear;
+}
16 src/com/kwanzoo/recurly/Plan.java
@@ -0,0 +1,16 @@
+package com.kwanzoo.recurly;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name="plan")
+public class Plan{
+ @XmlElement(name="plan_code")
+ public String planCode;
+
+ @XmlElement(name="name")
+ public String name;
+
+ @XmlElement(name="version")
+ public Integer version;
+}
92 src/com/kwanzoo/recurly/Subscription.java
@@ -0,0 +1,92 @@
+package com.kwanzoo.recurly;
+
+import java.util.Date;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import com.sun.jersey.api.client.GenericType;
+import com.sun.jersey.api.client.UniformInterfaceException;
+
+@XmlRootElement(name="subscription")
+public class Subscription extends Base{
+ private static String resourceName = "subscription";
+
+ @XmlElement(name="plan_code")
+ public String planCode;
+
+ @XmlElement(name="quantity")
+ public Integer quantity;
+
+ @XmlElement(name="unit_amount")
+ public Integer unitAmount;
+
+ @XmlElement(name="timeframe")
+ public String timeframe;
+
+ @XmlElement(name="account")
+ public Account account;
+
+ @XmlElement(name="account_code")
+ public String accountCode;
+
+ @XmlElement(name="plan")
+ public Plan plan;
+
+ @XmlElement(name="state")
+ public String state;
+
+ @XmlElement(name="total_amount_in_cents")
+ public Integer totalAmountInCents;
+
+ @XmlElement(name="activated_at")
+ public Date activatedAt;
+
+ @XmlElement(name="canceled_at")
+ public Date canceledAt;
+
+ @XmlElement(name="expires_at")
+ public Date expiresAt;
+
+ @XmlElement(name="current_period_started_at")
+ public Date currentPeriodStartedAt;
+
+ @XmlElement(name="current_period_ends_at")
+ public Date currentPeriodEndsAt;
+
+ @XmlElement(name="trial_started_at")
+ public Date trialStartedAt;
+
+ @XmlElement(name="trial_ends_at")
+ public Date trialEndsAt;
+
+ private static String getResourcePath(String accountCode){
+ return Account.pluralResourceName + "/" + accountCode + "/" + resourceName;
+ }
+
+ public static Subscription get(final String accountCode) throws Exception{
+ try{
+ return getWebResourceBuilder(getResourcePath(accountCode)).get(new GenericType<Subscription>(){});
+ }
+ catch(final UniformInterfaceException uie){
+ throwStatusBasedException(uie.getResponse());
+ return null;
+ }
+ }
+
+ @Override
+ protected String getResourcePath() {
+ return getResourcePath(accountCode);
+ }
+
+ @Override
+ protected String getResourceCreationPath() {
+ return getResourcePath();
+ }
+
+ public Subscription(){}
+
+ public Subscription(final String accountCode){
+ this.accountCode = accountCode;
+ }
+}
10 src/com/kwanzoo/recurly/exception/AccountNotFoundException.java
@@ -0,0 +1,10 @@
+package com.kwanzoo.recurly.exception;
+
+public class AccountNotFoundException extends Exception{
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public String toString(){
+ return "Error performing recurly operation because precondition(existing recurly account) failed";
+ }
+}
11 src/com/kwanzoo/recurly/exception/BadRequestException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class BadRequestException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public BadRequestException(final ClientResponse response){
+ super(response);
+ }
+}
22 src/com/kwanzoo/recurly/exception/Base.java
@@ -0,0 +1,22 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class Base extends Exception{
+ private static final long serialVersionUID = 1L;
+
+ protected final ClientResponse response;
+
+ public Base(final ClientResponse response){
+ this.response = response;
+ }
+
+ public ClientResponse getResponse(){
+ return response;
+ }
+
+ @Override
+ public String toString(){
+ return "Internal error talking to payment system";
+ }
+}
11 src/com/kwanzoo/recurly/exception/ForbiddenAccessException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class ForbiddenAccessException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public ForbiddenAccessException(final ClientResponse response){
+ super(response);
+ }
+}
11 src/com/kwanzoo/recurly/exception/InternalServerErrorException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class InternalServerErrorException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public InternalServerErrorException(final ClientResponse response){
+ super(response);
+ }
+}
11 src/com/kwanzoo/recurly/exception/PaymentRequiredException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class PaymentRequiredException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public PaymentRequiredException(final ClientResponse response){
+ super(response);
+ }
+}
11 src/com/kwanzoo/recurly/exception/PreconditionFailedException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class PreconditionFailedException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public PreconditionFailedException(final ClientResponse response){
+ super(response);
+ }
+}
11 src/com/kwanzoo/recurly/exception/ResourceNotFoundException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class ResourceNotFoundException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public ResourceNotFoundException(final ClientResponse response){
+ super(response);
+ }
+}
11 src/com/kwanzoo/recurly/exception/ServiceUnavailableException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class ServiceUnavailableException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public ServiceUnavailableException(final ClientResponse response){
+ super(response);
+ }
+}
11 src/com/kwanzoo/recurly/exception/UnauthorizedAccessException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class UnauthorizedAccessException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public UnauthorizedAccessException(final ClientResponse response){
+ super(response);
+ }
+}
11 src/com/kwanzoo/recurly/exception/UnknownRecurlyException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class UnknownRecurlyException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public UnknownRecurlyException(final ClientResponse response){
+ super(response);
+ }
+}
11 src/com/kwanzoo/recurly/exception/UnprocessableEntityException.java
@@ -0,0 +1,11 @@
+package com.kwanzoo.recurly.exception;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class UnprocessableEntityException extends Base{
+ private static final long serialVersionUID = 1L;
+
+ public UnprocessableEntityException(final ClientResponse response){
+ super(response);
+ }
+}
368 src/com/kwanzoo/recurly/test/RecurlyTest.java
@@ -0,0 +1,368 @@
+package com.kwanzoo.recurly.test;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Properties;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.lang.RandomStringUtils;
+import org.junit.Test;
+
+import com.kwanzoo.recurly.Account;
+import com.kwanzoo.recurly.Base;
+import com.kwanzoo.recurly.BillingInfo;
+import com.kwanzoo.recurly.CreditCard;
+import com.kwanzoo.recurly.Subscription;
+
+public class RecurlyTest extends TestCase{
+ private String getRandStr(final int n){
+ return RandomStringUtils.randomAlphanumeric(n);
+ }
+
+ /*private String getRandNum(final int n){
+ return RandomStringUtils.randomNumeric(n);
+ }*/
+
+ @Override
+ public void setUp(){
+ Properties p = new Properties();
+ try{
+ p.load(new FileInputStream(System.getProperty("user.home") + "/" + "recurly_auth"));
+ String username = p.getProperty("recurly_username");
+ String password = p.getProperty("recurly_password");
+ Base.setAuth(username, password);
+ }
+ catch(IOException e){
+ e.printStackTrace();
+ }
+ }
+
+ //Create an account that doesn't exist and check if it can be found and check if the updates were applied correctly
+ @Test public void test1() throws Exception{
+
+ //create fresh account
+ final String accountCode = getRandStr(5);
+ String username = getRandStr(5);
+ String firstName = getRandStr(5);
+ String lastName = getRandStr(5);
+ String email = getRandStr(5)+"@site.com";
+ String companyName = getRandStr(5);
+
+ Account account = new Account();
+
+ account.accountCode = accountCode;
+ account.username = username;
+ account.firstName = firstName;
+ account.lastName = lastName;
+ account.email = email;
+ account.companyName = companyName;
+
+ account.create();
+
+ //check if find works
+ account = Account.get(accountCode);
+
+ assertEquals(accountCode, account.accountCode);
+ assertEquals(username, account.username);
+ assertEquals(firstName, account.firstName);
+ assertEquals(lastName, account.lastName);
+ assertEquals(email, account.email);
+ assertEquals(companyName, account.companyName);
+
+ //check if update works
+ username = getRandStr(5);
+ firstName = getRandStr(5);
+ lastName = getRandStr(5);
+ email = getRandStr(5)+"@site.com";
+ companyName = getRandStr(5);
+
+ account.username = username;
+ account.firstName = firstName;
+ account.lastName = lastName;
+ account.email = email;
+ account.companyName = companyName;
+
+ account.update();
+
+ account = Account.get(accountCode);
+
+ assertEquals(accountCode, account.accountCode);
+ assertEquals(username, account.username);
+ assertEquals(firstName, account.firstName);
+ assertEquals(lastName, account.lastName);
+ assertEquals(email, account.email);
+ assertEquals(companyName, account.companyName);
+ }
+
+ @Test public void test2() throws Exception{
+
+ //create fresh account
+ final String accountCode = getRandStr(5);
+ final String username = getRandStr(5);
+ String firstName = getRandStr(5);
+ String lastName = getRandStr(5);
+ final String email = getRandStr(5)+"@site.com";
+ final String companyName = getRandStr(5);
+
+ Account account = new Account();
+
+ account.accountCode = accountCode;
+ account.username = username;
+ account.firstName = firstName;
+ account.lastName = lastName;
+ account.email = email;
+ account.companyName = companyName;
+
+ account.create();
+
+ account = Account.get(accountCode);
+
+ //check if adding billing info for a fresh account works
+ final String number = "1";
+ String verificationValue = getRandStr(4);
+ Integer expirationMonth = (new Random()).nextInt(12);
+ Integer expirationYear = 2010 + (new Random()).nextInt(20);
+
+ firstName = account.firstName;
+ lastName = account.lastName;
+ String address1 = getRandStr(10);
+ String address2 = getRandStr(10);
+ String city = "San Fransisco";
+ String state = "CA";
+ String zip = "20240";
+ String country = "US";
+ String ipAddress = "127.0.0.1";
+
+ CreditCard creditCard = new CreditCard();
+ creditCard.number = number;
+ creditCard.verificationValue = verificationValue;
+ creditCard.expirationMonth = expirationMonth;
+ creditCard.expirationYear = expirationYear;
+
+ BillingInfo billingInfo = new BillingInfo(accountCode);
+ billingInfo.firstName = firstName;
+ billingInfo.lastName = lastName;
+ billingInfo.address1 = address1;
+ billingInfo.address2 = address2;
+ billingInfo.city = city;
+ billingInfo.state = state;
+ billingInfo.zip = zip;
+ billingInfo.country = country;
+ billingInfo.ipAddress = ipAddress;
+ billingInfo.creditCard = creditCard;
+
+ billingInfo.update();
+
+ billingInfo = BillingInfo.get(accountCode);
+
+ assertEquals(firstName, billingInfo.firstName);
+ assertEquals(lastName, billingInfo.lastName);
+ assertEquals(address1, billingInfo.address1);
+ assertEquals(address2, billingInfo.address2);
+ assertEquals(city, billingInfo.city );
+ assertEquals(state, billingInfo.state);
+ assertEquals(zip, billingInfo.zip);
+ assertEquals(country, billingInfo.country);
+ assertEquals(ipAddress, billingInfo.ipAddress);
+
+ //Recurly somehow returns empty for both the fields below
+ //assertEquals(billingInfo.creditCard.number);
+ //assertEquals(billingInfo.creditCard.verificationValue);
+
+ assertEquals(expirationMonth, billingInfo.creditCard.expirationMonth);
+ assertEquals(expirationYear, billingInfo.creditCard.expirationYear);
+
+ //check if updating billing info of an account that already has billing info works
+ verificationValue = getRandStr(4);
+ expirationMonth = (new Random()).nextInt(12);
+ expirationYear = 2010 + (new Random()).nextInt(20);
+
+ firstName = account.firstName;
+ lastName = account.lastName;
+ address1 = getRandStr(10);
+ address2 = getRandStr(10);
+ city = "Some City";
+ state = "NJ";
+ zip = "94105";
+ country = "IN";
+ ipAddress = "127.0.0.1";
+
+ creditCard = new CreditCard();
+ creditCard.number = number;
+ creditCard.verificationValue = verificationValue;
+ creditCard.expirationMonth = expirationMonth;
+ creditCard.expirationYear = expirationYear;
+
+ billingInfo = new BillingInfo(accountCode);
+ billingInfo.firstName = firstName;
+ billingInfo.lastName = lastName;
+ billingInfo.address1 = address1;
+ billingInfo.address2 = address2;
+ billingInfo.city = city;
+ billingInfo.state = state;
+ billingInfo.zip = zip;
+ billingInfo.country = country;
+ billingInfo.ipAddress = ipAddress;
+ billingInfo.creditCard = creditCard;
+
+ billingInfo.checkedUpdate();
+
+ billingInfo = BillingInfo.get(accountCode);
+
+ assertEquals(firstName, billingInfo.firstName);
+ assertEquals(lastName, billingInfo.lastName);
+ assertEquals(address1, billingInfo.address1);
+ assertEquals(address2, billingInfo.address2);
+ assertEquals(city, billingInfo.city );
+ assertEquals(state, billingInfo.state);
+ assertEquals(zip, billingInfo.zip);
+ assertEquals(country, billingInfo.country);
+ assertEquals(ipAddress, billingInfo.ipAddress);
+
+ }
+
+ @Test public void test3() throws Exception{
+ //create fresh account
+ final String accountCode = getRandStr(5);
+ final String username = getRandStr(5);
+ String firstName = getRandStr(5);
+ String lastName = getRandStr(5);
+ final String email = getRandStr(5)+"@site.com";
+ final String companyName = getRandStr(5);
+
+ Account account = new Account();
+
+ account.accountCode = accountCode;
+ account.username = username;
+ account.firstName = firstName;
+ account.lastName = lastName;
+ account.email = email;
+ account.companyName = companyName;
+
+ account.create();
+
+ account = Account.get(accountCode);
+
+ //subscribe to plan1
+ final String number = "1";
+ final String verificationValue = getRandStr(4);
+ final Integer expirationMonth = (new Random()).nextInt(12);
+ final Integer expirationYear = 2010 + (new Random()).nextInt(20);
+
+ firstName = account.firstName;
+ lastName = account.lastName;
+ final String address1 = getRandStr(10);
+ final String address2 = getRandStr(10);
+ final String city = "San Fransisco";
+ final String state = "NM";
+ final String zip = "99546";
+ final String country = "US";
+ final String ipAddress = "127.0.0.1";
+
+ final CreditCard creditCard = new CreditCard();
+ creditCard.number = number;
+ creditCard.verificationValue = verificationValue;
+ creditCard.expirationMonth = expirationMonth;
+ creditCard.expirationYear = expirationYear;
+
+ BillingInfo billingInfo = new BillingInfo(accountCode);
+ billingInfo.firstName = firstName;
+ billingInfo.lastName = lastName;
+ billingInfo.address1 = address1;
+ billingInfo.address2 = address2;
+ billingInfo.city = city;
+ billingInfo.state = state;
+ billingInfo.zip = zip;
+ billingInfo.country = country;
+ billingInfo.ipAddress = ipAddress;
+ billingInfo.creditCard = creditCard;
+
+ account.billingInfo = billingInfo;
+
+ String planCode = "testplan1";
+ Integer quantity = 1;
+
+ Subscription subscription = new Subscription(accountCode);
+ subscription.account = account;
+ subscription.planCode = planCode;
+ subscription.quantity = quantity;
+
+ subscription.create();
+
+ //TODO: get subscription and check with asserts
+ subscription = Subscription.get(accountCode);
+
+ assertEquals(accountCode, subscription.accountCode);
+ assertEquals(planCode, subscription.plan.planCode);
+ assertEquals("active", subscription.state);
+ assertEquals(quantity, subscription.quantity);
+
+ billingInfo = BillingInfo.get(accountCode);
+ assertEquals(firstName, billingInfo.firstName);
+ assertEquals(lastName, billingInfo.lastName);
+ assertEquals(address1, billingInfo.address1);
+ assertEquals(address2, billingInfo.address2);
+ assertEquals(city, billingInfo.city );
+ assertEquals(state, billingInfo.state);
+ assertEquals(zip, billingInfo.zip);
+ assertEquals(country, billingInfo.country);
+ assertEquals(ipAddress, billingInfo.ipAddress);
+
+ //Recurly somehow returns a null for both the fields below, probably for security purposes.
+ //assertEquals(billingInfo.creditCard.number);
+ //assertEquals(billingInfo.creditCard.verificationValue);
+
+ assertEquals(expirationMonth, billingInfo.creditCard.expirationMonth);
+ assertEquals(expirationYear, billingInfo.creditCard.expirationYear);
+
+ //downgrade check
+
+ planCode = "testplan0";
+ quantity = 2;
+
+ subscription = new Subscription(accountCode);
+ subscription.timeframe = "now";
+ subscription.planCode = planCode;
+ subscription.quantity = quantity;
+
+ subscription.update();
+
+ subscription = Subscription.get(accountCode);
+
+ assertEquals(accountCode, subscription.accountCode);
+ assertEquals(planCode, subscription.plan.planCode);
+ assertEquals("active", subscription.state);
+ assertEquals(quantity, subscription.quantity);
+
+ //upgrade check
+
+ planCode = "testplan2";
+ quantity = 3;
+
+ subscription = new Subscription(accountCode);
+ subscription.timeframe = "now";
+ subscription.planCode = planCode;
+ subscription.quantity = quantity;
+
+ subscription.update();
+
+ subscription = Subscription.get(accountCode);
+
+ assertEquals(accountCode, subscription.accountCode);
+ assertEquals(planCode, subscription.plan.planCode);
+ assertEquals("active", subscription.state);
+ assertEquals(quantity, subscription.quantity);
+
+ //cancel & check with asserts
+
+ subscription = new Subscription(accountCode);
+ subscription.delete();
+
+ subscription = Subscription.get(accountCode);
+ assertEquals("canceled", subscription.state);
+
+
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.