Class: Returnly::Refunder

Inherits:
Object
  • Object
show all
Includes:
RefundsConfiguration
Defined in:
lib/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:, product_refund_amount:, shipping_refund_amount:) ⇒ Refunder

Returns a new instance of Refunder.



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

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

  configure
end

Instance Attribute Details

#customer_returnObject

Returns the value of attribute customer_return.



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

def customer_return
  @customer_return
end

#line_itemsObject

Returns the value of attribute line_items.



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

def line_items
  @line_items
end

#orderObject

Returns the value of attribute order.



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

def order
  @order
end

#product_refund_amountObject

Returns the value of attribute product_refund_amount.



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

def product_refund_amount
  @product_refund_amount
end

#refund_calculatorObject

Returns the value of attribute refund_calculator.



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

def refund_calculator
  @refund_calculator
end

#return_item_amount_calculatorObject

Returns the value of attribute return_item_amount_calculator.



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

def return_item_amount_calculator
  @return_item_amount_calculator
end

#return_item_restock_policyObject

Returns the value of attribute return_item_restock_policy.



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

def return_item_restock_policy
  @return_item_restock_policy
end

#shipping_refund_amountObject

Returns the value of attribute shipping_refund_amount.



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

def shipping_refund_amount
  @shipping_refund_amount
end

Instance Method Details

#proceed!Object



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

def proceed!
  return proceed_without_line_items if line_items.empty?
  process_return_items

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

#process_return_itemsObject



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

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_itemObject



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

def refund_amount_per_item
  refund_available_amount / line_items.size
end

#refund_available_amountObject



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

def refund_available_amount
  @available_amount ||= Money.from_amount(product_refund_amount.to_f + shipping_refund_amount.to_f)
end

#reimbursementObject



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

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