authorize-net 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/License.pdf +0 -0
  2. data/README.rdoc +124 -0
  3. data/Rakefile +74 -0
  4. data/generators/authorize_net_direct_post/USAGE +20 -0
  5. data/generators/authorize_net_direct_post/authorize_net_direct_post_generator.rb +21 -0
  6. data/generators/authorize_net_direct_post/templates/README-AuthorizeNet +49 -0
  7. data/generators/authorize_net_direct_post/templates/config.yml.erb +8 -0
  8. data/generators/authorize_net_direct_post/templates/config.yml.rails3.erb +8 -0
  9. data/generators/authorize_net_direct_post/templates/controller.rb.erb +31 -0
  10. data/generators/authorize_net_direct_post/templates/initializer.rb +4 -0
  11. data/generators/authorize_net_direct_post/templates/layout.erb +18 -0
  12. data/generators/authorize_net_direct_post/templates/payment.erb +10 -0
  13. data/generators/authorize_net_direct_post/templates/payment.rails3.erb +10 -0
  14. data/generators/authorize_net_direct_post/templates/receipt.erb +1 -0
  15. data/generators/authorize_net_direct_post/templates/relay_response.erb +1 -0
  16. data/generators/authorize_net_sim/USAGE +20 -0
  17. data/generators/authorize_net_sim/authorize_net_sim_generator.rb +19 -0
  18. data/generators/authorize_net_sim/templates/README-AuthorizeNet +52 -0
  19. data/generators/authorize_net_sim/templates/config.yml.erb +8 -0
  20. data/generators/authorize_net_sim/templates/config.yml.rails3.erb +8 -0
  21. data/generators/authorize_net_sim/templates/controller.rb.erb +21 -0
  22. data/generators/authorize_net_sim/templates/initializer.rb +4 -0
  23. data/generators/authorize_net_sim/templates/layout.erb +18 -0
  24. data/generators/authorize_net_sim/templates/payment.erb +6 -0
  25. data/generators/authorize_net_sim/templates/payment.rails3.erb +6 -0
  26. data/generators/authorize_net_sim/templates/thank_you.erb +1 -0
  27. data/generators/generator_extensions.rb +75 -0
  28. data/init.rb +2 -0
  29. data/install.rb +1 -0
  30. data/lib/app/helpers/authorize_net_helper.rb +24 -0
  31. data/lib/authorize-net.rb +4 -0
  32. data/lib/authorize_net.rb +92 -0
  33. data/lib/authorize_net/addresses/address.rb +29 -0
  34. data/lib/authorize_net/addresses/shipping_address.rb +26 -0
  35. data/lib/authorize_net/aim/response.rb +131 -0
  36. data/lib/authorize_net/aim/transaction.rb +184 -0
  37. data/lib/authorize_net/arb/response.rb +34 -0
  38. data/lib/authorize_net/arb/subscription.rb +72 -0
  39. data/lib/authorize_net/arb/transaction.rb +146 -0
  40. data/lib/authorize_net/authorize_net.rb +154 -0
  41. data/lib/authorize_net/cim/customer_profile.rb +19 -0
  42. data/lib/authorize_net/cim/payment_profile.rb +37 -0
  43. data/lib/authorize_net/cim/response.rb +110 -0
  44. data/lib/authorize_net/cim/transaction.rb +678 -0
  45. data/lib/authorize_net/customer.rb +27 -0
  46. data/lib/authorize_net/email_receipt.rb +24 -0
  47. data/lib/authorize_net/fields.rb +736 -0
  48. data/lib/authorize_net/key_value_response.rb +117 -0
  49. data/lib/authorize_net/key_value_transaction.rb +291 -0
  50. data/lib/authorize_net/line_item.rb +25 -0
  51. data/lib/authorize_net/order.rb +42 -0
  52. data/lib/authorize_net/payment_methods/credit_card.rb +74 -0
  53. data/lib/authorize_net/payment_methods/echeck.rb +72 -0
  54. data/lib/authorize_net/reporting/batch.rb +19 -0
  55. data/lib/authorize_net/reporting/batch_statistics.rb +19 -0
  56. data/lib/authorize_net/reporting/fds_filter.rb +11 -0
  57. data/lib/authorize_net/reporting/response.rb +127 -0
  58. data/lib/authorize_net/reporting/transaction.rb +116 -0
  59. data/lib/authorize_net/reporting/transaction_details.rb +25 -0
  60. data/lib/authorize_net/response.rb +27 -0
  61. data/lib/authorize_net/sim/hosted_payment_form.rb +38 -0
  62. data/lib/authorize_net/sim/hosted_receipt_page.rb +37 -0
  63. data/lib/authorize_net/sim/response.rb +142 -0
  64. data/lib/authorize_net/sim/transaction.rb +138 -0
  65. data/lib/authorize_net/transaction.rb +66 -0
  66. data/lib/authorize_net/xml_response.rb +172 -0
  67. data/lib/authorize_net/xml_transaction.rb +275 -0
  68. data/lib/generators/authorize_net/direct_post_generator.rb +51 -0
  69. data/lib/generators/authorize_net/sim_generator.rb +47 -0
  70. data/spec/aim_spec.rb +310 -0
  71. data/spec/arb_spec.rb +191 -0
  72. data/spec/authorize_net_spec.rb +200 -0
  73. data/spec/cim_spec.rb +450 -0
  74. data/spec/reporting_spec.rb +431 -0
  75. data/spec/sim_spec.rb +97 -0
  76. data/spec/spec.opts +5 -0
  77. data/spec/spec_helper.rb +2 -0
  78. data/uninstall.rb +1 -0
  79. metadata +223 -0
