Class: ActiveMerchant::Billing::SamuraiGateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/active_merchant/billing/gateways/samurai.rb

Constant Summary

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::CURRENCIES_WITHOUT_FRACTIONS, Gateway::DEBIT_CARDS

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods inherited from Gateway

#card_brand, card_brand, inherited, supports?, #test?

Methods included from CreditCardFormatting

#format

Constructor Details

#initialize(options = {}) ⇒ SamuraiGateway

Returns a new instance of SamuraiGateway.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_merchant/billing/gateways/samurai.rb', line 12

def initialize(options = {})
  begin
    require 'samurai'
  rescue LoadError
    raise "Could not load the samurai gem (>= 0.2.25).  Use `gem install samurai` to install it."
  end

  requires!(options, :login, :password, :processor_token)
  Samurai.options = {
    :merchant_key       => options[:login],
    :merchant_password  => options[:password],
    :processor_token    => options[:processor_token]
  }

  super
end

Instance Method Details

#authorize(money, credit_card_or_vault_id, options = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/active_merchant/billing/gateways/samurai.rb', line 29

def authorize(money, credit_card_or_vault_id, options = {})
  token = payment_method_token(credit_card_or_vault_id, options)
  return token if token.is_a?(Response)

  authorize = Samurai::Processor.authorize(token, amount(money), processor_options(options))
  handle_result(authorize)
end

#capture(money, authorization_id, options = {}) ⇒ Object



45
46
47
48
# File 'lib/active_merchant/billing/gateways/samurai.rb', line 45

def capture(money, authorization_id, options = {})
  transaction = Samurai::Transaction.find(authorization_id)
  handle_result(transaction.capture(amount(money)))
end

#purchase(money, credit_card_or_vault_id, options = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/active_merchant/billing/gateways/samurai.rb', line 37

def purchase(money, credit_card_or_vault_id, options = {})
  token = payment_method_token(credit_card_or_vault_id, options)
  return token if token.is_a?(Response)

  purchase = Samurai::Processor.purchase(token, amount(money), processor_options(options))
  handle_result(purchase)
end

#refund(money, transaction_id, options = {}) ⇒ Object



50
51
52
53
# File 'lib/active_merchant/billing/gateways/samurai.rb', line 50

def refund(money, transaction_id, options = {})
  transaction = Samurai::Transaction.find(transaction_id)
  handle_result(transaction.credit(amount(money)))
end

#store(creditcard, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_merchant/billing/gateways/samurai.rb', line 60

def store(creditcard, options = {})
  address = options[:billing_address] || options[:address] || {}

  result = Samurai::PaymentMethod.create({
    :card_number  => creditcard.number,
    :expiry_month => creditcard.month.to_s.rjust(2, "0"),
    :expiry_year  => creditcard.year.to_s,
    :cvv          => creditcard.verification_value,
    :first_name   => creditcard.first_name,
    :last_name    => creditcard.last_name,
    :address_1    => address[:address1],
    :address_2    => address[:address2],
    :city         => address[:city],
    :zip          => address[:zip],
    :sandbox      => test?
  })
  result.retain if options[:retain] && result.is_sensitive_data_valid && result.payment_method_token

  Response.new(result.is_sensitive_data_valid,
               message_from_result(result),
               { :payment_method_token => result.is_sensitive_data_valid && result.payment_method_token })
end

#void(transaction_id, options = {}) ⇒ Object



55
56
57
58
# File 'lib/active_merchant/billing/gateways/samurai.rb', line 55

def void(transaction_id, options = {})
  transaction = Samurai::Transaction.find(transaction_id)
  handle_result(transaction.void)
end