Class: Solidus::Returnly::Refunder

Inherits:
Object
  • Object
show all
Includes:
RefundsConfiguration
Defined in:
lib/solidus/returnly/refunder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RefundsConfiguration

#refund_calculator_class, #refunder_class, #return_item_amount_calculator_class, #return_item_restock_policy_class

Constructor Details

#initialize(order:, line_items:, restock:, transactions:) ⇒ Refunder

Returns a new instance of Refunder.



15
16
17
18
19
20
21
22
23
# File 'lib/solidus/returnly/refunder.rb', line 15

def initialize(order:, line_items:, restock:, transactions:)
  self.order = order
  self.line_items = line_items
  self.customer_return = Spree::CustomerReturn.new customer_return_params
  self.transactions = transactions
  self.restock = restock

  configure
end

Instance Attribute Details

#customer_return ⇒ Object

Returns the value of attribute customer_return.



6
7
8
# File 'lib/solidus/returnly/refunder.rb', line 6

def customer_return
  @customer_return
end

#line_items ⇒ Object

Returns the value of attribute line_items.



6
7
8
# File 'lib/solidus/returnly/refunder.rb', line 6

def line_items
  @line_items
end

#order ⇒ Object

Returns the value of attribute order.



6
7
8
# File 'lib/solidus/returnly/refunder.rb', line 6

def order
  @order
end

#refund_calculator ⇒ Object

Returns the value of attribute refund_calculator.



6
7
8
# File 'lib/solidus/returnly/refunder.rb', line 6

def refund_calculator
  @refund_calculator
end

#restock ⇒ Object

Returns the value of attribute restock.



6
7
8
# File 'lib/solidus/returnly/refunder.rb', line 6

def restock
  @restock
end

#return_item_amount_calculator ⇒ Object

Returns the value of attribute return_item_amount_calculator.



6
7
8
# File 'lib/solidus/returnly/refunder.rb', line 6

def return_item_amount_calculator
  @return_item_amount_calculator
end

#return_item_restock_policy ⇒ Object

Returns the value of attribute return_item_restock_policy.



6
7
8
# File 'lib/solidus/returnly/refunder.rb', line 6

def return_item_restock_policy
  @return_item_restock_policy
end

#transactions ⇒ Object

Returns the value of attribute transactions.



6
7
8
# File 'lib/solidus/returnly/refunder.rb', line 6

def transactions
  @transactions
end

Instance Method Details

#proceed! ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/solidus/returnly/refunder.rb', line 32

def proceed!
  process_return_items

  if customer_return.save!
    perform_reimbursement
    RefundPresenter.present_refund(self)
  end
end

#process_return_items ⇒ Object



25
26
27
28
29
30
# File 'lib/solidus/returnly/refunder.rb', line 25

def process_return_items
  each_return_item do |return_item|
    return_item.amount = return_item_amount_calculator.return_item_refund_amount return_item
    return_item.resellable = return_item_restock_policy.should_return_item? return_item
  end
end

#refund_amount_per_item ⇒ Object



45
46
47
# File 'lib/solidus/returnly/refunder.rb', line 45

def refund_amount_per_item
  refund_available_amount / line_items.size
end

#refund_available_amount ⇒ Object



41
42
43
# File 'lib/solidus/returnly/refunder.rb', line 41

def refund_available_amount
  @available_amount ||= transactions.sum { |t| Money.from_amount(t[:amount].to_f) }
end

#reimbursement ⇒ Object



49
50
51
# File 'lib/solidus/returnly/refunder.rb', line 49

def reimbursement
  @_reimbursement ||= Spree::Reimbursement.build_from_customer_return(customer_return)
end