Class: Refund

Inherits:
Object
  • Object
show all
Includes:
Adjustments
Defined in:
app/models/refund.rb

Constant Summary collapse

BRAINTREE_UNSETTLED_MESSAGE =
"Cannot refund a transaction unless it is settled. (91506)"
FRIENDLY_UNSETTLED_MESSAGE =
"The processor cannot refund that transaction yet. Please try again in a few hours."

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Adjustments

#number_of_non_free_items, #service_fee_per_item

Constructor Details

#initialize(order, items) ⇒ Refund

Returns a new instance of Refund.



8
9
10
11
# File 'app/models/refund.rb', line 8

def initialize(order, items)
  self.order = order
  self.items = items
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'app/models/refund.rb', line 3

def items
  @items
end

#messageObject

Returns the value of attribute message.



3
4
5
# File 'app/models/refund.rb', line 3

def message
  @message
end

#orderObject

Returns the value of attribute order.



3
4
5
# File 'app/models/refund.rb', line 3

def order
  @order
end

#refund_orderObject

Returns the value of attribute refund_order.



3
4
5
# File 'app/models/refund.rb', line 3

def refund_order
  @refund_order
end

Instance Method Details

#format_message(payment) ⇒ Object

This is brittle, sure, but active merchant doens’t pass along any processor codes so we have to match the whole stupid string



39
40
41
42
43
# File 'app/models/refund.rb', line 39

def format_message(payment)
  unless payment.errors.empty?
    (payment.errors[:base].first.eql? BRAINTREE_UNSETTLED_MESSAGE) ? FRIENDLY_UNSETTLED_MESSAGE : payment.errors.full_messages.to_sentence
  end
end

#refund_amountObject

The gross amount of the refund. This is the total amount of money we are returning to the patron



48
49
50
# File 'app/models/refund.rb', line 48

def refund_amount
  item_total + service_fee
end

#service_feeObject



52
53
54
# File 'app/models/refund.rb', line 52

def service_fee
  (number_of_non_free_items(items) * service_fee_per_item(order.items))
end

#submit(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/refund.rb', line 13

def submit(options = {})
  return_items_to_inventory = options[:and_return] || false

  items.each do |i|
    unless i.refundable?
      @message = "Those items have already been refunded."
      return
    end
  end

  @payment = Payment.create(@order.payment_method)
  @success = @payment.refund(refund_amount, order.transaction_id, options.merge({:service_fee => service_fee}))
  @message = format_message(@payment)
  
  if @success
    items.each { |i| i.return!(return_items_to_inventory) }
    items.each(&:refund!)
    create_refund_order(@payment.transaction_id)
  end
end

#successful?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/refund.rb', line 34

def successful?
  @success || false
end