Class: AuthorizeNet::SIM::Transaction

Inherits:
KeyValueTransaction show all
Defined in:
lib/authorize_net/sim/transaction.rb

Overview

The SIM transaction class. Handles building the transaction payload and generating a set of hidden form fields to be POSTed to the gateway.

Constant Summary collapse

RANDOM_SEQUENCE_MAX =
(1 << 32) - 1
@@digest =

Our MD5 digest generator.

OpenSSL::Digest::Digest.new('md5')
@@option_defaults =

The default options for the constructor.

{
  :sequence => nil,
  :timestamp => nil,
  :test => false,
  :hosted_payment_form => false,
  :relay_response => true,
  :relay_url => nil,
  :transaction_type => Type::AUTHORIZE_AND_CAPTURE
}

Constants included from TypeConversions

TypeConversions::API_FIELD_PREFIX

Instance Method Summary collapse

Methods inherited from KeyValueTransaction

#add_line_item, #authorize, #capture, #custom_fields, #prior_auth_capture, #purchase, #refund, #set_custom_fields, #set_email_receipt, #test?, #type, #type=, #unlinked_credit, #version, #void

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

#initialize(api_login_id, api_transaction_key, amount, options = {}) ⇒ Transaction

Constructs a SIM transaction. You can use the new SIM transaction object to build the hidden field payload needed to process a SIM transaction with the gateway. In particular, this class handles generating the MD5 fingerprint used to authenticate transactions at the gateway. Since the fingerprint includes the amount to charge, you should not construct this object until you know EXACTLY how much you want to charge (or authorize).

api_login_id

Your API login ID, as a string.

api_transaction_key

Your API transaction key, as a string.

amount

The amount of the transaction, as a string, Float or BigDecimal.

options

A hash of options. See below for values.

Options

sequence

The sequence number of the transaction as a string or Fixnum. This is usually something like an invoice number. If none is provided, the SDK generates one at random.

timestamp

The time the transaction was initiated as a string or Fixnum. This needs to be within 15 minutes of when the gateway receives the transaction. If no value is provided, the SDK defaults it to Time.now().

test

A boolean indicating if the transaction should be run in test mode or not (defaults to false).

hosted_payment_form

A boolean indicating if the transaction should use a hosted payment form (defaults to false).

relay_response

A boolean indicating if the transaction should use the relay response feature to return a receipt to the customer (defaults to true). Direct Post Method requires using a relay response.

relay_url

A string of the URL that the gateway should hit to get the relay response (defaults to nil).

transaction_type

The type of transaction to perform. Defaults to AuthorizeNet::Type::AUTHORIZE_AND_CAPTURE. This value is only used if run is called directly.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/authorize_net/sim/transaction.rb', line 44

def initialize(, api_transaction_key, amount, options = {})
  super()
  @api_transaction_key = api_transaction_key
  @api_login_id = 
  @amount = decimal_to_value(amount)
  options = @@option_defaults.merge(options)
  @sequence = options[:sequence]
  @timestamp = options[:timestamp]
  @test_mode = options[:test]
  @hosted_payment_form = options[:hosted_payment_form]
  @relay_url = options[:relay_url]
  @type = options[:transaction_type]
  unless @relay_url.nil?
    @relay_response = true
  else
    @relay_response = !!options[:relay_response]
  end
  @delim_data = !@relay_response
end

Instance Method Details

#fingerprintObject

Calculates and returns the HMAC-MD5 fingerprint needed to authenticate the transaction with the SIM gateway.



66
67
68
69
70
71
72
73
74
75
# File 'lib/authorize_net/sim/transaction.rb', line 66

def fingerprint
  if @timestamp.nil?
    @timestamp = Time.now.to_i
  end
  
  if @sequence.nil?
    @sequence = rand(RANDOM_SEQUENCE_MAX)
  end
  OpenSSL::HMAC.hexdigest(@@digest, @api_transaction_key, "#{@api_login_id.to_s.rstrip}^#{@sequence.to_s.rstrip}^#{@timestamp.to_s.rstrip}^#{@amount.to_s.rstrip}^")
end

#fingerprint_fieldsObject

Returns all the fields needed for the fingerprint. These must all be passed to the SIM exactly as returned. And these values are time sensitive.



79
80
81
82
83
84
85
86
87
# File 'lib/authorize_net/sim/transaction.rb', line 79

def fingerprint_fields
  {
    :login => @api_login_id,
    :fp_hash => fingerprint,
    :fp_sequence => @sequence,
    :fp_timestamp => @timestamp,
    :amount => @amount
  }
end

#form_fieldsObject

Returns all the fields (including custom) exactly as they should be named in the SIM form. Fields with multiple values are returned with an array for the key’s value.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/authorize_net/sim/transaction.rb', line 92

def form_fields
  form_fields = {}
  form_fields[:x_test_request] = boolean_to_value(@test_mode)
  if @hosted_payment_form
    form_fields[:x_show_form] = 'PAYMENT_FORM'
  end
  if @relay_response && !@relay_url.nil?
    form_fields[:x_relay_url] = @relay_url
  end
  fields.merge(:type => @type, :version => @version, :delim_data => boolean_to_value(@delim_data), :relay_response => boolean_to_value(@relay_response)).each do |k, v|
    form_fields[to_external_field(k)] = v
  end
  fingerprint_fields.each do |k, v|
    form_fields[to_external_field(k)] = v
  end
  form_fields.merge(custom_fields)
end

#runObject

An alias for form_fields.



132
133
134
# File 'lib/authorize_net/sim/transaction.rb', line 132

def run
  form_fields
end

#set_hosted_payment_form(form) ⇒ Object

Takes an instance of AuthorizeNet::SIM::HostedPaymentForm and adds it to the transaction. Note that many of the fields in AuthorizeNet::SIM::HostedPaymentForm are shared with those in AuthorizeNet::SIM::HostedReceiptPage. For the duplicate fields, which ever value is added to the transaction last will be the one used.



115
116
117
118
# File 'lib/authorize_net/sim/transaction.rb', line 115

def set_hosted_payment_form(form)
  @fields.merge!(form.to_hash)
  @hosted_payment_form = true
end

#set_hosted_payment_receipt(form) ⇒ Object

Takes an instance of AuthorizeNet::SIM::HostedReceiptPage and adds it to the transaction. Note that many of the fields in AuthorizeNet::SIM::HostedReceiptPage are shared with those in AuthorizeNet::SIM::HostedPaymentForm. For the duplicate fields, which ever value is added to the transaction last will be the one used. If you set a hosted payment receipt, the relay response will be disabled.



125
126
127
128
129
# File 'lib/authorize_net/sim/transaction.rb', line 125

def set_hosted_payment_receipt(form)
  @fields.merge!(form.to_hash)
  @relay_response = false
  @delim_data = true
end