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

- old
+ new

@@ -13,19 +13,16 @@ # # TO USE: # First, make sure you have everything setup correctly and all of your dependencies in place with: # # require 'rubygems' - # require 'money' # require 'active_merchant' # - # The second line is a require for the 'money' library. Make sure you have it installed with 'gem install money' + # ActiveMerchant expects amounts to be Integer values in cents # - # Using the money library, create a money object. Pass the dollar value in cents. In this case, $10 US becomes 1000. + # tendollar = 1000 # - # tendollar = Money.us_dollar(1000) - # # Next, create a credit card object using a TC approved test card. # # creditcard = ActiveMerchant::Billing::CreditCard.new( # :number => '4111111111111111', # :month => 8, @@ -101,46 +98,64 @@ attr_reader :url attr_reader :response attr_reader :options self.money_format = :cents + self.supported_cardtypes = [:visa, :master, :discover, :american_express, :diners_club, :jcb] + self.supported_countries = ['US'] + self.homepage_url = 'http://www.trustcommerce.com/' + self.display_name = 'TrustCommerce' + # Creates a new TrustCommerceGateway + # + # The gateway requires that a valid login and password be passed + # in the +options+ hash. + # + # ==== Options + # + # * <tt>:login</tt> -- The TrustCommerce account login. + # * <tt>:password</tt> -- The TrustCommerce account password. + # * <tt>:test => +true+ or +false+</tt> -- Perform test transactions + # + # ==== Test Account Credentials + # * <tt>:login</tt> -- TestMerchant + # * <tt>:password</tt> -- password def initialize(options = {}) requires!(options, :login, :password) - # these are the defaults for trustcommerce - @options = { - :login => "TestMerchant", - :password => "password" - }.update(options) - + + @options = options super end + def test? + @options[:test] || Base.gateway_mode == :test + end + # authorize() is the first half of the preauth(authorize)/postauth(capture) model. The TC API docs call this # preauth, we preserve active_merchant's nomenclature of authorize() for consistency with the rest of the library. This # method simply checks to make sure funds are available for a transaction, and returns a transid that can be used later to # postauthorize (capture) the funds. - def authorize(money, creditcard, options = {}) + def authorize(money, creditcard_or_billing_id, options = {}) parameters = { :amount => amount(money), } - add_creditcard(parameters, creditcard) + add_payment_source(parameters, creditcard_or_billing_id) add_address(parameters, options) commit('preauth', parameters) end # purchase() is a simple sale. This is one of the most common types of transactions, and is extremely simple. All that you need - # to process a purchase are an amount in cents or a money object and a creditcard object. + # to process a purchase are an amount in cents or a money object and a creditcard object or billingid string. - def purchase(money, creditcard, options = {}) + def purchase(money, creditcard_or_billing_id, options = {}) parameters = { :amount => amount(money), } - add_creditcard(parameters, creditcard) + add_payment_source(parameters, creditcard_or_billing_id) add_address(parameters, options) commit('sale', parameters) end # capture() is the second half of the preauth(authorize)/postauth(capture) model. The TC API docs call this @@ -233,16 +248,20 @@ :billingid => identification, } commit('unstore', parameters) end - - def self.supported_cardtypes - [:visa, :master, :discover, :american_express, :diners_club, :jcb] + + private + def add_payment_source(params, source) + if source.is_a?(String) + add_billing_id(params, source) + else + add_creditcard(params, source) + end end - private def expdate(creditcard) year = sprintf("%.4i", creditcard.year) month = sprintf("%.2i", creditcard.month) "#{month}#{year[-2..-1]}" @@ -252,9 +271,13 @@ params[:media] = "cc" params[:name] = creditcard.name params[:cc] = creditcard.number params[:exp] = expdate(creditcard) params[:cvv] = creditcard.verification_value if creditcard.verification_value? + end + + def add_billing_id(params, billingid) + params[:billingid] = billingid end def add_address(params, options) address = options[:billing_address] || options[:address] \ No newline at end of file