new: send alert email when Paybox payment arrives on cancelled order

When a Paybox callback confirms payment on an order with status 4
(Annulé), the payment is still recorded but the order status is no
longer forced to « Préparation ». Instead, an alert email is sent to
``commande@jardinenvie.com`` warning that a refund is likely needed.

New ``AlertePaiementAnnule`` mailable with DB template providing order
ref, amount, customer info and payment reference. New method
``OrderMails::sendCancelledOrderPaymentAlert()`` handles the dispatch.
This commit is contained in:
Valentin Lab
2026-02-20 11:52:03 +01:00
parent 5c10645af7
commit 5325fa1f06
4 changed files with 178 additions and 6 deletions

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Mail;
use App\Models\Core\Mail\MailTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Spatie\MailTemplates\TemplateMailable;
class AlertePaiementAnnule extends TemplateMailable
{
use Queueable, SerializesModels;
public $subject;
public $numero_commande;
public $date_commande;
public $montant;
public $client_nom;
public $client_email;
public $reference_paiement;
protected static $templateModelClass = MailTemplate::class;
public function __construct($order, $reference)
{
$this->numero_commande = $order->ref;
$this->date_commande = $order->created_at->format('d/m/Y H:i');
$this->montant = number_format($order->total_shipped, 2, ',', ' ').' €';
$this->client_nom = $order->customer
? $order->customer->last_name.' '.$order->customer->first_name
: 'Client supprimé';
$this->client_email = $order->customer->email ?? 'inconnu';
$this->reference_paiement = $reference;
}
public function envelope()
{
return new Envelope(
from: new Address('boutique@jardinenvie.com', 'Jardin\'en\'Vie'),
subject: $this->subject,
);
}
}

View File

@@ -3,9 +3,11 @@
namespace App\Repositories\Shop;
use App\Mail\Acheminement;
use App\Mail\AlertePaiementAnnule;
use App\Mail\ConfirmationCommande;
use App\Mail\Preparation;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Log;
class OrderMails
{
@@ -37,4 +39,25 @@ class OrderMails
return Mail::to($order->customer->email)->send($mail);
}
public static function sendCancelledOrderPaymentAlert($orderId, $reference)
{
$order = Orders::get($orderId, ['customer']);
try {
Mail::to('commande@jardinenvie.com')
->send(new AlertePaiementAnnule($order, $reference));
Log::info('Cancelled order payment alert sent', [
'order_id' => $orderId,
'order_ref' => $order->ref,
'reference' => $reference,
]);
} catch (\Throwable $e) {
Log::error('Failed to send cancelled order payment alert', [
'order_id' => $orderId,
'order_ref' => $order->ref,
'exception' => $e->getMessage(),
]);
}
}
}

View File

@@ -104,7 +104,9 @@ class Paybox
return true;
}
DB::transaction(function () use ($invoice, $order, $reference, $payload, $existingPayment, &$shouldNotify) {
$isCancelled = (int) $order->status === 4;
DB::transaction(function () use ($invoice, $order, $reference, $payload, $existingPayment, &$shouldNotify, $isCancelled) {
$attributes = [
'payment_type' => 1,
'amount' => $invoice->total_shipped,
@@ -134,14 +136,24 @@ class Paybox
Invoices::checkPayments($invoice->id);
$paidStatus = Orders::getStatusByName('Préparation');
if ($paidStatus !== '' && (int) $order->status !== (int) $paidStatus) {
$order->status = $paidStatus;
$order->save();
if (! $isCancelled) {
$paidStatus = Orders::getStatusByName('Préparation');
if ($paidStatus !== '' && (int) $order->status !== (int) $paidStatus) {
$order->status = $paidStatus;
$order->save();
}
}
});
if ($shouldNotify) {
if ($isCancelled && $shouldNotify) {
Log::warning('Paybox payment received on cancelled order', [
'order_id' => $order->id,
'order_ref' => $order->ref,
'invoice_id' => $invoice->id,
'reference' => $reference,
]);
OrderMails::sendCancelledOrderPaymentAlert($order->id, $reference);
} elseif ($shouldNotify) {
try {
OrderMails::sendOrderConfirmed($order->id);
} catch (\Throwable $exception) {