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>
103 lines
3.0 KiB
Ruby
103 lines
3.0 KiB
Ruby
module Billing
|
|
class ConfirmBankTransfer
|
|
class Error < StandardError; end
|
|
|
|
def self.call(order:, admin:, pdf: nil)
|
|
new(order: order, admin: admin, pdf: pdf).call
|
|
end
|
|
|
|
def initialize(order:, admin:, pdf: nil)
|
|
@order = order
|
|
@admin = admin
|
|
@pdf = pdf
|
|
end
|
|
|
|
def call
|
|
raise Error, "Bonifico già gestito" unless @order.awaiting_payment?
|
|
|
|
club = @order.club
|
|
payment = @order.billing_payment
|
|
raise Error, "Pagamento collegato mancante" if payment.blank?
|
|
|
|
ApplicationRecord.transaction do
|
|
cancel_existing_stripe!(club.subscription)
|
|
|
|
period_start, period_end = period_bounds(club.subscription)
|
|
AssignPlan.call(
|
|
club: club,
|
|
plan_slug: @order.plan_slug,
|
|
status: "active",
|
|
stripe_attrs: {
|
|
stripe_subscription_id: nil,
|
|
stripe_schedule_id: nil,
|
|
pending_plan_id: nil,
|
|
pending_billing_interval: nil,
|
|
billing_interval: @order.billing_interval,
|
|
current_period_start: period_start,
|
|
current_period_end: period_end,
|
|
cancel_at_period_end: true,
|
|
admin_comped: false,
|
|
admin_comped_reason: nil,
|
|
admin_comped_at: nil,
|
|
admin_comped_by_id: nil
|
|
}
|
|
)
|
|
|
|
payment.update!(status: "paid", paid_at: Time.current, provider: "bank_transfer")
|
|
@order.update!(
|
|
status: "paid",
|
|
confirmed_by_admin: @admin,
|
|
confirmed_at: Time.current
|
|
)
|
|
end
|
|
|
|
deliver_activation!(payment)
|
|
@order.reload
|
|
end
|
|
|
|
private
|
|
|
|
def period_bounds(subscription)
|
|
start_at = Time.current
|
|
if subscription&.premium? &&
|
|
!subscription.admin_comped? &&
|
|
subscription.plan.slug == @order.plan_slug &&
|
|
subscription.current_period_end.present? &&
|
|
subscription.current_period_end > Time.current
|
|
start_at = subscription.current_period_end
|
|
end
|
|
|
|
end_at = @order.billing_interval == "yearly" ? start_at.advance(years: 1) : start_at.advance(months: 1)
|
|
[start_at, end_at]
|
|
end
|
|
|
|
def cancel_existing_stripe!(subscription)
|
|
return if subscription.blank? || subscription.stripe_subscription_id.blank?
|
|
return unless MatchLiveTv.stripe_enabled?
|
|
|
|
::Stripe::Subscription.cancel(subscription.stripe_subscription_id)
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
Rails.logger.warn("[BankTransfer] stripe cancel club=#{subscription.club_id} #{e.message}")
|
|
end
|
|
|
|
def deliver_activation!(payment)
|
|
if pdf_present?
|
|
AttachPaymentInvoice.call(
|
|
payment: payment.reload,
|
|
pdf: @pdf,
|
|
mailer_action: :plan_activated_with_invoice
|
|
)
|
|
else
|
|
BankTransferMailer.with(order: @order.reload).plan_activated.deliver_now
|
|
end
|
|
end
|
|
|
|
def pdf_present?
|
|
return false if @pdf.blank?
|
|
return @pdf.present? unless @pdf.respond_to?(:tempfile)
|
|
|
|
@pdf.original_filename.present?
|
|
end
|
|
end
|
|
end
|