Il piano si attiva solo dopo la conferma admin; Stripe resta a listino se non c'è un prezzo commerciale. Co-authored-by: Cursor <cursoragent@cursor.com>
43 lines
1.2 KiB
Ruby
43 lines
1.2 KiB
Ruby
# Passa a Free le società con disdetta programmata e periodo scaduto (anche senza visita pagina billing).
|
|
class ExpireEndedSubscriptionsJob
|
|
include Sidekiq::Job
|
|
|
|
def perform
|
|
free_plan = Plan["free"]
|
|
|
|
Subscription.where(cancel_at_period_end: true)
|
|
.where("current_period_end <= ?", Time.current)
|
|
.where.not(plan_id: free_plan.id)
|
|
.where(admin_comped: false)
|
|
.find_each do |sub|
|
|
expire!(sub)
|
|
rescue StandardError => e
|
|
Rails.logger.warn("[ExpireEndedSubscriptions] club=#{sub.club_id} #{e.message}")
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def expire!(sub)
|
|
if sub.stripe_subscription_id.present?
|
|
Billing::Stripe::FinalizeSubscription.call(club: sub.club)
|
|
else
|
|
Billing::AssignPlan.call(
|
|
club: sub.club,
|
|
plan_slug: "free",
|
|
status: "active",
|
|
stripe_attrs: {
|
|
stripe_subscription_id: nil,
|
|
stripe_schedule_id: nil,
|
|
current_period_start: nil,
|
|
current_period_end: nil,
|
|
cancel_at_period_end: false,
|
|
billing_interval: nil,
|
|
pending_plan_id: nil,
|
|
pending_billing_interval: nil
|
|
}
|
|
)
|
|
end
|
|
end
|
|
end
|