app/models/spree/gateway/authorize_net.rb in spree_gateway-2.2.1 vs app/models/spree/gateway/authorize_net.rb in spree_gateway-3.0.0
- old
+ new
@@ -1,16 +1,46 @@
module Spree
class Gateway::AuthorizeNet < Gateway
preference :login, :string
preference :password, :string
+ preference :server, :string, default: "test"
def provider_class
ActiveMerchant::Billing::AuthorizeNetGateway
end
def options_with_test_preference
- options_without_test_preference.merge(:test => self.preferred_test_mode)
+ if !['live','test'].include?(self.preferred_server)
+ raise "You must set the 'server' preference in your payment method (Gateway::AuthorizeNet) to either 'live' or 'test'"
+ end
+ options_without_test_preference.merge(test: (self.preferred_server != "live") )
end
+ def cancel(response_code)
+ provider
+ # From: http://community.developer.authorize.net/t5/The-Authorize-Net-Developer-Blog/Refunds-in-Retail-A-user-friendly-approach-using-AIM/ba-p/9848
+ # DD: if unsettled, void needed
+ response = provider.void(response_code)
+ # DD: if settled, credit/refund needed (CAN'T DO WITHOUT CREDIT CARD ON AUTH.NET)
+ #response = provider.refund(response_code) unless response.success?
+
+ response
+ end
alias_method_chain :options, :test_preference
+
+ def credit(amount, response_code, refund, gateway_options = {})
+ gateway_options[:card_number] = refund[:originator].payment.source.last_digits
+ auth_net_gateway.refund(amount, response_code, gateway_options)
+ end
+
+ private
+
+ def auth_net_gateway
+ @_auth_net_gateway ||= begin
+ ActiveMerchant::Billing::Base.gateway_mode = preferred_server.to_sym
+ gateway_options = options
+ gateway_options[:test_requests] = false # DD: never ever do test requests because just returns transaction_id = 0
+ ActiveMerchant::Billing::AuthorizeNetGateway.new(gateway_options)
+ end
+ end
end
end