relocating
This commit is contained in:
@@ -8,6 +8,9 @@ use App\Models\OrderItem;
|
||||
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 Illuminate\Http\Request;
|
||||
|
||||
class OrderController extends Controller
|
||||
@@ -186,8 +189,7 @@ class OrderController extends Controller
|
||||
$orderItems[$itemKey] = [
|
||||
'product_id' => $productId,
|
||||
'quantity' => $quantity,
|
||||
'price' => $product->price,
|
||||
'stock_cost' => $stockCost,
|
||||
'price' => $subtotal / $quantity,
|
||||
'print_stock_id' => $printStockId,
|
||||
'type' => $type,
|
||||
'is_sample' => $isSample,
|
||||
@@ -572,6 +574,27 @@ class OrderController extends Controller
|
||||
$product->save();
|
||||
}
|
||||
|
||||
// Generate invoice PDF
|
||||
try {
|
||||
$invoicePath = InvoiceService::generateInvoice($order);
|
||||
|
||||
// Send invoice email via Mailjet
|
||||
Mail::to($order->customer_email)
|
||||
->send(new InvoiceEmail($order, $invoicePath));
|
||||
|
||||
\Log::info('Yoco Webhook: Invoice generated and email sent', [
|
||||
'order_uuid' => $orderUuid,
|
||||
'order_id' => $order->id,
|
||||
'invoice_path' => $invoicePath,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Yoco Webhook: Invoice generation or email failed', [
|
||||
'order_uuid' => $orderUuid,
|
||||
'order_id' => $order->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
\Log::info('Yoco Webhook: Order updated for successful payment', [
|
||||
'order_uuid' => $orderUuid,
|
||||
'order_id' => $order->id,
|
||||
@@ -652,4 +675,45 @@ class OrderController extends Controller
|
||||
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the track order search form
|
||||
*/
|
||||
public function trackForm()
|
||||
{
|
||||
return view('track-order');
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for an order by order number and email
|
||||
*/
|
||||
public function trackSearch(Request $request)
|
||||
{
|
||||
$key = 'track-order:' . $request->ip();
|
||||
|
||||
// Check if IP has already exceeded rate limit from previous failed attempts
|
||||
if (\Illuminate\Support\Facades\RateLimiter::tooManyAttempts($key, 5)) {
|
||||
return redirect()->route('track-order-form')
|
||||
->withErrors(['error' => 'Too many search attempts. Please try again later.']);
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'order_number' => 'required|string',
|
||||
'email' => 'required|email',
|
||||
]);
|
||||
|
||||
$order = Order::where('order_number', strtoupper($request->input('order_number')))
|
||||
->where('customer_email', strtolower($request->input('email')))
|
||||
->first();
|
||||
|
||||
if (!$order) {
|
||||
// Only increment rate limit on failed searches
|
||||
\Illuminate\Support\Facades\RateLimiter::hit($key, 600); // 10 minutes
|
||||
return redirect()->route('track-order-form')
|
||||
->withErrors(['error' => 'No order found with the provided information.']);
|
||||
}
|
||||
|
||||
// Successful search - no rate limit increment
|
||||
return view('track-order-result', ['order' => $order]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Models\Order;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Mail\Mailables\Attachment;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class InvoiceEmail extends Mailable implements ShouldQueue
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
public function __construct(
|
||||
public Order $order,
|
||||
public string $invoicePath
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get the message envelope.
|
||||
*/
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Invoice #' . $this->order->order_number . ' from Additional Design',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message content definition.
|
||||
*/
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'emails.invoice',
|
||||
with: [
|
||||
'order' => $this->order,
|
||||
'invoiceNumber' => $this->order->order_number,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attachments for the message.
|
||||
*
|
||||
* @return array<int, Attachment>
|
||||
*/
|
||||
public function attachments(): array
|
||||
{
|
||||
return [
|
||||
Attachment::fromPath($this->invoicePath)
|
||||
->as('Invoice-' . $this->order->order_number . '.pdf')
|
||||
->withMime('application/pdf'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ class Order extends Model
|
||||
'order_number',
|
||||
'total',
|
||||
'shipping_fee',
|
||||
'invoice_path',
|
||||
'status',
|
||||
'payment_method',
|
||||
'payment_status',
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Order;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
|
||||
class InvoiceService
|
||||
{
|
||||
|
||||
/**
|
||||
* Generate an invoice PDF for an order
|
||||
*
|
||||
* @param Order $order
|
||||
* @return string The file path to the stored PDF
|
||||
*/
|
||||
public static function generateInvoice(Order $order): string
|
||||
{
|
||||
|
||||
|
||||
// Create directory structure: invoices/{year}/{month}/
|
||||
$year = $order->created_at->year;
|
||||
$month = str_pad($order->created_at->month, 2, '0', STR_PAD_LEFT);
|
||||
$directory = "invoices/{$year}/{$month}";
|
||||
|
||||
// Ensure directory exists in public disk
|
||||
if (!Storage::disk('public')->exists($directory)) {
|
||||
Storage::disk('public')->makeDirectory($directory, 0755, true);
|
||||
}
|
||||
|
||||
// Verify directory was created
|
||||
if (!Storage::disk('public')->exists($directory)) {
|
||||
throw new \Exception('Failed to create directory: ' . $directory);
|
||||
}
|
||||
|
||||
// Create filename: INV-{order_number}-{uuid}.pdf
|
||||
$filename = 'INV-' . $order->order_number . '-' . $order->uuid . '.pdf';
|
||||
$path = "{$directory}/{$filename}";
|
||||
|
||||
try {
|
||||
// Generate PDF from blade template with font configuration
|
||||
$pdf = Pdf::loadView('invoices.order-invoice', [
|
||||
'order' => $order,
|
||||
]);
|
||||
|
||||
// Get PDF output as string
|
||||
$pdfContent = $pdf->output();
|
||||
|
||||
if (empty($pdfContent)) {
|
||||
throw new \Exception('PDF generation produced empty output');
|
||||
}
|
||||
|
||||
// Store PDF to disk (using public disk)
|
||||
$stored = Storage::disk('public')->put($path, $pdfContent);
|
||||
|
||||
if (!$stored) {
|
||||
throw new \Exception('Storage::put() returned false for path: ' . $path);
|
||||
}
|
||||
|
||||
// Verify file was created
|
||||
if (!Storage::disk('public')->exists($path)) {
|
||||
throw new \Exception('File does not exist after being stored: ' . $path);
|
||||
}
|
||||
|
||||
// Update order with invoice path (include storage prefix for retrieval)
|
||||
$order->update(['invoice_path' => 'storage/' . $path]);
|
||||
|
||||
return $path;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Invoice generation failed', [
|
||||
'order_uuid' => $order->uuid,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
]);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full path to an invoice PDF
|
||||
*
|
||||
* @param string $invoicePath
|
||||
* @return string
|
||||
*/
|
||||
public static function getInvoicePath(string $invoicePath): string
|
||||
{
|
||||
return Storage::path($invoicePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an invoice exists for an order
|
||||
*
|
||||
* @param Order $order
|
||||
* @return bool
|
||||
*/
|
||||
public static function invoiceExists(Order $order): bool
|
||||
{
|
||||
return !empty($order->invoice_path) && Storage::disk('public')->exists($order->invoice_path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an invoice PDF
|
||||
*
|
||||
* @param Order $order
|
||||
* @return bool
|
||||
*/
|
||||
public static function deleteInvoice(Order $order): bool
|
||||
{
|
||||
if ($order->invoice_path && Storage::disk('public')->exists($order->invoice_path)) {
|
||||
Storage::disk('public')->delete($order->invoice_path);
|
||||
$order->update(['invoice_path' => null]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user