Skip to content

Commit

Permalink
initial import from 1.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Van Dyk committed Mar 16, 2011
0 parents commit 953614c
Show file tree
Hide file tree
Showing 78 changed files with 6,765 additions and 0 deletions.
Binary file added License.pdf
Binary file not shown.
124 changes: 124 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
= Authorize.Net Ruby SDK

The Authorize.Net Ruby SDK is meant to offer an alternate object-oriented
model of development with the Authorize.net APIs (Version 3.1). The SDK is
based entirely off the name-value pair API, but performs the core payment
activities (such as error handling/parsing, network communication, and data
encoding) behind the scenes. Providing the end developer with this allows the
developer to start integrating immediately without having to write out a mass
of boiler plate code.

== Prerequisites

* Ruby 1.8.7 or higher
* Nokogiri 1.4.3 or higher
* RubyGem 1.3.7 or higher (to build the gem)
* RDoc 1.0 or higher (to build documentation)
* Rake 0.8.7 or higher (to use the rake tasks)

== Installation

Goto the Authorize.Net SDK download page and download the Ruby gem. Then

> gem install authorize-net-1.5.1.gem

== Usage

=== Advanced Merchant Integration (AIM)

require 'rubygems'
require 'authorize_net'

transaction = AuthorizeNet::AIM::Transaction.new('API_LOGIN', 'API_KEY', :gateway => :sandbox)
credit_card = AuthorizeNet::CreditCard.new('4111111111111111', '1120')
response = transaction.purchase('10.00', credit_card)

if response.success?
puts "Successfully made a purchase (authorization code: #{response.authorization_code})"
else
raise "Failed to make purchase."
end

=== Direct Post Method (DPM)

A generator is provided to aid in setting up a Direct Post Method application. In the example below +payments+ is the name of the controller to generate.

> sudo gem install rails -v '~> 2.1'
> rails my_direct_post_app
> cd my_direct_post_app
> script/generate authorize_net_direct_post payments YOUR_API_LOGIN_ID YOUR_TRANSACTION_KEY MERCH_HASH_KEY
> script/server

After running the generator you will probably want to customize the payment form found in <tt>app/views/payments/payment.erb</tt> and the receipt found in <tt>app/views/payments/receipt.erb</tt>.

There is also a default layout generated, <tt>app/views/layouts/authorize_net.erb</tt>. If you already have your own layout, you can delete that file and the reference to it in the controller (<tt>app/controllers/payments_controller.rb</tt>).

=== Server Integration Method (SIM)

A generator is provided to aid in setting up a Server Integration Method application. In the example below +payments+ is the name of the controller to generate.

> sudo gem install rails -v '~> 2.1'
> rails my_sim_app
> cd my_sim_app
> script/generate authorize_net_sim payments YOUR_API_LOGIN_ID YOUR_TRANSACTION_KEY MERCH_HASH_KEY
> script/server

After running the generator you will probably want to customize the payment page found in <tt>app/views/payments/payment.erb</tt> and the thank you page found in <tt>app/views/payments/thank_you.erb</tt>.

You may also want to customize the actual payment form and receipt pages. That can be done by making the necessary changes to the AuthorizeNet::SIM::Transaction object (<tt>@sim_transaction</tt>) found in the +payment+ action in <tt>app/controllers/payments_controller.rb</tt>. The styling of those hosted pages are controlled by the AuthorizeNet::SIM::HostedReceiptPage and AuthorizeNet::SIM::HostedPaymentForm objects (which are passed to the AuthorizeNet::SIM::Transaction).

There is also a default layout generated, <tt>app/views/layouts/authorize_net.erb</tt>. If you already have your own layout, you can delete that file and the reference to it in the controller (<tt>app/controllers/payments_controller.rb</tt>).

=== Automated Recurring Billing (ARB)

require 'rubygems'
require 'authorize_net'

