Stripe Connect & Rails — Part 3

This is Part 3 of my series on integrating Stripe Connect into a ruby on rails application

Tinus Wagner
5 min readJun 11, 2017

Read part 1 here

Read part 2 here

Moving on!

So in Part 2 we built a simple little application using Stripe Connect. We now have merchants and customers freely transacting and making payments. In the final part of our series we’ll allow our platform to:

  • Charge a fee for purchases, and make some money!
  • Credit our users, allowing them to purchase goods at reduced prices!

Charging a fee

First things first, let’s generate a migration to add the fee field to each of our merchants, and a field to track fee’s charged on transactions.

P.S. if you plan on changing fee percentages often, it may be worth saving the fee in your transaction record as well for historical accuracy!

Once this has been migrated let’s add it to our admin view & controller, and create a little form with a PUT action to set a fee.

And our view

let’s set a little model method to handle updating this attribute:

def set_fee(amount)
update_attribute(:fee, amount)
end

We’ll also need to update our merchant controller to be able to deal with a record being updated from two forms on two seperate pages, and with seperate parameters. So let’s dive into our update action and add some conditional routing.

Awesome, now our admins can set a fee for each individual merchant.

Let’s string this onto our Stripe Charge.

The first thing we’ll need to do is convert our fee (Which Stripe refers to as an application_fee) to a percentage out of 100, which we can then string it into our existing Stripe::Charge.

So if we try to purchase something now we should see something like this on success:

We should also be able to confirm this from within Stripe if we look up that specific charge.

And That’s Fees sorted! It’s that simple!

Next up

Crediting Users

You may have noticed that we already have a few things in place for this functionality. We have:

  1. In the admin view a button to credit a user with $10.00
  2. A field in the users records tracking the amount credited.
A very popular tactic for spending VC money ;)

So to tackle this within a stripe charge we need to:

  1. First check if a user has credit
  2. Determine if the user has enough credit to cover the entire charge
  3. Or whether it is only going to be partially covered by credit, and we’re supposed to split the charge into two charges!

The credited portion (which is covered by the users credit), should be paid by the marketplace, and not charged a fee (why would we charge ourselves a fee?)

The remainder should be paid by the user as normal, and thereafter, we should update how much credit the user has left.

P.S. Stripe has a minimum charge amount which is varies by currency, but for the sake of our USD based example it’s $0.50.

So the first thing we need to do is register a credit card associated with our platform, This is where an admin account comes in useful.

So grab the old company card, and register as a customer with that card. Once you’ve successfully done that, grab the customer_id of the admin user and save that in our .env file as our MASTER_CREDIT_ACCOUNT. When the user has credit and the platform needs to cover the credited portion of a transaction, this is the account we will use.

Now we need to start refactoring our transaction.rb model file to first check if the user has credit, if he the user does have credit:

  1. Is it enough to cover the entire charge?
  2. Is it enough to cover a portion of the charge
  3. If a portion is covered, what is the remainder we need to charge the user?

So let’s start refactoring our transactions model

Basically,

  • if the user has no credit, we make a regular charge.
  • If the user does have credit, we see how much.
  • If it’s enough to cover the entire charge, we make a single payment from the MASTER_CREDIT_ACCOUNT.
  • If not, then we split the payment into two charges.
  • If the charge was covered by the user, we’ll update the transaction record to reflect that
  • If the charge was covered by the platform, we’ll update the transaction record to reflect that.
  • We also render an alternate description within our charge if a credited transaction has occurred, so we can reconcile that to the original purchase.

So if a charge is totally covered by credit:

Our application will visualise this as follows:

If we then check in Stripe we’ll see the following

Note that the fee here is the Stripe processing fee, not our application_fee

If a charge is partially covered by credit

Our users will only be charged the portion that credit doesn’t cover, and we will charge a fee based on this remaining amount.

If we now check stripe, we’ll see two separate charges:

The credited charge :

And the remainder with an application fee

Finally, a regular charge

And there you have it!

Congrats! You’ve now set up Stripe Connect to charge a platform fee, and you’re able to credit users with a bit of startup money to get them spending!

Well done, and thanks for reading.

Please get in touch if you’ve spotted any significant errors or mistakes! I’m only human!

--

--