Add Woo Manager email notification milestones with shared mail templates.
Deploy Ladill Woo Manager / deploy (push) Successful in 2m11s

New order, store connected, Pro expiry, and past-due alerts use the Ladill notification layout with woo branding; expiry reminders dedupe per threshold like hosting.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-07-07 02:59:45 +00:00
co-authored by Cursor
parent 1edf5aad43
commit cfdc8c7c09
22 changed files with 926 additions and 12 deletions
@@ -0,0 +1,21 @@
<?php
namespace App\Console\Commands;
use App\Services\Woo\WooProExpiryAlertService;
use Illuminate\Console\Command;
class NotifyProExpiringCommand extends Command
{
protected $signature = 'woo:pro-expiry-alerts';
protected $description = 'Send Woo Manager Pro/Business expiry milestone emails';
public function handle(WooProExpiryAlertService $alerts): int
{
$sent = $alerts->checkAll();
$this->info("Sent {$sent} pro expiry alert(s).");
return self::SUCCESS;
}
}
@@ -6,13 +6,17 @@ use App\Http\Controllers\Controller;
use App\Jobs\StoreBootstrapSync;
use App\Models\WooStore;
use App\Services\Woo\InstallTokenService;
use App\Services\Woo\WooNotificationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use RuntimeException;
class StoreActivationController extends Controller
{
public function __construct(private InstallTokenService $tokens) {}
public function __construct(
private InstallTokenService $tokens,
private WooNotificationService $notifications,
) {}
public function activate(Request $request): JsonResponse
{
@@ -35,9 +39,14 @@ class StoreActivationController extends Controller
return response()->json(['message' => 'Site URL does not match the connection request.'], 422);
}
$wasPending = $store->status === WooStore::STATUS_PENDING;
$pluginToken = $this->tokens->issuePluginToken($store);
$store = $store->fresh();
if ($wasPending) {
$this->notifications->storeConnected($store);
}
StoreBootstrapSync::dispatch($store->id)->afterResponse();
return response()->json([
@@ -6,6 +6,7 @@ use App\Models\WooStore;
use App\Services\Woo\CategoryIngestService;
use App\Services\Woo\OrderIngestService;
use App\Services\Woo\ProductIngestService;
use App\Services\Woo\WooNotificationService;
use App\Services\Woo\WooWebhookVerifier;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -17,6 +18,7 @@ class WooWebhookController extends Controller
private OrderIngestService $orders,
private ProductIngestService $products,
private CategoryIngestService $categories,
private WooNotificationService $notifications,
) {}
public function __invoke(Request $request, string $storePublicId): JsonResponse
@@ -46,7 +48,7 @@ class WooWebhookController extends Controller
}
return match (true) {
str_starts_with($topic, 'order.') => $this->handleOrder($store, $payload),
str_starts_with($topic, 'order.') => $this->handleOrder($store, $topic, $payload),
str_starts_with($topic, 'product_category.') => $this->handleCategory($store, $topic, $payload),
str_starts_with($topic, 'product.') => $this->handleProduct($store, $topic, $payload),
default => response()->json(['ignored' => true]),
@@ -54,10 +56,14 @@ class WooWebhookController extends Controller
}
/** @param array<string, mixed> $payload */
private function handleOrder(WooStore $store, array $payload): JsonResponse
private function handleOrder(WooStore $store, string $topic, array $payload): JsonResponse
{
$order = $this->orders->ingest($store, $payload);
if ($topic === 'order.created' && $order->wasRecentlyCreated) {
$this->notifications->orderReceived($order, $store);
}
return response()->json([
'ok' => true,
'order_id' => $order->id,
+2 -1
View File
@@ -23,7 +23,7 @@ class ProSubscription extends Model
protected $fillable = [
'user_id', 'status', 'plan', 'price_minor', 'currency', 'auto_renew',
'started_at', 'current_period_end', 'last_charged_at', 'canceled_at',
'last_reference', 'last_error',
'last_reference', 'last_error', 'metadata',
];
protected $casts = [
@@ -33,6 +33,7 @@ class ProSubscription extends Model
'current_period_end' => 'datetime',
'last_charged_at' => 'datetime',
'canceled_at' => 'datetime',
'metadata' => 'array',
];
public function user(): BelongsTo
@@ -0,0 +1,74 @@
<?php
namespace App\Notifications\Woo;
use App\Models\WooOrder;
use App\Models\WooStore;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class WooOrderReceivedNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(
private readonly WooOrder $order,
private readonly WooStore $store,
) {}
public function via(object $notifiable): array
{
return $this->channels('order_received');
}
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage())
->subject($this->subjectLine())
->view('mail.notifications.woo-order-received', [
'order' => $this->order,
'store' => $this->store,
'manageUrl' => route('woo.orders.index'),
]);
}
/** @return array<string, mixed> */
public function toArray(object $notifiable): array
{
$label = $this->store->site_name ?? parse_url($this->store->site_url, PHP_URL_HOST) ?? 'your store';
return [
'title' => 'New order received',
'message' => "Order #{$this->order->order_number} for {$this->order->totalFormatted()} from {$label}.",
'icon' => 'order',
'milestone' => 'order_received',
'url' => route('woo.orders.index'),
'order_id' => $this->order->id,
'store_id' => $this->store->id,
];
}
private function subjectLine(): string
{
$storeLabel = $this->store->site_name ?? parse_url($this->store->site_url, PHP_URL_HOST) ?? 'WooCommerce store';
return "New order #{$this->order->order_number}{$storeLabel}";
}
/** @return list<string> */
private function channels(string $event): array
{
$config = (array) config("notifications.woo_events.{$event}", ['mail' => true, 'database' => true]);
$channels = [];
if ($config['mail'] ?? false) {
$channels[] = 'mail';
}
if ($config['database'] ?? false) {
$channels[] = 'database';
}
return $channels ?: ['database'];
}
}
@@ -0,0 +1,87 @@
<?php
namespace App\Notifications\Woo;
use App\Models\Woo\ProSubscription;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class WooProExpiringNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(
private readonly ProSubscription $subscription,
private readonly int $daysRemaining,
) {}
public function via(object $notifiable): array
{
return $this->channels('pro_expiring');
}
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage())
->subject($this->subjectLine())
->view('mail.notifications.woo-pro-expiring', [
'subscription' => $this->subscription,
'daysRemaining' => $this->daysRemaining,
'planLabel' => $this->planLabel(),
'renewUrl' => route('woo.pro.index'),
]);
}
/** @return array<string, mixed> */
public function toArray(object $notifiable): array
{
return [
'title' => $this->headline(),
'message' => "Your Woo Manager {$this->planLabel()} plan expires in {$this->daysRemaining} day(s).",
'icon' => 'billing',
'milestone' => 'pro_expiring',
'url' => route('woo.pro.index'),
'days_remaining' => $this->daysRemaining,
];
}
private function subjectLine(): string
{
return match (true) {
$this->daysRemaining <= 1 => 'Woo Manager plan expires tomorrow',
$this->daysRemaining <= 3 => "Woo Manager plan expires in {$this->daysRemaining} days",
default => 'Woo Manager plan renewal reminder',
};
}
private function headline(): string
{
return match (true) {
$this->daysRemaining <= 1 => 'Plan expires soon',
$this->daysRemaining <= 3 => 'Urgent plan renewal',
default => 'Plan renewal reminder',
};
}
private function planLabel(): string
{
return $this->subscription->plan === ProSubscription::PLAN_ENTERPRISE ? 'Business' : 'Pro';
}
/** @return list<string> */
private function channels(string $event): array
{
$config = (array) config("notifications.woo_events.{$event}", ['mail' => true, 'database' => true]);
$channels = [];
if ($config['mail'] ?? false) {
$channels[] = 'mail';
}
if ($config['database'] ?? false) {
$channels[] = 'database';
}
return $channels ?: ['database'];
}
}
@@ -0,0 +1,64 @@
<?php
namespace App\Notifications\Woo;
use App\Models\Woo\ProSubscription;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class WooProPastDueNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(private readonly ProSubscription $subscription) {}
public function via(object $notifiable): array
{
return $this->channels('pro_past_due');
}
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage())
->subject('Woo Manager subscription payment failed')
->view('mail.notifications.woo-pro-past-due', [
'subscription' => $this->subscription,
'planLabel' => $this->planLabel(),
'renewUrl' => route('woo.pro.index'),
]);
}
/** @return array<string, mixed> */
public function toArray(object $notifiable): array
{
return [
'title' => 'Subscription payment failed',
'message' => "We could not renew your Woo Manager {$this->planLabel()} plan. Top up your wallet to restore access.",
'icon' => 'billing',
'milestone' => 'pro_past_due',
'url' => route('woo.pro.index'),
];
}
private function planLabel(): string
{
return $this->subscription->plan === ProSubscription::PLAN_ENTERPRISE ? 'Business' : 'Pro';
}
/** @return list<string> */
private function channels(string $event): array
{
$config = (array) config("notifications.woo_events.{$event}", ['mail' => true, 'database' => true]);
$channels = [];
if ($config['mail'] ?? false) {
$channels[] = 'mail';
}
if ($config['database'] ?? false) {
$channels[] = 'database';
}
return $channels ?: ['database'];
}
}
@@ -0,0 +1,61 @@
<?php
namespace App\Notifications\Woo;
use App\Models\WooStore;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class WooStoreConnectedNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(private readonly WooStore $store) {}
public function via(object $notifiable): array
{
return $this->channels('store_connected');
}
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage())
->subject('WooCommerce store connected — '.($this->store->site_name ?? parse_url($this->store->site_url, PHP_URL_HOST)))
->view('mail.notifications.woo-store-connected', [
'store' => $this->store,
'manageUrl' => route('woo.dashboard'),
]);
}
/** @return array<string, mixed> */
public function toArray(object $notifiable): array
{
$label = $this->store->site_name ?? parse_url($this->store->site_url, PHP_URL_HOST) ?? 'your store';
return [
'title' => 'Store connected',
'message' => "{$label} is now linked to Ladill Woo Manager.",
'icon' => 'success',
'milestone' => 'store_connected',
'url' => route('woo.stores.index'),
'store_id' => $this->store->id,
];
}
/** @return list<string> */
private function channels(string $event): array
{
$config = (array) config("notifications.woo_events.{$event}", ['mail' => true, 'database' => true]);
$channels = [];
if ($config['mail'] ?? false) {
$channels[] = 'mail';
}
if ($config['database'] ?? false) {
$channels[] = 'database';
}
return $channels ?: ['database'];
}
}
+16 -1
View File
@@ -11,7 +11,11 @@ use Illuminate\Support\Str;
class SubscriptionService
{
public function __construct(private readonly BillingClient $billing) {}
public function __construct(
private readonly BillingClient $billing,
private readonly WooNotificationService $notifications,
private readonly WooProExpiryAlertService $expiryAlerts,
) {}
public function gatingActive(): bool
{
@@ -238,13 +242,18 @@ class SubscriptionService
'last_reference' => $reference,
'last_error' => null,
])->save();
$this->expiryAlerts->resetAlerts($sub->fresh());
return;
}
$graceEnd = optional($sub->current_period_end)->addDays((int) config('woo.pro.grace_days', 3));
if ($graceEnd && $graceEnd->isPast()) {
$wasPastDue = $sub->status === ProSubscription::STATUS_PAST_DUE;
$sub->forceFill(['status' => ProSubscription::STATUS_PAST_DUE, 'last_error' => $result['error']])->save();
if (! $wasPastDue) {
$this->notifications->proPastDue($sub->fresh());
}
} else {
$sub->forceFill(['last_error' => $result['error']])->save();
}
@@ -282,9 +291,15 @@ class SubscriptionService
'canceled_at' => null,
'last_reference' => $reference,
'last_error' => null,
'metadata' => null,
],
);
$subscription = $this->subscriptionFor($user);
if ($subscription) {
$this->expiryAlerts->resetAlerts($subscription);
}
return [true, $successMessage.' Your subscription is active.'];
}
}
@@ -0,0 +1,67 @@
<?php
namespace App\Services\Woo;
use App\Models\User;
use App\Models\Woo\ProSubscription;
use App\Models\WooOrder;
use App\Models\WooStore;
use App\Notifications\Woo\WooOrderReceivedNotification;
use App\Notifications\Woo\WooProExpiringNotification;
use App\Notifications\Woo\WooProPastDueNotification;
use App\Notifications\Woo\WooStoreConnectedNotification;
use Illuminate\Notifications\Notification;
class WooNotificationService
{
public function orderReceived(WooOrder $order, WooStore $store): void
{
$order->loadMissing('store');
$user = $store->user;
if (! $user) {
return;
}
$this->send($user, 'order_received', new WooOrderReceivedNotification($order, $store));
}
public function storeConnected(WooStore $store): void
{
$user = $store->user;
if (! $user) {
return;
}
$this->send($user, 'store_connected', new WooStoreConnectedNotification($store));
}
public function proExpiring(ProSubscription $subscription, int $daysRemaining): void
{
$user = $subscription->user;
if (! $user) {
return;
}
$this->send($user, 'pro_expiring', new WooProExpiringNotification($subscription, $daysRemaining));
}
public function proPastDue(ProSubscription $subscription): void
{
$user = $subscription->user;
if (! $user) {
return;
}
$this->send($user, 'pro_past_due', new WooProPastDueNotification($subscription));
}
private function send(User $user, string $event, Notification $notification): void
{
$channels = (array) config("notifications.woo_events.{$event}", ['mail' => true, 'database' => true]);
if (! ($channels['mail'] ?? false) && ! ($channels['database'] ?? false)) {
return;
}
$user->notify($notification);
}
}
@@ -0,0 +1,82 @@
<?php
namespace App\Services\Woo;
use App\Models\Woo\ProSubscription;
/** Sends one-time Woo Manager Pro/Business expiry reminders. */
class WooProExpiryAlertService
{
public function __construct(private readonly WooNotificationService $notifications) {}
/** @return list<int> */
public function milestones(): array
{
return array_values(array_map(
'intval',
(array) config('notifications.pro_expiry_milestones', [7, 3, 1])
));
}
public function checkAll(): int
{
$sent = 0;
ProSubscription::query()
->whereIn('status', [ProSubscription::STATUS_ACTIVE, ProSubscription::STATUS_CANCELED])
->whereNotNull('current_period_end')
->where('current_period_end', '>', now())
->with('user')
->each(function (ProSubscription $subscription) use (&$sent): void {
if ($this->checkSubscription($subscription)) {
$sent++;
}
});
return $sent;
}
public function checkSubscription(ProSubscription $subscription): bool
{
if (! $subscription->current_period_end || ! $subscription->user || $subscription->current_period_end->isPast()) {
return false;
}
if ($subscription->auto_renew && $subscription->status === ProSubscription::STATUS_ACTIVE) {
return false;
}
$daysRemaining = $this->daysUntilExpiry($subscription);
if (! in_array($daysRemaining, $this->milestones(), true)) {
return false;
}
$metadata = is_array($subscription->metadata) ? $subscription->metadata : [];
$sent = array_map('intval', (array) ($metadata['expiry_alerts_sent'] ?? []));
if (in_array($daysRemaining, $sent, true)) {
return false;
}
$this->notifications->proExpiring($subscription, $daysRemaining);
$metadata['expiry_alerts_sent'] = array_values(array_unique([...$sent, $daysRemaining]));
$subscription->update(['metadata' => $metadata]);
return true;
}
public function resetAlerts(ProSubscription $subscription): void
{
$metadata = is_array($subscription->metadata) ? $subscription->metadata : [];
unset($metadata['expiry_alerts_sent']);
$subscription->update(['metadata' => $metadata]);
}
private function daysUntilExpiry(ProSubscription $subscription): int
{
return (int) now()->startOfDay()->diffInDays(
$subscription->current_period_end->copy()->startOfDay(),
false
);
}
}