Class: AuthorizeNet::KeyValueTransaction

Inherits:
Transaction show all
Defined in:
lib/authorize_net/key_value_transaction.rb

Overview

The core, key/value transaction class. You shouldn’t instantiate this one. Instead you should use AuthorizeNet::AIM::Transaction or AuthorizeNet::SIM::Transaction.

Direct Known Subclasses

AIM::Transaction, SIM::Transaction

Defined Under Namespace

Modules: DeviceType, Gateway, MarketType, Type

Constant Summary collapse

@@purchase_option_defaults =

The default options for purchase.

{
  :cardholder_auth_value => nil,
  :cardholder_auth_indicator => nil
}
@@authorize_option_defaults =

The default options for authorize.

{
  :cardholder_auth_value => nil,
  :cardholder_auth_indicator => nil
}
@@boolean_fields =

Fields to convert to/from booleans.

[]
@@decimal_fields =

Fields to convert to/from BigDecimal.

[]

Constants included from TypeConversions

TypeConversions::API_FIELD_PREFIX

Instance Method Summary collapse

Methods inherited from Transaction

#fields, #set_address, #set_customer, #set_fields, #set_shipping_address

Methods included from TypeConversions

#boolean_to_value, #date_to_value, #datetime_to_value, #decimal_to_value, #integer_to_value, #to_external_field, #to_internal_field, #to_param, #value_to_boolean, #value_to_date, #value_to_datetime, #value_to_decimal, #value_to_integer

Constructor Details

#initializeKeyValueTransaction

DO NOT USE. Instantiate AuthorizeNet::AIM::Transaction or AuthorizeNet::SIM::Transaction instead.



65
66
67
68
69
70
# File 'lib/authorize_net/key_value_transaction.rb', line 65

def initialize()
  super
  @custom_fields ||= {}
  @test_mode ||= false
  @version = '3.1'
end

Instance Method Details

#add_line_item(id = nil, name = nil, description = nil, quantity = nil, price = nil, taxable = nil) ⇒ Object

Convenience method for adding line items to a transaction.



96
97
98
99
100
101
102
103
# File 'lib/authorize_net/key_value_transaction.rb', line 96

def add_line_item(id = nil, name = nil, description = nil, quantity = nil, price = nil, taxable = nil)
  line_item = "#{id}<|>#{name}<|>#{description}<|>#{quantity}<|>#{price}<|>#{taxable}"
  if @fields.has_key?(:line_item)
    @fields[:line_item] = @fields[:line_item].to_a << line_item
  else
    @fields[:line_item] = [line_item]
  end
end

#authorize(amount, credit_card, options = {}) ⇒ Object

Sets up and submits an authorize (AUTH_ONLY) transaction. Returns a response object. If the transaction has already been run, it will return nil. Use this to put a hold on funds, but don’t actually charge the card yet.

amount

The amount to authorize. Accepts a string in the format “%0.2f”, a Float or a BigDecimal.

credit_card

The credit card or eCheck to charge. Accepts an instance of AuthorizeNet::CreditCard, AuthorizeNet::ECheck, or a string of digits (in which case the expiration should be added using set_fields).

options

A hash with any of following keys: cardholder_auth_indicator, cardholder_auth_value. These are for CAVV and can be ignored in most cases.

Typical usage:

credit_card = AuthorizeNet::CreditCard.new('4111111111111111', '1120')
response = transaction.authorize(10.0, credit_card)


225
226
227
228
229
230
231
232
# File 'lib/authorize_net/key_value_transaction.rb', line 225

def authorize(amount, credit_card, options = {})
  handle_payment_argument(credit_card)
  options = @@authorize_option_defaults.merge(options)
  handle_cavv_options(options)
  set_fields(:amount => amount)
  self.type = Type::AUTHORIZE_ONLY
  run
end

#capture(amount, authorization_code) ⇒ Object

Sets up and submits a capture (CAPTURE_ONLY) transaction. Returns a response object. If the transaction has already been run, it will return nil. Note that you can not capture a transaction that was made though the AIM API using this method. Use prior_auth_capture instead.

amount

The amount to capture. Accepts a string in the format “%0.2f”, a Float or a BigDecimal.

authorization_code

The authorization code of the transaction to capture. Accepts a string.

Typical usage:

response = transaction.capture(10.0, '123FAKE')


246
247
248
249
250
# File 'lib/authorize_net/key_value_transaction.rb', line 246

def capture(amount, authorization_code)
  set_fields(:amount => amount, :auth_code => authorization_code)
  self.type = Type::CAPTURE_ONLY
  run
end

#custom_fieldsObject

Returns the current hash of custom fields.



91
92
93
# File 'lib/authorize_net/key_value_transaction.rb', line 91

def custom_fields
  @custom_fields
end

#prior_auth_capture(transaction, amount = nil) ⇒ Object

Sets up and submits a capture (PRIOR_AUTH_CAPTURE) transaction. Returns a response object. Use this method to actually charge a card that you previously put a hold on (using authorize).

transaction

The transaction ID to capture. Accepts a string of the transaction ID, an instance of AuthorizeNet::Transaction or AuthorizeNet::Response.

amount

The amount to capture (only if less than the amount originally authorized, otherwise leave as Nil). Accepts a string in the format “%0.2f”, a Float, a BigDecimal or Nil.

Typical usage:

response = transaction.prior_auth_capture('123456789')


263
264
265
266
267
268
# File 'lib/authorize_net/key_value_transaction.rb', line 263

def prior_auth_capture(transaction, amount = nil)
  handle_transaction_argument(transaction)
  set_fields(:amount => amount)
  self.type = Type::PRIOR_AUTHORIZATION_AND_CAPTURE
  run
