Class: Gold::ApplyDiscountOp

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

Overview

Applies a discount to a shop and creates a new charge for them.

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

Constructor Details

#initialize(billing, percentage, return_url, test = false) ⇒ ApplyDiscountOp

Returns a new instance of ApplyDiscountOp.



11
12
13
14
15
16
# File 'app/operations/gold/apply_discount_op.rb', line 11

def initialize(billing, percentage, return_url, test = false)
  @billing = billing
  @percentage = percentage.to_i
  @return_url = return_url
  @test = test
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/operations/gold/apply_discount_op.rb', line 18

def call
  # Validate the discount
  if @percentage < 0 || @percentage >= 100
    return CannotApplyDiscount.new(:invalid_percentage)
  end

  return SameDiscount.new if @percentage == @billing.discount_percentage

  @billing.update(discount_percentage: @percentage)

  # If we can transition to apply discount, let's do that. This means we're
  # in a billing state and a new charge will be created. Otherwise, it's
  # OK to just set the `discount_percentage` field for future charges
  if @billing.can_transition_to?(:apply_discount)
    @billing.transition_to!(:apply_discount, percentage: @percentage)
    ChargeOp.new(@billing, @return_url, @test).call
  else
    Success.new
  end
end