How to load data from Braintree to Redshift

Blendo Team

How may I load data from Braintree to Redshift for further analysis? The purpose of this post is to help you define a process or pipeline, for getting your subscription related data from Braintree and load it into Amazon Redshift for further analysis. We will see how to access and extract your data from Braintree through its API and how to load it into Redshift. This process requires from you to write the code to get the data and make sure that this process will run every time new data are generated. Alternatively, in order to load your data from Braintree to Redshift you can use products like Blendo that can handle this kind of problems automatically for you.

About Braintree

Load data from Braintree

Braintree is a full-stack payments platform that makes it easy to accept payments in your app or website. Our service replaces the traditional model of sourcing a payment gateway and merchant account from different providers. It offers a simple and robust way to accept payments or enable buying from almost anywhere, in your mobile app or online. Braintree gives access to multiple payment methods:

  • Credit / Debit cards: you can accept cards of any type.
  • Apple pay: support for Apple wallet.
  • Android Pay: Accept payments from Android Pay.
  • Venmo: Simplified mobile buying.

It offers simple pricing:

  • First $50K are free of fees
  • There are no minimum or monthly fees
  • After the first $50K the cost is 2.9% + $.30 per transaction

And top notch security:

  • AVS: Helps to verify the provided address.
  • CVV: Ensures that the verification numbers are always validated
  • Risk Threshold: Configure rules to detect fraud

And finally together with all the above, world-class support.

A payment platform like Braintree holds a large number of data related to your company and your customers that are extremely valuable for your business. With data coming from your payment system, you can calculate important KPIs like your revenues and your churn and if you can get access to all the available data your analysts can do wonders. Fortunately Braintree exposes a rich ecosystem of tools and APIs that you can use to get the most out of your payment data.

About Amazon Redshift

Load data to Amazon Redshift

Load data to Amazon Redshift

Amazon Redshift is one of the most popular data warehousing solutions which is part of the Amazon Web Services (AWS) ecosystem. It is a petabyte scale, fully managed data warehouse as a service solution that runs on the cloud. It is SQL based and you can communicate with it as you would do with PostgreSQL, actually you can use the same driver although it would be better to use the drivers recommended by Amazon. You can connect either through JDBC or ODBC connections.

Extract your data from Braintree

Braintree, as it is common with payment gateways, exposes an API which can be used to integrate a product with payment services. The access to this API happens through a number of clients or SDKs that Braintree offers:

Instead of a public REST API, Braintree provides client libraries in seven languages to ease integration with our gateway. This choice is deliberate as Braintree believes that in this way they can guarantee:

  1. Better security
  2. Better platform support. And
  3. Backward compatibility

The languages they targeted with their SDKs cover the majority of the frameworks and needs. For example, with the Java SDK they can also support the rest of the JVM languages like Scala and Clojure.

Braintree API Authentication

To authenticate against the Braintree API and perform either transactions or pull data, the following credentials are required.

  • Public key: user-specific public identifier
  • Private key: user-specific secure identifier that should not be shared.
  • Merchant ID: unique identifier for the gateway account.
  • Environment: Sandbox (for testing) or production.

For more information on how to retrieve the above information you can check the credentials documentation.

Braintree API Rate Limiting

For a system that handles payments, rate limiting doesn’t really make sense. I guess you wouldn’t like to see some of your payments failing because it happens that you have too many customers you are dying to pay you. For this reason, Braintree has implemented some really sophisticated algorithms to ensure that if one of their users goes crazy for any reason, this will not affect the others. So they are actually operating outside of the conventional practices of setting up rate limits. Nevertheless you should always make sure that you respect the service you are interacting with and the code you write is not abusing it.

Endpoints and available Resources

The Braintree API exposes a number of resource through the available SDKs, with these you can interact with the service and perform anything that is part of the functionalities of the Braintree platform.

  • Add-ons: returns a collection all the add-ons that are available.
  • Address: through this resource you can create and manage addresses for your customers. There’s a limit of 50 addresses per customer and a customer ID is always required for the operations associated with this resource.
  • Client Token: This resource is available for creating tokens that will authenticate your client to the Braintree platform.
  • Credit Card: Deprecated
  • Credit card verification: Returns information related to the verification of credit cards.
  • Customer: your customer 🙂 with all the information needed in Braintree to perform payments
  • Discount: Access to all the discounts that you have created in the Braintree platform.
  • Merchant Account: information about merchants in the platform.
  • Payment methods: Objects that represent payments
  • Plan: Information about the different plans that you have created in Braintree platform.
  • Settlement Batch Summary: The settlement batch summary displays the total sales and credits for each batch for a particular date.
  • Subscription: All the subscriptions that have been created on behalf of your customers inside the Braintree platform.
  • Transaction: This functionality is specific to Marketplace

All the above resources are manipulated through the SDKs that Braintree maintains. In most cases the full range of CRUD operations are supported, unless it doesn’t make sense or if there are security concerns. In general you can interact with everything that is available on the platform. Through the same SDKs we can all fetch information that we can then store locally to perform our analytics. Each one can offer back al its of results that we can consume, let’s assume that we want to get a list of all the Customers we have with all their associated data. In order to do that we first need to perform a search query on the Braintree API, for example in Java:

CustomerSearchRequest request = new CustomerSearchRequest()
    .id().is("the_customer_id");

ResourceCollection<Customer> collection = gateway.customer().search(request);

for (Customer customer : collection) {
    System.out.println(customer.getFirstName());
}

With the above query we will be searching for all the entries that belong to a customer with the given ID. Braintree has a very reach search mechanism that allows you to perform complex queries based on your data. For example, you might search based on dates and get only the new customers back. Each customer object that will be returned, will contain the following fields.

