Evita errore «No such customer» dopo passaggio da sk_test a sk_live. Co-authored-by: Cursor <cursoragent@cursor.com>
76 lines
2.2 KiB
Ruby
76 lines
2.2 KiB
Ruby
module Billing
|
|
module Stripe
|
|
class CheckoutSession
|
|
VALID_PLANS = %w[premium_light premium_full].freeze
|
|
|
|
def initialize(club:, user:, plan_slug: "premium_light", interval: PriceCatalog::DEFAULT_INTERVAL)
|
|
@club = club
|
|
@user = user
|
|
@plan_slug = plan_slug.to_s
|
|
@interval = PriceCatalog.normalize_interval(interval)
|
|
end
|
|
|
|
def url
|
|
raise "Stripe non configurato" unless MatchLiveTv.stripe_enabled?
|
|
raise "Piano non valido" unless VALID_PLANS.include?(@plan_slug)
|
|
|
|
price_id = PriceCatalog.price_id(plan_slug: @plan_slug, interval: @interval)
|
|
|
|
customer_id = EnsureCustomer.call(club: @club, user: @user)
|
|
CustomerSync.call(club: @club, customer_id: 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: {
|
|
club_id: @club.id,
|
|
user_id: @user.id,
|
|
plan_slug: @plan_slug,
|
|
billing_interval: @interval
|
|
},
|
|
subscription_data: {
|
|
metadata: {
|
|
club_id: @club.id,
|
|
plan_slug: @plan_slug,
|
|
billing_interval: @interval
|
|
}
|
|
},
|
|
billing_address_collection: "required",
|
|
customer_update: { address: "auto", name: "auto" },
|
|
allow_promotion_codes: true
|
|
)
|
|
session.url
|
|
end
|
|
|
|
def portal_url
|
|
raise "Stripe non configurato" unless MatchLiveTv.stripe_enabled?
|
|
|
|
customer_id = EnsureCustomer.call(club: @club, user: @user)
|
|
CustomerSync.call(club: @club, customer_id: customer_id)
|
|
session = ::Stripe::BillingPortal::Session.create(
|
|
customer: customer_id,
|
|
return_url: billing_url
|
|
)
|
|
session.url
|
|
end
|
|
|
|
private
|
|
|
|
def success_url
|
|
"#{billing_url}?checkout=success&session_id={CHECKOUT_SESSION_ID}"
|
|
end
|
|
|
|
def cancel_url
|
|
"#{billing_url}?checkout=canceled"
|
|
end
|
|
|
|
def billing_url
|
|
"#{MatchLiveTv.app_public_url.chomp('/')}/clubs/#{@club.id}/billing"
|
|
end
|
|
end
|
|
end
|
|
end
|