Search code examples
phplaravellaravel-cashier

Stripe Recurring Payments but every 3 or 6 months


I'm new to Stripe PHP Laravel. Are recurring payments in Laravel Stripe possible for billing cycles that are not 1 month but 3 months or 6 months? Can anyone send me to the right documentation because I'm not familiar with the term? I have checked cycles in Stripe but I don't think that's what I need.


Solution

  • On stripe dashboard under belling-> products create new product for example prod-1 , add pricing plan for example plan-1(stripe will generate ID for this plan) to that product(prod-1), while adding pricing plan under billing interval select or customize the interval that you want.

    Now let work on laravel app side. I will recommend to use Laravel Cashier.

    Run Composer composer require laravel/cashier

    Update your USER Migration Table:

     Schema::table('users', function ($table) {
        $table->string('stripe_id')->nullable()->collation('utf8mb4_bin');
        $table->string('card_brand')->nullable();
        $table->string('card_last_four', 4)->nullable();
        $table->timestamp('trial_ends_at')->nullable();
    });
    
    Schema::create('subscriptions', function ($table) {
        $table->increments('id');
        $table->unsignedInteger('user_id');
        $table->string('name');
        $table->string('stripe_id')->collation('utf8mb4_bin');
        $table->string('stripe_plan');
        $table->integer('quantity');
        $table->timestamp('trial_ends_at')->nullable();
        $table->timestamp('ends_at')->nullable();
        $table->timestamps();
    });
    

    Update Your User Model:

    use Laravel\Cashier\Billable;
    
    class User extends Authenticatable
    {
        use Billable;
    }
    

    In your Controller ex. MyController.php:

    use Cartalyst\Stripe\Laravel\Facades\Stripe;
    use Cartalyst\Stripe\Exception\CardErrorException;
    use Session;
    use Auth;
    
    public function store(Request $request)
    {
    
        $token = $_POST['stripeToken'];
        $user = Auth::user();
    
        try {
    
            $user->newSubscription('prod-1', 'ID of plan-1')->create($token);
    
            Session::flash('success', 'You are now a premium member');
            return redirect()->back();
    
        } catch (CardErrorException $e) {
            return back()->withErrors('Error!'.  $e->getMessage());
        }
    
    
    }
    

    In your view:

    <form action="{{ route('subscribe')}}" method="POST">
      <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="pk_test_0000000000000000000" // Your api key
        data-image="/images/marketplace.png" // You can change this image to your image
        data-name="My App Name"
        data-description="Subscription for 1 weekly box"
        data-amount="2000" //the price is in cents 2000 = 20.00
        data-label="Sign Me Up!">
      </script>
    </form>
    

    Create Route:

    Route::POST('subscription', 'MyController@store')->name('susbcribe');
    

    Let me know if it works.