transaction = AuthorizeNet::ARB::Transaction.new('API_LOGIN', 'API_KEY', :gateway => :sandbox)
subscription = AuthorizeNet::ARB::Subscription.new(
:name => "Monthly Gift Basket",
:length => 1,
:unit => :month,
:start_date => Date.today,
:total_occurrences => :unlimited,
:amount => 100.00,
:invoice_number => '1234567',
:description => "John Doe's Monthly Gift Basket",
:credit_card => AuthorizeNet::CreditCard.new('4111111111111111', '1120'),
:billing_address => AuthorizeNet::Address.new(:first_name => 'John', :last_name => 'Doe')
)
response = transaction.create(subscription)

if response.success?
puts "Successfully created a subscription (subscription id: #{response.subscription_id})"
else
raise "Failed to create a subscription."
end

=== Card Present (CP)

require 'rubygems'
require 'authorize_net'

transaction = AuthorizeNet::AIM::Transaction.new('API_LOGIN', 'API_KEY', :gateway => :card_present_sandbox)
credit_card = AuthorizeNet::CreditCard.new(nil, nil, :track_1 => '%B4111111111111111^DOE/JOHN^1803101000000000020000831000000?')
response = transaction.purchase('10.00', credit_card)

if response.success?
puts "Successfully made a purchase (authorization code: #{response.authorization_code})"
else
raise "Failed to make purchase."
end

== Credit Card Test Numbers

For your reference, you can use the following test credit card numbers.
The expiration date must be set to the present date or later. Use 123 for
the CCV code.

American Express:: 370000000000002
Discover:: 6011000000000012
Visa:: 4007000000027
JCB:: 3088000000000017
Diners Club/ Carte Blanche:: 38000000000006
Visa (Card Present Track 1):: %B4111111111111111^DOE/JOHN^1803101000000000020000831000000?
74 changes: 74 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require "rake"
require "spec/rake/spectask"

task :default => :spec
desc "Run all specs"
Spec::Rake::SpecTask.new('spec') do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = ['--options', 'spec/spec.opts']
end

namespace "spec" do
desc "Run the CIM spec"
Spec::Rake::SpecTask.new('cim') do |t|
t.spec_files = FileList['spec/cim_spec.rb']
t.spec_opts = ['--options', 'spec/spec.opts']
end

desc "Run the ARB spec"
Spec::Rake::SpecTask.new('arb') do |t|
t.spec_files = FileList['spec/arb_spec.rb']
t.spec_opts = ['--options', 'spec/spec.opts']
end

desc "Run the AIM spec"
Spec::Rake::SpecTask.new('aim') do |t|
t.spec_files = FileList['spec/aim_spec.rb']
t.spec_opts = ['--options', 'spec/spec.opts']
end

desc "Run the SIM spec"
Spec::Rake::SpecTask.new('sim') do |t|
t.spec_files = FileList['spec/sim_spec.rb']
t.spec_opts = ['--options', 'spec/spec.opts']
end

desc "Run the Reporting spec"
Spec::Rake::SpecTask.new('reporting') do |t|
t.spec_files = FileList['spec/reporting_spec.rb']
t.spec_opts = ['--options', 'spec/spec.opts']
end
end

desc "Builds the gem"
task :gem do
%x"gem build authorize-net.gemspec"
end

desc "Builds the documentation"
task :doc do
%x"rdoc -U -S --main=README.rdoc -A documented_accessor=RW README.rdoc lib/"
end

namespace "doc" do
desc "Builds the documentation with graphical class hierarchy"
task :graph do
%x"rdoc -U -d -S --main=README.rdoc -A documented_accessor=RW README.rdoc lib/"
end
end

desc "Builds the documentation (alias of :doc)"
task :rdoc do
Rake::Task[:doc].execute
end

desc "Bundles the sample app."
task :samples do
%x". sample_app_version && zip -r anet_ruby_samples-$VERSION.zip sample_app -x '*/.*' -x '*/Icon' -x '*/__MACOSX'"
end

