Class: Gold::IssueCreditOp

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

Overview

Provide a credit to a merchant.

Constant Summary collapse

MAX_ATTEMPTS =
3

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, amount, reason, test = false) ⇒ IssueCreditOp

Returns a new instance of IssueCreditOp.



15
16
17
18
19
20
# File 'app/operations/gold/issue_credit_op.rb', line 15

def initialize(billing, amount, reason, test = false)
  @billing = billing
  @amount = amount.to_d
  @reason = reason
  @test = test
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
# File 'app/operations/gold/issue_credit_op.rb', line 22

def call
  return CannotIssueCredit.new(:amount_not_positive) if @amount <= 0
  return CannotIssueCredit.new(:missing_reason) if @reason.blank?
  return CannotIssueCredit.new(:invalid_tier) if @billing.last_selected_tier&.free?

  @billing.transition_to!(:issue_credit, amount: @amount, reason: @reason)

  # Now create the credit on Shopify's end
  credit = new_credit

  request_with_retries do
    credit.save
  end

  # Mark the credit as applied
  @billing.transition_to!(:billing)

  if credit.valid?
    Success.new
  else
    CannotIssueCredit.new(:rejected_by_shopify,
                          credit.errors.full_messages.join(", "))
  end
end

#new_creditObject



47
48
49
50
51
52
53
# File 'app/operations/gold/issue_credit_op.rb', line 47

def new_credit
  ShopifyAPI::ApplicationCredit.new(
    description: @reason,
    amount: @amount,
    test: @test
  )
end