Class: Refund

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

Constant Summary collapse

BRAINTREE_UNSETTLED_MESSAGE =
"Cannot refund a transaction unless it is settled. (91506)"
FRIENDLY_UNSETTLED_MESSAGE =
"Unfortunately we cannot refund credit card transactions until the day after they were processed. Please re-issue the refund tomorrow."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, items) ⇒ Refund

Returns a new instance of Refund.



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

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

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#orderObject

Returns the value of attribute order.



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

def order
  @order
end

#refund_orderObject

Returns the value of attribute refund_order.



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

def refund_order
  @refund_order
end

#send_email_confirmationObject

Returns the value of attribute send_email_confirmation.



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

def send_email_confirmation
  @send_email_confirmation
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



49
50
51
52
53
# File 'app/models/refund.rb', line 49

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



58
59
60
# File 'app/models/refund.rb', line 58

def refund_amount
  item_total + service_fee
end

#service_feeObject



62
63
64
# File 'app/models/refund.rb', line 62

def service_fee
  items.collect(&:service_fee).sum
end

#submit(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/refund.rb', line 12

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


  ActiveRecord::Base.transaction do
    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
      #
      # NOTE: That this will clean up the items, but the tickets will remain sold
      #  if the show date has passed.
      # because ticket.reset_price! is guarded with sold? and return_to_inventory
      # is guarded with expired? (show date passed)
      #
      items.each { |i| i.return!(return_items_to_inventory) }
      items.each(&:refund!)
      create_refund_order(@payment.transaction_id)
    end
  #TODO: rollback refund as well
  end
end

#successful?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/refund.rb', line 44

def successful?
  @success || false
end