Class: Gold::BillingController

Inherits:
AuthenticatedController show all
Includes:
Concerns::MerchantFacing, Outcomes
Defined in:
app/controllers/gold/billing_controller.rb

Overview

Handles all merchant billing interactions.

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

Methods included from Concerns::MerchantFacing

#billing, #confront_mandatory_billing_action, included

Instance Method Details

#declined_chargeObject

Show the merchant that the charge has expired and direct them to try again.



129
130
131
# File 'app/controllers/gold/billing_controller.rb', line 129

def declined_charge
  ensure_billing_is :sudden_charge_declined, :delayed_charge_declined
end

#expired_chargeObject

Show the merchant that the charge has expired and direct them to try again.



135
136
137
# File 'app/controllers/gold/billing_controller.rb', line 135

def expired_charge
  ensure_billing_is :sudden_charge_expired, :delayed_charge_expired
end

#missing_chargeObject

Explain to the merchant that their charge is missing and direct them to try again.



141
142
# File 'app/controllers/gold/billing_controller.rb', line 141

def missing_charge
end

#outstanding_chargeObject

Show the merchant that they have an outstanding charge and let them approve/decline it.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/gold/billing_controller.rb', line 73

def outstanding_charge
  outcome = ResolveOutstandingChargeOp.new(billing).call

  case outcome
  when PendingCharge
    Gold.logger.info("[#{billing.id}] Charge is pending")
    @confirmation_url = outcome.confirmation_url
    # Will render view with confirmation URL
  when AcceptedCharge
    Gold.logger.info("[#{billing.id}] Charge is accepted")
    redirect_to outcome.return_url
  when MissingCharge
    Gold.logger.info("[#{billing.id}] Charge is missing")
    redirect_to missing_charge_url
  when ExpiredCharge
    Gold.logger.info("[#{billing.id}] Charge is expired")
    redirect_to expired_charge_url
  when ActiveCharge
    unless billing.current_state == :billing
      AcceptOrDeclineChargeOp.new(billing, outcome.charge_id).call

      unless ApplyTierOp.new(billing).call.ok?
        raise "Charge was #{accepted_outcome}, but should have been active"
      end

      Gold.logger.info("[#{billing.id}] Charge is ready")
    end

    redirect_to Gold.configuration.after_tier_redirect_path
  else
    raise "Not sure how to handle #{outcome} on outstanding charge"
  end
end

#process_chargeObject

Process a charge confirmation when a merchant is redirected back from Shopify.



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

def process_charge
  outcome = AcceptOrDeclineChargeOp.new(billing, params[:charge_id]).call

  case outcome
  when ActiveCharge
    ApplyTierOp.new(billing).call
    outer_redirect(Gold.configuration.after_tier_redirect_path)
  when DeclinedCharge
    redirect_to declined_charge_url
  when ExpiredCharge
    redirect_to expired_charge_url
  when Uninstalled
    redirect_to uninstalled_url
  when CannotProcessCharge
    redirect_to missing_charge_url
  end
end

#process_termsObject

Handle acceptance or declination of the terms of service.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/gold/billing_controller.rb', line 27

def process_terms
  accepted = params[:accept].present?
  outcome = AcceptOrDeclineTermsOp.new(billing, accepted).call
  case outcome
  when AcceptedTerms
    params.permit!
    Gold.configuration.on_terms&.call(billing, params.to_h)
    redirect_to select_tier_url
  else
    @error = true
    render :terms
  end
end

#retry_chargeObject

Allow the merchant to retry the charge when necessary.



145
146
147
# File 'app/controllers/gold/billing_controller.rb', line 145

def retry_charge
  handle_charge_outcome ChargeOp.new(billing, process_charge_url).call
end

#select_tierObject

Process a tier selection.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/gold/billing_controller.rb', line 53

def select_tier
  @tiers = Tier.visible
  @tier =  Tier.find(params[:tier])
  outcome = SelectTierOp.new(billing, @tier).call
  case outcome
  when SameTier, TierApplied
    cookies.delete(:gold_tier_id)
    redirect_to Gold.configuration.after_tier_redirect_path
  when CannotSelectTier
    flash.now[:error] = "Your shop is not eligible for this plan"
    render :tier
  when ChargeNeeded
    handle_charge_outcome ChargeOp.new(billing, process_charge_url).call
  else
    raise "Not sure how to handle #{outcome} on tier selection"
  end
end

#suspendedObject

Show the merchant a suspension page if they have been suspended.



150
151
152
# File 'app/controllers/gold/billing_controller.rb', line 150

def suspended
  ensure_billing_is :marked_as_suspended, :suspended
end

#termsObject

Show the terms of service.



22
23
24
# File 'app/controllers/gold/billing_controller.rb', line 22

def terms
  @already_accepted = !billing.can_transition_to?(:accepted_terms)
end

#tierObject

Show the possible tiers to select from.



42
43
44
45
46
47
48
49
50
# File 'app/controllers/gold/billing_controller.rb', line 42

def tier
  billing # Expose the @billing variable to the view

  apply_referral_cookie unless billing.tier

  params[:id] = cookies[:gold_tier_id] if cookies[:gold_tier_id].present?

  @tiers = Tier.visible
end

#uninstalledObject

Show the merchant a message about them uninstalling the app if they are still logged in.



156
157
158
159
160
161
162
# File 'app/controllers/gold/billing_controller.rb', line 156

def uninstalled
  ensure_billing_is :marked_as_uninstalled,
                    :uninstalled,
                    :frozen,
                    :cleanup,
                    :done
end