desc "Bundles the sample app and gem."
task :bundle do
Rake::Task[:samples].execute
Rake::Task[:gem].execute
end
20 changes: 20 additions & 0 deletions generators/authorize_net_direct_post/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Description:
Generates a simple implementation of Authorize.Net's Direct Post Method integration method.

Example:
./script/generate authorize_net_direct_post payments API_LOGIN_ID API_TRANSACTION_KEY MERCHANT_HASH_VALUE

This will create:
README-AuthorizeNet
app/views/payments
app/views/payments/payment.erb
app/views/payments/receipt.erb
app/views/payments/relay_response.erb
app/views/layouts/authorize_net.erb
config/authorize_net.yml
config/initializers/authorize_net.rb
app/controller/payments_controller.rb
route payment_payment
route payment_receipt
route payment_relay_response
gem authorize-net
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require File.join(File.dirname(__FILE__), '..', 'generator_extensions.rb')

class AuthorizeNetDirectPostGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
m.file "README-AuthorizeNet", "README-AuthorizeNet"
m.directory "app/views/#{file_name}"
m.file "payment.erb", "app/views/#{file_name}/payment.erb"
m.file "receipt.erb", "app/views/#{file_name}/receipt.erb"
m.file "relay_response.erb", "app/views/#{file_name}/relay_response.erb"
m.file "layout.erb", "app/views/layouts/authorize_net.erb"
m.template "config.yml.erb", "config/authorize_net.yml"
m.file "initializer.rb", "config/initializers/authorize_net.rb"
m.template "controller.rb.erb", "app/controllers/#{file_name}_controller.rb"
m.route :name => "#{singular_name}_receipt", :path => "/#{plural_name}/receipt", :controller => file_name, :action => 'receipt', :conditions => "{:method => :get}"
m.route :name => "#{singular_name}_relay_response", :path => "/#{plural_name}/relay_response", :controller => file_name, :action => 'relay_response', :conditions => "{:method => :post}"
m.route :name => "#{singular_name}_payment", :path => "/#{plural_name}/payment", :controller => file_name, :action => 'payment', :conditions => "{:method => :get}"
m.gem :name => 'authorize-net'
end
end
end
49 changes: 49 additions & 0 deletions generators/authorize_net_direct_post/templates/README-AuthorizeNet
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
= Authorize.Net Ruby SDK

The Authorize.Net Ruby SDK is meant to offer an alternate object-oriented
model of development with the Authorize.net APIs (Version 3.1). The SDK is
based entirely off the name-value pair API, but performs the core payment
activities (such as error handling/parsing, network communication, and data
encoding) behind the scenes. Providing the end developer with this allows the
developer to start integrating immediately without having to write out a mass
of boiler plate code.

== Prerequisites

* Ruby 1.8.7 or higher
* RubyGem 1.3.7 or higher (to build the gem)
* RDoc 1.0 or higher (to build documentation)
* Rake 0.8.7 or higher (to use the rake tasks)

== Installation

Goto the Authorize.Net SDK download page and download the Ruby gem. Then

> gem install authorize-net-1.0.0.gem

== Usage

A generator is provided to aid in setting up a Direct Post Method application. In the example below 'payments' is the name of the controller to generate.

> sudo gem install rails -v '~> 2.1'
> rails my_direct_post_app
> cd my_direct_post_app
> script/generate authorize_net_direct_post payments YOUR_API_LOGIN_ID YOUR_TRANSACTION_KEY MERCH_HASH_KEY
> script/server

After running the generator you will probably want to customize the payment form found in 'app/views/payments/payment.erb' and the receipt found in 'app/views/payments/receipt.erb'.

There is also a default layout generated, 'app/views/layouts/authorize_net.erb'. If you already have your own layout, you can delete that file and the reference to it in the controller ('app/controllers/payments_controller.rb').


== Credit Card Test Numbers

For your reference, you can use the following test credit card numbers.
The expiration date must be set to the present date or later. Use 123 for
the CCV code.

