module Public class ClubsController < WebBaseController include BrandingAttachments before_action :require_login! before_action :set_club, only: %i[show edit update billing checkout portal] before_action :require_billing_profile_for_premium_checkout!, only: :checkout def new redirect_to public_club_path(current_user.primary_club) if current_user.primary_club end def create if current_user.owned_clubs.exists? redirect_to public_club_path(current_user.primary_club), alert: "Hai già registrato una società" return end club = Club.new(club_params) attach_branding_logo(club) club.save! ClubMembership.create!(user: current_user, club: club, role: "owner") first_team_name = params.dig(:first_team, :name).presence || "Prima squadra" club.teams.create!( name: first_team_name, sport: club.sport ) plan = params[:plan].presence_in(%w[free premium_light premium_full]) || "free" Billing::AssignPlan.call(club: club, plan_slug: plan) if plan.in?(%w[premium_light premium_full]) && MatchLiveTv.stripe_enabled? redirect_to public_club_checkout_path(club, plan: plan) else redirect_to public_club_path(club), notice: "Società e prima squadra create." end rescue ActiveRecord::RecordInvalid => e flash.now[:alert] = e.record.errors.full_messages.join(", ") render :new, status: :unprocessable_entity end def show require_club_owner!(@club) apply_checkout_flash! @entitlements_team = @club.teams.first @entitlements = @entitlements_team&.entitlements @teams = @club.teams.order(:name) end def edit require_club_owner!(@club) end def update require_club_owner!(@club) @club.assign_attributes(club_params) attach_branding_logo(@club) @club.save! redirect_to public_club_path(@club), notice: "Società aggiornata." rescue ActiveRecord::RecordInvalid => e flash.now[:alert] = e.record.errors.full_messages.join(", ") render :edit, status: :unprocessable_entity end def billing require_club_owner!(@club) apply_checkout_flash! @team = @club.teams.first! @entitlements = @team.entitlements @subscription = @club.subscription @plans = Plan.ordered @payments = @club.billing_payments.recent.limit(50) @invoices = @club.billing_invoices.recent.limit(50) end def checkout require_club_owner!(@club) unless MatchLiveTv.stripe_enabled? redirect_to public_club_billing_path(@club), alert: "Pagamenti non ancora configurati sul server" return end plan_slug = params[:plan].presence_in(%w[premium_light premium_full]) || "premium_light" sub = @club.subscription if sub&.stripe_subscription_id.present? && sub.active? && sub.plan.slug == plan_slug redirect_to public_club_billing_path(@club), notice: "Sei già su questo piano." return end # Sempre Stripe Checkout (hosted): i dati carta non passano mai dal nostro server. url = Billing::Stripe::CheckoutSession.new(club: @club, user: current_user, plan_slug: plan_slug).url redirect_to url, allow_other_host: true rescue ::Stripe::StripeError => e Rails.logger.warn("[Stripe checkout] #{e.message}") redirect_to public_club_billing_path(@club), alert: "Errore Stripe: #{e.message}" end def portal require_club_owner!(@club) unless MatchLiveTv.stripe_enabled? redirect_to public_club_billing_path(@club), alert: "Portale pagamenti non configurato" return end url = Billing::Stripe::CheckoutSession.new(club: @club, user: current_user).portal_url redirect_to url, allow_other_host: true end private def set_club @club = current_user.owned_clubs.find(params[:id]) end def club_params p = params.require(:club).permit( :name, :sport, :logo_url, :primary_color, :secondary_color, :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 ) p[:primary_color] = normalize_hex_color(p[:primary_color], "#e53935") p[:secondary_color] = normalize_hex_color(p[:secondary_color], "#ffffff") p end def require_billing_profile_for_premium_checkout! require_club_owner!(@club) plan_slug = params[:plan].presence_in(%w[premium_light premium_full]) return if plan_slug.blank? return if @club.billing_profile_complete? redirect_to public_club_billing_profile_path(@club), alert: "Completa i dati di fatturazione prima di attivare un piano premium." end def apply_checkout_flash! case params[:checkout] when "success" flash.now[:notice] = "Pagamento completato! Il piano premium è attivo per la società." when "canceled" flash.now[:alert] = "Pagamento annullato. Nessun addebito è stato effettuato." end end end end