Handling Coupons with Stripe and Cashier

Laravel Cashier is fantastic for working with Stripe. It provides a simple API for handling common tasks associated with subscriptions. In this tutorial we'll take an in-depth look at working with subscriptions and coupons.

Integrating coupons into Stripe subscriptions is a two-step process. First you'll need to create them using the Stripe administration console. Once created, you'll need to integrate the necessary logic into your web application's subscription workflow. Let's start by entering the administration console and creating a coupon.

Creating a Stripe Coupon

To create a coupon for your users, you first need to create it within the Stripe administration console. The form to add a coupon can be found in the sidebar under Subscriptions -> Coupons and then click the new link on the right. The form will then open the following modal window:

Stripe Coupons

Let's have a look at the relevant settings:

  • Percent Off and Amount Off: These two fields allow you to define a discount according to a specific dollar amount or percentage. When creating a new coupon, you will only enter a number for the type you are creating.
  • Duration: This setting allows you to define how the coupon lasts, once applied. The options are once, multi-month, or forever.
  • ID Code: This is the code the user will enter to take advantage of the discount.
  • Max Redemptions: Use this setting if you would like to restrict the coupon to a set number of redemptions.
  • Redeem By: For short term sales you can set a date here, and this coupon will not be available to use after that date.

After creating your coupon, you can then e-mail the ID Code to your user and allow them to apply the discount.

Adding a Coupon to a New Subscription

If a user hasn't signed up for your service yet applying the coupon is simple. All you have to do is utilize Cashier's withCoupon() method when creating the subscription. Here is an example:

try {
} catch (Exception $e) {
    return back()->withError($e->getMessage());

Laravel will send this coupon code to the Stripe service via the API and if it is successfully verified, the coupon will be applied to the new subscription. But what if the user already has a subscription? Let's take a look at how to do that next.

Adding a Coupon to an Existing Subscription

If a user already has a subscription you can assign a coupon to their subscription using the applyCoupon method as follows:

try {
    return redirect()->back()->withMessage('Coupon Applied');
} catch (Exception $e) {
    return back()->withError($e->getMessage());

When applying a coupon, it's best to wrap this in a try / catch block because if the user enters an invalid coupon, Stripe will return an exception with the reason for the failure.

As you can see from this post working with coupons is really simple with Laravel and Stripe.

Written by Eric L. Barnes on Jun 03, 2015

What are you waiting for? Start implementing Stripe and Laravel today!

Buy Now for $29.99

European customers: Leanpub will add VAT to the purchase price.

Stay up to date by joining the newsletter

No Spam, Unsubscribe Anytime