Class: Gold::AcceptOrDeclineChargeOp

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

Overview

The charge was accepted or denied by the merchant.

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, charge_id) ⇒ AcceptOrDeclineChargeOp

Returns a new instance of AcceptOrDeclineChargeOp.



12
13
14
15
16
17
18
19
20
# File 'app/operations/gold/accept_or_decline_charge_op.rb', line 12

def initialize(billing, charge_id)
  @billing = billing
  @charge_id = charge_id.to_i
  @charge_transition = @billing.state_machine.last_transition_to(
    :sudden_charge,
    :delayed_charge,
    :optional_charge
  )
end

Instance Method Details

#callObject



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

def call
  unless @charge_transition
    Gold.logger.warn("[#{@billing.id}] Cannot find any charge creation events")
    return CannotProcessCharge.new(:not_found)
  end

  expected_charge_id = @charge_transition.["charge_id"]

  if expected_charge_id != @charge_id
    Gold.logger.warn(
      "[#{@billing.id}] Expecting charge to be #{expected_charge_id.inspect} " \
      "but it was #{@charge_id.inspect}"
    )
    return CannotProcessCharge.new(:old_charge)
  end

  charge = find_charge

  if !charge
    CannotProcessCharge.new(:not_found)
  elsif charge.status == "active"
    Gold.logger.info("[#{@billing.id}] This charge has already been activated")
    @billing.transition_to!(accepted_state)
    @billing.transition_to!(activated_state)
    return ActiveCharge.new(@charge_id)
  elsif charge.status == "accepted"
    @billing.transition_to!(accepted_state)
    activate_charge(charge)
  elsif charge.status == "declined"
    case @charge_transition.to_state.to_sym
    when :sudden_charge
      @billing.transition_to!(:sudden_charge_declined)
    when :delayed_charge
      @billing.transition_to!(:delayed_charge_declined)
    when :optional_charge
      # Also see FindOutstandingCharge, as it does the same logic
      @billing.transition_to_or_stay_in!(:optional_charge_declined)
      @billing.transition_to!(:billing)
    end

    DeclinedCharge.new
  elsif charge.status == "expired"
    @billing.transition_to!(expired_state)
    ExpiredCharge.new
  else
    Gold.logger.warn("[#{@billing.id}] Charge failed '#{charge.status}'")
    CannotProcessCharge.new(:bad_state)
  end
rescue ActiveResource::ForbiddenAccess
  @billing.transition_to!(:marked_as_uninstalled)
  Outcomes::Uninstalled.new
end