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.



69
70
71
72
73
74
# File 'lib/authorize_net/key_value_transaction.rb', line 69

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.



100
101
102
103
104
105
106
107
# File 'lib/authorize_net/key_value_transaction.rb', line 100

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)


229
230
231
232
233
234
235
236
# File 'lib/authorize_net/key_value_transaction.rb', line 229

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')


250
251
252
253
254
# File 'lib/authorize_net/key_value_transaction.rb', line 250

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.



95
96
97
# File 'lib/authorize_net/key_value_transaction.rb', line 95

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')


267
268
269
270
271
272
# File 'lib/authorize_net/key_value_transaction.rb', line 267

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)


152
153
154
155
156
157
158
159
# File 'lib/authorize_net/key_value_transaction.rb', line 152

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')


173
174
175
176
177
178
179
# File 'lib/authorize_net/key_value_transaction.rb', line 173

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.



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

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.



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

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)


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

def test?
  !!@test_mode
end

#typeObject

Returns the type of transaction.



115
116
117
# File 'lib/authorize_net/key_value_transaction.rb', line 115

def type
  @type
end

#type=(type) ⇒ Object

Sets the type of transaction.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/authorize_net/key_value_transaction.rb', line 120

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')


192
193
194
195
196
197
# File 'lib/authorize_net/key_value_transaction.rb', line 192

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



84
85
86
# File 'lib/authorize_net/key_value_transaction.rb', line 84

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')


209
210
211
212
213
# File 'lib/authorize_net/key_value_transaction.rb', line 209

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