Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions database/migrations/remove_cancels_at_from_subscriptions_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table(config('laravel-subscriptions.tables.subscriptions'), static function (Blueprint $table): void {
$table->dropColumn('cancels_at');
});
}
};
12 changes: 9 additions & 3 deletions src/Models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* @property-read ?CarbonInterface $trial_ends_at
* @property-read ?CarbonInterface $starts_at
* @property-read ?CarbonInterface $ends_at
* @property-read ?CarbonInterface $cancels_at
* @property-read ?CarbonInterface $canceled_at
* @property-read CarbonInterface $created_at
* @property-read CarbonInterface $updated_at
Expand All @@ -59,7 +58,6 @@ class Subscription extends Model
'trial_ends_at',
'starts_at',
'ends_at',
'cancels_at',
'canceled_at',
];

Expand All @@ -69,7 +67,6 @@ class Subscription extends Model
'trial_ends_at' => 'datetime',
'starts_at' => 'datetime',
'ends_at' => 'datetime',
'cancels_at' => 'datetime',
'canceled_at' => 'datetime',
'deleted_at' => 'datetime',
];
Expand Down Expand Up @@ -177,6 +174,15 @@ public function changePlan(Plan $plan): self

// Attach new plan to subscription
$this->fill(['plan_id' => $plan->getKey()]);

// Free plans never expire
if ($plan->isFree()) {
$this->fill([
'ends_at' => null,
'trial_ends_at' => null,
]);
}

$this->save();

return $this;
Expand Down
9 changes: 8 additions & 1 deletion src/Traits/HasPlanSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,19 @@ public function newPlanSubscription(string $subscription, Plan $plan, ?Carbon $s
start: $trial->getEndDate()
);

return $this->planSubscriptions()->create([
/** @var Subscription $subscription */
$subscription = $this->planSubscriptions()->create([
'name' => $subscription,
'plan_id' => $plan->getKey(),
'trial_ends_at' => $trial->getEndDate(),
'starts_at' => $period->getStartDate(),
'ends_at' => $period->getEndDate(),
]);

if ($plan->isFree()) {
$subscription->update(['ends_at' => null, 'trial_ends_at' => null]);
}

return $subscription;
}
}