lib/active_merchant/billing/gateways/authorize_net.rb in activemerchant-1.1.0 vs lib/active_merchant/billing/gateways/authorize_net.rb in activemerchant-1.2.0

- old
+ new

@@ -1,13 +1,16 @@ module ActiveMerchant #:nodoc: module Billing #:nodoc: class AuthorizeNetGateway < Gateway API_VERSION = '3.1' - LIVE_URL = "https://secure.authorize.net/gateway/transact.dll" - TEST_URL = "https://test.authorize.net/gateway/transact.dll" - + + class_inheritable_accessor :test_url, :live_url + + self.test_url = "https://test.authorize.net/gateway/transact.dll" + self.live_url = "https://secure.authorize.net/gateway/transact.dll" + APPROVED, DECLINED, ERROR = 1, 2, 3 RESPONSE_CODE, RESPONSE_REASON_CODE, RESPONSE_REASON_TEXT = 0, 2, 3 AVS_RESULT_CODE, TRANSACTION_ID, CARD_CODE_RESPONSE_CODE = 5, 6, 38 @@ -41,10 +44,15 @@ # URL attr_reader :url attr_reader :response attr_reader :options + + self.supported_countries = ['US'] + self.supported_cardtypes = [:visa, :master, :american_express, :discover] + self.homepage_url = 'http://www.authorize.net/' + self.display_name = 'Authorize.net' def initialize(options = {}) requires!(options, :login, :password) @options = options super @@ -88,39 +96,27 @@ :card_num => options[:card_number] } commit('CREDIT', money, post) end - - # We support visa and master card - def self.supported_cardtypes - [:visa, :master, :american_express, :discover] - end - + private - - def expdate(creditcard) - year = sprintf("%.4i", creditcard.year) - month = sprintf("%.2i", creditcard.month) - - "#{year[-2..-1]}#{month}" - end - def commit(action, money, parameters) parameters[:amount] = amount(money) unless action == 'VOID' # Only activate the test_request when the :test option is passed in parameters[:test_request] = @options[:test] ? 'TRUE' : 'FALSE' if result = test_result_from_cc_number(parameters[:card_num]) return result end - url = test? ? TEST_URL : LIVE_URL + url = test? ? self.test_url : self.live_url data = ssl_post url, post_data(action, parameters) @response = parse(data) + success = @response[:response_code] == APPROVED message = message_from(@response) # Return the response. The authorization can be taken out of the transaction_id # Test Mode on/off is something we have to parse from the response text. @@ -136,12 +132,12 @@ :authorization => @response[:transaction_id] ) end def parse(body) - fields = body[1..-2].split(/\$,\$/) - + fields = split(body) + results = { :response_code => fields[RESPONSE_CODE].to_i, :response_reason_code => fields[RESPONSE_REASON_CODE], :response_reason_text => fields[RESPONSE_REASON_TEXT], :avs_result_code => fields[AVS_RESULT_CODE], @@ -228,17 +224,21 @@ if results[:response_code] == DECLINED return CARD_CODE_MESSAGES[results[:card_code]] if CARD_CODE_ERRORS.include?(results[:card_code]) return AVS_MESSAGES[results[:avs_result_code]] if AVS_ERRORS.include?(results[:avs_result_code]) end - return results[:response_reason_text][0..-2] # Forget the punctuation at the end + return results[:response_reason_text].nil? ? '' : results[:response_reason_text][0..-2] end def expdate(creditcard) year = sprintf("%.4i", creditcard.year) month = sprintf("%.2i", creditcard.month) "#{month}#{year[-2..-1]}" + end + + def split(response) + response[1..-2].split(/\$,\$/) end end AuthorizedNetGateway = AuthorizeNetGateway end