Skip to content

Commit 992f7a2

Browse files
authored
feat: make free plans never expire (#42)
* feat: make free plans never expire and remove cancels_at columns Free plans now have ends_at and trial_ends_at set to null, meaning they never expire and don't require renewal. - Update newPlanSubscription() to set null dates for free plans - Update changePlan() to set null dates when switching to free plan * refactor: remove all mentions on cancels_at
1 parent 74b313b commit 992f7a2

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
public function up(): void
12+
{
13+
Schema::table(config('laravel-subscriptions.tables.subscriptions'), static function (Blueprint $table): void {
14+
$table->dropColumn('cancels_at');
15+
});
16+
}
17+
};

src/Models/Subscription.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
* @property-read ?CarbonInterface $trial_ends_at
3333
* @property-read ?CarbonInterface $starts_at
3434
* @property-read ?CarbonInterface $ends_at
35-
* @property-read ?CarbonInterface $cancels_at
3635
* @property-read ?CarbonInterface $canceled_at
3736
* @property-read CarbonInterface $created_at
3837
* @property-read CarbonInterface $updated_at
@@ -59,7 +58,6 @@ class Subscription extends Model
5958
'trial_ends_at',
6059
'starts_at',
6160
'ends_at',
62-
'cancels_at',
6361
'canceled_at',
6462
];
6563

@@ -69,7 +67,6 @@ class Subscription extends Model
6967
'trial_ends_at' => 'datetime',
7068
'starts_at' => 'datetime',
7169
'ends_at' => 'datetime',
72-
'cancels_at' => 'datetime',
7370
'canceled_at' => 'datetime',
7471
'deleted_at' => 'datetime',
7572
];
@@ -177,6 +174,15 @@ public function changePlan(Plan $plan): self
177174

178175
// Attach new plan to subscription
179176
$this->fill(['plan_id' => $plan->getKey()]);
177+
178+
// Free plans never expire
179+
if ($plan->isFree()) {
180+
$this->fill([
181+
'ends_at' => null,
182+
'trial_ends_at' => null,
183+
]);
184+
}
185+
180186
$this->save();
181187

182188
return $this;

src/Traits/HasPlanSubscriptions.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,19 @@ public function newPlanSubscription(string $subscription, Plan $plan, ?Carbon $s
7777
start: $trial->getEndDate()
7878
);
7979

80-
return $this->planSubscriptions()->create([
80+
/** @var Subscription $subscription */
81+
$subscription = $this->planSubscriptions()->create([
8182
'name' => $subscription,
8283
'plan_id' => $plan->getKey(),
8384
'trial_ends_at' => $trial->getEndDate(),
8485
'starts_at' => $period->getStartDate(),
8586
'ends_at' => $period->getEndDate(),
8687
]);
88+
89+
if ($plan->isFree()) {
90+
$subscription->update(['ends_at' => null, 'trial_ends_at' => null]);
91+
}
92+
93+
return $subscription;
8794
}
8895
}

0 commit comments

Comments
 (0)