Laravel: Subscriptions, on premises
If you liked this, then remember to plan, charge and subscribe!
Using subscriptions on applications has been always problematic. Unless you push that along your billing to a third party like Stripe, Paddle, Chargebee, Chargify, Fastsprint, to name a few, you will find that is not something everyone is happy to do, let alone develop.
Sometimes you don’t have that luxury to hand-off charges. For example, sometimes you need to use local payment services, which offer their own obnoxious API or don’t support subscriptions at all. That was my case for project that required a local payment system, Webpay.
We needed an on-premises subscription system.
That’s why I created a package to handle subscriptions, with whatever payment system you may plan to have.
Subscriptions easy as Plan, charge and Subscribe
The package is relatively simple to understand. Everything starts with Plans, and end up with Entities, which can be your simple User model, or something like “Admins” or “Companies”.
Plans work like a blueprint to create Subscriptions from it. This separates the global state of a Plan from a Subscription, so if the Plan later is deleted, the Subscription is not affected.
Subscriptions themselves contain information about the duration, name, along other data. A subscription will become expired, thus no longer active, after the ending date.
Subscribers are the ones to are attached to these subscriptions. The package goes for a Many-to-Many approach. For example, an User can be subscribed to multiple Subscriptions at the same time, and Subscriptions can be shared (if you want to) across multiple subscribers. Obviously, this is opt-in, so you can disable it.
The above seems basic, but is supported with a plethora of helpers, and even Policies, out of the box.
Show me how it works
Well, firs you execute the Artisan command subscriptions:install
, which adds some convenience files.
The Plans you may want to create are read from the plans/plans.php
, using a convenient Plan Builder, much like your average Routes.
Once your plans are ready, push them into the database using subscriptions:plans
.
Finally, add the WithSubscriptions
trait to your User model (or any other model) and that’s it.
Subscribing and renewing subscriptions are easy as one method call. For example, to subscribe a use, we can call subscribeTo()
and the instance of the Plan.
What about capabilities, or metadata?
The neat part is that Plans, hence all their Subscriptions, support “capabilities”. You can think of these as a configuration tree, much like the config()
helper. The capabilities set at plan-level gets cloned to its Subscriptions when they’re created.
This seems conveniente, but what’s more convenient is that you can make checks over a capability value. For example, does the Subscription allow to use deliveries? Easy:
But what, there is more…
I know how cumbersome is to manage subscriptions in an application, as I have done it several times. This package also includes enough tools to think about more your idea than wiring up a Subscription system:
- Support for one-type subscribers or polymorphic subscribers.
- One method for renewing, cancelling, terminating, attaching and detaching users.
- Per-subscription and per-subscriber metadata.
- Plan and Subscription Factories.
- Plan and Subscription Authorization Gates (editable).
- Editable migrations (add your own columns).
- Events for Subscriptions.
- On-demand custom Plans.
How do I install this?
I just hosted the README if you’re curious on how to install and use the package, which is available for my sponsors.
You can also walk around other packages I have made that may be useful for you.