Invoice system working
This commit is contained in:
@@ -9,8 +9,7 @@ use App\Models\Product;
|
||||
use App\Models\PrintStock;
|
||||
use App\Services\ShippingService;
|
||||
use App\Services\InvoiceService;
|
||||
use App\Mail\InvoiceEmail;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Services\MailjetService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OrderController extends Controller
|
||||
@@ -574,19 +573,35 @@ class OrderController extends Controller
|
||||
$product->save();
|
||||
}
|
||||
|
||||
// Generate invoice PDF
|
||||
// Generate invoice PDF and send email via Mailjet
|
||||
try {
|
||||
$invoicePath = InvoiceService::generateInvoice($order);
|
||||
$fullPath = storage_path('app/public/' . $invoicePath);
|
||||
|
||||
// Send invoice email via Mailjet
|
||||
Mail::to($order->customer_email)
|
||||
->send(new InvoiceEmail($order, $invoicePath));
|
||||
// Send invoice email directly via Mailjet API (no queue needed)
|
||||
$mailjetService = new MailjetService();
|
||||
$customerName = $order->customer_name ?? explode('@', $order->customer_email)[0];
|
||||
|
||||
\Log::info('Yoco Webhook: Invoice generated and email sent', [
|
||||
'order_uuid' => $orderUuid,
|
||||
'order_id' => $order->id,
|
||||
'invoice_path' => $invoicePath,
|
||||
]);
|
||||
$success = $mailjetService->send(
|
||||
toEmail: $order->customer_email,
|
||||
toName: $customerName,
|
||||
subject: 'Invoice #' . $order->order_number . ' - ADDITIONAL DESIGN',
|
||||
htmlContent: $this->getBasicInvoiceHtml($order),
|
||||
attachments: [$fullPath]
|
||||
);
|
||||
|
||||
if ($success) {
|
||||
\Log::info('Yoco Webhook: Invoice generated and email sent via Mailjet', [
|
||||
'order_uuid' => $orderUuid,
|
||||
'order_id' => $order->id,
|
||||
'invoice_path' => $invoicePath,
|
||||
]);
|
||||
} else {
|
||||
\Log::warning('Yoco Webhook: Mailjet email send returned false', [
|
||||
'order_uuid' => $orderUuid,
|
||||
'order_id' => $order->id,
|
||||
]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Yoco Webhook: Invoice generation or email failed', [
|
||||
'order_uuid' => $orderUuid,
|
||||
@@ -716,4 +731,55 @@ class OrderController extends Controller
|
||||
// Successful search - no rate limit increment
|
||||
return view('track-order-result', ['order' => $order]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate basic HTML template for invoice email
|
||||
*/
|
||||
private function getBasicInvoiceHtml(Order $order): string
|
||||
{
|
||||
return <<<HTML
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; color: #333; }
|
||||
.header { background-color: #f5f5f5; padding: 20px; text-align: center; }
|
||||
.content { padding: 20px; }
|
||||
.footer { background-color: #f5f5f5; padding: 20px; text-align: center; font-size: 12px; }
|
||||
table { width: 100%; border-collapse: collapse; margin: 20px 0; }
|
||||
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
|
||||
th { background-color: #f5f5f5; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<h1>Invoice #{$order->order_number}</h1>
|
||||
</div>
|
||||
<div class="content">
|
||||
<p>Dear {$order->customer_name},</p>
|
||||
<p>Please find your invoice attached to this email.</p>
|
||||
<h3>Order Details</h3>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Order Number</th>
|
||||
<td>{$order->order_number}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Order Date</th>
|
||||
<td>{$order->created_at->format('d M Y')}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Total Amount</th>
|
||||
<td>R {$order->total}</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>Thank you for your order!</p>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<p>© 2025 ADDITIONAL DESIGN. All rights reserved.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
HTML;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user