Sha256: d524ab119e8133f20948cf4095093ce752e64a74b4086ca184a83c8f1f366335

Contents?: true

Size: 1.73 KB

Versions: 22

Compression:

Stored size: 1.73 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 = 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

    # 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
    )

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

    # Return the new subscription.
    new_subscription
  end

end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
disco_app-0.9.4 app/services/disco_app/subscription_service.rb
disco_app-0.9.5 app/services/disco_app/subscription_service.rb
disco_app-0.9.6 app/services/disco_app/subscription_service.rb
disco_app-0.9.7 app/services/disco_app/subscription_service.rb
disco_app-0.9.8 app/services/disco_app/subscription_service.rb
disco_app-0.9.9 app/services/disco_app/subscription_service.rb
disco_app-0.9.10 app/services/disco_app/subscription_service.rb
disco_app-0.9.11 app/services/disco_app/subscription_service.rb
disco_app-0.10.0 app/services/disco_app/subscription_service.rb
disco_app-0.10.1 app/services/disco_app/subscription_service.rb
disco_app-0.10.2 app/services/disco_app/subscription_service.rb
disco_app-0.10.3 app/services/disco_app/subscription_service.rb
disco_app-0.10.4 app/services/disco_app/subscription_service.rb
disco_app-0.10.5 app/services/disco_app/subscription_service.rb
disco_app-0.11.0 app/services/disco_app/subscription_service.rb
disco_app-0.11.1 app/services/disco_app/subscription_service.rb
disco_app-0.12.0 app/services/disco_app/subscription_service.rb
disco_app-0.12.1 app/services/disco_app/subscription_service.rb
disco_app-0.12.5 app/services/disco_app/subscription_service.rb
disco_app-0.12.6 app/services/disco_app/subscription_service.rb