Introduction

Last updated 2 months ago

The primary purpose of Masonite Billing is to handle all of the trials, subscriptions and charges for your application. Let Masonite Billing handle all subscription integrations for your next SaaS.

Requires

  • Masonite 2.0.0 +

You can check your Masonite version by running pip show masonite

Installation

Installing Masonite Billing is simple. We just need a new configuration file, 2 new migrations and extend our User model.

Pip Install

terminal
$ pip install masonite-billing

Add the Service Provider

Simply add the Masonite Billing Service Provider to your providers list:

config/application.py
from billing.providers import BillingProvider
PROVIDERS = [
...
# Third Party Providers
BillingProvider,
# Application Providers
...
]

This will add a new install:billing command to craft. Just run:

terminal
$ craft install:billing

This will create a new configuration file in config/billing.py

Configuration File

All billing information will be located in the config/billing.py file and has 2 important constants:

...
DRIVER = 'stripe'
...
DRIVERS = {
'stripe': {
'client': os.getenv('STRIPE_CLIENT'),
'secret': os.getenv('STRIPE_SECRET'),
'currency': 'usd',
}
}

The DRIVER is the processor that Masonite billing will use and the DRIVERS constant is the configuration setting for the driver.

Although there is the DRIVER constant, Masonite Billing currently only supports Stripe. Other drivers like Braintree will be supported in later releases which should be backwards compatible.

Migrations

We'll need to create 2 new migrations: one to add columns to the users table and one migration to create a new subscriptions table. Just create these migration files with craft and copy and paste the migration into those files and migrate them.

Let's first add 2 new columns to our users table.

terminal
$ craft migration add_subscription_info_to_users --table users

Now just add this column to the migration file:

databases/migrations/2018_04_19_190813_add_subscription_columns_to_user.py
with self.schema.table('users') as table:
table.string('customer_id').nullable()
table.string('plan_id').nullable()

Now let's add a new subscriptions table.

terminal
$ craft migration create_subscriptions_table --create subscriptions
databases/migrations/2018_04_19_212703_create_subscriptions_table.py
with self.schema.create('subscriptions') as table:
table.increments('id')
table.integer('user_id').unsigned()
table.string('plan')
table.string('plan_id')
table.string('plan_name')
table.timestamp('trial_ends_at').nullable()
table.timestamp('ends_at').nullable()
table.timestamps()

Now just migrate the new migrations:

terminal
$ craft migrate

Stripe Authentication Keys

Just add your Stripe public and secret keys to your .env file:

.env
STRIPE_CLIENT=pk_Njsd993hdc...
STRIPE_SECRET=sk_sjs8yd78H8...

Billable Model

Masonite Billing consists of a model that should be inherited by whatever model you want to add subscription billing information to. In this example here, we will focus on adding the billing integration to our User model.

from config.database import Model
from billing.models.Billable import Billable
class User(Model, Billable):
__fillable__ = ['name', 'email', 'password']
__auth__ = 'email'

Once that is added we will now have a plethora of methods we can use to subscribe and charge our user.

Read more about how to handle subscription and payment information in the Usage documentation.