fix(promotion code): Cap the minimum invoice for Stripe
Stripe does not support negative invoices, so to allow correct invoice generation, we apply dismiss negative invoices.
This commit is contained in:
@@ -130,11 +130,16 @@ class OrdersController < ApplicationController
|
||||
if params[:promotion_code].present?
|
||||
promotion_code = PromotionCode.valid.find_by(code: params[:promotion_code].upcase)
|
||||
if promotion_code
|
||||
# Apply the promotion code to the order
|
||||
@order.promotion_codes << promotion_code
|
||||
@order.calculate_total!
|
||||
@total_amount = @order.total_amount_cents
|
||||
flash.now[:notice] = "Code promotionnel appliqué: #{promotion_code.code}"
|
||||
# Check if promotion code is already applied to this order
|
||||
if @order.promotion_codes.include?(promotion_code)
|
||||
flash.now[:alert] = "Ce code promotionnel est déjà appliqué à cette commande"
|
||||
else
|
||||
# Apply the promotion code to the order
|
||||
@order.promotion_codes << promotion_code
|
||||
@order.calculate_total!
|
||||
@total_amount = @order.total_amount_cents
|
||||
flash.now[:notice] = "Code promotionnel appliqué: #{promotion_code.code}"
|
||||
end
|
||||
else
|
||||
flash.now[:alert] = "Code promotionnel invalide"
|
||||
end
|
||||
@@ -302,7 +307,14 @@ class OrdersController < ApplicationController
|
||||
end
|
||||
|
||||
def create_stripe_session
|
||||
# Calculate the discount amount per ticket to distribute the promotion evenly
|
||||
total_tickets = @order.tickets.count
|
||||
discount_per_ticket = @order.discount_amount_cents / total_tickets if total_tickets > 0
|
||||
|
||||
line_items = @order.tickets.map do |ticket|
|
||||
# Apply discount proportionally to each ticket
|
||||
discounted_price = [ticket.price_cents - discount_per_ticket.to_i, 0].max
|
||||
|
||||
{
|
||||
price_data: {
|
||||
currency: "eur",
|
||||
@@ -310,7 +322,7 @@ class OrdersController < ApplicationController
|
||||
name: "#{@order.event.name} - #{ticket.ticket_type.name}",
|
||||
description: ticket.ticket_type.description
|
||||
},
|
||||
unit_amount: ticket.price_cents
|
||||
unit_amount: discounted_price
|
||||
},
|
||||
quantity: 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user