Files
MatchLiveTv/backend/app/controllers/public/club_billing_controller.rb
T
eminuxandCursor e436f5ece4 Aggiunge pagamento con bonifico e importo concordato per società.
Il piano si attiva solo dopo la conferma admin; Stripe resta a listino se non c'è un prezzo commerciale.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-18 23:20:43 +02:00

121 lines
4.0 KiB
Ruby

module Public
class ClubBillingController < WebBaseController
before_action :require_login!
before_action :set_club
before_action :require_club_owner_for_club!
def profile
end
def update_profile
@club.assign_attributes(billing_profile_params)
if @club.save(context: :billing_profile)
if premium_checkout_return_params.present? && @club.billing_profile_complete?
if @club.active_billing_quote.present?
redirect_to public_club_billing_path(@club), notice: t("flash.club_billing.profile_updated")
else
redirect_to public_club_checkout_path(
@club,
plan: premium_checkout_return_params[:plan],
interval: premium_checkout_return_params[:interval]
), notice: t("flash.club_billing.profile_saved_proceed_payment")
end
else
redirect_to public_club_billing_path(@club), notice: t("flash.club_billing.profile_updated")
end
else
flash.now[:alert] = @club.errors.full_messages.join(", ")
render :profile, status: :unprocessable_entity
end
end
def cancel_subscription
unless MatchLiveTv.stripe_enabled?
redirect_to public_club_billing_path(@club), alert: t("flash.club_billing.stripe_not_configured")
return
end
Billing::Stripe::CancelSubscription.call(club: @club)
sub = @club.subscription.reload
date = sub.current_period_end ? helpers.l_local(sub.current_period_end.to_date) : t("billing.subscription_status.end_of_period")
redirect_to public_club_billing_path(@club),
notice: t("flash.club_billing.subscription_canceled", date: date)
rescue ArgumentError, RuntimeError => e
redirect_to public_club_billing_path(@club), alert: e.message
rescue ::Stripe::StripeError => e
redirect_to public_club_billing_path(@club), alert: t("flash.club_billing.stripe_error", message: e.message)
end
def request_bank_transfer
if @club.subscription&.admin_comped?
redirect_to public_club_billing_path(@club), alert: t("flash.clubs.comped_change_denied")
return
end
unless @club.billing_profile_complete?
redirect_to public_club_billing_profile_path(@club, plan: params[:plan], interval: params[:interval]),
alert: t("flash.clubs.complete_billing_first")
return
end
Billing::RequestBankTransfer.call(
club: @club,
user: current_user,
plan_slug: params[:plan],
interval: params[:interval]
)
redirect_to public_club_billing_path(@club), notice: t("flash.club_billing.bank_transfer_requested")
rescue Billing::RequestBankTransfer::Error, ArgumentError => e
redirect_to public_club_billing_path(@club), alert: e.message
end
def download_invoice
invoice = @club.billing_invoices.find(params[:invoice_id])
unless invoice.pdf.attached?
redirect_to public_club_billing_path(@club), alert: t("flash.club_billing.invoice_pdf_unavailable")
return
end
redirect_to rails_blob_path(invoice.pdf, disposition: "attachment"), allow_other_host: true
end
private
def set_club
@club = current_user.owned_clubs.find(params[:id])
end
def require_club_owner_for_club!
require_club_owner!(@club)
end
def premium_checkout_return_params
plan = params[:plan].presence_in(%w[premium_light premium_full])
return nil if plan.blank?
{
plan: plan,
interval: params[:interval].presence || Billing::Stripe::PriceCatalog::DEFAULT_INTERVAL
}
end
def billing_profile_params
params.require(:club).permit(
:billing_entity_type,
:billing_legal_name,
:billing_vat_number,
:billing_fiscal_code,
:billing_email,
:billing_phone,
:billing_address_line,
:billing_city,
:billing_province,
:billing_postal_code,
:billing_country,
:billing_recipient_code,
:billing_pec
)
end
end
end