class Returnly::Refunder

Attributes

customer_return[RW]
line_items[RW]
order[RW]
product_refund_amount[RW]
refund_calculator[RW]
return_item_amount_calculator[RW]
return_item_restock_policy[RW]
shipping_refund_amount[RW]

Public Class Methods

new(order:, line_items:, product_refund_amount:, shipping_refund_amount:) click to toggle source
# 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.product_refund_amount = product_refund_amount
  self.shipping_refund_amount = shipping_refund_amount

  configure
end

Public Instance Methods

proceed!() click to toggle source
# File lib/returnly/refunder.rb, line 31
def proceed!
  return proceed_without_line_items if line_items.empty?
  return proceed_with_product_zero_amount if product_refund_amount.to_d.zero?
  process_return_items

  if customer_return.save!
    perform_reimbursement
    RefundPresenter.present_refund(self)
  end
end
process_return_items() click to toggle source
# File lib/returnly/refunder.rb, line 23
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.additional_tax_total = 0
    return_item.resellable           = return_item_restock_policy.should_return_item? return_item
  end
end
product_available_amount() click to toggle source
# File lib/returnly/refunder.rb, line 50
def product_available_amount
  total = order.total - (order.shipment_total + refund_calculator.shipping_tax + refunds)
  [total, product_refund_amount.to_d].min
end
refund_available_amount() click to toggle source
# File lib/returnly/refunder.rb, line 42
def refund_available_amount
  @available_amount ||= Money.from_amount(product_available_amount)
end
reimbursement() click to toggle source
# File lib/returnly/refunder.rb, line 46
def reimbursement
  @_reimbursement ||= Spree::Reimbursement.build_from_customer_return(customer_return)
end

Private Instance Methods

add_shipping_amount!() click to toggle source
# File lib/returnly/refunder.rb, line 57
def add_shipping_amount!
  ::ReimbursementShipping.new(reimbursement).update!(shipping_refund_amount.to_d)
end
configure() click to toggle source
# File lib/returnly/refunder.rb, line 65
def configure
  self.return_item_amount_calculator = return_item_amount_calculator_class.new(self)
  self.return_item_restock_policy = return_item_restock_policy_class.new(self)
  self.refund_calculator = refund_calculator_class.new(order, line_items)
end
each_return_item() { |return_item| ... } click to toggle source
# File lib/returnly/refunder.rb, line 97
def each_return_item
  return_items = refund_calculator.line_items_return_items.values.flatten
  self.customer_return = Returnly::Builders::CustomerReturn.build_by_return_items(return_items)

  return_items.each do |return_item|
    yield return_item
  end
end
perform_reimbursement() click to toggle source
# File lib/returnly/refunder.rb, line 82
def perform_reimbursement
  Returnly::Services::CreateReimbursement.new(reimbursement).perform!
  add_shipping_amount!
end
proceed_with_product_zero_amount() click to toggle source
# File lib/returnly/refunder.rb, line 77
def proceed_with_product_zero_amount
  Returnly::Services::MarkItemsAsReturned.new(order, line_items).perform!
  RefundPresenter.present_refund_with_zero_amount(self)
end
proceed_without_line_items() click to toggle source
# File lib/returnly/refunder.rb, line 71
def proceed_without_line_items
  self.customer_return = Returnly::Builders::CustomerReturn.build_by_stock_location(stock_location)
  reimburse_without_items!
  RefundPresenter.present_refund(self)
end
refunds() click to toggle source
# File lib/returnly/refunder.rb, line 61
def refunds
  order.refunds.sum(&:amount).to_d.round(2, :down)
end
reimburse_without_items!() click to toggle source
# File lib/returnly/refunder.rb, line 87
def reimburse_without_items!
  reimbursement_total = product_refund_amount.to_d + shipping_refund_amount.to_d
  @_reimbursement = Spree::Reimbursement.new(
    order: order,
    total: reimbursement_total.round(2, :down)
  )
  reimbursement.save!
  ::ReimbursementType::OriginalPaymentNoItems.reimburse(reimbursement, nil, false)
end
stock_location() click to toggle source
# File lib/returnly/refunder.rb, line 106
def stock_location
  order.shipments.last.stock_location
end