Start Saving Now: Create Stripe Coupons in Laravel (PHP)

How to use Stripe API's to create coupons in Laravel

Start-Saving-Now-Create-Stripe-Coupons-in-Laravel-(PHP)

When it comes to integrating payment solution on a project Stripe comes with great solutions. Stripe provides lots of services to provide solution to our complex requirements. One of its service is coupon. coupon is one of the great ways to acquire customer in your product.

In this article we will learn how to create coupon using Stripe API's in Laravel , you can follow the same steps for a PHP project also. While creating coupons we will see different parameters provided by stripe to manipulate or customize our coupons as per our requirement.

You can read our other articles of how to integrate stripe payment gateway , How can you save cards etc.

Let's see how can we work with coupon in stripe.


Create a route :

Route::get('payment/createcoupon', [paymentController::class,'createCoupon'])->name('create-coupon');

Paymentcontroller.php :

Now we will write code on our controller to create coupon . Basically we can create two types of coupon as follows.

  • Percent off
  • Amount off
While creating Amount off coupons , you have to provide currency as an extra parameter . You can get the currency codes for Stripe's official site. While creating amount off coupon make sure to multiply the amount with 100 as stripe by default divides the amount from 100 .

Creating coupon with percent off

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade as PDF;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class paymentController extends Controller
{
    public function createCoupon(){
      try{
        // Connect to your stripe account

        $stripe_account = Config('constants.Stripe_account');
        $stripe_secret = $stripe_account['secret_key'];

        $stripe = new \Stripe\StripeClient(
          $stripe_secret
        );
        // create coupon
        $coupon = $stripe->coupons->create([
          'percent_off' => 15,
          'duration' => 'once'
        ]);

        return $coupon;
      }
      catch(\Exception $e){
        Log::error(
          'Failed to create coupon',
          ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]
        );
        return false;
      }
    }
}

Creating coupon with amount off

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade as PDF;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class paymentController extends Controller
{
    public function createCoupon(){
      try{
        // Connect to your stripe account

        $stripe_account = Config('constants.Stripe_account');
        $stripe_secret = $stripe_account['secret_key'];

        $stripe = new \Stripe\StripeClient(
          $stripe_secret
        );
        // create coupon
        $coupon = $stripe->coupons->create([
          'amount_off' => 15*100,
          'duration' => 'once',
          'currency' => 'INR'
        ]);

        return $coupon;
      }
      catch(\Exception $e){
        Log::error(
          'Failed to create coupon',
          ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]
        );
        return false;
      }
    }
}

Output response :

{
    "id": "XbC9tMrf",
    "object": "coupon",
    "amount_off": 1500,
    "created": 1656782552,
    "currency": "inr",
    "duration": "once",
    "duration_in_months": null,
    "livemode": false,
    "max_redemptions": null,
    "metadata": [],
    "name": null,
    "percent_off": null,
    "redeem_by": null,
    "times_redeemed": 0,
    "valid": true
}

How to use Stripe API's to create coupons in Laravel

There are some other parameters are there provided by stripe to customize while creating our coupon you can refer these parameters on Stripe's official site.


Thank you for reading this article 😊

For any query do not hesitate to comment 💬

Previous Post Next Post

Contact Form