Class: ErpCommerce::ActiveMerchantWrappers::BrainTreeGatewayWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/erp_commerce/active_merchant_wrappers/brain_tree_gateway_wrapper.rb

Class Method Summary collapse

Class Method Details

.purchase(credit_card, amount, cvv, gateway_options = {}) ⇒ Object



7
8
9
10
11
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/erp_commerce/active_merchant_wrappers/brain_tree_gateway_wrapper.rb', line 7

def self.purchase(credit_card, amount, cvv, gateway_options={})
  result = {}

  #this needs config options
  gateway_options.merge!({:login => 'demo', :password => 'password'})

  ActiveMerchant::Billing::Base.mode = :test

  #setup gateway
  gateway = ActiveMerchant::Billing::BraintreeGateway.new(gateway_options)

  #set credit card info
  credit_card_result = ActiveMerchant::Billing::CreditCard.new({
      :first_name         => credit_card.first_name_on_card,
      :last_name          => credit_card.last_name_on_card,
      :number             => credit_card.private_card_number,
      :month              => credit_card.expiration_month,
      :year               => credit_card.expiration_year,
      :verification_value => cvv,
      :type               => ErpCommerce::ActiveMerchantWrappers::CreditCardValidation.get_card_type(credit_card.private_card_number)
    })

  if credit_card_result.valid?
    cents = (amount.to_d * 100)
    response = gateway.purchase(cents, credit_card_result)

    if response.success?
      result[:message] = response.message
      result[:payment] = Payment.new
      result[:payment].authorization_code = response.authorization
      result[:payment].success = true
      result[:payment].purchase
    else
      result[:message] = response.message
      result[:payment] = Payment.new
      result[:payment].success = false
      result[:payment].decline
    end

    gateway = PaymentGateway.create(
      :response => response.message,
      :payment_gateway_action => PaymentGatewayAction.find_by_internal_identifier('authorize')
    )

    result[:payment].payment_gateways << gateway
    result[:payment].save
  else
    result[:message] = "<ul>"
    credit_card_result.errors.full_messages.each do |current_notice_msg|
      result[:message] << "<li>"
      result[:message] << current_notice_msg
      result[:message] << "</li>"
    end
    result[:message] << "<ul>"
  end

  result
end