Class: Forge::CreditCardProcessor

Inherits:
Object
  • Object
show all
Includes:
ActiveMerchant::Billing
Defined in:
lib/forge/lib/forge/credit_card_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, currency = "CAD") ⇒ CreditCardProcessor

Returns a new instance of CreditCardProcessor.



9
10
11
12
# File 'lib/forge/lib/forge/credit_card_processor.rb', line 9

def initialize(order, currency = "CAD")
  @order = order
  @currency = currency
end

Instance Attribute Details

#credit_cardObject (readonly)

Returns the value of attribute credit_card.



7
8
9
# File 'lib/forge/lib/forge/credit_card_processor.rb', line 7

def credit_card
  @credit_card
end

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/forge/lib/forge/credit_card_processor.rb', line 7

def message
  @message
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/forge/lib/forge/credit_card_processor.rb', line 7

def status
  @status
end

Instance Method Details

#create_credit_card(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/forge/lib/forge/credit_card_processor.rb', line 14

def create_credit_card(args)
  @credit_card = ActiveMerchant::Billing::CreditCard.new(
    :first_name => @order.billing_address[:first_name],
    :last_name => @order.billing_address[:last_name],
    :month => args[:month],
    :year => args[:year],
    :verification_value => args[:verification_value],
    :number => args[:number]
  )
end

#pay(order) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/forge/lib/forge/credit_card_processor.rb', line 25

def pay(order)
  raise "Credit card cannot be nil" if @credit_card.nil?

  # build the options hash for the payment
  options = options_hash("Order ## #{order.id}")
  if @credit_card.valid?
    authorization = order.authorize_payment(@credit_card, options)
    if authorization.success? && order.authorized?
      # proceed to capture payment
      capture = order.capture_payment
      if capture.success? && order.paid?
        @message = "Successfully charged $#{sprintf("%.2f", order.total_price)} to the credit card #{credit_card.display_number}"
      else
        @status = :failure
        @message = "Payment capture failed.  Please contact your credit card company to determine the problem, or retry with a different card."
      end
    else
      # could not authorize
      @status = :failure
      @message = "Payment authorization failed.  Please contact your credit card company to determine the problem, or retry with a different card."
    end
    @status = @order.state.to_sym
  else
    @status = :invalid_credit_card
    @message = "The credit card number you have entered is not valid."
  end
  if @status == :paid
    true
  else
    false
  end
end