Class: Gold::ResolveOutstandingChargeOp

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

Overview

Look up billing information and see if they have a charge or not based on the charges we’ve previously created

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) ⇒ ResolveOutstandingChargeOp

Returns a new instance of ResolveOutstandingChargeOp.



8
9
10
# File 'app/operations/gold/resolve_outstanding_charge_op.rb', line 8

def initialize(billing)
  @billing = billing
end

Instance Method Details

#callObject



12
13
14
15
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/operations/gold/resolve_outstanding_charge_op.rb', line 12

def call
  return MissingCharge.new(:no_charge_state) unless charge_state_transition

  charge_id = charge_state_transition.["charge_id"]

  begin
    charge = request_with_retries do
      ShopifyAPI::RecurringApplicationCharge.find(charge_id)
    end
  rescue ActiveResource::ResourceNotFound
    return MissingCharge.new(:shopify_missing)
  end

  case charge.status
  when "active", "frozen"
    return ActiveCharge.new(charge_id)
  when "accepted"
    return AcceptedCharge.new(charge.decorated_return_url)
  when "pending"
    return PendingCharge.new(charge.confirmation_url)
  when "expired"
    if sudden_charge?
      @billing.transition_to!(:sudden_charge_expired)
      return ExpiredCharge.new
    elsif delayed_charge?
      @billing.transition_to!(:delayed_charge_expired)
      return ExpiredCharge.new
    elsif optional_charge?
      # It is OK for an optional charge to expire. Put the merchant back in
      # a normal billing state and let them retry their charge later, if
      # desired.
      @billing.transition_to!(:optional_charge_declined)
      @billing.transition_to!(:billing)
      return ActiveCharge.new(charge_id)
    end
    @billing.transition_to!(expired_state)
    return ExpiredCharge.new
  when "declined"
    if sudden_charge?
      @billing.transition_to!(:sudden_charge_declined)
      return MissingCharge.new(:charge_declined)
    elsif delayed_charge?
      @billing.transition_to!(:delayed_charge_declined)
      return MissingCharge.new(:charge_declined)
    elsif optional_charge?
      @billing.transition_to_or_stay_in!(:optional_charge_declined)
      @billing.transition_to!(:billing)
      return ActiveCharge.new(charge_id)
    end
  when "cancelled"
    return MissingCharge.new(:charge_cancelled)
  else
    raise MissingCharge.new(:bad_status, "Bad status: '#{charge.status}'")
  end
end