end

#purchase(amount, credit_card, options = {}) ⇒ Object

Sets up and submits a standard purchase (AUTHORIZE_AND_CAPTURE) transaction. Returns a response object. If the transaction has already been run, it will return nil.

amount

The amount to charge. Accepts a string in the format “%0.2f”, a Float or a BigDecimal.

credit_card

The credit card or eCheck to charge. Accepts an instance of AuthorizeNet::CreditCard, AuthorizeNet::ECheck, or a string of digits (in which case the expiration should be added using set_fields).

options

A hash with any of following keys: cardholder_auth_indicator, cardholder_auth_value. These are for CAVV and can be ignored in most cases.

Typical usage:

credit_card = AuthorizeNet::CreditCard.new('4111111111111111', '1120')
response = transaction.purchase(10.0, credit_card)


148
149
150
151
152
153
154
155
# File 'lib/authorize_net/key_value_transaction.rb', line 148

def purchase(amount, credit_card, options = {})
  handle_payment_argument(credit_card)
  options = @@purchase_option_defaults.merge(options)
  handle_cavv_options(options)
  set_fields(:amount => amount)
  self.type = Type::AUTHORIZE_AND_CAPTURE
  run
end

#refund(amount, transaction, credit_card) ⇒ Object

Sets up and submits a refund (CREDIT) transaction. Returns a response object. If the transaction has already been run, it will return nil.

amount

The amount to refund. Accepts a string in the format “%0.2f”, a Float or a BigDecimal.

transaction

The transaction ID to apply the refund to. Accepts a string of the transaction ID, an instance of AuthorizeNet::Transaction or AuthorizeNet::Response.

credit_card

The credit card or eCheck to charge. Accepts an instance of AuthorizeNet::CreditCard, AuthorizeNet::ECheck, or a string of digits. In many cases you only need the last 4 digits of the credit card here.

Typical usage:

response = transaction.refund(10.0, '123456789', '1212')


169
170
171
172
173
174
175
# File 'lib/authorize_net/key_value_transaction.rb', line 169

def refund(amount, transaction, credit_card)
  handle_payment_argument(credit_card)
  handle_transaction_argument(transaction)
  set_fields(:amount => amount)
  self.type = Type::CREDIT
  run
end

#set_custom_fields(fields = {}) ⇒ Object

Sets arbitrary custom fields, overwriting existing values if they exist. Takes a hash of key/value pairs, where the keys are the field names. You can set a field to Nil to unset it.



86
87
88
# File 'lib/authorize_net/key_value_transaction.rb', line 86

def set_custom_fields(fields = {})
  @custom_fields.merge!(fields)
end

#set_email_receipt(email) ⇒ Object

Takes an instance of AuthorizeNet::EmailReceipt and adds it to the transaction.



106
107
108
# File 'lib/authorize_net/key_value_transaction.rb', line 106

def set_email_receipt(email)
  @fields.merge!(email.to_hash)
end

#test?Boolean

Checks if the transaction has been configured for test mode or not. Return TRUE if the transaction is a test transaction, FALSE otherwise. All transactions run against the sandbox are considered test transactions.

Returns:

  • (Boolean)


75
76
77
# File 'lib/authorize_net/key_value_transaction.rb', line 75

def test?
  !!@test_mode
end

#typeObject

Returns the type of transaction.



111
112
113
# File 'lib/authorize_net/key_value_transaction.rb', line 111

def type
  @type
end

#type=(type) ⇒ Object

Sets the type of transaction.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/authorize_net/key_value_transaction.rb', line 116

def type=(type)
  case type
  when :authorize, :auth_only
    @type = Type::AUTHORIZE_ONLY
  when :purchase, :auth_and_capture
    @type = Type::AUTHORIZE_AND_CAPTURE
  when :refund, :credit
    @type = Type::CREDIT
  when :void
    @type = Type::VOID
  when :capture, :capture_only
    @type = Type::CAPTURE_ONLY
  when :prior_auth_capture
    @type = Type::PRIOR_AUTHORIZATION_AND_CAPTURE
  else
    @type = type
  end
end

#unlinked_credit(amount, credit_card) ⇒ Object

Sets up and submits a refund (CREDIT) transaction for a transaction that was not originally submitted via the payment gateway. Note that this is a special feature which requires your account to support ECC (expanded credits capability) transactions.

amount

The amount to refund. Accepts a string in the format “%0.2f”, a Float or a BigDecimal.

credit_card

The credit card or eCheck to charge. Accepts an instance of AuthorizeNet::CreditCard, AuthorizeNet::ECheck, or a string of digits.

Typical usage:

response = transaction.unlinked_credit(10.0, '4111111111111111')


188
189
190
191
192
193
# File 'lib/authorize_net/key_value_transaction.rb', line 188

def unlinked_credit(amount, credit_card)
  handle_payment_argument(credit_card)
  set_fields(:amount => amount)
  self.type = Type::CREDIT
  run
end

#versionObject

Returns the current API version that we are adhering to



80
81
82
# File 'lib/authorize_net/key_value_transaction.rb', line 80

def version
  @version
end

#void(transaction) ⇒ Object

Sets up and submits a void (VOID) transaction. Returns a response object. If the transaction has already been run, it will return nil. Note that you can only void unsettled transactions.

transaction

The transaction ID of the transaction to void. Accepts a string of the transaction ID, an instance of AuthorizeNet::Transaction or AuthorizeNet::Response.

Typical usage:

response = transaction.void('123456789')


205
206
207
208
209
# File 'lib/authorize_net/key_value_transaction.rb', line 205

def void(transaction)
  handle_transaction_argument(transaction)
  self.type = Type::VOID
  run
end