Sha256: 292973af67097b1825862068cfbed57bafee0cdd3b106026904e1e1ca752b46c

Contents?: true

Size: 1.94 KB

Versions: 20

Compression:

Stored size: 1.94 KB

Contents

class DiscoApp::SubscriptionService

  # Subscribe the given shop to the given plan, optionally using the given plan
  # code and optionally tracking the subscription source.
  def self.subscribe(shop, plan, plan_code = nil, source_name = nil)

    # If a plan code was provided, fetch it for the given plan.
    plan_code_instance = nil
    if plan_code.present?
      plan_code_instance = DiscoApp::PlanCode.available.find_by(plan: plan, code: plan_code)
    end

    # If a source name has been provided, fetch or create it
    source_instance = nil
    if source_name.present?
      source_instance = DiscoApp::Source.find_or_create_by(source: source_name)
    end

    # Cancel any existing current subscriptions.
    shop.subscriptions.current.update_all(
      status: DiscoApp::Subscription.statuses[:cancelled],
      cancelled_at: Time.now
    )

    # Get the amount that should be charged for the subscription.
    subscription_amount = plan_code_instance.present? ? plan_code_instance.amount : plan.amount

    # Get the date the subscription trial should end.
    subscription_trial_period_days = plan_code_instance.present? ? plan_code_instance.trial_period_days : plan.trial_period_days

    # Create the new subscription.
    new_subscription = DiscoApp::Subscription.create!(
      shop: shop,
      plan: plan,
      plan_code: plan_code_instance,
      status: DiscoApp::Subscription.statuses[plan.has_trial? ? :trial : :active],
      subscription_type: plan.plan_type,
      amount: subscription_amount,
      trial_period_days: plan.has_trial? ? subscription_trial_period_days : nil,
      trial_start_at: plan.has_trial? ? Time.now : nil,
      trial_end_at: plan.has_trial? ? subscription_trial_period_days.days.from_now : nil,
      source: source_instance
    )

    # Enqueue the subscription changed background job.
    DiscoApp::SubscriptionChangedJob.perform_later(shop, new_subscription)

    # Return the new subscription.
    new_subscription
  end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
disco_app-0.16.1.pre.sidekiq.pre.6.pre.release app/services/disco_app/subscription_service.rb
disco_app-0.13.0 app/services/disco_app/subscription_service.rb
disco_app-0.13.1 app/services/disco_app/subscription_service.rb
disco_app-0.13.2 app/services/disco_app/subscription_service.rb
disco_app-0.13.3 app/services/disco_app/subscription_service.rb
disco_app-0.13.4 app/services/disco_app/subscription_service.rb
disco_app-0.13.5 app/services/disco_app/subscription_service.rb
disco_app-0.13.6 app/services/disco_app/subscription_service.rb
disco_app-0.13.7 app/services/disco_app/subscription_service.rb
disco_app-0.13.8 app/services/disco_app/subscription_service.rb
disco_app-0.14.1 app/services/disco_app/subscription_service.rb
disco_app-0.14.2 app/services/disco_app/subscription_service.rb
disco_app-0.15.0 app/services/disco_app/subscription_service.rb
disco_app-0.16.0 app/services/disco_app/subscription_service.rb
disco_app-0.14.4 app/services/disco_app/subscription_service.rb
disco_app-0.14.3 app/services/disco_app/subscription_service.rb
disco_app-0.16.1 app/services/disco_app/subscription_service.rb
disco_app-0.15.2 app/services/disco_app/subscription_service.rb
disco_app-0.14.0 app/services/disco_app/subscription_service.rb
disco_app-0.13.6.pre.puma.pre.3 app/services/disco_app/subscription_service.rb