Skip to content

mobilecause/authorize.net-sdk-ruby

 
 

Repository files navigation

Authorize.Net Ruby SDK

Build Status

gem install authorizenet

Prerequisites

  • Ruby 1.9.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)
  • Bundle 1.6 or higher
  • RSpec 2.1 or higher (to run rspec tests)

Installation from rubygems.org

  > sudo gem install authorizenet

Installation from project

  > bundle install
  > rake gem
  > sudo gem install ./authorizenet-1.8.3.gem

Registration & Configuration

Get a sandbox account at https://developer.authorize.net/sandbox/ To run rspec tests, create a spec/credentials.yml with the following keys and the values obtained as described below.

#obtain an API login_id and transaction_id according to instructions at https://developer.authorize.net/faqs/#gettranskey
api_login_id: {login_id_value}
api_transaction_key: {transaction_key_value}
#obtained md5 hash value by first setting the hash value in https://sandbox.authorize.net/ under the Account tab->MD5 Hash
md5_value: {md5_value}

Running the Tests

To run the integration tests (hitting the sandbox):

rake spec

To run the unit tests:

rake spec:ci

To get spec/reporting_spec.rb to pass, go to https://sandbox.authorize.net/ under Account tab->Transaction Details API and enable it.

Usage

Advanced Merchant Integration (AIM)

require 'rubygems'
  require 'authorizenet'

  include AuthorizeNet::API

  transaction = Transaction.new('API_LOGIN', 'API_KEY', :gateway => :sandbox)

  request = CreateTransactionRequest.new

  request.transactionRequest = TransactionRequestType.new()
  request.transactionRequest.amount = 16.00
  request.transactionRequest.payment = PaymentType.new
  request.transactionRequest.payment.creditCard = CreditCardType.new('4242424242424242','0220','123') 
  request.transactionRequest.transactionType = TransactionTypeEnum::AuthCaptureTransaction
  
  response = transaction.create_transaction(request)

  if response.messages.resultCode == MessageTypeEnum::Ok
    puts "Successful charge (auth + capture) (authorization code: #{response.transactionResponse.authCode})"

  else
    puts response.messages.messages[0].text
    puts response.transactionResponse.errors.errors[0].errorCode
    puts response.transactionResponse.errors.errors[0].errorText
    raise "Failed to charge card."
  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 '>= 3.2'
  > sudo gem install authorizenet
  > rails new my_direct_post_app
  > cd my_direct_post_app

Edit Gemfile and add the line "gem 'authorizenet'"

  > rails g authorize_net:direct_post payments YOUR_API_LOGIN_ID YOUR_TRANSACTION_KEY MERCH_HASH_KEY
  > rails 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).

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 '>= 3.2'
  > sudo gem install authorizenet
  > rails new my_sim_app
  > cd my_sim_app

Edit Gemfile and add the line "gem 'authorizenet'"

  > rails g authorize_net:sim payments YOUR_API_LOGIN_ID YOUR_TRANSACTION_KEY MERCH_HASH_KEY
  > rails server

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

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 (@sim_transaction) found in the +payment+ action in app/controllers/payments_controller.rb. 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, 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).

Automated Recurring Billing (ARB)

  require 'rubygems'
  require 'authorizenet'
  
  include AuthorizeNet::API
  
  transaction = Transaction.new('API_LOGIN', 'API_KEY', :gateway => :sandbox)
  
  request = ARBCreateSubscriptionRequest.new
  request.refId = '2238251169'
  request.subscription = ARBSubscriptionType.new
  request.subscription.name = "Jane Doe"
  request.subscription.paymentSchedule = PaymentScheduleType.new
  request.subscription.paymentSchedule.interval = PaymentScheduleType::Interval.new("3","months")
  request.subscription.paymentSchedule.startDate = '2017-08-30'
  request.subscription.paymentSchedule.totalOccurrences ='12'
  request.subscription.paymentSchedule.trialOccurrences ='1'
  
  request.subscription.amount = 0.09
  request.subscription.trialAmount = 0.00
  request.subscription.payment = PaymentType.new
  request.subscription.payment.creditCard = CreditCardType.new('4111111111111111','0120','123')
  
  request.subscription.order = OrderType.new('invoiceNumber123','description123')
  request.subscription.customer =  CustomerDataType.new(CustomerTypeEnum::Individual,'custId1','[email protected]')
  request.subscription.billTo = NameAndAddressType.new('John','Doe','xyt','10800 Blue St','New York','NY','10010','USA')
  request.subscription.shipTo = NameAndAddressType.new('John','Doe','xyt','10800 Blue St','New York','NY','10010','USA')
  
  response = transaction.create_subscription(request)
    


  if response != nil
    if response.messages.resultCode == MessageTypeEnum::Ok
      puts "Successfully created a subscription #{response.subscriptionId}"
  
    else
      puts response.messages.messages[0].code
      puts response.messages.messages[0].text
      raise "Failed to create a subscription"
    end
  end