Addresses
AndroidPayCards
ApplePayCards
Company
CreatedAt
CreditCards
CustomFields
Email
Fax
FirstName
Id
LastName
PaymentMethods
PaypalAccounts
Phone
UpdatedAt
Website

The above fields will be the columns of the Customer table that we will create for storing the Customer data.

Paging is transparently managed by the SDK and the Braintree API so you won’t have to worry about how to iterate on a large number of records. When you get your results you will get an Iterator object which will iterate over all the results in a lazy way for keeping the resource consumption low.

What is important to notice is that the above data are available encapsulated into the structures that each SDK is exposing, so if you need the data in JSON format for example, this is something that you have to take care by converting the objects you get as results into JSON objects.

Prepare your Braintree Data for Amazon Redshift

Amazon Redshift is built around industry-standard SQL with added functionality to manage very large datasets and high performance analysis. So, in order to load your data into it you will have to follow its data model which is a typical relational database model. The data you extract from your data source should be mapped into tables and columns. Where you can consider the table as a map to the resource you want to store and columns the attributes of that resource. Also, each attribute should adhere to the datatypes that are supported by Redshift, currently the datatypes that are supported are the following:

  • SMALLINT
  • INTEGER
  • BIGINT
  • DECIMAL
  • REAL
  • DOUBLE PRECISION
  • BOOLEAN
  • CHAR
  • VARCHAR
  • DATE
  • TIMESTAMP

As your data are probably coming in a representation like JSON that supports a much smaller range of data types you have to be really careful about what data you feed into Redshift and make sure that you have mapped your types into one of the datatypes that is supported by Redshift. Designing a Schema for Redshift and mapping the data from your data source to it is a process that you should take seriously as it can both affect the performance of your cluster and the questions that you can answer. It’s always a good idea to have in your mind the best practices that Amazon has published regarding the design of a Redshift database. When you have concluded on the design of your database you need to load your data on one of the datasources that are supported as input by Redshift, these are the following:

Load data from Braintree to Redshift

The first step to load your Braintree data to Redshift, is to put them in a source that Redshift can pull it from. As it was mentioned earlier there are three main data sources supported, Amazon S3, Amazon DynamoDB and Amazon Kinesis Firehose, with Firehose being the most recent addition as a way to insert data into Redshift.

To upload your data to Amazon S3 you will have to use the AWS REST API, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. The first task that you have to perform is to create a bucket, you do that by executing an HTTP PUT on the Amazon AWS REST API endpoints for S3. You can do this by using a tool like CURL or Postman or Apirise. Or use the libraries provided by Amazon for your favourite language. You can find more information by reading the API reference for the Bucket operations on Amazon AWS documentation.

After you have created your bucket you can start sending your data to Amazon S3, using again the same AWS REST API but by using the endpoints for Object operations. As in the Bucket case you can either access the HTTP endpoints directly or use the library of your preference.

DynamoDB imports data again from S3, it adds another step between S3 and Amazon Redshift so if you don’t need it for other reasons you can avoid it.

Amazon Kinesis Firehose is the latest addition as a way to insert data into Redshift and offers a real time streaming approach into data importing. The necessary steps for adding data to Redshift through Kinesis Firehose are the following:

  1. create a delivery stream
  2. add data to the stream

whenever you add new data to the stream, Kinesis takes care of adding these data to S3 or Redshift, again going through S3 in this case is redundant if your goal is to move your data to Redshift. The execution of the previous two steps can be performed either through the REST API or through your favourite library just as in the previous two cases. The difference here is that for pushing your data into the stream you’ll be using a Kinesis Agent.

Amazon Redshift supports two methods for loading data into it. The first one is by invoking an INSERT command. You can connect to your Amazon Redshift instance with your client, using either a JDBC or ODBC connection and then you perform an INSERT command for your data.

insert into category_stage values
(12, 'Concerts', 'Comedy', 'All stand-up comedy performances');

The way you invoke the INSERT command is the same as you would do with any other SQL database, for more information you can check the INSERT examples page on the Amazon Redshift documentation.

Redshift is not designed for INSERT like operations, on the contrary, the most efficient way of loading data into it is by doing bulk uploads using a COPY command. You can perform a COPY command for data that lives as flat files on S3 or from an Amazon DynamoDB table. When you perform COPY commands, Redshift is able to read multiple files in simultaneously and it automatically distributes the workload to the cluster nodes and performs the load in parallel. As a command COPY is quite flexible and allows for many different ways of using it, depending on your  use case. Performing a COPY on amazon S3 is as simple as the following command:

copy listing
from 's3://mybucket/data/listing/'
credentials 'aws_access_key_id=;aws_secret_access_key=';

For more examples on how to invoke a COPY command you can check the COPY examples page on Amazon Redshift documentation. As in the INSERT case, the way to perform the COPY command is by connecting to your Amazon Redshift instance using a JDBC or ODBC connection and then invoke the commands you want using the SQL Reference from Amazon Redshift documentation.

The best way to load data from Braintree to Amazon Redshift and possible alternatives

So far we just scraped the surface of what can be done with Amazon Redshift and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from and the requirements of your use case. Things can get even more complicated if you want to integrate data coming from different sources. A possible alternative, instead of writing, hosting and maintaining a flexible data infrastructure, is to use a product like Blendo that can handle this kind of problems automatically for you.

 Blendo integrates with multiple sources or services like databases, CRM, email campaigns, analytics and more.  Quickly and safely move all your data from Braintree into Amazon Redshift and start generating insights from your data.