Subscriptions
Creating Subscriptions
To create a subscription, first retrieve an instance of your billable model, which typically will be an instance of App\Models\User
. Once you have retrieved the model instance, you may use the newSubscription
method to create the model's subscription:
The first argument passed to the newSubscription
method should be the internal type of the subscription. If your application only offers a single subscription, you might call this default
or primary
. This subscription type is only for internal application usage and is not meant to be shown to users. In addition, it should not contain spaces and it should never be changed after creating the subscription. The second argument is the specific price the user is subscribing to. This value should correspond to the price's identifier in Stripe.
The create
method, which accepts a Stripe payment method identifier or Stripe PaymentMethod
object, will begin the subscription as well as update your database with the billable model's Stripe customer ID and other relevant billing information.
Passing a payment method identifier directly to the create
subscription method will also automatically add it to the user's stored payment methods.
Collecting Recurring Payments via Invoice Emails
Instead of collecting a customer's recurring payments automatically, you may instruct Stripe to email an invoice to the customer each time their recurring payment is due. Then, the customer may manually pay the invoice once they receive it. The customer does not need to provide a payment method up front when collecting recurring payments via invoices:
The amount of time a customer has to pay their invoice before their subscription is canceled is determined by the days_until_due
option. By default, this is 30 days; however, you may provide a specific value for this option if you wish:
Quantities
If you would like to set a specific quantity for the price when creating the subscription, you should invoke the quantity
method on the subscription builder before creating the subscription:
Additional Details
If you would like to specify additional customer or subscription options supported by Stripe, you may do so by passing them as the second and third arguments to the create
method:
Coupons
If you would like to apply a coupon when creating the subscription, you may use the withCoupon
method:
Or, if you would like to apply a Stripe promotion code, you may use the withPromotionCode
method:
The given promotion code ID should be the Stripe API ID assigned to the promotion code and not the customer facing promotion code. If you need to find a promotion code ID based on a given customer facing promotion code, you may use the findPromotionCode
method:
In the example above, the returned promotionCode
object is an instance of Shopkeeper's PromotionCode
. This class decorates an underlying Stripe.PromotionCode
object. You can retrieve the coupon related to the promotion code by invoking the coupon
method:
The coupon instance allows you to determine the discount amount and whether the coupon represents a fixed discount or percentage based discount:
You can also retrieve the discounts that are currently applied to a customer or subscription:
The returned Shopkeeper's Discount
instances decorate an underlying Stripe.Discount
object instance. You may retrieve the coupon related to this discount by invoking the coupon
method:
If you would like to apply a new coupon or promotion code to a customer or subscription, you may do so via the applyCoupon
or applyPromotionCode
methods:
Remember, you should use the Stripe API ID assigned to the promotion code and not the customer facing promotion code. Only one coupon or promotion code can be applied to a customer or subscription at a given time.
For more info on this subject, please consult the Stripe documentation regarding coupons and promotion codes.
Adding Subscriptions
If you would like to add a subscription to a customer who already has a default payment method you may invoke the add
method on the subscription builder:
Creating Subscriptions From the Stripe Dashboard
You may also create subscriptions from the Stripe dashboard itself. When doing so, Shopkeeper will sync newly added subscriptions and assign them a type of default
. To customize the subscription type that is assigned to dashboard created subscriptions, define webhook event handlers.
In addition, you may only create one type of subscription via the Stripe dashboard. If your application offers multiple subscriptions that use different types, only one type of subscription may be added through the Stripe dashboard.
Finally, you should always make sure to only add one active subscription per type of subscription offered by your application. If a customer has two default
subscriptions, only the most recently added subscription will be used by Shopkeeper even though both would be synced with your application's database.
Checking Subscription Status
Once a customer is subscribed to your application, you may easily check their subscription status using a variety of convenient methods. First, the subscribed
method returns true
if the customer has an active subscription, even if the subscription is currently within its trial period. The subscribed
method accepts the type of the subscription as its first argument:
The subscribed
method also makes a great candidate for a route middleware, allowing you to filter access to routes and controllers based on the user's subscription status:
If you would like to determine if a user is still within their trial period, you may use the onTrial
method. This method can be useful for determining if you should display a warning to the user that they are still on their trial period:
The subscribedToProduct
method may be used to determine if the user is subscribed to a given product based on a given Stripe product's identifier. In Stripe, products are collections of prices. In this example, we will determine if the user's default
subscription is actively subscribed to the application's "premium" product. The given Stripe product identifier should correspond to one of your product's identifiers in the Stripe dashboard:
By passing an array to the subscribedToProduct
method, you may determine if the user's default
subscription is actively subscribed to the application's "basic" or "premium" product:
The subscribedToPrice
method may be used to determine if a customer's subscription corresponds to a given price ID:
The recurring
method may be used to determine if the user is currently subscribed and is no longer within their trial period:
If a user has two subscriptions with the same type, the most recent subscription will always be returned by the subscription
method. For example, a user might have two subscription records with the type of default
; however, one of the subscriptions may be an old, expired subscription, while the other is the current, active subscription. The most recent subscription will always be returned while older subscriptions are kept in the database for historical review.
Canceled Subscription Status
To determine if the user was once an active subscriber but has canceled their subscription, you may use the canceled
method:
You may also determine if a user has canceled their subscription but are still on their "grace period" until the subscription fully expires. For example, if a user cancels a subscription on March 5th that was originally scheduled to expire on March 10th, the user is on their "grace period" until March 10th. Note that the subscribed
method still returns true
during this time:
To determine if the user has canceled their subscription and is no longer within their "grace period", you may use the ended
method:
Incomplete and Past Due Status
If a subscription requires a secondary payment action after creation the subscription will be marked as incomplete
. Subscription statuses are stored in the stripe_status
column of Shopkeeper's subscriptions
database table.
Similarly, if a secondary payment action is required when swapping prices the subscription will be marked as past_due
. When your subscription is in either of these states it will not be active until the customer has confirmed their payment. Determining if a subscription has an incomplete payment may be accomplished using the hasIncompletePayment
method on the billable model or a subscription instance:
When a subscription has an incomplete payment, you should direct the user to Shopkeeper's payment confirmation page, passing the latestPayment
identifier. You may use the latestPayment
method available on subscription instance to retrieve this identifier:
If you would like the subscription to still be considered active when it's in a past_due
or incomplete
state, you may set the keepPastDueSubscriptionsActive
and keepIncompleteSubscriptionsActive
options in your Shopkeeper's configuration:
When a subscription is in an incomplete
state it cannot be changed until the payment is confirmed. Therefore, the swap
and updateQuantity
methods will throw an exception when the subscription is in an incomplete
state.
Changing prices
After a customer is subscribed to your application, they may occasionally want to change to a new subscription price. To swap a customer to a new price, pass the Stripe price's identifier to the swap
method. When swapping prices, it is assumed that the user would like to re-activate their subscription if it was previously canceled. The given price identifier should correspond to a Stripe price identifier available in the Stripe dashboard:
If the customer is on trial, the trial period will be maintained. Additionally, if a "quantity" exists for the subscription, that quantity will also be maintained.
If you would like to swap prices and cancel any trial period the customer is currently on, you may invoke the skipTrial
method:
If you would like to swap prices and immediately invoice the customer instead of waiting for their next billing cycle, you may use the swapAndInvoice
method:
Prorations
By default, Stripe prorates charges when swapping between prices. The noProrate
method may be used to update the subscription's price without prorating the charges:
For more information on subscription proration, consult the Stripe documentation.
Executing the noProrate
method before the swapAndInvoice
method will have no effect on proration. An invoice will always be issued.
Subscription Quantity
Sometimes subscriptions are affected by "quantity". For example, a project management application might charge $10 per month per project. You may use the incrementQuantity
and decrementQuantity
methods to easily increment or decrement your subscription quantity:
Alternatively, you may set a specific quantity using the updateQuantity
method:
The noProrate
method may be used to update the subscription's quantity without prorating the charges:
For more information on subscription quantities, consult the Stripe documentation.
Quantities for Subscriptions With Multiple Products
If your subscription is a subscription with multiple products, you should pass the ID of the price whose quantity you wish to increment or decrement as the second argument to the increment / decrement methods:
Subscriptions With Multiple Products
Subscription with multiple products allow you to assign multiple billing products to a single subscription. For example, imagine you are building a customer service "helpdesk" application that has a base subscription price of $10 per month but offers a live chat add-on product for an additional $15 per month. Information for subscriptions with multiple products is stored in Shopkeeper's subscription_items
database table.
You may specify multiple products for a given subscription by passing an array of prices as the second argument to the newSubscription
method:
In the example above, the customer will have two prices attached to their default
subscription. Both prices will be charged on their respective billing intervals. If necessary, you may use the quantity
method to indicate a specific quantity for each price:
If you would like to add another price to an existing subscription, you may invoke the subscription's addPrice
method:
The example above will add the new price and the customer will be billed for it on their next billing cycle. If you would like to bill the customer immediately you may use the addPriceAndInvoice
method:
If you would like to add a price with a specific quantity, you can pass the quantity as the second argument of the addPrice
or addPriceAndInvoice
methods:
You may remove prices from subscriptions using the removePrice
method:
You may not remove the last price on a subscription. Instead, you should simply cancel the subscription.
Swapping Prices
You may also change the prices attached to a subscription with multiple products. For example, imagine a customer has a price_basic
subscription with a price_chat
add-on product and you want to upgrade the customer from the price_basic
to the price_pro
price:
When executing the example above, the underlying subscription item with the price_basic
is deleted and the one with the price_chat
is preserved. Additionally, a new subscription item for the price_pro
is created.
You can also specify subscription item options by passing an array of key / value pairs to the swap
method. For example, you may need to specify the subscription price quantities:
If you want to swap a single price on a subscription, you may do so using the swap
method on the subscription item itself. This approach is particularly useful if you would like to preserve all of the existing metadata on the subscription's other prices:
Proration
By default, Stripe will prorate charges when adding or removing prices from a subscription with multiple products. If you would like to make a price adjustment without proration, you should chain the noProrate
method onto your price operation:
Quantities
If you would like to update quantities on individual subscription prices, you may do so using the existing quantity methods by passing the ID of the price as an additional argument to the method:
When a subscription has multiple prices the stripe_price
and quantity
attributes on the Subscription
model will be null
. To access the individual price attributes, you should use the items
relationship available on the Subscription
model.
Subscription Items
When a subscription has multiple prices, it will have multiple subscription "items" stored in your database's subscription_items
table. You may access these via the items
relationship on the subscription:
You can also retrieve a specific price using the findItemOrFail
method:
Multiple Subscriptions
Stripe allows your customers to have multiple subscriptions simultaneously. For example, you may run a gym that offers a swimming subscription and a weight-lifting subscription, and each subscription may have different pricing. Of course, customers should be able to subscribe to either or both plans.
When your application creates subscriptions, you may provide the type of the subscription to the newSubscription
method. The type may be any string that represents the type of subscription the user is initiating:
In this example, we initiated a monthly swimming subscription for the customer. However, they may want to swap to a yearly subscription at a later time. When adjusting the customer's subscription, we can simply swap the price on the swimming
subscription:
Of course, you may also cancel the subscription entirely:
Metered Billing
Metered billing allows you to charge customers based on their product usage during a billing cycle. For example, you may charge customers based on the number of text messages or emails they send per month.
To start using metered billing, you will first need to create a new product in your Stripe dashboard with a metered price. Then, use the meteredPrice
to add the metered price ID to a customer subscription:
You may also start a metered subscription via Stripe Checkout:
Reporting Usage
As your customer uses your application, you will report their usage to Stripe so that they can be billed accurately. To increment the usage of a metered subscription, you may use the reportUsage
method:
By default, a "usage quantity" of 1 is added to the billing period. Alternatively, you may pass a specific amount of "usage" to add to the customer's usage for the billing period:
If your application offers multiple prices on a single subscription, you will need to use the reportUsageFor
method to specify the metered price you want to report usage for:
Sometimes, you may need to update usage which you have previously reported. To accomplish this, you may pass a timestamp or a DateTimeInterface
instance as the second parameter to reportUsage
. When doing so, Stripe will update the usage that was reported at that given time. You can continue to update previous usage records as the given date and time is still within the current billing period:
Retrieving Usage Records
To retrieve a customer's past usage, you may use a subscription instance's usageRecords
method:
If your application offers multiple prices on a single subscription, you may use the usageRecordsFor
method to specify the metered price that you wish to retrieve usage records for:
The usageRecords
and usageRecordsFor
methods return a Collection instance containing an associative array of usage records. You may iterate over this array to display a customer's total usage:
For a full reference of all usage data returned and how to use Stripe's cursor based pagination, please consult the official Stripe API documentation.
Subscription Taxes
Instead of calculating Tax Rates manually, you can automatically calculate taxes using Stripe Tax
To specify the tax rates a user pays on a subscription, you should implement the taxRates
method on your billable model and return an array containing the Stripe tax rate IDs. You can define these tax rates in your Stripe dashboard:
The taxRates
method enables you to apply a tax rate on a customer-by-customer basis, which may be helpful for a user base that spans multiple countries and tax rates.
If you're offering subscriptions with multiple products, you may define different tax rates for each price by implementing a priceTaxRates
method on your billable model:
The taxRates
method only applies to subscription charges. If you use Shopkeeper to make "one-off" charges, you will need to manually specify the tax rate at that time.
Syncing Tax Rates
Shopkeeper also offers the isNotTaxExempt
, isTaxExempt
, and reverseChargeApplies
methods to determine if the customer is tax exempt. These methods will call the Stripe API to determine a customer's tax exemption status:
These methods are also available on any Invoice
object. However, when invoked on an Invoice
object, the methods will determine the exemption status at the time the invoice was created.
Subscription Anchor Date
By default, the billing cycle anchor is the date the subscription was created or, if a trial period is used, the date that the trial ends. If you would like to modify the billing anchor date, you may use the anchorBillingCycleOn
method:
For more information on managing subscription billing cycles, consult the Stripe billing cycle documentation
Cancelling Subscriptions
To cancel a subscription, call the cancel
method on the user's subscription:
When a subscription is canceled, Shopkeeper will automatically set the ends_at
column in your subscriptions
database table. This column is used to know when the subscribed
method should begin returning false
.
For example, if a customer cancels a subscription on March 1st, but the subscription was not scheduled to end until March 5th, the subscribed
method will continue to return true
until March 5th. This is done because a user is typically allowed to continue using an application until the end of their billing cycle.
You may determine if a user has canceled their subscription but are still on their "grace period" using the onGracePeriod
method:
If you wish to cancel a subscription immediately, call the cancelNow
method on the user's subscription:
If you wish to cancel a subscription immediately and invoice any remaining un-invoiced metered usage or new / pending proration invoice items, call the cancelNowAndInvoice
method on the user's subscription:
You may also choose to cancel the subscription at a specific moment in time:
Finally, you should always cancel user subscriptions before deleting the associated user model:
Resuming Subscriptions
If a customer has canceled their subscription and you wish to resume it, you may invoke the resume
method on the subscription. The customer must still be within their "grace period" in order to resume a subscription:
If the customer cancels a subscription and then resumes that subscription before the subscription has fully expired the customer will not be billed immediately. Instead, their subscription will be re-activated and they will be billed on the original billing cycle.