Initial commit: monorepo Match Live TV.
Rails API, app Flutter, infrastruttura Docker/MediaMTX, sito marketing e documentazione di deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
79
backend/app/services/billing/stripe/checkout_session.rb
Normal file
79
backend/app/services/billing/stripe/checkout_session.rb
Normal file
@@ -0,0 +1,79 @@
|
||||
module Billing
|
||||
module Stripe
|
||||
class CheckoutSession
|
||||
VALID_PLANS = %w[premium_light premium_full].freeze
|
||||
|
||||
def initialize(team:, user:, plan_slug: "premium_light")
|
||||
@team = team
|
||||
@user = user
|
||||
@plan_slug = plan_slug.to_s
|
||||
end
|
||||
|
||||
def url
|
||||
raise "Stripe non configurato" unless MatchLiveTv.stripe_enabled?
|
||||
raise "Piano non valido" unless VALID_PLANS.include?(@plan_slug)
|
||||
|
||||
plan = Plan[@plan_slug]
|
||||
price_id = plan.stripe_price_id.presence || stripe_price_id_for(@plan_slug)
|
||||
raise "Price ID Stripe mancante per #{@plan_slug}" if price_id.blank?
|
||||
|
||||
customer_id = ensure_customer_id!
|
||||
session = ::Stripe::Checkout::Session.create(
|
||||
mode: "subscription",
|
||||
customer: customer_id,
|
||||
line_items: [{ price: price_id, quantity: 1 }],
|
||||
success_url: success_url,
|
||||
cancel_url: cancel_url,
|
||||
metadata: { team_id: @team.id, user_id: @user.id, plan_slug: @plan_slug },
|
||||
subscription_data: { metadata: { team_id: @team.id, plan_slug: @plan_slug } }
|
||||
)
|
||||
session.url
|
||||
end
|
||||
|
||||
def portal_url
|
||||
raise "Stripe non configurato" unless MatchLiveTv.stripe_enabled?
|
||||
|
||||
customer_id = ensure_customer_id!
|
||||
session = ::Stripe::BillingPortal::Session.create(
|
||||
customer: customer_id,
|
||||
return_url: dashboard_url
|
||||
)
|
||||
session.url
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def stripe_price_id_for(slug)
|
||||
case slug
|
||||
when "premium_light" then MatchLiveTv.stripe_premium_light_price_id
|
||||
when "premium_full" then MatchLiveTv.stripe_premium_full_price_id
|
||||
end
|
||||
end
|
||||
|
||||
def ensure_customer_id!
|
||||
sub = @team.subscription || @team.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: @team.name,
|
||||
metadata: { team_id: @team.id }
|
||||
)
|
||||
sub.update!(stripe_customer_id: customer.id)
|
||||
customer.id
|
||||
end
|
||||
|
||||
def success_url
|
||||
"#{dashboard_url}?checkout=success"
|
||||
end
|
||||
|
||||
def cancel_url
|
||||
"#{dashboard_url}?checkout=canceled"
|
||||
end
|
||||
|
||||
def dashboard_url
|
||||
"#{MatchLiveTv.app_public_url.chomp('/')}/teams/#{@team.id}/dashboard"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
131
backend/app/services/billing/stripe/webhook_handler.rb
Normal file
131
backend/app/services/billing/stripe/webhook_handler.rb
Normal file
@@ -0,0 +1,131 @@
|
||||
module Billing
|
||||
module Stripe
|
||||
class WebhookHandler
|
||||
def self.call(event)
|
||||
new(event).call
|
||||
end
|
||||
|
||||
def initialize(event)
|
||||
@event = event
|
||||
end
|
||||
|
||||
def call
|
||||
case @event.type
|
||||
when "checkout.session.completed"
|
||||
handle_checkout_completed(@event.data.object)
|
||||
when "customer.subscription.updated", "customer.subscription.created"
|
||||
sync_subscription(@event.data.object)
|
||||
when "customer.subscription.deleted"
|
||||
downgrade_to_free(@event.data.object)
|
||||
when "invoice.payment_failed"
|
||||
mark_past_due(@event.data.object)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def handle_checkout_completed(session)
|
||||
team_id = metadata_team_id(session)
|
||||
return if team_id.blank?
|
||||
|
||||
team = Team.find_by(id: team_id)
|
||||
return unless team
|
||||
|
||||
sub = team.subscription || team.build_subscription
|
||||
sub.update!(stripe_customer_id: session.customer) if session.customer.present?
|
||||
|
||||
if session.subscription.present?
|
||||
stripe_sub = ::Stripe::Subscription.retrieve(session.subscription)
|
||||
plan_slug = metadata_plan_slug(session) || "premium_light"
|
||||
plan_slug = "premium_full" if plan_slug == "premium"
|
||||
sync_subscription(stripe_sub, team: team, plan_slug: plan_slug)
|
||||
end
|
||||
end
|
||||
|
||||
def sync_subscription(stripe_sub, team: nil, plan_slug: nil)
|
||||
team ||= Team.joins(:subscription)
|
||||
.find_by(subscriptions: { stripe_subscription_id: stripe_sub.id })
|
||||
team ||= Team.find_by(id: metadata_team_id(stripe_sub))
|
||||
return unless team
|
||||
|
||||
plan_slug ||= metadata_plan_slug(stripe_sub) || "premium_light"
|
||||
plan_slug = "premium_full" if plan_slug == "premium"
|
||||
plan = Plan[plan_slug]
|
||||
status = map_status(stripe_sub.status)
|
||||
period_start = unix_time(stripe_sub.current_period_start)
|
||||
period_end = unix_time(stripe_sub.current_period_end)
|
||||
|
||||
Billing::AssignPlan.call(
|
||||
team: team,
|
||||
plan_slug: plan.slug,
|
||||
status: status,
|
||||
stripe_attrs: {
|
||||
stripe_customer_id: stripe_sub.customer,
|
||||
stripe_subscription_id: stripe_sub.id,
|
||||
current_period_start: period_start,
|
||||
current_period_end: period_end,
|
||||
cancel_at_period_end: stripe_sub.cancel_at_period_end
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def downgrade_to_free(stripe_sub)
|
||||
team = Team.joins(:subscription)
|
||||
.find_by(subscriptions: { stripe_subscription_id: stripe_sub.id })
|
||||
return unless team
|
||||
|
||||
Billing::AssignPlan.call(
|
||||
team: team,
|
||||
plan_slug: "free",
|
||||
status: "active",
|
||||
stripe_attrs: {
|
||||
stripe_subscription_id: nil,
|
||||
current_period_start: nil,
|
||||
current_period_end: nil,
|
||||
cancel_at_period_end: false
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def mark_past_due(invoice)
|
||||
return if invoice.subscription.blank?
|
||||
|
||||
team = Team.joins(:subscription)
|
||||
.find_by(subscriptions: { stripe_subscription_id: invoice.subscription })
|
||||
return unless team&.subscription
|
||||
|
||||
team.subscription.update!(status: "past_due")
|
||||
end
|
||||
|
||||
def unix_time(value)
|
||||
return nil if value.blank?
|
||||
|
||||
Time.zone.at(value.to_i)
|
||||
end
|
||||
|
||||
def metadata_plan_slug(obj)
|
||||
meta = obj.metadata
|
||||
return nil if meta.blank?
|
||||
|
||||
meta["plan_slug"] || meta[:plan_slug] || (meta.respond_to?(:plan_slug) ? meta.plan_slug : nil)
|
||||
end
|
||||
|
||||
def metadata_team_id(obj)
|
||||
meta = obj.metadata
|
||||
return nil if meta.blank?
|
||||
|
||||
meta["team_id"] || meta[:team_id] || (meta.respond_to?(:team_id) ? meta.team_id : nil)
|
||||
end
|
||||
|
||||
def map_status(stripe_status)
|
||||
case stripe_status
|
||||
when "trialing" then "trialing"
|
||||
when "active" then "active"
|
||||
when "past_due", "unpaid" then "past_due"
|
||||
when "canceled", "incomplete_expired" then "canceled"
|
||||
else "incomplete"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user