Class: Gold::Admin::BillingController

Inherits:
AdminController show all
Defined in:
app/controllers/gold/admin/billing_controller.rb

Overview

Main controller for Gold to be used in app

Instance Method Summary collapse

Instance Method Details

#apply_discountObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/gold/admin/billing_controller.rb', line 110

def apply_discount
  outcome = ApplyDiscountOp.new(billing,
                                params[:percentage].to_i,
                                process_charge_url).call

  if outcome.is_a?(Outcomes::PendingCharge)
    flash[:warning] = "Discount applied, merchant must reauthorize the charge"
  elsif outcome.ok?
    flash[:success] = "Discount updated, will be used for future charges"
  else
    flash[:danger] = "Failed to apply discount because '#{outcome.message}'"
  end

  go_back
end

#cancel_chargeObject



76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/gold/admin/billing_controller.rb', line 76

def cancel_charge
  begin
    charge = ShopifyAPI::RecurringApplicationCharge.find(params[:charge_id])
    charge.cancel
    flash["success"] = "Cancelled charge"
  rescue ActiveResource::ResourceNotFound
    flash["warning"] = "That charge was not found for '#{ShopifyAPI::Base.site}'"
  end

  go_back
end

#change_tierObject

rubocop:disable Metrics/LineLength:Length



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
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/gold/admin/billing_controller.rb', line 26

def change_tier
  tier = Tier.find(params[:tier])

  outcome = SelectTierOp.new(billing, tier).call

  if outcome.is_a?(Outcomes::TierApplied)
    flash[:success] = "Changed billing to '#{tier.name}'"
  elsif outcome.ok?
    charge_outcome = ChargeOp.new(billing, process_charge_url).call

    if charge_outcome.is_a?(Outcomes::ActiveCharge)
      accept_or_decline_outcome = AcceptOrDeclineChargeOp.new(billing, charge_outcome.charge_id).call

      if accept_or_decline_outcome.ok?
        apply_tier_outcome = ApplyTierOp.new(billing).call

        if apply_tier_outcome.ok? # rubocop:disable Metrics/BlockNesting
          flash[:success] = "Changed billing to '#{tier.name}', no merchant action required"
        else
          flash[:danger] = "Failed to apply tier because '#{apply_tier_outcome.reason}'"
        end
      else
        flash[:danger] = "Failed to accept or decline charge because '#{accept_or_decline_outcome.reason}'"
      end
    elsif charge_outcome.is_a?(Outcomes::ChargeNotNeeded)
      flash[:success] = "Applied free tier '#{tier.name}', no merchant action required. You may need to cancel their current charge."
    else
      flash[:warning] = "Changing tier, but merchant action is required before tier is applied"
    end
  else
    flash[:danger] = "Failed to apply tier because '#{outcome.reason}'"
  end

  go_back
end

#issue_creditObject

Issue a credit to a shop



64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/gold/admin/billing_controller.rb', line 64

def issue_credit
  outcome = IssueCreditOp.new(billing, params[:amount], params[:reason]).call

  if outcome.ok?
    flash["success"] = "Issued credit"
  else
    flash["danger"] = "Failed to issue credit because '#{outcome.message}'"
  end

  go_back
end

#override_shopify_planObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/gold/admin/billing_controller.rb', line 126

def override_shopify_plan
  plan = params[:plan].presence
  if billing.update(shopify_plan_override: plan)
    flash[:success] = if plan
                        "Changed Shopify plan to '#{plan}'"
                      else
                        "Removed Shopify plan override"
                      end
  else
    flash[:danger] = "Failed to save shop"
  end

  go_back
end

#reset_trial_daysObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/gold/admin/billing_controller.rb', line 141

def reset_trial_days
  case params[:trial_action]
  when "reset"
    if billing.update(trial_starts_at: Time.current)
      flash[:success] = "New trial starts... now! You might need to" \
                        "cancel their charge first."
    else
      flash[:danger] = "Failed to reset trial days"
    end
  when "clear"
    if billing.update(trial_starts_at: nil)
      flash[:success] = "Trial started over. The merchant will be faced" \
                        "with a new charge upon login."
    else
      flash[:danger] = "Failed to clear trial days"
    end
  else
    flash[:danger] = "Don't know what to do with" \
                    " '#{params[:trial_action]}' trial action"
  end

  go_back
end

#suspendObject

Suspend a shop



89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/gold/admin/billing_controller.rb', line 89

def suspend
  if params[:confirm_suspend].blank?
    flash[:danger] = "Please confirm you want to suspend this account"
    go_back && return
  end

  SuspendOp.new(billing).call
  flash[:success] = "Account suspended"
  go_back
end

#transitionObject



14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/gold/admin/billing_controller.rb', line 14

def transition
  transitions = [params[:to]].flatten

  transitions.each do |t|
    billing.transition_to!(t, )
  end

  flash[:success] = "Transitioned to '#{transitions.join(',')}'"
  go_back
end

#unsuspendObject

Unsuspend a shop



101
102
103
104
105
106
107
108
# File 'app/controllers/gold/admin/billing_controller.rb', line 101

def unsuspend
  outcome = UnsuspendOp.new(billing).call

  Gold::ChargeOp.new(billing, process_charge_url).call unless outcome.ok?

  flash[:success] = "Account unsuspended, charge is '#{outcome}'"
  go_back
end