Card Present (CP)

  require 'rubygems'
  require 'authorizenet'

  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?

New Model

We’re exploring a new model of maintaining the SDKs which allows us to be more responsive to API changes. This model is consistent across the different SDK languages, which is great for us, however we do not want to sacrifice your productivity by losing the inherent efficiencies in the Ruby language. To this end we’re introducing the new model as purely “experimental” at this time and we would appreciate your feedback. Let us know what you really think! Here’s an example of a server side call with ApplePay data in the new model.

Apple Pay Example

  require 'rubygems'
  require 'authorizenet'

  include AuthorizeNet::API

  transaction = Transaction.new('5KP3u95bQpv', '4Ktq966gC55GAX7S', :gateway => :sandbox)
  request = CreateTransactionRequest.new
  request.transactionRequest = TransactionRequestType.new
  request.transactionRequest.amount = 39.55

  request.transactionRequest.payment = PaymentType.new
  request.transactionRequest.payment.opaqueData = OpaqueDataType.new('COMMON.APPLE.INAPP.PAYMENT','eyJkYXRhIjoiQkRQTldTdE1tR2V3UVVXR2c0bzdFXC9qKzFjcTFUNzhxeVU4NGI2N2l0amNZSTh3UFlBT2hzaGpoWlBycWRVcjRYd1BNYmo0emNHTWR5KysxSDJWa1BPWStCT01GMjV1YjE5Y1g0bkN2a1hVVU9UakRsbEIxVGdTcjhKSFp4Z3A5ckNnc1NVZ2JCZ0tmNjBYS3V0WGY2YWpcL284WkliS25yS1E4U2gwb3VMQUtsb1VNbit2UHU0K0E3V0tycXJhdXo5SnZPUXA2dmhJcStIS2pVY1VOQ0lUUHlGaG1PRXRxK0grdzB2UmExQ0U2V2hGQk5uQ0hxenpXS2NrQlwvMG5xTFpSVFliRjBwK3Z5QmlWYVdIZWdoRVJmSHhSdGJ6cGVjelJQUHVGc2ZwSFZzNDhvUExDXC9rXC8xTU5kNDdrelwvcEhEY1JcL0R5NmFVTStsTmZvaWx5XC9RSk4rdFMzbTBIZk90SVNBUHFPbVhlbXZyNnhKQ2pDWmxDdXcwQzltWHpcL29iSHBvZnVJRVM4cjljcUdHc1VBUERwdzdnNjQybTRQendLRitIQnVZVW5lV0RCTlNEMnU2amJBRzMiLCJ2ZXJzaW9uIjoiRUNfdjEiLCJoZWFkZXIiOnsiYXBwbGljYXRpb25EYXRhIjoiOTRlZTA1OTMzNWU1ODdlNTAxY2M0YmY5MDYxM2UwODE0ZjAwYTdiMDhiYzdjNjQ4ZmQ4NjVhMmFmNmEy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dNQWJ3QnRBaEJjbCtQZjMrVTRwazEzblZEOW53UVFNQWtHQlNzT0F3SWFCUUF3RFFZSktvWklodmNOQVFFQkJRQUVnWUJhSzNFbE9zdGJIOFdvb3NlREFCZitKZ1wvMTI5SmNJYXdtN2M2VnhuN1phc05iQXEzdEF0OFB0eSt1UUNnc3NYcVprTEE3a3oyR3pNb2xOdHY5d1ltdTlVandhcjFQSFlTK0JcL29Hbm96NTkxd2phZ1hXUnowbk1vNXkzTzFLelgwZDhDUkhBVmE4OFNyVjFhNUpJaVJldjNvU3RJcXd2NXh1WmxkYWc2VHI4dz09In0=') 

  request.transactionRequest.transactionType = TransactionTypeEnum::AuthCaptureTransaction

  response = transaction.create_transaction(request)

  if response.messages.resultCode == MessageTypeEnum::Ok
    puts "Successfully made a purchase (authorization code: #{response.transactionResponse.authCode})"
  else
    raise "Failed to make purchase."
  end

Visa Checkout Example

Our code samples repository has sample code for sending a transaction with Visa Checkout payment data (https://github.com/AuthorizeNet/sample-code-ruby/blob/master/VisaCheckout/visacheckout-transaction.rb) and for decrypting Visa Checkout payment data (https://github.com/AuthorizeNet/sample-code-ruby/blob/master/VisaCheckout/visacheckout-decrypt.rb).

About

Ruby SDK for the Authorize.Net API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 99.1%
  • HTML 0.9%