74 lines
2.6 KiB
PHP
74 lines
2.6 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\Notification\BaseNotification;
|
|
use Modules\Core\Order\Events\OrderCaptured;
|
|
use Modules\Core\Order\Services\OrderStatusFlow;
|
|
use Modules\Core\Order\Support\OrderReferenceDisplay;
|
|
|
|
/**
|
|
* "Payment captured" is only a meaningful, distinct update for a bank
|
|
* transfer order — placing that order and paying for it are genuinely two
|
|
* separate moments (Order::paid stays false at checkout; a shopper is
|
|
* told their order is confirmed and awaiting a wire, then separately told
|
|
* once staff mark it paid). For every other payment method (card,
|
|
* authorize-then-capture, COD), placing the order already means the
|
|
* shopper did everything they need to on their end — a card payment is
|
|
* captured in the very same request that places the order, so a second
|
|
* "payment captured" email would just repeat what OrderPlacedNotification
|
|
* already said. See OrderStatusFlow's own docblock for why payment method
|
|
* never affects the order's status SEQUENCE, only this kind of business
|
|
* decision about which events actually matter to the shopper.
|
|
*/
|
|
class OrderCapturedNotification extends BaseNotification
|
|
{
|
|
public function __construct(private readonly OrderCaptured $event) {}
|
|
|
|
public static function getKey(): string
|
|
{
|
|
return 'order.captured.customer.mail';
|
|
}
|
|
|
|
public static function listensTo(): string
|
|
{
|
|
return OrderCaptured::class;
|
|
}
|
|
|
|
public function via(object $notifiable): array
|
|
{
|
|
if (! app(OrderStatusFlow::class)->isBankTransfer($this->event->order)) {
|
|
return [];
|
|
}
|
|
|
|
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(__('Payment captured for your order :reference', ['reference' => $reference]))
|
|
->view('core::order.notifications.captured', [
|
|
'order' => $order,
|
|
'reference' => $reference,
|
|
'amount' => $this->event->transaction->amount->formatted,
|
|
]);
|
|
}
|
|
}
|