Class: TransactionGateway::BraintreeGateway

Inherits:
Result
  • Object
show all
Includes:
GatewayErrors
Defined in:
lib/transaction_gateway/braintree_gateway.rb

Constant Summary

Constants inherited from Result

Result::AUTHORIZEDOTNET, Result::BRAINTREE, Result::FORTIS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Result

#handle_result

Constructor Details

#initialize(merchant_id, public_key, private_key, environment = :sandbox) ⇒ BraintreeGateway

Returns a new instance of BraintreeGateway.



9
10
11
12
13
14
15
16
# File 'lib/transaction_gateway/braintree_gateway.rb', line 9

def initialize(merchant_id, public_key, private_key, environment = :sandbox)
  self.merchant_id  = merchant_id
  self.public_key   = public_key
  self.private_key  = private_key
  
  self.environment  = environment.to_s.gsub(/\s/, '').downcase
  self.environment  = (self.environment == "") ? :sandbox : self.environment.to_sym
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



7
8
9
# File 'lib/transaction_gateway/braintree_gateway.rb', line 7

def environment
  @environment
end

#merchant_idObject

Returns the value of attribute merchant_id.



7
8
9
# File 'lib/transaction_gateway/braintree_gateway.rb', line 7

def merchant_id
  @merchant_id
end

#private_keyObject

Returns the value of attribute private_key.



7
8
9
# File 'lib/transaction_gateway/braintree_gateway.rb', line 7

def private_key
  @private_key
end

#public_keyObject

Returns the value of attribute public_key.



7
8
9
# File 'lib/transaction_gateway/braintree_gateway.rb', line 7

def public_key
  @public_key
end

Instance Method Details

#configure_braintreeObject



18
19
20
21
22
23
# File 'lib/transaction_gateway/braintree_gateway.rb', line 18

def configure_braintree
  Braintree::Configuration.environment  = self.environment
  Braintree::Configuration.merchant_id  = self.merchant_id
  Braintree::Configuration.public_key   = self.public_key
  Braintree::Configuration.private_key  = self.private_key
end

#create_card_account(card, customer_profile_id) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/transaction_gateway/braintree_gateway.rb', line 107

def (card, customer_profile_id)
  raise TransactionGatewayInvalidCardType unless card.class == GatewayCard

  configure_braintree

  result = Braintree::Customer.update(customer_profile_id,
    :credit_card => {
      :number           => card.card_number,
      :cvv              => card.cvv,
      :expiration_month => card.expiration_month,
      :expiration_year  => card.expiration_year
    })
  
  return handle_result(result, BRAINTREE)
end

#create_customer_profile(customer, card = nil) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/transaction_gateway/braintree_gateway.rb', line 98

def create_customer_profile(customer, card = nil)
  raise InvalidCustomerType unless customer.class == GatewayCustomer

  configure_braintree

  result = Braintree::Customer.create(customer.convert_to_braintree_input(card))
  return handle_result(result, BRAINTREE)
end

#create_transaction(card_or_profile, amount, settle = false, customer = nil) ⇒ 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
57
58
59
60
61
62
63
64
65
66
# File 'lib/transaction_gateway/braintree_gateway.rb', line 25

def create_transaction(card_or_profile, amount, settle = false, customer = nil)
  raise TransactionGatewayInvalidAmount if amount.to_f <= 0

  case card_or_profile
  when GatewayCard
    credit_card = {
      :credit_card =>
        {
        :number =>  card_or_profile.card_number,
        :expiration_month => card_or_profile.expiration_month,
        :expiration_year => card_or_profile.expiration_year,
        :cvv => card_or_profile.cvv
      }
    }

  when GatewayCustomerProfile
    credit_card ={:payment_method_token   =>  card_or_profile.payment_profile_id}

  else
    raise TransactionGatewayInvalidCardType unless card_or_profile.class == GatewayCard
  end

  sale_info = { :amount => amount }
  
  sale_info[:options] = {
    :submit_for_settlement => true
  } if settle == true

  sale_info[:customer] = {
    :first_name => customer.first_name,
    :last_name => customer.last_name,
    :email => customer.email_id
  } if customer and customer.class == GatewayCustomer

  sale_info.merge!(credit_card)

  configure_braintree

  result = Braintree::Transaction.sale(sale_info)

  return handle_result(result, BRAINTREE)
end

#force_settle(profile, authorize_code, amount) ⇒ Object

Patched for voice authorization settlement. Not provided any method from Braintree in Ruby but referred from Braintree control panel new transaction HTML page.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/transaction_gateway/braintree_gateway.rb', line 124

def force_settle(profile, authorize_code, amount)
  raise InvalidPaymentProfileType unless profile.class == GatewayCustomerProfile
  raise TransactionGatewayInvalidAmount if amount.to_f <= 0
  
  configure_braintree

  sale_info = {
    :type                         => :sale,
    :amount                       => amount,
    :voice_authorization          => "1",
    :processor_authorization_code => authorize_code,
    :payment_method_token         => profile.payment_profile_id,
  }
  
  gateway = Braintree::Gateway.new(Braintree::Configuration.instantiate)
  transaction = Braintree::TransactionGateway.new(gateway)
  Braintree::Util.verify_keys(Braintree::TransactionGateway._create_signature.concat([:voice_authorization, :processor_authorization_code]) , sale_info)
  result = transaction._do_create("/transactions", :transaction => sale_info)

  return handle_result(result, BRAINTREE)
end

#refund(profile, amount) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/transaction_gateway/braintree_gateway.rb', line 85

def refund(profile, amount)
  raise InvalidPaymentProfileType unless profile.class == GatewayCustomerProfile
  raise TransactionGatewayInvalidAmount if amount.to_f == 0

  configure_braintree

  result = Braintree::Transaction.credit(
    :amount                 =>  amount.abs,
    :payment_method_token   =>  profile.payment_profile_id
  )
  return handle_result(result, BRAINTREE)
end

#settle_transaction(transaction_id, amount = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/transaction_gateway/braintree_gateway.rb', line 68

def settle_transaction(transaction_id, amount = nil)
  raise TransactionIDInvalid if transaction_id.to_s.gsub(/\s/, "") == ""

  configure_braintree

  result = Braintree::Transaction.submit_for_settlement(transaction_id, amount)
  
  return handle_result(result, BRAINTREE)
end

#void_transaction(transaction_id) ⇒ Object



78
79
80
81
82
83
# File 'lib/transaction_gateway/braintree_gateway.rb', line 78

def void_transaction(transaction_id)
  configure_braintree
  
  result = Braintree::Transaction.void(transaction_id)
  return handle_result(result, BRAINTREE)
end