American Express:: 370000000000002
Discover:: 6011000000000012
Visa:: 4007000000027
JCB:: 3088000000000017
Diners Club/ Carte Blanche:: 38000000000006
8 changes: 8 additions & 0 deletions generators/authorize_net_direct_post/templates/config.yml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
default:
api_login_id: <%= args[0] %>
api_transaction_key: <%= args[1] %>
merchant_hash_value: <%= args[2] %>

development:
test:
production:
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
default:
api_login_id: <%= self.api_login_id %>
api_transaction_key: <%= self.api_transaction_key %>
merchant_hash_value: <%= self.merchant_hash_value %>

development:
test:
production:
31 changes: 31 additions & 0 deletions generators/authorize_net_direct_post/templates/controller.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class <%= class_name %>Controller < ApplicationController

layout 'authorize_net'
helper :authorize_net
protect_from_forgery :except => :relay_response

# GET
# Displays a payment form.
def payment
@amount = 10.00
@sim_transaction = AuthorizeNet::SIM::Transaction.new(AUTHORIZE_NET_CONFIG['api_login_id'], AUTHORIZE_NET_CONFIG['api_transaction_key'], @amount, :relay_url => <%= singular_name %>_relay_response_url(:only_path => false))
end

# POST
# Returns relay response when Authorize.Net POSTs to us.
def relay_response
sim_response = AuthorizeNet::SIM::Response.new(params)
if sim_response.success?(AUTHORIZE_NET_CONFIG['api_login_id'], AUTHORIZE_NET_CONFIG['merchant_hash_value'])
render :text => sim_response.direct_post_reply(<%= singular_name %>_receipt_url(:only_path => false), :include => true)
else
render
end
end

# GET
# Displays a receipt.
def receipt
@auth_code = params[:x_auth_code]
end

end
4 changes: 4 additions & 0 deletions generators/authorize_net_direct_post/templates/initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
yml = YAML.load_file("#{RAILS_ROOT}/config/authorize_net.yml")
AUTHORIZE_NET_CONFIG = yml['default']
AUTHORIZE_NET_CONFIG.merge!(yml[RAILS_ENV]) unless yml[RAILS_ENV].nil?
AUTHORIZE_NET_CONFIG.freeze
18 changes: 18 additions & 0 deletions generators/authorize_net_direct_post/templates/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Direct Post Sample App</title>
<style>
body {
text-align: center;
font-family: Helvetica;
}
label {
display: block;
}
</style>
</head>
<body>
<%= yield %>
</body>
</html>
10 changes: 10 additions & 0 deletions generators/authorize_net_direct_post/templates/payment.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% form_for :sim_transaction, :url => AuthorizeNet::SIM::Transaction::Gateway::TEST do |f| %>
<%= sim_fields(@sim_transaction) %>
<%= label_tag 'x_amount', "Total: #{number_to_currency(@amount)}" %>
<%= label_tag 'x_card_num', 'Credit Card Number'%>
<%= text_field_tag 'x_card_num' %>
<%= label_tag 'x_exp_date', 'Expiration Date (MMYY)'%>
<%= text_field_tag 'x_exp_date' %>
<br />
<%= f.submit 'Purchase'%>
<% end %>
10 changes: 10 additions & 0 deletions generators/authorize_net_direct_post/templates/payment.rails3.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= form_for :sim_transaction, :url => AuthorizeNet::SIM::Transaction::Gateway::TEST do |f| %>
<%= sim_fields(@sim_transaction) %>
<%= label_tag 'x_amount', "Total: #{number_to_currency(@amount)}" %>
<%= label_tag 'x_card_num', 'Credit Card Number'%>
<%= text_field_tag 'x_card_num' %>
<%= label_tag 'x_exp_date', 'Expiration Date (MMYY)'%>
<%= text_field_tag 'x_exp_date' %>
<br />
<%= f.submit 'Purchase'%>
<% end %>
1 change: 1 addition & 0 deletions generators/authorize_net_direct_post/templates/receipt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Thanks for your purchase! Your authorization code is <%=h @auth_code %>.
Loading

0 comments on commit 953614c

Please sign in to comment.