-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #81 send e-mail notifications regarding ending trial
- Loading branch information
1 parent
ad0586b
commit 00ec406
Showing
9 changed files
with
973 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Models\Team; | ||
use App\Notifications\TrialEndsSoonNotification; | ||
use Carbon\CarbonInterval; | ||
use Illuminate\Events\Dispatcher; | ||
use Laravel\Paddle\Events\SubscriptionCreated; | ||
use Laravel\Paddle\Events\SubscriptionUpdated; | ||
|
||
class ScheduleTrialEndNotifications | ||
{ | ||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the event. | ||
*/ | ||
public function subscribe(Dispatcher $dispatcher): array | ||
{ | ||
return [ | ||
SubscriptionCreated::class => 'scheduleNotifications', | ||
SubscriptionUpdated::class => 'scheduleNotifications', | ||
]; | ||
} | ||
|
||
public function scheduleNotifications(SubscriptionCreated|SubscriptionUpdated $event): void | ||
{ | ||
if (! $event->subscription->trial_ends_at) { | ||
return; | ||
} | ||
|
||
$subscription = $event->subscription; | ||
|
||
/* @var Team $team */ | ||
$team = $subscription->billable; | ||
|
||
$days3 = $subscription->trial_ends_at->sub(CarbonInterval::days(3))->diffAsDateInterval(now(), absolute: true); | ||
$team->notify((new TrialEndsSoonNotification($team))->delay($days3)); | ||
|
||
$days1 = $subscription->trial_ends_at->sub(CarbonInterval::day())->diffAsDateInterval(now(), absolute: true); | ||
$team->notify((new TrialEndsSoonNotification($team))->delay($days1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use App\Models\Team; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class TrialEndsSoonNotification extends Notification implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new notification instance. | ||
*/ | ||
public function __construct( | ||
protected Team $team, | ||
) | ||
{ | ||
$this->afterCommit(); | ||
} | ||
|
||
public function shouldSend(object $notifiable, string $channel): bool | ||
{ | ||
return $this->team->onTrial(); | ||
} | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @return array<int, string> | ||
*/ | ||
public function via(object $notifiable): array | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
/** | ||
* Get the mail representation of the notification. | ||
*/ | ||
public function toMail(object $notifiable): MailMessage | ||
{ | ||
$nextPayment = $this->team->subscription()->nextPayment(); | ||
|
||
return (new MailMessage) | ||
->line('Your trial ends soon.') | ||
->line("You will be charged {$nextPayment->amount()} on {$nextPayment->date->toDateTimeString()} ({$nextPayment->date->longRelativeToNowDiffForHumans()}).") | ||
->action('Manage Subscription', url(route('teams.billing.show', $this->team))) | ||
->line('Any other questions? Contact us at '.config('app.email')); | ||
} | ||
|
||
/** | ||
* Get the array representation of the notification. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(object $notifiable): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.