Billing upgrade/downgrade, inviti in app e diretta YouTube da mobile.

Upgrade Stripe con addebito immediato; downgrade programmato a fine periodo.
UX billing più sobria con box info; icone piani. API inviti e OAuth YouTube
per staff trasmissione; deep link join; scelta partita programmata o nuova.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-02 22:15:27 +02:00
parent 2cb267f991
commit 566104aff4
49 changed files with 1615 additions and 173 deletions

View File

@@ -67,4 +67,13 @@ RSpec.describe Public::BillingHelper, type: :helper do
it "mostra prima il pulsante annuale" do
expect(helper.plan_intervals_for_display(%w[monthly yearly])).to eq(%w[yearly monthly])
end
it "sulla card Free non mostra testo se c'è abbonamento Stripe" do
action = helper.plan_billing_action(
current_slug: "premium_full",
target_plan: Plan["free"],
stripe_subscription_active: true
)
expect(action[:kind]).to eq(:none)
end
end

View File

@@ -0,0 +1,54 @@
require "rails_helper"
RSpec.describe "Api::V1::Invitations", type: :request do
let(:club) { Club.create!(name: "FC Test", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
let(:team) { club.teams.create!(name: "Squadra A", sport: "volleyball") }
let(:owner) do
User.create!(email: "owner@test.it", name: "Owner", password: "password123", role: "coach").tap do |u|
ClubMembership.create!(user: u, club: club, role: "owner")
end
end
let(:invitee) { User.create!(email: "streamer@test.it", name: "Streamer", password: "password123", role: "coach") }
let(:token) { TeamInvitation.generate_token }
let!(:invitation) do
team.team_invitations.create!(
email: invitee.email,
token_digest: Digest::SHA256.hexdigest(token),
role: "member",
staff_kind: "transmission",
expires_at: 7.days.from_now
)
end
describe "GET /api/v1/invitations/:token" do
it "mostra anteprima invito valido" do
get "/api/v1/invitations/#{token}"
expect(response).to have_http_status(:ok)
expect(response.parsed_body["email"]).to eq("streamer@test.it")
expect(response.parsed_body["team_name"]).to eq("Squadra A")
end
end
describe "POST /api/v1/invitations/:token/accept" do
it "accetta con email corretta" do
post "/api/v1/auth/login", params: { email: invitee.email, password: "password123" }
access = response.parsed_body["access_token"]
post "/api/v1/invitations/#{token}/accept",
headers: { "Authorization" => "Bearer #{access}" }
expect(response).to have_http_status(:ok)
expect(invitee.user_teams.find_by(team: team)&.staff_kind).to eq("transmission")
end
it "rifiuta email diversa" do
post "/api/v1/auth/login", params: { email: owner.email, password: "password123" }
access = response.parsed_body["access_token"]
post "/api/v1/invitations/#{token}/accept",
headers: { "Authorization" => "Bearer #{access}" }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

View File

@@ -11,48 +11,69 @@ RSpec.describe Billing::Stripe::ChangePlan do
status: "active",
stripe_attrs: {
stripe_customer_id: "cus_test",
stripe_subscription_id: "sub_test_123"
stripe_subscription_id: "sub_test_123",
billing_interval: "monthly",
current_period_end: 1.month.from_now
}
)
allow(MatchLiveTv).to receive(:stripe_enabled?).and_return(true)
allow(MatchLiveTv).to receive(:stripe_premium_light_yearly_price_id).and_return("price_light_y")
allow(MatchLiveTv).to receive(:stripe_premium_light_monthly_price_id).and_return("price_light_m")
allow(MatchLiveTv).to receive(:stripe_premium_full_yearly_price_id).and_return("price_full_y")
allow(MatchLiveTv).to receive(:stripe_premium_light_yearly_price_id).and_return("price_light_y")
allow(MatchLiveTv).to receive(:stripe_premium_full_monthly_price_id).and_return("price_full_m")
allow(MatchLiveTv).to receive(:stripe_premium_full_yearly_price_id).and_return("price_full_y")
end
it "aggiorna Stripe e il piano locale verso premium_full" do
stripe_item = double(id: "si_test")
it "upgrade: always_invoice e addebito immediato" do
stripe_item = double(id: "si_test", price: double(id: "price_light_m"))
stripe_sub = double(
id: "sub_test_123",
customer: "cus_test",
status: "active",
current_period_start: Time.current.to_i,
current_period_end: 1.year.from_now.to_i,
cancel_at_period_end: false,
items: double(data: [stripe_item])
)
updated_sub = stripe_sub
allow(stripe_sub).to receive(:current_period_start).and_return(Time.current.to_i)
allow(stripe_sub).to receive(:current_period_end).and_return(1.month.from_now.to_i)
allow(stripe_item).to receive(:current_period_start).and_return(Time.current.to_i)
allow(stripe_item).to receive(:current_period_end).and_return(1.month.from_now.to_i)
invoice = double(id: "in_up", status: "paid", amount_paid: 1500, currency: "eur")
allow(Stripe::Subscription).to receive(:retrieve).with("sub_test_123").and_return(stripe_sub)
allow(Stripe::Subscription).to receive(:update).and_return(updated_sub)
allow(Stripe::Subscription).to receive(:update).and_return(stripe_sub)
allow(Stripe::Invoice).to receive(:list).and_return(double(data: [invoice]))
allow(Billing::Stripe::RecordPayment).to receive(:call)
described_class.call(club: club, plan_slug: "premium_full", interval: "yearly")
result = described_class.call(club: club, plan_slug: "premium_full", interval: "monthly")
expect(Stripe::Subscription).to have_received(:update).with(
"sub_test_123",
hash_including(
items: [{ id: "si_test", price: "price_full_y" }],
metadata: { club_id: club.id, plan_slug: "premium_full", billing_interval: "yearly" }
)
hash_including(proration_behavior: "always_invoice", payment_behavior: "error_if_incomplete")
)
expect(result).to be_upgrade
expect(club.reload.subscription.plan.slug).to eq("premium_full")
end
it "rifiuta il cambio sullo stesso piano e intervallo" do
club.subscription.update!(billing_interval: "yearly")
expect {
described_class.call(club: club, plan_slug: "premium_light", interval: "yearly")
}.to raise_error(/già su questo piano/)
it "downgrade: programma ScheduleDowngrade senza cambiare piano subito" do
Billing::AssignPlan.call(
club: club,
plan_slug: "premium_full",
status: "active",
stripe_attrs: { stripe_subscription_id: "sub_test_123", billing_interval: "monthly" }
)
allow(Billing::Stripe::ScheduleDowngrade).to receive(:call).and_return(
Billing::Stripe::ChangePlanResult.new(
kind: :downgrade,
plan_slug: "premium_light",
interval: "monthly",
effective_at: 1.month.from_now
)
)
result = described_class.call(club: club, plan_slug: "premium_light", interval: "monthly")
expect(Billing::Stripe::ScheduleDowngrade).to have_received(:call)
expect(result).to be_downgrade
expect(club.reload.subscription.plan.slug).to eq("premium_full")
end
end

View File

@@ -0,0 +1,32 @@
require "rails_helper"
RSpec.describe Billing::Stripe::PlanChangeKind do
before { load Rails.root.join("db/seeds/plans.rb") }
it "classifica light → full come upgrade" do
expect(described_class.classify(
from_plan_slug: "premium_light",
from_interval: "monthly",
to_plan_slug: "premium_full",
to_interval: "monthly"
)).to eq(:upgrade)
end
it "classifica full → light come downgrade" do
expect(described_class.classify(
from_plan_slug: "premium_full",
from_interval: "monthly",
to_plan_slug: "premium_light",
to_interval: "monthly"
)).to eq(:downgrade)
end
it "classifica passaggio a annuale come upgrade sullo stesso piano" do
expect(described_class.classify(
from_plan_slug: "premium_light",
from_interval: "monthly",
to_plan_slug: "premium_light",
to_interval: "yearly"
)).to eq(:upgrade)
end
end