Class: Piggybak::Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/piggybak/payment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#numberObject

Returns the value of attribute number.



12
13
14
# File 'app/models/piggybak/payment.rb', line 12

def number
  @number
end

#verification_valueObject

Returns the value of attribute verification_value.



13
14
15
# File 'app/models/piggybak/payment.rb', line 13

def verification_value
  @verification_value
end

Instance Method Details

#credit_cardObject



27
28
29
30
31
32
33
34
# File 'app/models/piggybak/payment.rb', line 27

def credit_card
  { "number" => self.number,
    "month" => self.month,
    "year" => self.year,
    "verification_value" => self.verification_value,
    "first_name" => self.line_item ? self.line_item.order.billing_address.firstname : nil,
    "last_name" => self.line_item ? self.line_item.order.billing_address.lastname : nil }
end

#detailsObject



65
66
67
68
69
70
71
72
# File 'app/models/piggybak/payment.rb', line 65

def details
  if !self.new_record? 
    return "Payment ##{self.id} (#{self.created_at.strftime("%m-%d-%Y")}): " #+ 
      #"$#{"%.2f" % self.total}" reference line item total here instead
  else
    return ""
  end
end

#month_enumObject



19
20
21
# File 'app/models/piggybak/payment.rb', line 19

def month_enum
  1.upto(12).to_a
end

#process(order) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/piggybak/payment.rb', line 36

def process(order)
  return true if !self.new_record?

  ActiveMerchant::Billing::Base.mode = Piggybak.config.activemerchant_mode

  payment_gateway = self.payment_method.klass.constantize
  gateway = payment_gateway::KLASS.new(self.payment_method.key_values)
  p_credit_card = ActiveMerchant::Billing::CreditCard.new(self.credit_card)
  gateway_response = gateway.authorize(order.total_due*100, p_credit_card, :address => order.avs_address)
  if gateway_response.success?
    self.attributes = { :transaction_id => payment_gateway.transaction_id(gateway_response),
                        :masked_number => self.number.mask_cc_number }
    gateway.capture(order.total_due*100, gateway_response.authorization, { :credit_card => p_credit_card } )
    return true
  else
    self.errors.add :payment_method_id, gateway_response.message
    return false
  end
end

#refundObject

Note: It is not added now, because for methods that do not store user profiles, a credit card number must be passed If encrypted credit cards are stored on the system, this can be updated



60
61
62
63
# File 'app/models/piggybak/payment.rb', line 60

def refund
  # TODO: Create ActiveMerchant refund integration 
  return
end

#status_enumObject



15
16
17
# File 'app/models/piggybak/payment.rb', line 15

def status_enum
  ["paid"]
end

#year_enumObject



23
24
25
# File 'app/models/piggybak/payment.rb', line 23

def year_enum
  Time.now.year.upto(Time.now.year + 10).to_a
end