For more information on the Authorize.Net Gateway please visit their Integration Center
The login and password are not the username and password you use to login to the Authorize.Net Merchant Interface. Instead, you will use the API Login ID as the login and Transaction Key as the password.
Log into the Merchant Interface
Select Settings from the Main Menu
Click on API Login ID and Transaction Key in the Security section
Type in the answer to the secret question configured on setup
Click Submit
Automated Recurring Billing (ARB) is an optional service for submitting and managing recurring, or subscription-based, transactions.
To use recurring, update_recurring, cancel_recurring and status_recurring ARB must be enabled for your account.
Information about ARB is available on the Authorize.Net website. Information about the ARB API is available at the Authorize.Net Integration Center
Creates a new AuthorizeNetGateway
The gateway requires that a valid login and password be passed in the options hash.
:login — The Authorize.Net API Login ID (REQUIRED)
:password — The Authorize.Net Transaction Key. (REQUIRED)
:test — true or false. If true, perform transactions against the test server. Otherwise, perform transactions against the production server.
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 73 73: def initialize(options = {}) 74: requires!(options, :login, :password) 75: @options = options 76: super 77: end
Cancel a recurring payment.
This transaction cancels an existing Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled and the subscription must have already been created previously by calling recurring()
subscription_id — A string containing the subscription_id of the recurring payment already in place for a given credit card. (REQUIRED)
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 241 241: def cancel_recurring(subscription_id) 242: request = build_recurring_request(:cancel, :subscription_id => subscription_id) 243: recurring_commit(:cancel, request) 244: end
Captures the funds from an authorized transaction.
money — The amount to be captured as an Integer value in cents.
authorization — The authorization returned from the previous authorize request.
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 122 122: def capture(money, authorization, options = {}) 123: post = {:trans_id => authorization} 124: add_customer_data(post, options) 125: commit('PRIOR_AUTH_CAPTURE', money, post) 126: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 173 173: def credit(money, identification, options = {}) 174: deprecated CREDIT_DEPRECATION_MESSAGE 175: refund(money, identification, options) 176: end
Perform a purchase, which is essentially an authorization and capture in a single operation.
money — The amount to be purchased as an Integer value in cents.
creditcard — The CreditCard details for the transaction.
options — A hash of optional parameters.
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 105 105: def purchase(money, creditcard, options = {}) 106: post = {} 107: add_invoice(post, options) 108: add_creditcard(post, creditcard) 109: add_address(post, options) 110: add_customer_data(post, options) 111: add_duplicate_window(post) 112: 113: commit('AUTH_CAPTURE', money, post) 114: end
Create a recurring payment.
This transaction creates a new Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled.
money — The amount to be charged to the customer at each interval as an Integer value in cents.
creditcard — The CreditCard details for the transaction.
options — A hash of parameters.
:interval — A hash containing information about the interval of time between payments. Must contain the keys :length and :unit. :unit can be either :months or :days. If :unit is :months then :length must be an integer between 1 and 12 inclusive. If :unit is :days then :length must be an integer between 7 and 365 inclusive. For example, to charge the customer once every three months the hash would be +:interval => { :unit => :months, :length => 3 }+ (REQUIRED)
:duration — A hash containing keys for the :start_date the subscription begins (also the date the initial billing occurs) and the total number of billing :occurences or payments for the subscription. (REQUIRED)
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 198 198: def recurring(money, creditcard, options={}) 199: requires!(options, :interval, :duration, :billing_address) 200: requires!(options[:interval], :length, [:unit, :days, :months]) 201: requires!(options[:duration], :start_date, :occurrences) 202: requires!(options[:billing_address], :first_name, :last_name) 203: 204: options[:credit_card] = creditcard 205: options[:amount] = money 206: 207: request = build_recurring_request(:create, options) 208: recurring_commit(:create, request) 209: end
Refund a transaction.
This transaction indicates to the gateway that money should flow from the merchant to the customer.
money — The amount to be credited to the customer as an Integer value in cents.
identification — The ID of the original transaction against which the refund is being issued.
options — A hash of parameters.
:card_number — The credit card number the refund is being issued to. (REQUIRED)
:first_name — The first name of the account being refunded.
:last_name — The last name of the account being refunded.
:zip — The postal code of the account being refunded.
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 156 156: def refund(money, identification, options = {}) 157: requires!(options, :card_number) 158: 159: post = { :trans_id => identification, 160: :card_num => options[:card_number] 161: } 162: 163: post[:first_name] = options[:first_name] if options[:first_name] 164: post[:last_name] = options[:last_name] if options[:last_name] 165: post[:zip] = options[:zip] if options[:zip] 166: 167: add_invoice(post, options) 168: add_duplicate_window(post) 169: 170: commit('CREDIT', money, post) 171: end
Get Subscription Status of a recurring payment.
This transaction gets the status of an existing Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled.
subscription_id — A string containing the subscription_id of the recurring payment already in place for a given credit card. (REQUIRED)
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 254 254: def status_recurring(subscription_id) 255: request = build_recurring_request(:status, :subscription_id => subscription_id) 256: recurring_commit(:status, request) 257: end
Update a recurring payment’s details.
This transaction updates an existing Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled and the subscription must have already been created previously by calling +recurring()+. The ability to change certain details about a recurring payment is dependent on transaction history and cannot be determined until after calling +update_recurring()+. See the ARB XML Guide for such conditions.
options — A hash of parameters.
:subscription_id — A string containing the :subscription_id of the recurring payment already in place for a given credit card. (REQUIRED)
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 226 226: def update_recurring(options={}) 227: requires!(options, :subscription_id) 228: request = build_recurring_request(:update, options) 229: recurring_commit(:update, request) 230: end
Void a previous transaction
authorization - The authorization returned from the previous authorize request.
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 133 133: def void(authorization, options = {}) 134: post = {:trans_id => authorization} 135: add_duplicate_window(post) 136: commit('VOID', nil, post) 137: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 365 365: def add_address(post, options) 366: if address = options[:billing_address] || options[:address] 367: post[:address] = address[:address1].to_s 368: post[:company] = address[:company].to_s 369: post[:phone] = address[:phone].to_s 370: post[:zip] = address[:zip].to_s 371: post[:city] = address[:city].to_s 372: post[:country] = address[:country].to_s 373: post[:state] = address[:state].blank? ? 'n/a' : address[:state] 374: end 375: 376: if address = options[:shipping_address] 377: post[:ship_to_first_name] = address[:first_name].to_s 378: post[:ship_to_last_name] = address[:last_name].to_s 379: post[:ship_to_address] = address[:address1].to_s 380: post[:ship_to_company] = address[:company].to_s 381: post[:ship_to_phone] = address[:phone].to_s 382: post[:ship_to_zip] = address[:zip].to_s 383: post[:ship_to_city] = address[:city].to_s 384: post[:ship_to_country] = address[:country].to_s 385: post[:ship_to_state] = address[:state].blank? ? 'n/a' : address[:state] 386: end 387: end
Adds address information
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 636 636: def add_arb_address(xml, container_name, address) 637: return if address.blank? 638: xml.tag!(container_name) do 639: xml.tag!('firstName', address[:first_name]) 640: xml.tag!('lastName', address[:last_name]) 641: xml.tag!('company', address[:company]) 642: xml.tag!('address', address[:address1]) 643: xml.tag!('city', address[:city]) 644: xml.tag!('state', address[:state]) 645: xml.tag!('zip', address[:zip]) 646: xml.tag!('country', address[:country]) 647: end 648: end
Adds customer’s bank account information Note: This element should only be included when the payment method is bank account.
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 573 573: def add_arb_bank_account(xml, options) 574: bank_account = options[:bank_account] 575: return unless bank_account 576: xml.tag!('bankAccount') do 577: # The type of bank account used for payment of the subscription 578: xml.tag!('accountType', bank_account[:account_type]) 579: # The routing number of the customer’s bank 580: xml.tag!('routingNumber', bank_account[:routing_number]) 581: # The bank account number used for payment of the subscription 582: xml.tag!('accountNumber', bank_account[:account_number]) 583: # The full name of the individual associated 584: # with the bank account number 585: xml.tag!('nameOfAccount', bank_account[:name_of_account]) 586: # The full name of the individual associated 587: # with the bank account number (optional) 588: xml.tag!('bankName', bank_account[:bank_name]) if bank_account[:bank_name] 589: # The type of electronic check transaction used for the subscription 590: xml.tag!('echeckType', bank_account[:echeck_type]) 591: end 592: end
Adds customer’s credit card information Note: This element should only be included when the payment method is credit card.
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 559 559: def add_arb_credit_card(xml, options) 560: credit_card = options[:credit_card] 561: return unless credit_card 562: xml.tag!('creditCard') do 563: # The credit card number used for payment of the subscription 564: xml.tag!('cardNumber', credit_card.number) 565: # The expiration date of the credit card used for the subscription 566: xml.tag!('expirationDate', arb_expdate(credit_card)) 567: end 568: end
Adds information about the customer
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 607 607: def add_arb_customer(xml, options) 608: customer = options[:customer] 609: return unless customer 610: xml.tag!('customer') do 611: xml.tag!('type', customer[:type]) if customer[:type] 612: xml.tag!('id', customer[:id]) if customer[:id] 613: xml.tag!('email', customer[:email]) if customer[:email] 614: xml.tag!('phoneNumber', customer[:phone_number]) if customer[:phone_number] 615: xml.tag!('faxNumber', customer[:fax_number]) if customer[:fax_number] 616: add_arb_drivers_license(xml, options) 617: xml.tag!('taxId', customer[:tax_id]) if customer[:tax_id] 618: end 619: end
Adds the customer’s driver’s license information (conditional)
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 622 622: def add_arb_drivers_license(xml, options) 623: return unless customer = options[:customer] 624: return unless drivers_license = customer[:drivers_license] 625: xml.tag!('driversLicense') do 626: # The customer's driver's license number 627: xml.tag!('number', drivers_license[:number]) 628: # The customer's driver's license state 629: xml.tag!('state', drivers_license[:state]) 630: # The customer's driver's license date of birth 631: xml.tag!('dateOfBirth', drivers_license[:date_of_birth]) 632: end 633: end
Adds information about the subscription duration
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 522 522: def add_arb_duration(xml, options) 523: duration = options[:duration] 524: return unless duration 525: # The date the subscription begins 526: # (also the date the initial billing occurs) 527: xml.tag!('startDate', duration[:start_date]) if duration[:start_date] 528: # Number of billing occurrences or payments for the subscription 529: xml.tag!('totalOccurrences', duration[:occurrences]) if duration[:occurrences] 530: end
Adds information about the interval of time between payments
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 508 508: def add_arb_interval(xml, options) 509: interval = options[:interval] 510: return unless interval 511: xml.tag!('interval') do 512: # The measurement of time, in association with the Interval Unit, 513: # that is used to define the frequency of the billing occurrences 514: xml.tag!('length', interval[:length]) 515: # The unit of time, in association with the Interval Length, 516: # between each billing occurrence 517: xml.tag!('unit', interval[:unit].to_s) 518: end 519: end
Contains the merchant’s payment gateway account authentication information
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 441 441: def add_arb_merchant_authentication(xml) 442: xml.tag!('merchantAuthentication') do 443: xml.tag!('name', @options[:login]) 444: xml.tag!('transactionKey', @options[:password]) 445: end 446: end
Adds order information (optional)
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 595 595: def add_arb_order(xml, options) 596: order = options[:order] 597: return unless order 598: xml.tag!('order') do 599: # Merchant-assigned invoice number for the subscription (optional) 600: xml.tag!('invoiceNumber', order[:invoice_number]) 601: # Description of the subscription (optional) 602: xml.tag!('description', order[:description]) 603: end 604: end
Adds customer’s credit card or bank account payment information
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 546 546: def add_arb_payment(xml, options) 547: return unless options[:credit_card] || options[:bank_account] 548: xml.tag!('payment') do 549: # Contains the customer’s credit card information 550: add_arb_credit_card(xml, options) 551: # Contains the customer’s bank account information 552: add_arb_bank_account(xml, options) 553: end 554: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 532 532: def add_arb_payment_schedule(xml, options) 533: return unless options[:interval] || options[:duration] 534: xml.tag!('paymentSchedule') do 535: # Contains information about the interval of time between payments 536: add_arb_interval(xml, options) 537: add_arb_duration(xml, options) 538: if trial = options[:trial] 539: # Number of billing occurrences or payments in the trial period (optional) 540: xml.tag!('trialOccurrences', trial[:occurrences]) if trial[:occurrences] 541: end 542: end 543: end
Adds subscription information
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 480 480: def add_arb_subscription(xml, options) 481: xml.tag!('subscription') do 482: # Merchant-assigned name for the subscription (optional) 483: xml.tag!('name', options[:subscription_name]) if options[:subscription_name] 484: # Contains information about the payment schedule 485: add_arb_payment_schedule(xml, options) 486: # The amount to be billed to the customer 487: # for each payment in the subscription 488: xml.tag!('amount', amount(options[:amount])) if options[:amount] 489: if trial = options[:trial] 490: # The amount to be charged for each payment during a trial period (conditional) 491: xml.tag!('trialAmount', amount(trial[:amount])) if trial[:amount] 492: end 493: # Contains either the customer’s credit card 494: # or bank account payment information 495: add_arb_payment(xml, options) 496: # Contains order information (optional) 497: add_arb_order(xml, options) 498: # Contains information about the customer 499: add_arb_customer(xml, options) 500: # Contains the customer's billing address information 501: add_arb_address(xml, 'billTo', options[:billing_address]) 502: # Contains the customer's shipping address information (optional) 503: add_arb_address(xml, 'shipTo', options[:shipping_address]) 504: end 505: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 333 333: def add_creditcard(post, creditcard) 334: post[:card_num] = creditcard.number 335: post[:card_code] = creditcard.verification_value if creditcard.verification_value? 336: post[:exp_date] = expdate(creditcard) 337: post[:first_name] = creditcard.first_name 338: post[:last_name] = creditcard.last_name 339: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 341 341: def add_customer_data(post, options) 342: if options.has_key? :email 343: post[:email] = options[:email] 344: post[:email_customer] = false 345: end 346: 347: if options.has_key? :customer 348: post[:cust_id] = options[:customer] 349: end 350: 351: if options.has_key? :ip 352: post[:customer_ip] = options[:ip] 353: end 354: end
x_duplicate_window won’t be sent by default, because sending it changes the response. “If this field is present in the request with or without a value, an enhanced duplicate transaction response will be sent.” (as of 2008-12-30) www.authorize.net/support/AIM_guide_SCC.pdf
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 359 359: def add_duplicate_window(post) 360: unless duplicate_window.nil? 361: post[:duplicate_window] = duplicate_window 362: end 363: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 328 328: def add_invoice(post, options) 329: post[:invoice_num] = options[:order_id] 330: post[:description] = options[:description] 331: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 650 650: def arb_expdate(credit_card) 651: sprintf('%04d-%02d', credit_card.year, credit_card.month) 652: end
Builds body for ARBCancelSubscriptionRequest
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 466 466: def build_arb_cancel_subscription_request(xml, options) 467: xml.tag!('subscriptionId', options[:subscription_id]) 468: 469: xml.target! 470: end
Builds body for ARBCreateSubscriptionRequest
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 449 449: def build_arb_create_subscription_request(xml, options) 450: # Subscription 451: add_arb_subscription(xml, options) 452: 453: xml.target! 454: end
Builds body for ARBGetSubscriptionStatusRequest
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 473 473: def build_arb_status_subscription_request(xml, options) 474: xml.tag!('subscriptionId', options[:subscription_id]) 475: 476: xml.target! 477: end
Builds body for ARBUpdateSubscriptionRequest
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 457 457: def build_arb_update_subscription_request(xml, options) 458: xml.tag!('subscriptionId', options[:subscription_id]) 459: # Adds Subscription 460: add_arb_subscription(xml, options) 461: 462: xml.target! 463: end
Builds recurring billing request
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 425 425: def build_recurring_request(action, options = {}) 426: unless RECURRING_ACTIONS.include?(action) 427: raise StandardError, "Invalid Automated Recurring Billing Action: #{action}" 428: end 429: 430: xml = Builder::XmlMarkup.new(:indent => 2) 431: xml.instruct!(:xml, :version => '1.0', :encoding => 'utf-8') 432: xml.tag!("#{RECURRING_ACTIONS[action]}Request", :xmlns => AUTHORIZE_NET_ARB_NAMESPACE) do 433: add_arb_merchant_authentication(xml) 434: # Merchant-assigned reference ID for the request 435: xml.tag!('refId', options[:ref_id]) if options[:ref_id] 436: send("build_arb_#{action}_subscription_request", xml, options) 437: end 438: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 261 261: def commit(action, money, parameters) 262: parameters[:amount] = amount(money) unless action == 'VOID' 263: 264: # Only activate the test_request when the :test option is passed in 265: parameters[:test_request] = @options[:test] ? 'TRUE' : 'FALSE' 266: 267: url = test? ? self.test_url : self.live_url 268: data = ssl_post url, post_data(action, parameters) 269: 270: response = parse(data) 271: 272: message = message_from(response) 273: 274: # Return the response. The authorization can be taken out of the transaction_id 275: # Test Mode on/off is something we have to parse from the response text. 276: # It usually looks something like this 277: # 278: # (TESTMODE) Successful Sale 279: test_mode = test? || message =~ /TESTMODE/ 280: 281: Response.new(success?(response), message, response, 282: :test => test_mode, 283: :authorization => response[:transaction_id], 284: :fraud_review => fraud_review?(response), 285: :avs_result => { :code => response[:avs_result_code] }, 286: :cvv_result => response[:card_code] 287: ) 288: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 411 411: def expdate(creditcard) 412: year = sprintf("%.4i", creditcard.year) 413: month = sprintf("%.2i", creditcard.month) 414: 415: "#{month}#{year[-2..-1]}" 416: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 294 294: def fraud_review?(response) 295: response[:response_code] == FRAUD_REVIEW 296: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 400 400: def message_from(results) 401: if results[:response_code] == DECLINED 402: return CVVResult.messages[ results[:card_code] ] if CARD_CODE_ERRORS.include?(results[:card_code]) 403: if AVS_REASON_CODES.include?(results[:response_reason_code]) && AVS_ERRORS.include?(results[:avs_result_code]) 404: return AVSResult.messages[ results[:avs_result_code] ] 405: end 406: end 407: 408: (results[:response_reason_text] ? results[:response_reason_text].chomp('.') : '') 409: end
Make a ruby type out of the response string
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 390 390: def normalize(field) 391: case field 392: when "true" then true 393: when "false" then false 394: when "" then nil 395: when "null" then nil 396: else field 397: end 398: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 298 298: def parse(body) 299: fields = split(body) 300: 301: results = { 302: :response_code => fields[RESPONSE_CODE].to_i, 303: :response_reason_code => fields[RESPONSE_REASON_CODE], 304: :response_reason_text => fields[RESPONSE_REASON_TEXT], 305: :avs_result_code => fields[AVS_RESULT_CODE], 306: :transaction_id => fields[TRANSACTION_ID], 307: :card_code => fields[CARD_CODE_RESPONSE_CODE] 308: } 309: results 310: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 312 312: def post_data(action, parameters = {}) 313: post = {} 314: 315: post[:version] = API_VERSION 316: post[:login] = @options[:login] 317: post[:tran_key] = @options[:password] 318: post[:relay_response] = "FALSE" 319: post[:type] = action 320: post[:delim_data] = "TRUE" 321: post[:delim_char] = "," 322: post[:encap_char] = "$" 323: 324: request = post.merge(parameters).collect { |key, value| "x_#{key}=#{CGI.escape(value.to_s)}" }.join("&") 325: request 326: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 654 654: def recurring_commit(action, request) 655: url = test? ? arb_test_url : arb_live_url 656: xml = ssl_post(url, request, "Content-Type" => "text/xml") 657: 658: response = recurring_parse(action, xml) 659: 660: message = response[:message] || response[:text] 661: test_mode = test? || message =~ /Test Mode/ 662: success = response[:result_code] == 'Ok' 663: 664: Response.new(success, message, response, 665: :test => test_mode, 666: :authorization => response[:subscription_id] 667: ) 668: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 670 670: def recurring_parse(action, xml) 671: response = {} 672: xml = REXML::Document.new(xml) 673: root = REXML::XPath.first(xml, "//#{RECURRING_ACTIONS[action]}Response") || 674: REXML::XPath.first(xml, "//ErrorResponse") 675: if root 676: root.elements.to_a.each do |node| 677: recurring_parse_element(response, node) 678: end 679: end 680: 681: response 682: end
# File lib/active_merchant/billing/gateways/authorize_net.rb, line 684 684: def recurring_parse_element(response, node) 685: if node.has_elements? 686: node.elements.each{|e| recurring_parse_element(response, e) } 687: else 688: response[node.name.underscore.to_sym] = node.text 689: end 690: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.