Files
core/src/Order/Notifications/OrderPlacedNotification.php
T

67 lines
2.2 KiB
PHP

<?php
namespace Modules\Core\Order\Notifications;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Notification as NotificationFacade;
use Modules\Core\Checkout\Events\OrderPlaced;
use Modules\Core\Notification\BaseNotification;
use Modules\Core\Order\Support\OrderReferenceDisplay;
/**
* The order confirmation email — fires once, for every capture_mode and
* driver alike (Stripe, offline, bank-transfer), since OrderPlaced is
* dispatched from the one place an order's placed_at actually gets set
* (Modules\Core\Order\Listeners\ApplyResolvedPaymentStatus), not from a
* driver-specific event like OrderCaptured. Before this existed, an
* offline/bank-transfer order got no placement email at all — only a
* Stripe (auto-captured) order did, via OrderCapturedNotification, which is
* a different concern (payment confirmation, not order confirmation) that
* happens to fire at the same moment for that one driver.
*/
class OrderPlacedNotification extends BaseNotification
{
public function __construct(private readonly OrderPlaced $event) {}
public static function getKey(): string
{
return 'order.placed.customer.mail';
}
public static function listensTo(): string
{
return OrderPlaced::class;
}
public function via(object $notifiable): array
{
return ['mail'];
}
public function notifiable(): AnonymousNotifiable
{
$order = $this->event->order;
$email = $order->billingAddress?->contact_email ?? $order->shippingAddress?->contact_email;
return NotificationFacade::route('mail', $email);
}
public function toMail(object $notifiable): MailMessage
{
$order = $this->event->order;
$reference = OrderReferenceDisplay::resolve($order);
return (new MailMessage)
->subject(__('Your order :reference is confirmed', ['reference' => $reference]))
->view('core::order.notifications.placed', [
'order' => $order,
'reference' => $reference,
'total' => $order->total->formatted,
'lines' => $order->lines,
]);
}
}