require "rails_helper" RSpec.describe Billing::Stripe::CheckoutSync do let(:club) do Club.create!( name: "Sync Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff", billing_legal_name: "ASD", billing_email: "b@test.it", billing_address_line: "Via 1", billing_city: "Milano", billing_postal_code: "20100", billing_vat_number: "12345678901", billing_recipient_code: "ABCDEFG" ) end before do load Rails.root.join("db/seeds/plans.rb") Billing::AssignPlan.call(club: club, plan_slug: "free") club.subscription.update!(stripe_customer_id: "cus_sync") allow(MatchLiveTv).to receive(:stripe_enabled?).and_return(true) allow(MatchLiveTv).to receive(:stripe_premium_light_monthly_price_id).and_return("price_light_m") 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 "allinea il piano da checkout session pagata" do session = double( customer: "cus_sync", subscription: "sub_new", payment_status: "paid", status: "complete", metadata: { "club_id" => club.id, "plan_slug" => "premium_light", "billing_interval" => "monthly" } ) stripe_sub = double( id: "sub_new", customer: "cus_sync", status: "active", cancel_at_period_end: false, metadata: { "club_id" => club.id, "plan_slug" => "premium_light", "billing_interval" => "monthly" }, items: double(data: [double(price: double(id: "price_light_m", recurring: double(interval: "month")))]) ) allow(Stripe::Checkout::Session).to receive(:retrieve).with("cs_test").and_return(session) allow(Stripe::Subscription).to receive(:retrieve).with("sub_new").and_return(stripe_sub) allow(Billing::Stripe::SubscriptionPeriod).to receive(:times).and_return([Time.current, 1.month.from_now]) described_class.call(club: club, session_id: "cs_test") sub = club.reload.subscription expect(sub.plan.slug).to eq("premium_light") expect(sub.billing_interval).to eq("monthly") expect(sub.stripe_subscription_id).to eq("sub_new") end it "usa l'abbonamento Stripe attivo del cliente se manca session_id" do stripe_sub = double( id: "sub_fallback", customer: "cus_sync", status: "active", created: 2, cancel_at_period_end: false, metadata: { "club_id" => club.id, "plan_slug" => "premium_light", "billing_interval" => "yearly" }, items: double(data: [double(price: double(id: "price_light_y", recurring: double(interval: "year")))]) ) list = double(data: [stripe_sub]) allow(Stripe::Subscription).to receive(:list).and_return(list) allow(Billing::Stripe::SubscriptionPeriod).to receive(:times).and_return([Time.current, 1.year.from_now]) described_class.call(club: club) expect(club.reload.subscription.plan.slug).to eq("premium_light") expect(club.subscription.billing_interval).to eq("yearly") end end