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.



123
124
125
# File 'app/controllers/gold/billing_controller.rb', line 123

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.



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

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.



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

def missing_charge
end

#outstanding_chargeObject

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



67
68
69
70
71
72
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
# File 'app/controllers/gold/billing_controller.rb', line 67

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 main_app.root_url
  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.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/gold/billing_controller.rb', line 103

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

  case outcome
  when ActiveCharge
    ApplyTierOp.new(billing).call
    outer_redirect_to_main
  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.



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

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

#select_tierObject

Process a tier selection.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/gold/billing_controller.rb', line 48

def select_tier
  @tiers = Tier.visible
  @tier = Tier.find(params[:tier])
  outcome = SelectTierOp.new(billing, @tier).call
  case outcome
  when SameTier, TierApplied
    redirect_to main_app.root_url
  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.



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

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
# File 'app/controllers/gold/billing_controller.rb', line 42

def tier
  billing # Expose the @billing variable to the view
  @tiers = Tier.visible
end

#uninstalledObject

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



150
151
152
153
154
155
156
# File 'app/controllers/gold/billing_controller.rb', line 150

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