Here at Rootstrap, we don't believe in reinventing the wheel. We believe in making things simple and high quality of course.
In this blog post, you’ll learn how to start working with Stripe and quickly have fully functioning online payments in your apps.
- Easy to implement and use
- Fast to develop, so your client will be happy
- Solves most of your usual payment problems, so you don't lose time or clients (even worst)
- Amazing dashboard with a lot of capabilities so your clients financial team can work along with you
- Expensive (high % fee)
This post assumes you already created a Stripe account and so you have access to dashboard and its configuration. These examples are for Rails and React, but this also works for many other web and custom mobile app frameworks.
- Add these two gems:
- Stripe to achieve the integration
- Stripe Testing to test your integration, you don't want to end up writing lots of mocking classes, right?
- Configure your keys & version from the Stripe dashboard
- Add this package Stripe
- Configure your App to use the same api key as for rails (make sure it is the same, as you start moving between envs you may forget it). Remember that there is a testing key and a live one.
Add an env file to store your keys
Add your Stripe wrapper
START USING PAYMENTS WITH STRIPE
REACT - DO YOURSELF A FAVOR AND USE THE EXISTING COMPONENT
I'm not a fan of reinventing the wheel by any means, the design these components provide is more than enough for 99% of the apps you will be building. But if you insist, be prepared to spend 2 weeks dealing with details instead of 2 days.
RAILS - DON'T TRY TO STORE ALL THE INFO (IT'S ILEGAL)
You will tend to store more credit card info that you need. The only info that you need to store in your database (for basic usage) is:
- [.c-inline-code]customer_id[.c-inline-code] : Stripe customer identifier that you will store in your User for example
- [.c-inline-code]card_id[.c-inline-code] : Stripe card identifier
The [.c-inline-code]token_id[.c-inline-code] you will get from your frontend is a short lived token that is only needed for an atomic operation.
Add a [.c-inline-code]customer_id[.c-inline-code] field to your user (or Shop in next example). Add a [.c-inline-code]card_id[.c-inline-code] to your user (or Shop in next example).
Now take this service example (Shopify page example):
And this simple controller:
And that's all! You can start charging your users now! All fraud detections and customer service actions can be managed directly from Stripe's dashboard.
To create a subscription you need to define it, then create a product in Stripe (this last one is really clear looking at the dashboard, so I'm not going to explain it)
CREATING THE SUBSCRIPTION
In that model you will store attribures like: [.c-inline-code]expires_at[.c-inline-code], [.c-inline-code]type[.c-inline-code] or even [.c-inline-code]provider[.c-inline-code] if later you want to extend to other providers like PayPal or Apple Pay
Finally to create them on Stripe is quite simple:
Coupons are the abstract concept of [.c-inline-code]30% off[.c-inline-code] for example, when you apply that coupon to a user that's called a [.c-inline-code]discount[.c-inline-code]. So you should define some discounts on Stripe and store their ids in your database to apply them to users. There are two types of coupons [.c-inline-code]percentage[.c-inline-code] & [.c-inline-code]fixed amount[.c-inline-code], and any of them can be one time only or have the capability to be applied multiple times. So when you try to apply a coupon to a subscription, for example, remember that it can fail if you reached the maximum usage number.
Another useful case that is worth mentioning is to apply a coupon to a user, this means that they will have a positive balance for any future invoice (be careful if you charge users with multiple products)
These are your billing items, so for the case of a web subscription, you will just have 1 subscription item. For specific cases like an amazon cart or any complicated use case (where you have multiple items being added to purchase) is where you have to start considering adding some specific logic to your app. I won't get really into detail about this, I just wanted to show the general concept behind this, maybe I will write more in detail in a future post.
Don't overthink it, there is a webhook for most of your use cases. But for this specific need you can configure the following events:
- customer.subscription.updated This event happens every time a susbscription is updated according to this documentation
- customer.subscription.deleted As simple as it sounds, it tells you when a subscription is canceled so you can take the actions needed in your app (possibly disable the associated account)
- invoice.payment_succeeded This is a really important one! It tells us when payment is actually accepted by the credit card provider (some times there can be fraud or the payment could get declined)
There are a lot of them and they will solve most of your problems, the only downcase is the headache trying to understand which exactly to use. I'm sorry to disappoint you if you reached here trying to answer this question but up to now I only know this page that explains the different existing webhooks and what they do. The other option is when you go to create a webhook from the developer's Stripe dashboard, they explain a bit more in detail what each event does.
RECOMMENDATIONS FOR FURTHER PAYMENT IMPLEMENTATION
Keep these Stripe documentation pages as your friends:
Sometimes there are two or even three ways of solving a problem, so consider this and take your time to analyze each requirement properly before you start coding.
You can easily add online payments to your app and test it in just 1 week (or so), that's amazing! The other amazing thing is that you can start managing most of the daily based situations like fraud of disputes just from the dashboard (you don't need to keep coding).
The difficult part of this is when you start adding more concrete and detailed transactions and supporting multiple transfer types (like bank account transfers instead of just Visa or MasterCard). So if you liked this post and want to know more don't hesitate to leave some comments asking for it!