Class: Gold::Admin::BillingController

Inherits:
Gold::ApplicationController 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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/gold/admin/billing_controller.rb', line 115

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



80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/gold/admin/billing_controller.rb', line 80

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



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
61
62
63
64
# File 'app/controllers/gold/admin/billing_controller.rb', line 30

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



68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/gold/admin/billing_controller.rb', line 68

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/gold/admin/billing_controller.rb', line 131

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/gold/admin/billing_controller.rb', line 146

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



93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/gold/admin/billing_controller.rb', line 93

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



18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/gold/admin/billing_controller.rb', line 18

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



105
106
107
108
109
110
111
112
113
# File 'app/controllers/gold/admin/billing_controller.rb', line 105

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

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

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