Fix PCI Stripe e anteprima regia; checkout sempre hosted.
Blocca l'invio di numeri carta dall'API server, usa Checkout anche per i cambi piano e nasconde correttamente il placeholder video in regia. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -83,17 +83,15 @@ module Public
|
||||
end
|
||||
|
||||
plan_slug = params[:plan].presence_in(%w[premium_light premium_full]) || "premium_light"
|
||||
target_plan = Plan[plan_slug]
|
||||
sub = @club.subscription
|
||||
|
||||
if sub&.stripe_subscription_id.present? && sub.active? && sub.plan.slug != plan_slug
|
||||
Billing::Stripe::ChangePlan.call(club: @club, plan_slug: plan_slug)
|
||||
redirect_to public_club_billing_path(@club),
|
||||
notice: "Piano aggiornato a #{target_plan.name}. L'eventuale differenza verrà addebitata o accreditata da Stripe."
|
||||
else
|
||||
url = Billing::Stripe::CheckoutSession.new(club: @club, user: current_user, plan_slug: plan_slug).url
|
||||
redirect_to url, allow_other_host: true
|
||||
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}"
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
module Billing
|
||||
module Stripe
|
||||
# Solo per sincronizzazione interna/test mockati.
|
||||
# I cambi piano utente devono passare da CheckoutSession (Stripe Checkout hosted).
|
||||
# Non creare mai PaymentMethod con card[number] sul server (violazione PCI).
|
||||
class ChangePlan
|
||||
VALID_PLANS = CheckoutSession::VALID_PLANS.freeze
|
||||
|
||||
|
||||
@@ -18,15 +18,29 @@ module Billing
|
||||
raise "Price ID Stripe mancante per #{@plan_slug}" if price_id.blank?
|
||||
|
||||
customer_id = ensure_customer_id!
|
||||
session = ::Stripe::Checkout::Session.create(
|
||||
CustomerSync.call(club: @club, customer_id: customer_id)
|
||||
|
||||
session_params = {
|
||||
mode: "subscription",
|
||||
customer: customer_id,
|
||||
line_items: [{ price: price_id, quantity: 1 }],
|
||||
success_url: success_url,
|
||||
cancel_url: cancel_url,
|
||||
metadata: { club_id: @club.id, user_id: @user.id, plan_slug: @plan_slug },
|
||||
subscription_data: { metadata: { club_id: @club.id, plan_slug: @plan_slug } }
|
||||
)
|
||||
subscription_data: { metadata: { club_id: @club.id, plan_slug: @plan_slug } },
|
||||
billing_address_collection: "required",
|
||||
customer_update: { address: "auto", name: "auto" },
|
||||
allow_promotion_codes: true
|
||||
}
|
||||
|
||||
existing_subscription_id = active_stripe_subscription_id
|
||||
if existing_subscription_id.present?
|
||||
# Cambio piano: conferma su pagina Stripe (mai dati carta sul nostro server).
|
||||
session_params[:subscription] = existing_subscription_id
|
||||
session_params.delete(:subscription_data)
|
||||
end
|
||||
|
||||
session = ::Stripe::Checkout::Session.create(session_params)
|
||||
session.url
|
||||
end
|
||||
|
||||
@@ -34,9 +48,10 @@ module Billing
|
||||
raise "Stripe non configurato" unless MatchLiveTv.stripe_enabled?
|
||||
|
||||
customer_id = ensure_customer_id!
|
||||
CustomerSync.call(club: @club, customer_id: customer_id)
|
||||
session = ::Stripe::BillingPortal::Session.create(
|
||||
customer: customer_id,
|
||||
return_url: dashboard_url
|
||||
return_url: billing_url
|
||||
)
|
||||
session.url
|
||||
end
|
||||
@@ -50,13 +65,20 @@ module Billing
|
||||
end
|
||||
end
|
||||
|
||||
def active_stripe_subscription_id
|
||||
sub = @club.subscription
|
||||
return nil unless sub&.stripe_subscription_id.present? && sub.active?
|
||||
|
||||
sub.stripe_subscription_id
|
||||
end
|
||||
|
||||
def ensure_customer_id!
|
||||
sub = @club.subscription || @club.build_subscription(plan: Plan["free"], status: "active")
|
||||
return sub.stripe_customer_id if sub.stripe_customer_id.present?
|
||||
|
||||
customer = ::Stripe::Customer.create(
|
||||
email: @user.email,
|
||||
name: @club.name,
|
||||
name: @club.billing_legal_name.presence || @club.name,
|
||||
metadata: { club_id: @club.id }
|
||||
)
|
||||
sub.update!(stripe_customer_id: customer.id)
|
||||
@@ -74,10 +96,6 @@ module Billing
|
||||
def billing_url
|
||||
"#{MatchLiveTv.app_public_url.chomp('/')}/clubs/#{@club.id}/billing"
|
||||
end
|
||||
|
||||
def dashboard_url
|
||||
"#{MatchLiveTv.app_public_url.chomp('/')}/clubs/#{@club.id}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
41
backend/app/services/billing/stripe/customer_sync.rb
Normal file
41
backend/app/services/billing/stripe/customer_sync.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
module Billing
|
||||
module Stripe
|
||||
# Sincronizza solo dati di fatturazione (mai dati carta) sul Customer Stripe.
|
||||
class CustomerSync
|
||||
def self.call(club:, customer_id:)
|
||||
new(club: club, customer_id: customer_id).call
|
||||
end
|
||||
|
||||
def initialize(club:, customer_id:)
|
||||
@club = club
|
||||
@customer_id = customer_id
|
||||
end
|
||||
|
||||
def call
|
||||
return @customer_id if @customer_id.blank?
|
||||
|
||||
::Stripe::Customer.update(@customer_id, customer_attributes)
|
||||
@customer_id
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def customer_attributes
|
||||
attrs = {
|
||||
name: @club.billing_legal_name.presence || @club.name,
|
||||
email: @club.billing_email.presence,
|
||||
metadata: { club_id: @club.id },
|
||||
address: {
|
||||
line1: @club.billing_address_line,
|
||||
city: @club.billing_city,
|
||||
state: @club.billing_province,
|
||||
postal_code: @club.billing_postal_code,
|
||||
country: @club.billing_country.presence || "IT"
|
||||
}
|
||||
}
|
||||
attrs[:address] = nil if attrs[:address].values.all?(&:blank?)
|
||||
attrs.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -43,7 +43,7 @@
|
||||
<% unless @stream_closed %>
|
||||
<video id="regia-preview" playsinline muted autoplay></video>
|
||||
<% end %>
|
||||
<div id="regia-preview-placeholder" class="regia-preview__placeholder"<%= ' hidden' unless @stream_closed %>>
|
||||
<div id="regia-preview-placeholder" class="regia-preview__placeholder">
|
||||
<%= @stream_closed ? "Diretta terminata" : "Anteprima in attesa del segnale…" %>
|
||||
</div>
|
||||
</div>
|
||||
@@ -94,4 +94,4 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/regia.js?v=1"></script>
|
||||
<script src="/regia.js?v=2"></script>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
<% elsif plan.slug == "free" %>
|
||||
<p style="color:#888;font-size:0.85rem">Contatta il supporto per downgrade.</p>
|
||||
<% elsif MatchLiveTv.stripe_enabled? %>
|
||||
<%= link_to "Attiva #{plan.name}", public_team_checkout_path(@team, plan: plan.slug), class: "btn btn-primary" %>
|
||||
<%= link_to "Attiva #{plan.name}", public_club_checkout_path(@team.club, plan: plan.slug), class: "btn btn-primary" %>
|
||||
<% else %>
|
||||
<p style="color:#888">Stripe non configurato</p>
|
||||
<% end %>
|
||||
@@ -42,7 +42,7 @@
|
||||
</div>
|
||||
|
||||
<% if @entitlements.premium_active? && MatchLiveTv.stripe_enabled? %>
|
||||
<p style="margin-top:20px"><%= button_to "Gestisci su Stripe", public_team_portal_path(@team), class: "btn btn-secondary" %></p>
|
||||
<p style="margin-top:20px"><%= button_to "Gestisci su Stripe", public_club_portal_path(@team.club), class: "btn btn-secondary" %></p>
|
||||
<% end %>
|
||||
|
||||
<p><%= link_to "← Dettagli squadra", public_team_details_path(@team) %></p>
|
||||
|
||||
Reference in New Issue
Block a user