Plain Dev Blog
โ† Back to all posts

Create coupons Stripe API

ยท

Create coupons Stripe API

If you using Stripe long enough at some point inevitably you will create discount coupons which your users can use with your service.

Creating from Dashboard

The most straight forward way is:

  • Dashboard

  • Product Catalog

  • Coupons

From there it's pretty easy to create one, only a few things to choose:

  • Name - the which will be displayed to the users

  • ID - it is marked optional, but in my experience if do not plan to have some type of mapping for your coupons in your back-end, set the ID to be same as the Name

  • Type - it could one of two options: Percent & Fixed it's something like 10% off or $10 off, for me personally the percentage one is more versatile because it work the same for any price: If you use 10% off for $10 works just as well as using it for $100, but the fixed amount works differently, whatever your case maybe there are options to choose from

  • Value - regarding of the type a value needs to be set: One in percent other in your proffered currency

  • Add to specific product - it allows you to attach the coupon to specific product only: If you select a product named Basic the coupon will only work on this product it won't work on Premium product very useful feature when you need more control over the redemption

  • Duration - it determines if the code can be used once, for multiple months e.g multiple invoices or to be used forever it's your choice based on what you need for your particular case

Creating using Stripe API

With the API it's pretty easy to do:

$stripe = new \Stripe\StripeClient('sk_test_51O6GXtGEt10LQr5JCDNVuBy5MC64QRxjHJOyUJfiK45i1xdqZC27Bl1PQxV1aUf89APJQCEaSH82jwk2NiO6DwjP00RHXMRfRO');

$coupon = $stripe->coupons->create([
  'duration' => 'forever',
  'percent_off' => 25.5,
]);

With it you can use all the parameters listed bellow, one of the main things here to look for is a check of the coupon already exists, using the API can be very useful when you need to create coupons dynamically for specific products for example:

$stripe = new \Stripe\StripeClient('sk_test_51O6GXtGEt10LQr5JCDNVuBy5MC64QRxjHJOyUJfiK45i1xdqZC27Bl1PQxV1aUf89APJQCEaSH82jwk2NiO6DwjP00RHXMRfRO');

$coupon = $stripe->coupons->create([
  'duration' => 'forever',
  'applies_to' => [
     "products": ["prod_ABC123", "prod_XYZ789"]
   ]
  'percent_off' => 25.5,
]);

You can use a list of valid coupon ids relevant for your implementation.

Something that can be particularly useful when creating coupons: max_redemptions if have a specific promotion like: Only the first 10 people will get discount(Povilas from Laravel Daily does promotions like this quite often):

$stripe = new \Stripe\StripeClient('sk_test_51O6GXtGEt10LQr5JCDNVuBy5MC64QRxjHJOyUJfiK45i1xdqZC27Bl1PQxV1aUf89APJQCEaSH82jwk2NiO6DwjP00RHXMRfRO');

$coupon = $stripe->coupons->create([
  'duration' => 'forever',
  'percent_off' => 25.5,
  'max_redemptions' => 10
]);

This way the coupon that is been creating will be redeemed this number of times after which it will be inactive and won't be redeemable, if you prefer something more broad you can use:

`redeem_by` you can provide a timestamp(which can't more than 5 years in the future), after the provided date the coupon will became invalid and your users won't be able to redeem. It's suitable if you need to create time base promotion: For Black Friday, Christmas or something that ends in specific date.

Conclusion

Creating coupons in Stripe it's a powerful option regarding of the way you are going to do it, there are plenty of options from which can choose to supercharge you application's promotional campaigns. Give a shot won't regret it.

Share this post

Subscribe to the newsletter

Get new posts delivered to your inbox.