Class: Gold::SelectTierOp

Inherits:
Object
  • Object
show all
Includes:
Outcomes
Defined in:
app/operations/gold/select_tier_op.rb

Overview

Allows a merchant to select their initial tier or change their tier.

Constant Summary

Constants included from Outcomes

Outcomes::AcceptedCharge, Outcomes::AcceptedTerms, Outcomes::ActiveCharge, Outcomes::CannotApplyDiscount, Outcomes::CannotIssueCredit, Outcomes::CannotProcessCharge, Outcomes::CannotSelectTier, Outcomes::ChargeNeeded, Outcomes::ChargeNotNeeded, Outcomes::DeclinedCharge, Outcomes::DeclinedTerms, Outcomes::ExpiredCharge, Outcomes::FrozenCharge, Outcomes::MismatchCharge, Outcomes::MissingCharge, Outcomes::PendingCharge, Outcomes::SameDiscount, Outcomes::SameTier, Outcomes::TierApplied, Outcomes::TierNotFound, Outcomes::Uninstalled

Instance Method Summary collapse

Constructor Details

#initialize(billing, tier, enforce_locking = true) ⇒ SelectTierOp

Returns a new instance of SelectTierOp.



11
12
13
14
15
# File 'app/operations/gold/select_tier_op.rb', line 11

def initialize(billing, tier, enforce_locking = true)
  @billing = billing
  @tier = tier
  @enforce_locking = enforce_locking
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/operations/gold/select_tier_op.rb', line 17

def call
  @billing.transaction do
    return CannotSelectTier.new(:nil) unless @tier
    return CannotSelectTier.new(:locked) if @enforce_locking && @tier.locked?

    if (@billing.tier != @tier) && !@billing.qualifies_for_tier?(@tier)
      return CannotSelectTier.new(:disqualified)
    end

    # Mark the tier as selected
    if @billing.transition_to(:select_tier, tier_id: @tier.id)
    elsif @billing.transition_to(:change_tier, tier_id: @tier.id)
    else
      message = "Cannot transition from '#{@billing.current_state}' " \
                "to 'select_tier' or 'change_tier'"
      raise Statesman::TransitionFailedError, message
    end

    # Mark the beginning of the trial if it hasn't been set already
    @billing.update(trial_starts_at: Time.current) unless @billing.trial_starts_at
  end

  # Process the tier selection, creating charges if necessary
  if @billing.shopify_plan.affiliate? ||
     @billing.shopify_plan.staff? ||
     @tier.free?

    # If the shop doesn't need a charge, apply the tier
    return ApplyTierOp.new(@billing).call
  else
    # A valid charge is necessary before applying the new tier
    return ChargeNeeded.new
  end
end