Chatwoot/app/services/subscription/chargebee_service.rb

71 lines
2.1 KiB
Ruby
Raw Normal View History

class Subscription::ChargebeeService
class << self
def create_subscription(account)
2019-10-20 08:47:26 +00:00
result = ChargeBee::Subscription.create(
plan_id: Plan.paid_plan.id,
customer: {
email: account.users.administrator.try(:first).try(:email),
first_name: account.name,
company: account.name
}
)
subscription = account.subscription
subscription.stripe_customer_id = result.subscription.customer_id
subscription.save
rescue StandardError => e
Raven.capture_exception(e)
end
def update_subscription(account)
2019-10-20 08:47:26 +00:00
subscription = account.subscription
agents_count = account.users.count
ChargeBee::Subscription.update(subscription.stripe_customer_id, plan_quantity: agents_count)
rescue StandardError => e
Raven.capture_exception(e)
end
def cancel_subscription(account)
2019-10-20 08:47:26 +00:00
subscription = account.subscription
ChargeBee::Subscription.delete(subscription.stripe_customer_id)
rescue StandardError => e
Raven.capture_exception(e)
end
def reactivate_subscription(account)
2019-10-20 08:47:26 +00:00
subscription = account.subscription
ChargeBee::Subscription.reactivate(subscription.stripe_customer_id)
subscription.active!
rescue StandardError => e
Raven.capture_exception(e)
end
def deactivate_subscription(account)
2019-10-20 08:47:26 +00:00
subscription = account.subscription
ChargeBee::Subscription.cancel(subscription.stripe_customer_id)
subscription.cancelled!
rescue StandardError => e
Raven.capture_exception(e)
end
def hosted_page_url(account)
2019-10-20 08:47:26 +00:00
subscription = account.subscription
2019-10-20 08:47:26 +00:00
# result = ChargeBee::HostedPage.checkout_existing({
# :subscription => {
# :id => subscription.stripe_customer_id,
# :plan_id => Plan.paid_plan.id
# }
# })
2019-10-20 08:47:26 +00:00
result = ChargeBee::HostedPage.update_payment_method(
customer: {
id: subscription.stripe_customer_id
}
)
result.hosted_page.url
rescue StandardError => e
Raven.capture_exception(e)
end
end
end