diff --git a/backend/app/controllers/public/clubs_controller.rb b/backend/app/controllers/public/clubs_controller.rb index 4da8036..895d579 100644 --- a/backend/app/controllers/public/clubs_controller.rb +++ b/backend/app/controllers/public/clubs_controller.rb @@ -23,23 +23,28 @@ module Public ClubMembership.create!(user: current_user, club: club, role: "owner") first_team_name = params.dig(:first_team, :name).presence || t("club.new.default_first_team_name") - club.teams.create!( + first_team = 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) + desired_plan = params[:plan].presence_in(%w[free premium_light premium_full]) || "free" + Billing::AssignPlan.call(club: club, plan_slug: "free") + Teams::StaffAssignment.designate_club_owner!(team: first_team, user: current_user) - if plan.in?(%w[premium_light premium_full]) && MatchLiveTv.stripe_enabled? - unless club.billing_profile_complete? - redirect_to public_club_billing_profile_path(club, plan: plan, interval: checkout_interval_param), - alert: t("flash.clubs.complete_billing_first") - return + if desired_plan.in?(%w[premium_light premium_full]) + interval = Billing::Stripe::PriceCatalog::DEFAULT_INTERVAL + if MatchLiveTv.stripe_enabled? + unless club.billing_profile_complete? + redirect_to public_club_billing_profile_path(club, plan: desired_plan, interval: interval), + alert: t("flash.clubs.complete_billing_first") + return + end + + redirect_to public_club_checkout_path(club, plan: desired_plan, interval: interval) + else + redirect_to public_club_billing_path(club), notice: t("flash.clubs.created_complete_subscription") end - - interval = checkout_interval_param - redirect_to public_club_checkout_path(club, plan: plan, interval: interval) else redirect_to public_club_path(club), notice: t("flash.clubs.created") end diff --git a/backend/app/controllers/public/teams_controller.rb b/backend/app/controllers/public/teams_controller.rb index 84e6374..22ef10b 100644 --- a/backend/app/controllers/public/teams_controller.rb +++ b/backend/app/controllers/public/teams_controller.rb @@ -19,6 +19,7 @@ module Public require_club_owner!(@club) team = @club.teams.create!(team_params) attach_branding_logo(team) + Teams::StaffAssignment.designate_club_owner!(team: team, user: current_user) redirect_to public_club_path(@club), notice: t("flash.teams.added", name: team.name) rescue ActiveRecord::RecordInvalid => e flash.now[:alert] = e.record.errors.full_messages.join(", ") diff --git a/backend/app/helpers/public/billing_helper.rb b/backend/app/helpers/public/billing_helper.rb index 63ec21e..152ebe4 100644 --- a/backend/app/helpers/public/billing_helper.rb +++ b/backend/app/helpers/public/billing_helper.rb @@ -11,6 +11,13 @@ module Public quote = club&.active_billing_quote if quote if quote.plan_slug == target_plan.slug + if current_paid_plan?(subscription, target_plan) + return { + kind: :current, + label: "#{I18n.t('billing.actions.current_plan')} — #{quote.price_label}" + } + end + return { kind: :quoted, plan: target_plan, quote: quote, intervals: [quote.billing_interval] } end @@ -19,6 +26,9 @@ module Public intervals = bank_transfer_intervals_for(target_plan) unless MatchLiveTv.stripe_enabled? + if current_paid_plan?(subscription, target_plan) + return { kind: :current, label: current_plan_label(target_plan, subscription) } + end if MatchLiveTv.bank_transfer_configured? return { kind: :bank_only, plan: target_plan, intervals: intervals } end @@ -28,6 +38,9 @@ module Public stripe_intervals = Billing::Stripe::PriceCatalog.available_intervals(plan_slug: target_plan.slug) if stripe_intervals.empty? + if current_paid_plan?(subscription, target_plan) + return { kind: :current, label: current_plan_label(target_plan, subscription) } + end if MatchLiveTv.bank_transfer_configured? return { kind: :bank_only, plan: target_plan, intervals: intervals } end @@ -54,6 +67,10 @@ module Public } end + if current_paid_plan?(subscription, target_plan) + return { kind: :current, label: current_plan_label(target_plan, subscription) } + end + if current_slug == "free" || !stripe_subscription_active { kind: :checkout_options, plan: target_plan, intervals: stripe_intervals.presence || intervals, subscription: subscription } else @@ -136,8 +153,21 @@ module Public return false unless MatchLiveTv.bank_transfer_configured? return false if quote && !quote.matches?(plan.slug, interval) return false if pending_transfer&.awaiting_payment? + return false if current_paid_plan?(club.subscription, plan) true end + + def current_paid_plan?(subscription, target_plan) + return false unless subscription&.active? && subscription.premium? + return false if subscription.plan_change_pending? + + subscription.plan.slug == target_plan.slug + end + + def current_plan_label(target_plan, subscription) + interval = subscription&.billing_interval.presence || Billing::Stripe::PriceCatalog::DEFAULT_INTERVAL + "#{I18n.t('billing.actions.current_plan')} — #{Billing::Stripe::PriceCatalog.label(plan_slug: target_plan.slug, interval: interval)}" + end end end diff --git a/backend/app/services/billing/confirm_bank_transfer.rb b/backend/app/services/billing/confirm_bank_transfer.rb index 2c28475..c228981 100644 --- a/backend/app/services/billing/confirm_bank_transfer.rb +++ b/backend/app/services/billing/confirm_bank_transfer.rb @@ -88,7 +88,7 @@ module Billing mailer_action: :plan_activated_with_invoice ) else - BankTransferMailer.with(order: @order.reload).plan_activated.deliver_now + MatchLiveTv.deliver_mail(BankTransferMailer.with(order: @order.reload).plan_activated) end end diff --git a/backend/app/services/billing/issue_invoice.rb b/backend/app/services/billing/issue_invoice.rb index bdabfe2..ae25159 100644 --- a/backend/app/services/billing/issue_invoice.rb +++ b/backend/app/services/billing/issue_invoice.rb @@ -26,8 +26,10 @@ module Billing @invoice.update!(status: "issued") - Billing::InvoiceMailer.with(invoice: @invoice).public_send(@mailer_action).deliver_now - @invoice.update!(status: "sent", emailed_at: Time.current) + mail = Billing::InvoiceMailer.with(invoice: @invoice).public_send(@mailer_action) + if MatchLiveTv.deliver_mail(mail) + @invoice.update!(status: "sent", emailed_at: Time.current) + end @invoice end diff --git a/backend/app/services/billing/request_bank_transfer.rb b/backend/app/services/billing/request_bank_transfer.rb index 92197ef..c95deca 100644 --- a/backend/app/services/billing/request_bank_transfer.rb +++ b/backend/app/services/billing/request_bank_transfer.rb @@ -28,7 +28,7 @@ module Billing existing = @club.billing_transfer_orders.awaiting_payment.first if existing if existing.plan_slug == @plan_slug && existing.billing_interval == @interval && existing.amount_cents == amount_cents - BankTransferMailer.with(order: existing).instructions.deliver_now + MatchLiveTv.deliver_mail(BankTransferMailer.with(order: existing).instructions) return existing end @@ -59,7 +59,7 @@ module Billing ) end - BankTransferMailer.with(order: order).instructions.deliver_now + MatchLiveTv.deliver_mail(BankTransferMailer.with(order: order).instructions) order end diff --git a/backend/app/services/teams/staff_assignment.rb b/backend/app/services/teams/staff_assignment.rb index 14ecc2e..330b2ae 100644 --- a/backend/app/services/teams/staff_assignment.rb +++ b/backend/app/services/teams/staff_assignment.rb @@ -23,5 +23,12 @@ module Teams ut.update!(staff_kind: "transmission") ut end + + def self.designate_club_owner!(team:, user:) + membership = user.user_teams.find_or_initialize_by(team: team) + membership.role = "member" if membership.new_record? + membership.save! + call(team: team, user: user, membership: membership) + end end end diff --git a/backend/app/views/admin/billing/index.html.erb b/backend/app/views/admin/billing/index.html.erb index 824c2d1..4194e4c 100644 --- a/backend/app/views/admin/billing/index.html.erb +++ b/backend/app/views/admin/billing/index.html.erb @@ -42,6 +42,10 @@
+ <%= t("admin.billing.index.transfers_table.holder") %>:
+ <%= MatchLiveTv.bank_transfer_account_holder %>
+ <%= t("admin.billing.index.transfers_table.iban") %>:
+ <%= MatchLiveTv.bank_transfer_iban %>
<%= t("admin.billing.index.transfers_table.causal") %>:
<%= order.payment_causal %>