@@ -0,0 +1,8 @@
1
+ default:
2
+ api_login_id: <%= args[0] %>
3
+ api_transaction_key: <%= args[1] %>
4
+ merchant_hash_value: <%= args[2] %>
5
+
6
+ development:
7
+ test:
8
+ production:
@@ -0,0 +1,8 @@
1
+ default:
2
+ api_login_id: <%= self.api_login_id %>
3
+ api_transaction_key: <%= self.api_transaction_key %>
4
+ merchant_hash_value: <%= self.merchant_hash_value %>
5
+
6
+ development:
7
+ test:
8
+ production:
@@ -0,0 +1,21 @@
1
+ class <%= class_name %>Controller < ApplicationController
2
+
3
+ layout 'authorize_net'
4
+ helper :authorize_net
5
+ protect_from_forgery :except => :relay_response
6
+
7
+ # GET
8
+ # Displays a payment form.
9
+ def payment
10
+ @amount = 10.00
11
+ @sim_transaction = AuthorizeNet::SIM::Transaction.new(AUTHORIZE_NET_CONFIG['api_login_id'], AUTHORIZE_NET_CONFIG['api_transaction_key'], @amount, :hosted_payment_form => true)
12
+ @sim_transaction.set_hosted_payment_receipt(AuthorizeNet::SIM::HostedReceiptPage.new(:link_method => AuthorizeNet::SIM::HostedReceiptPage::LinkMethod::GET, :link_text => 'Continue', :link_url => <%= singular_name %>_thank_you_url(:only_path => false)))
13
+ end
14
+
15
+ # GET
16
+ # Displays a thank you page.
17
+ def thank_you
18
+ @auth_code = params[:x_auth_code]
19
+ end
20
+
21
+ end
@@ -0,0 +1,4 @@
1
+ yml = YAML.load_file("#{RAILS_ROOT}/config/authorize_net.yml")
2
+ AUTHORIZE_NET_CONFIG = yml['default']
3
+ AUTHORIZE_NET_CONFIG.merge!(yml[RAILS_ENV]) unless yml[RAILS_ENV].nil?
4
+ AUTHORIZE_NET_CONFIG.freeze
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>SIM Sample App</title>
5
+ <style>
6
+ body {
7
+ text-align: center;
8
+ font-family: Helvetica;
9
+ }
10
+ label {
11
+ display: block;
12
+ }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <%= yield %>
17
+ </body>
18
+ </html>
@@ -0,0 +1,6 @@
1
+ <% form_for :sim_transaction, :url => AuthorizeNet::SIM::Transaction::Gateway::TEST do |f| %>
2
+ <%= sim_fields(@sim_transaction) %>
3
+ <%= label_tag 'x_amount', "Total: #{number_to_currency(@amount)}" %>
4
+ <br />
5
+ <%= f.submit 'Purchase'%>
6
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <%= form_for :sim_transaction, :url => AuthorizeNet::SIM::Transaction::Gateway::TEST do |f| %>
2
+ <%= sim_fields(@sim_transaction) %>
3
+ <%= label_tag 'x_amount', "Total: #{number_to_currency(@amount)}" %>
4
+ <br />
5
+ <%= f.submit 'Purchase'%>
6
+ <% end %>
@@ -0,0 +1 @@
1
+ Thanks for your purchase! Your authorization code is <%=h @auth_code %>.
@@ -0,0 +1,75 @@
1
+ # Extends the Rails::Generator::Commands classes to add support for route and gem configuration generation.
2
+
3
+ module Rails
4
+ module Generator
5
+ module Commands
6
+
7
+ class Base
8
+ def route_code(route_options)
9
+ route_options[:path] = route_options[:name] unless route_options.has_key? :path
10
+ code = "map.#{route_options[:name]} '#{route_options[:path]}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
11
+ code += ", :conditions => #{route_options[:conditions]}" if route_options.has_key? :conditions
12
+ end
13
+
14
+ def gsub_file_check(relative_destination, regexp, *args, &block)
15
+ path = destination_path(relative_destination)
16
+ content = File.read(path)
17
+ !(content =~ regexp).nil?
18
+ end
19
+
20
+ def gem_code(gem_options)
21
+ gem_options[:lib] = gem_options[:name] unless gem_options.has_key? :lib
22
+ code = "config.gem '#{gem_options[:name]}', :lib => '#{gem_options[:lib]}'"
23
+ end
24
+ end
25
+
26
+ # Here's a readable version of the long string used above in route_code;
27
+ # but it should be kept on one line to avoid inserting extra whitespace
28
+ # into routes.rb when the generator is run:
29
+ # "map.#{route_options[:name]} '#{route_options[:name]}',
30
+ # :controller => '#{route_options[:controller]}',
31
+ # :action => '#{route_options[:action]}'"
32
+
33
+ class Create
34
+ def route(route_options)
35
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
36
+ unless gsub_file_check 'config/routes.rb', /(#{Regexp.escape(route_code(route_options))})/mi
37
+ logger.route route_code(route_options)
38
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |m|
39
+ "#{m}\n #{route_code(route_options)}"
40
+ end
41
+ else
42
+ logger.identical route_code(route_options)
43
+ end
44
+ end
45
+
46
+ def gem(gem_options)
47
+ sentinel = 'Rails::Initializer.run do |config|'
48
+ unless gsub_file_check 'config/environment.rb', /(#{Regexp.escape(gem_code(gem_options))})/mi
49
+ logger.gem gem_code(gem_options)
50
+ gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |m|
51
+ "#{m}\n #{gem_code(gem_options)}"
52
+ end
53
+ else
54
+ logger.identical gem_code(gem_options)
55
+ end
56
+ end
57
+ end
58
+
59
+ class Destroy
60
+ def route(route_options)
61
+ logger.remove_route route_code(route_options)
62
+ to_remove = "\n #{Regexp.escape(route_code(route_options))}"
63
+ gsub_file 'config/routes.rb', /(#{to_remove})/mi, ''
64
+ end
65
+
66
+ def gem(gem_options)
67
+ logger.remove_gem gem_code(gem_options)
68
+ to_remove = "\n #{Regexp.escape(gem_code(gem_options))}"
69
+ gsub_file 'config/environment.rb', /(#{to_remove})/mi, ''
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'authorize_net'
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,24 @@
1
+ # The Authorize.Net Rails Helper module. Provides methods to assist with integrating the various APIs.
2
+
3
+ module AuthorizeNetHelper
4
+
5
+ # Generates a collection of hidden form fields (as a raw HTML string) for a AuthorizeNet::SIM::Transaction
6
+ # (sim_transaction). You can specify any html_options that hidden_field_tag accepts, and the
7
+ # hidden fields will be built with those options.
8
+ def sim_fields(sim_transaction, html_options = {})
9
+ fields = sim_transaction.form_fields.collect do |k, v|
10
+ if v.kind_of? Array
11
+ v.collect { |val| hidden_field_tag(k, val, html_options) }
12
+ else
13
+ hidden_field_tag(k, v, html_options)
14
+ end
15
+ end
16
+ fields.flatten!
17
+ field_str = fields.join("\n")
18
+ if field_str.respond_to?(:html_safe)
19
+ return field_str.html_safe
20
+ else
21
+ return field_str
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ # This file is just here to avoid obnoxious gem name/require name issues. All this
2
+ # file does is require authorize_net.rb, the real initialization file.
3
+
4
+ require 'authorize_net'
@@ -0,0 +1,92 @@
1
+ # The SDK initialization enters here. Loads all needed libraries and files. Inspects
2
+ # the current runtime to see if Rails is present. If it is, we inject our helper into
3
+ # ActiveSupport.
4
+
5
+ require "cgi"
6
+ require "net/https"
7
+ require "uri"
8
+ require "openssl"
9
+ require 'bigdecimal'
10
+ require 'nokogiri'
11
+ require 'date'
12
+
13
+ # TODO: Add local data validation where possible
14
+
15
+ $:.unshift File.dirname(__FILE__)
16
+
17
+ require "authorize_net/authorize_net"
18
+ require "authorize_net/payment_methods/credit_card"
19
+ require "authorize_net/payment_methods/echeck"
20
+ require "authorize_net/addresses/address"
21
+ require "authorize_net/addresses/shipping_address"
22
+ require "authorize_net/customer"
23
+ require "authorize_net/email_receipt"
24
+ require "authorize_net/order"
25
+ require "authorize_net/line_item"
26
+ require "authorize_net/cim/payment_profile"
27
+ require "authorize_net/cim/customer_profile"
28
+ require "authorize_net/reporting/batch"
29
+ require "authorize_net/reporting/batch_statistics"
30
+ require "authorize_net/reporting/transaction_details"
31
+ require "authorize_net/reporting/fds_filter"
32
+ require "authorize_net/response"
33
+ require "authorize_net/key_value_response"
34
+ require "authorize_net/xml_response"
35
+ require "authorize_net/transaction"
36
+ require "authorize_net/key_value_transaction"
37
+ require "authorize_net/xml_transaction"
38
+ require "authorize_net/fields"
39
+
40
+ # AIM
41
+
42
+ require "authorize_net/aim/transaction"
43
+ require "authorize_net/aim/response"
44
+
45
+ # SIM
46
+
47
+ require "authorize_net/sim/hosted_payment_form"
48
+ require "authorize_net/sim/hosted_receipt_page"
49
+ require "authorize_net/sim/transaction"
50
+ require "authorize_net/sim/response"
51
+
52
+ # ARB
53
+
54
+ require "authorize_net/arb/subscription"
55
+ require "authorize_net/arb/response"
56
+ require "authorize_net/arb/transaction"
57
+
58
+ # CIM
59
+
60
+ require "authorize_net/cim/response"
61
+ require "authorize_net/cim/transaction"
62
+
63
+ # Reporting
64
+
65
+ require "authorize_net/reporting/response"
66
+ require "authorize_net/reporting/transaction"
67
+
68
+ # Load our Rails plugin
69
+
70
+ if defined?(Rails)
71
+ if defined?(Rails::Railtie)
72
+ module AuthorizeNet
73
+ class Railtie < Rails::Railtie
74
+ initializer "authorize_net.load_path_initialize" do |app|
75
+ %w{ models controllers helpers }.each do |dir|
76
+ path = File.join(File.dirname(__FILE__), 'app', dir)
77
+ $LOAD_PATH << path
78
+ ActiveSupport::Dependencies.autoload_paths << path
79
+ ActiveSupport::Dependencies.autoload_once_paths.delete(path)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ else
85
+ %w{ models controllers helpers }.each do |dir|
86
+ path = File.join(File.dirname(__FILE__), 'app', dir)
87
+ $LOAD_PATH << path
88
+ ActiveSupport::Dependencies.load_paths << path
89
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,29 @@
1
+ module AuthorizeNet
2
+
3
+ # Models an address.
4
+ class Address
5
+
6
+ include AuthorizeNet::Model
7
+
8
+ attr_accessor :first_name, :last_name, :company, :street_address, :city, :state, :zip, :country, :phone, :fax, :customer_address_id
9
+
10
+ def to_hash
11
+ hash = {
12
+ :first_name => @first_name,
13
+ :last_name => @last_name,
14
+ :company => @company,
15
+ :address => @street_address,
16
+ :city => @city,
17
+ :state => @state,
18
+ :zip => @zip,
19
+ :country => @country,
20
+ :phone => @phone,
21
+ :fax => @fax,
22
+ :customer_address_id => @customer_address_id
23
+ }
24
+ hash.delete_if {|k, v| v.nil?}
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,26 @@
1
+ module AuthorizeNet
2
+
3
+ # Models a shipping address.
4
+ class ShippingAddress < Address
5
+
6
+ include AuthorizeNet::Model
7
+
8
+ def to_hash
9
+ hash = {
10
+ :ship_to_first_name => @first_name,
11
+ :ship_to_last_name => @last_name,
12
+ :ship_to_company => @company,
13
+ :ship_to_address => @street_address,
14
+ :ship_to_city => @city,
15
+ :ship_to_state => @state,
16
+ :ship_to_zip => @zip,
17
+ :ship_to_country => @country,
18
+ :ship_to_phone => @phone,
19
+ :ship_to_fax => @fax
20
+ }
21
+ hash.delete_if {|k, v| v.nil?}
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,131 @@
1
+ module AuthorizeNet::AIM
2
+
3
+ # The AIM response class. Handles parsing the response from the gateway.
4
+ class Response < AuthorizeNet::KeyValueResponse
5
+
6
+ # Our MD5 digest generator.
7
+ @@digest = OpenSSL::Digest::Digest.new('md5')
8
+
9
+ include AuthorizeNet::AIM::Fields
10
+
11
+ # Fields to convert to/from booleans.
12
+ @@boolean_fields = [:tax_exempt]
13
+
14
+ # Fields to convert to/from BigDecimal.
15
+ @@decimal_fields = [:amount, :tax, :freight, :duty, :requested, :balance_on_card]
16
+
17
+
18
+ # Constructs a new response object from a +raw_response+ and the +transaction+ that generated
19
+ # the +raw_response+. You don't typically construct this object yourself, as AuthorizeNet::AIM::Transaction
20
+ # will build one for you when it makes the request to the gateway.
21
+ def initialize(raw_response, transaction)
22
+ @version = transaction.version
23
+ raise "AuthorizeNet gem only supports AIM version 3.1" unless @version.to_s == '3.1'
24
+ @raw_response = raw_response
25
+ @fields = {}
26
+ @transaction = transaction
27
+ custom_field_names = transaction.custom_fields.keys.collect(&:to_s).sort.collect(&:to_sym)
28
+ @custom_fields = {}
29
+ split_on = transaction.delimiter
30
+ if @raw_response.kind_of?(Net::HTTPOK) || @raw_response.kind_of?(Nokogiri::XML::Element)
31
+ if @raw_response.kind_of?(Net::HTTPOK)
32
+ raw_data = @raw_response.body
33
+ else
34
+ raw_data = @raw_response.text
35
+ end
36
+ unless transaction.encapsulation_character.nil?
37
+ split_on = transaction.encapsulation_character + split_on + transaction.encapsulation_character
38
+ raw_data = raw_data[1..raw_data.length - 2]
39
+ end
40
+ raw_data.split(split_on).each_with_index do |field, index|
41
+ if transaction.cp_version.nil?
42
+ field_desc = FIELDS
43
+ else
44
+ field_desc = CP_FIELDS
45
+ end
46
+ if index < field_desc.length
47
+ @fields[field_desc[index]] = field
48
+ else
49
+ @custom_fields[custom_field_names[index - field_desc.length]] = field
50
+ end
51
+ end
52
+ @fields.delete(nil)
53
+ @fields.each do |k, v|
54
+ if @@boolean_fields.include?(k)
55
+ @fields[k] = value_to_boolean(v)
56
+ elsif @@decimal_fields.include?(k)
57
+ @fields[k] = value_to_decimal(v)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ # Returns True if the MD5 hash found in the response payload validates using
64
+ # the supplied api_login and secret merchant_value (THIS IS NOT YOUR API KEY).
65
+ def valid_md5?(api_login, merchant_value)
66
+ if @fields[:md5_hash].nil?
67
+ return false
68
+ end
69
+ @@digest.hexdigest("#{merchant_value}#{api_login}#{@fields[:transaction_id]}#{@transaction.fields[:amount]}").downcase == @fields[:md5_hash].downcase
70
+ end
71
+
72
+ # Returns the current API version that we are adhering to.
73
+ def version
74
+ @version
75
+ end
76
+
77
+ # Check to see if the response indicated success. Success is defined as a 200 OK response indicating
78
+ # that the transaction was approved.
79
+ def success?
80
+ !connection_failure? && approved?
81
+ end
82
+
83
+ # Returns true if we failed to open a connection to the gateway or got back a non-200 OK HTTP response.
84
+ def connection_failure?
85
+ !@raw_response.kind_of?(Net::HTTPOK) && !@raw_response.kind_of?(Nokogiri::XML::Element)
86
+ end
87
+
88
+ # Returns the underlying Net::HTTPResponse object. This has the original response body along with
89
+ # headers and such. Note that if an exception is generated while making the request (which happens
90
+ # if there is no internet connection for example), you will get the exception object here instead of
91
+ # a Net::HTTPResponse object.
92
+ def raw
93
+ @raw_response
94
+ end
95
+
96
+ # Returns the AuthorizeNet::Transaction instance that owns this response.
97
+ def transaction
98
+ @transaction
99
+ end
100
+
101
+ # Returns the transaction's authorization code. This should be shown to the
102
+ # end user.
103
+ def authorization_code
104
+ @fields[:authorization_code]
105
+ end
106
+
107
+ # Returns the transaction's authorization id. You will need this for future void, refund
108
+ # and prior authorization capture requests.
109
+ def transaction_id
110
+ @fields[:transaction_id]
111
+ end
112
+
113
+ # Returns the customer id from the response.
114
+ def customer_id
115
+ @fields[:customer_id]
116
+ end
117
+
118
+ # Returns a response code (from AVSResponseCode) indicating the result of any Address Verification
119
+ # Service checks.
120
+ def avs_response
121
+ @fields[:avs_response]
122
+ end
123
+
124
+ # Returns the credit card type used in the transaction. The values returned can be found in CardType.
125
+ def card_type
126
+ @fields[:card_type]
127
+ end
128
+
129
+ end
130
+
131
+ end