Payment processing can be a common solution that most web applications need, however most people probably haven’t considered implementing a custom solution with cryptocurrency. In this article I will share with you how to implement your own payment processor using Stellar.

This tutorial assumes you already have some basic Rails knowledge but don’t let it deter you. The application is very simple.

Payment Processor Design

The solution we are going to build in this article takes advantage of a simple feature most payment systems provide called memo. A memo is metadata attached to a transaction to help you pass extra information. Here is an example scenario.

You will be purchasing “content” in this example tutorial.

Start by purchasing

You will click “Purchase” to generate a memo address which you must include when creating a transaction on the Stellar Network.

Copy memo

The payment processor will be listening to transactions by the merchant account through Stellar’s Rest API.

It will mark the purchase as completed when the memo matches.

Setup Test Network Accounts

When making a transaction with a memo you must first understand what resources are necessary to create a transaction within Stellar Network. First, you must create an account in Stellar Test Network or even easier use the Stellar Laboratory to do so.

You will need to create two accounts. One as the merchant account of the application and one as an example customer account.

You can then fund your customer account on the test network using the friendbot by Stellar.

Lastly you will need to create a transaction with the memo generated from the Purchase object.

Rails Application Setup

For this example application, I used Ruby on Rails. You can clone the base application which includes a basic database schema, user interface, and user authentication using Devise. Everything is already setup except the payment processor. Below we will go over how the payment processor works with commented code. I highly encourage you to expand its functionality to get the most out of this tutorial.

Database Design

Schema for this tutorial

The most important object to focus on for this tutorial is the Purchase record which has a reference to the Content record (the object being purchased). The Purchase table includes a memo column to build a 28-bit string (maximum size you can include in transaction memo).

The Setting record is there to help us store an important attribute in the database each time we run the payment processor. We will expand on this more below in the code example.

Creating the Payment Processor

So you might be wondering how we plan on listening for transactions with a memo. Well Stellar’s API makes this really easy with its beautifully written API. You can look up transactions by an account public key.

However you wouldn’t want to always look up every transaction each time you need to process payments as that wouldn’t scale. That is where the cursor attribute comes in handy. You can start from where you left off by storing the cursor from the transaction object. We plan on storing the cursor in a Setting record.

Code is commented below explaining each line, first is the HTTP object to call Stellar Network Rest API and the second is the Payment Processor.

Stellar SDK

Payment Processor

So now if you run PaymentProcessor.perform in a job or in rails console manually. It will read your account for any transactions and extract memo’s to mark payments as completed. Now this is a pretty simple solution but gives you an idea of how powerful the Stellar Network API is.

Completed Application

The completed payment processor code is on the completed branch just incase you prefer to clone the full solution or want to know how to arrange your lib files.

Conclusion

If you have read the Stellar docs before you probably know you can stream transactions by account public key, however you may not want to stream a HTTP connection so this is a great alternative. If you want to take this tutorial to the next level, you could take balance into consideration when making a transaction. You could remove the completed boolean flag and apply a balance owed against the Purchase object to track when content is purchased.

Hope this tutorial helped you understand the Stellar Network API a little better or influenced you to create something of your own. If you have any questions feel free to comment on this article or reach out to me. I am also open to any suggestions to this tutorial or ideas for other tutorials that would interest you.

Resources