diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 7378648..008c8e4 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -4,7 +4,7 @@ # Orders group multiple tickets together for better transaction management class OrdersController < ApplicationController before_action :authenticate_user! - before_action :set_order, only: [ :show, :checkout, :retry_payment, :increment_payment_attempt ] + before_action :set_order, only: [ :show, :checkout, :retry_payment, :increment_payment_attempt, :invoice ] before_action :set_event, only: [ :new, :create ] # Display new order form with name collection @@ -157,6 +157,26 @@ class OrdersController < ApplicationController redirect_to checkout_order_path(@order) end + # Display invoice for an order + def invoice + unless @order.status == "paid" || @order.status == "completed" + redirect_to order_path(@order), alert: "La facture n'est disponible qu'après le paiement de la commande" + return + end + + @tickets = @order.tickets.includes(:ticket_type) + + # Get the Stripe invoice if it exists + begin + @stripe_invoice_id = @order.create_stripe_invoice! + @stripe_invoice_pdf_url = @order.stripe_invoice_pdf_url if @stripe_invoice_id + rescue => e + Rails.logger.error "Failed to retrieve or create Stripe invoice for order #{@order.id}: #{e.message}" + @stripe_invoice_id = nil + @stripe_invoice_pdf_url = nil + end + end + # Handle successful payment def payment_success session_id = params[:session_id] diff --git a/app/views/orders/invoice.html.erb b/app/views/orders/invoice.html.erb new file mode 100644 index 0000000..af7a0f3 --- /dev/null +++ b/app/views/orders/invoice.html.erb @@ -0,0 +1,173 @@ +
Commande #<%= @order.id %> • <%= @order.created_at.strftime("%d %B %Y") %>
+123 Avenue des Événements
+75000 Paris, France
+contact@apero-night.fr
+<%= @order.user.email %>
+ <% if @order.user.company_name.present? %> +<%= @order.user.company_name %>
+ <% end %> +| Description | +Quantité | +Prix unitaire | +Total | +
|---|---|---|---|
|
+ <%= ticket_type.name %>
+ <%= ticket_type.description %>
+ |
+ <%= tickets.count %> | +<%= "%.2f" % (ticket_type.price_cents / 100.0) %>€ | +<%= "%.2f" % (tickets.count * ticket_type.price_cents / 100.0) %>€ | +
| Frais de service | +1 | +1.00€ | +1.00€ | +
| Total | +<%= "%.2f" % @order.total_amount_euros %>€ | +||
Commande #<%= @order.id %> payée le <%= @order.updated_at.strftime("%d %B %Y") %>
+Téléchargez ou consultez la facture de votre commande.
+