|
1 | 1 | # Authorize.Net Ruby SDK |
2 | 2 |
|
3 | | -[](https://travis-ci.org/AuthorizeNet/sdk-ruby) |
4 | | -[](https://coveralls.io/github/AuthorizeNet/sdk-ruby?branch=master) |
| 3 | +[](https://travis-ci.org/AuthorizeNet/sdk-ruby) |
| 4 | +[](https://coveralls.io/github/AuthorizeNet/sdk-ruby?branch=master) |
5 | 5 | [](https://scrutinizer-ci.com/g/AuthorizeNet/sdk-ruby/?branch=master) |
6 | | -[](https://rubygems.org/gems/authorizenet) |
| 6 | +[](https://badge.fury.io/rb/authorizenet) |
7 | 7 |
|
8 | | -`gem install authorizenet` |
9 | 8 |
|
10 | | -## Prerequisites |
11 | | - |
12 | | -* Ruby 2.1.0 or higher |
| 9 | +## Requirements |
| 10 | +* Ruby 2.2.0 or higher |
13 | 11 | * RubyGem 1.3.7 or higher (to build the gem) |
14 | 12 | * RDoc 1.0 or higher (to build documentation) |
15 | 13 | * Rake 0.8.7 or higher (to use the rake tasks) |
16 | 14 | * Bundle 1.6 or higher |
17 | 15 | * RSpec 2.1 or higher (to run rspec tests) |
| 16 | +* An Authorize.Net account (see _Registration & Configuration_ section below) |
18 | 17 |
|
19 | | -## Installation from rubygems.org |
| 18 | +### TLS 1.2 |
| 19 | +The Authorize.Net APIs only support connections using the TLS 1.2 security protocol. It's important to make sure you have new enough versions of all required components to support TLS 1.2. Additionally, it's very important to keep these components up to date going forward to mitigate the risk of any security flaws that may be discovered in your system or any libraries it uses. |
20 | 20 |
|
21 | | -```` |
22 | | - > sudo gem install authorizenet |
23 | | -```` |
24 | | -## |
25 | 21 |
|
26 | | -# Installation from project |
| 22 | +## Installation |
27 | 23 |
|
28 | | -```` |
| 24 | +### Installation from rubygems.org |
| 25 | +``` |
| 26 | + > sudo gem install authorizenet |
| 27 | +``` |
| 28 | + |
| 29 | +### Installation from project |
| 30 | +``` |
29 | 31 | > bundle install |
30 | 32 | > rake gem |
31 | | - > sudo gem install ./authorizenet-1.9.0.gem |
32 | | -```` |
33 | | -## Registration & Configuration |
| 33 | + > sudo gem install ./authorizenet-1.9.3.gem |
| 34 | +``` |
34 | 35 |
|
35 | | -Get a sandbox account at https://developer.authorize.net/sandbox/ |
36 | | -To run rspec tests, create a spec/credentials.yml with the following keys and the values obtained as described below. |
37 | | -```` |
38 | | -#obtain an API login_id and transaction_id according to instructions at https://developer.authorize.net/faqs/#gettranskey |
39 | | -api_login_id: {login_id_value} |
40 | | -api_transaction_key: {transaction_key_value} |
41 | | -#obtained md5 hash value by first setting the hash value in https://sandbox.authorize.net/ under the Account tab->MD5 Hash |
42 | | -md5_value: {md5_value} |
43 | | -```` |
44 | | - |
45 | | -## Usage |
46 | | -Apart from this README, you can find details and examples of using the SDK in the following places: |
47 | | - |
48 | | -- [Github Sample Code Repository](https://github.com/AuthorizeNet/sample-code-ruby) |
49 | | -- [Developer Center Reference](http://developer.authorize.net/api/reference/index.html) |
50 | 36 |
|
51 | | -### Charging a Credit Card |
52 | | - |
53 | | -````ruby |
54 | | -require 'rubygems' |
55 | | - require 'authorizenet' |
56 | | - |
57 | | - include AuthorizeNet::API |
58 | | - |
59 | | - transaction = Transaction.new('API_LOGIN', 'API_KEY', :gateway => :sandbox) |
| 37 | +## Registration & Configuration |
| 38 | +Use of this SDK and the Authorize.Net APIs requires having an account on our system. You can find these details in the Settings section. |
| 39 | +If you don't currently have a production Authorize.Net account and need a sandbox account for testing, you can easily sign up for one [here](https://developer.authorize.net/sandbox/). |
60 | 40 |
|
61 | | - request = CreateTransactionRequest.new |
| 41 | +### Authentication |
| 42 | +To authenticate with the Authorize.Net API you will need to use your account's API Login ID and Transaction Key. If you don't have these values, you can obtain them from our Merchant Interface site. Access the Merchant Interface for production accounts at (https://account.authorize.net/) or sandbox accounts at (https://sandbox.authorize.net). |
62 | 43 |
|
63 | | - request.transactionRequest = TransactionRequestType.new() |
64 | | - request.transactionRequest.amount = 16.00 |
65 | | - request.transactionRequest.payment = PaymentType.new |
66 | | - request.transactionRequest.payment.creditCard = CreditCardType.new('4242424242424242','0220','123') |
67 | | - request.transactionRequest.transactionType = TransactionTypeEnum::AuthCaptureTransaction |
68 | | - |
69 | | - response = transaction.create_transaction(request) |
| 44 | +Once you have your keys simply load them into the appropriate variables in your code, as per the below sample code dealing with the authentication part of the API request. |
70 | 45 |
|
71 | | - if response.messages.resultCode == MessageTypeEnum::Ok |
72 | | - puts "Successful charge (auth + capture) (authorization code: #{response.transactionResponse.authCode})" |
| 46 | +#### To set your API credentials for an API request: |
| 47 | +```ruby |
| 48 | +transaction = Transaction.new('YOUR_API_LOGIN_ID', 'YOUR_TRANSACTION_KEY', :gateway => :sandbox) |
| 49 | +``` |
73 | 50 |
|
74 | | - else |
75 | | - puts response.messages.messages[0].text |
76 | | - puts response.transactionResponse.errors.errors[0].errorCode |
77 | | - puts response.transactionResponse.errors.errors[0].errorText |
78 | | - raise "Failed to charge card." |
79 | | - end |
80 | | -```` |
| 51 | +You should never include your Login ID and Transaction Key directly in a file that's in a publically accessible portion of your website. A better practice would be to define these in a constants file, and then reference those constants in the appropriate place in your code. |
81 | 52 |
|
82 | | -### Setting the Production Environment |
83 | | -Replace the environment constant in the transaction instantiation. For example, in the method above: |
| 53 | +#### Setting OAuth credentials |
| 54 | +Access Tokens can be setup using the transaction instantiation without the constructor. For example, in the method above: |
84 | 55 | ```ruby |
85 | | -transaction = Transaction.new('API_LOGIN', 'API_KEY', :gateway => :production) |
| 56 | +transaction = Transaction.new |
| 57 | +transaction.access_token = 'testTokenValue' |
| 58 | +transaction.options_OAuth = {:gateway => :sandbox, :verify_ssl => true} |
86 | 59 | ``` |
87 | 60 |
|
88 | | -### Direct Post Method (DPM) |
89 | | - |
90 | | -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. |
91 | | -```` |
92 | | - > sudo gem install rails -v '>= 3.2' |
93 | | - > sudo gem install authorizenet |
94 | | - > rails new my_direct_post_app |
95 | | - > cd my_direct_post_app |
96 | | -```` |
97 | | - Edit Gemfile and add the line "gem 'authorizenet'" |
98 | | -```` |
99 | | - > rails g authorize_net:direct_post payments YOUR_API_LOGIN_ID YOUR_TRANSACTION_KEY MERCH_HASH_KEY |
100 | | - > rails server |
101 | | -```` |
| 61 | +### Switching between the sandbox environment and the production environment |
| 62 | +Authorize.Net maintains a complete sandbox environment for testing and development purposes. This sandbox environment is an exact duplicate of our production environment with the transaction authorization and settlement process simulated. By default, this SDK is configured to communicate with the sandbox environment. To switch to the production environment, replace the environment constant in the transaction instantiation. For example: |
| 63 | +```ruby |
| 64 | +# For PRODUCTION use |
| 65 | +transaction = Transaction.new('YOUR_API_LOGIN_ID', 'YOUR_TRANSACTION_KEY', :gateway => :production) |
| 66 | +``` |
102 | 67 |
|
103 | | -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>. |
| 68 | +API credentials are different for each environment, so be sure to switch to the appropriate credentials when switching environments. |
104 | 69 |
|
105 | | -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>). |
106 | 70 |
|
107 | | -### Server Integration Method (SIM) |
| 71 | +## SDK Usage Examples and Sample Code |
| 72 | +To get started using this SDK, it's highly recommended to download our sample code repository: |
| 73 | +* [Authorize.Net Ruby Sample Code Repository (on GitHub)](https://github.com/AuthorizeNet/sample-code-ruby) |
108 | 74 |
|
109 | | -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. |
110 | | -```` |
111 | | - > sudo gem install rails -v '>= 3.2' |
112 | | - > sudo gem install authorizenet |
113 | | - > rails new my_sim_app |
114 | | - > cd my_sim_app |
115 | | -```` |
116 | | - Edit Gemfile and add the line "gem 'authorizenet'" |
117 | | -```` |
118 | | - > rails g authorize_net:sim payments YOUR_API_LOGIN_ID YOUR_TRANSACTION_KEY MERCH_HASH_KEY |
119 | | - > rails server |
120 | | -```` |
121 | | -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>. |
| 75 | +In that respository, we have comprehensive sample code for all common uses of our API: |
122 | 76 |
|
123 | | -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). |
| 77 | +Additionally, you can find details and examples of how our API is structured in our API Reference Guide: |
| 78 | +* [Developer Center API Reference](http://developer.authorize.net/api/reference/index.html) |
124 | 79 |
|
125 | | -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>). |
| 80 | +The API Reference Guide provides examples of what information is needed for a particular request and how that information would be formatted. Using those examples, you can easily determine what methods would be necessary to include that information in a request using this SDK. |
126 | 81 |
|
127 | 82 |
|
| 83 | +## Building & Testing the SDK |
128 | 84 |
|
129 | | -## Running the Tests |
| 85 | +### Running the SDK Tests |
130 | 86 | To run the integration tests (hitting the sandbox): |
131 | | -```` |
| 87 | +``` |
132 | 88 | rake spec |
133 | | -```` |
| 89 | +``` |
134 | 90 | To run the unit tests: |
135 | | -```` |
| 91 | +``` |
136 | 92 | rake spec:ci |
137 | | -```` |
| 93 | +``` |
138 | 94 |
|
139 | 95 | To get spec/reporting_spec.rb to pass, go to https://sandbox.authorize.net/ under Account tab->Transaction Details API and enable it. |
140 | 96 |
|
| 97 | +To run rspec tests, create a spec/credentials.yml with the following keys and the values obtained as described below. |
| 98 | +```ruby |
| 99 | +#obtain an API login_id and transaction_id according to instructions at https://developer.authorize.net/faqs/#gettranskey |
| 100 | +api_login_id: {login_id_value} |
| 101 | +api_transaction_key: {transaction_key_value} |
| 102 | +#obtained md5 hash value by first setting the hash value in https://sandbox.authorize.net/ under the Account tab->MD5 Hash |
| 103 | +md5_value: {md5_value} |
| 104 | +``` |
141 | 105 |
|
142 | | -## Credit Card Test Numbers |
143 | | - |
144 | | -For your reference, you can use the following test credit card numbers. |
145 | | -The expiration date must be set to the present date or later. Use 123 for |
146 | | -the CCV code. |
147 | | - |
148 | | -* American Express: 370000000000002 |
149 | | -* Discover: 6011000000000012 |
150 | | -* Visa: 4007000000027 |
151 | | -* JCB: 3088000000000017 |
152 | | -* Diners Club/ Carte Blanche: 38000000000006 |
153 | | -* Visa (Card Present Track 1): %B4111111111111111^DOE/JOHN^1803101000000000020000831000000? |
154 | | - |
155 | | - |
| 106 | +### Testing Guide |
| 107 | +For additional help in testing your own code, Authorize.Net maintains a [comprehensive testing guide](http://developer.authorize.net/hello_world/testing_guide/) that includes test credit card numbers to use and special triggers to generate certain responses from the sandbox environment. |
156 | 108 |
|
157 | 109 |
|
| 110 | +## License |
| 111 | +This repository is distributed under a proprietary license. See the provided [`LICENSE.txt`](/LICENSE.txt) file. |
0 commit comments