Class: Gold::CheckChargeOp

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

Overview

Ensure that the charge is correct and active.

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 Retries

#request_with_retries

Constructor Details

#initialize(billing, test_charge = Gold.configuration.test_charge?) ⇒ CheckChargeOp

Returns a new instance of CheckChargeOp.



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

def initialize(billing, test_charge = Gold.configuration.test_charge?)
  @billing = billing
  @test_charge = test_charge
end

Instance Method Details

#callObject



16
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
# File 'app/operations/gold/check_charge_op.rb', line 16

def call
  @billing.transition_to(:check_charge) unless @billing.current_state == :check_charge

  charges = request_with_retries do
    ShopifyAPI::RecurringApplicationCharge.all || []
  end

  active_charge = charges.find { |charge| charge.status == "active" }
  frozen_charge = charges.find { |charge| charge.status == "frozen" }

  outcome = if active_charge && billing_matches_charge?(active_charge)
              @billing.transition_to!(:billing)
              ActiveCharge.new(active_charge.id)
            elsif frozen_charge && @billing.transition_to(:frozen)
              FrozenCharge.new
            elsif active_charge
              @billing.transition_to!(:charge_missing)
              MismatchCharge.new
            else
              @billing.transition_to!(:charge_missing)
              MissingCharge.new
            end

  Gold.configuration.on_check_charge&.call(@billing, active_charge)
  outcome
end