require File.join(File.dirname(__FILE__), 'test_helper') class TrustCommerceSubscriptionTest < Test::Unit::TestCase # --- [ TrustCommerce test data ] --- # reference: https://vault.trustcommerce.com/downloads/TCDevGuide.html#testdata CARDS = { :visa => { :cc => '4111111111111111', :exp => '0412', :cvv => 123, :address => '123 Test St.', :city => 'Somewhere', :state => 'CA', :zip => 90001 }, :mastercard => { :cc => '5411111111111115', :exp => '0412', :cvv => 777, :address => '4000 Main St.', :city => 'Anytown', :state => 'MA', :zip => 85001 }, :amex => { :cc => '341111111111111', :exp => '0412', :cvv => 4000, :address => '12 Colorado Blvd.', :city => 'Elsewhere', :state => 'IL', :zip => 54321 } } def setup if ENV['TC_USERNAME'].nil? || ENV['TC_PASSWORD'].nil? puts 'parameters TC_USERNAME and TC_PASSWORD required!' puts 'Usage: TC_USERNAME=username TC_PASSWORD=password TC_VAULT_PASSWORD=password ruby test/trustcommerce_subscription_test.rb' exit 1 else TrustCommerceGateway.custid = ENV['TC_USERNAME'] TrustCommerceGateway.password = ENV['TC_PASSWORD'] TrustCommerceGateway.vault_password = ENV['TC_VAULT_PASSWORD'] if ENV['TC_VAULT_PASSWORD'] end end def test_subscription_create via_tclink_and_https do response = TrustCommerceGateway::Subscription.create( :cc => CARDS[:visa][:cc], :exp => CARDS[:visa][:exp], :address1 => CARDS[:visa][:address], :zip => CARDS[:visa][:zip], :avs => 'y', :name => 'Jennifer Smith - create() test', :amount => 1200, :cycle => '1m', :demo => 'y' ) assert_equal HashWithIndifferentAccess, response.class assert_not_nil response['billingid'] assert_not_nil response[:billingid] assert response.keys.include?('transid') assert_equal 'approved', response['status'] end end def test_subscription_update via_tclink_and_https do # --- [ create subscription ] --- create_response = TrustCommerceGateway::Subscription.create( :cc => CARDS[:visa][:cc], :exp => CARDS[:visa][:exp], :address1 => CARDS[:visa][:address], :zip => CARDS[:visa][:zip], :avs => 'y', :name => 'Jennifer Smith - update() test', :amount => 1200, :cycle => '1m', :demo => 'y' ) assert_equal 'approved', create_response['status'] assert create_response.keys.include?('billingid') # --- [ update subscription ] --- update_response = TrustCommerceGateway::Subscription.update( :billingid => create_response['billingid'], :cc => CARDS[:mastercard][:cc], :exp => CARDS[:mastercard][:exp], :address1 => CARDS[:mastercard][:address], :zip => CARDS[:mastercard][:zip], :avs => 'y' ) assert_equal 'accepted', update_response['status'] end end def test_subscription_destroy via_tclink_and_https do # --- [ create subscription ] --- create_response = TrustCommerceGateway::Subscription.create( :cc => CARDS[:visa][:cc], :exp => CARDS[:visa][:exp], :address1 => CARDS[:visa][:address], :zip => CARDS[:visa][:zip], :avs => 'y', :name => 'Jennifer Smith - destroy() test', :amount => 1200, :cycle => '1m', :demo => 'y' ) assert create_response.keys.include?('billingid') assert_equal 'approved', create_response['status'] # --- [ destroy subscription ] --- destroy_response = TrustCommerceGateway::Subscription.destroy( :billingid => create_response['billingid'] ) assert_equal HashWithIndifferentAccess, destroy_response.class assert destroy_response.keys.include?('transid') assert_equal 'accepted', destroy_response['status'] end end def test_subscription_charge_and_credit via_tclink_and_https do # --- [ create subscription ] --- create_response = TrustCommerceGateway::Subscription.create( :cc => CARDS[:visa][:cc], :exp => CARDS[:visa][:exp], :address1 => CARDS[:visa][:address], :zip => CARDS[:visa][:zip], :avs => 'y', :name => 'Jennifer Smith - charge_and_credit() test', :amount => 1200, :cycle => '1m', :demo => 'y' ) assert create_response.keys.include?('billingid') assert_equal 'approved', create_response['status'] # --- [ process charge ] --- charge_response = TrustCommerceGateway::Subscription.charge( :billingid => create_response['billingid'], :amount => 1995, :demo => 'y' ) assert_equal HashWithIndifferentAccess, charge_response.class assert charge_response.keys.include?('transid') assert_equal 'approved', charge_response['status'] # --- [ process credit ] --- credit_response = TrustCommerceGateway::Subscription.credit( :transid => charge_response['transid'], :amount => 995, :demo => 'y' ) assert_equal HashWithIndifferentAccess, credit_response.class assert credit_response.keys.include?('transid') assert_equal 'accepted', credit_response['status'] end end def test_subscription_query puts "\n" puts "---------------------------------------------------------------------------" puts "IMPORTANT: This query test will likely take between 1 and 2 minutes!" puts "Make sure TC_VAULT_PASSWORD is set if it differs from your TCLink password." puts "---------------------------------------------------------------------------" # --- [ create subscription ] --- create_response = TrustCommerceGateway::Subscription.create( :cc => CARDS[:visa][:cc], :exp => CARDS[:visa][:exp], :address1 => CARDS[:visa][:address], :zip => CARDS[:visa][:zip], :avs => 'y', :name => 'Jennifer Smith', :amount => 1200, :cycle => '1m', :demo => 'y' ) assert create_response.keys.include?('billingid') assert_equal 'approved', create_response['status'] # --- [ query for charges ] --- options = { :querytype => 'transaction', :action => 'sale', :billingid => create_response['billingid'] } while (query_response = TrustCommerceGateway::Subscription.query(options)) if query_response.body =~ /error/i fail(query_response.body) break elsif query_response.body.split("\n").size < 2 puts 'Transaction has not yet showed up... will try again in 15 seconds.' sleep(15) else puts 'Transaction found.' # --- [ Setup index hash ] --- field_names = query_response.body.split("\n")[0].split(',') date_line_1 = query_response.body.split("\n")[1].split(',') indexes = field_names.inject({}) {|h, field| h[field.to_sym] = field_names.index(field); h } # --- [ Check transaction data ] --- assert_equal '1111', date_line_1[indexes[:cc]] assert_equal '1200', date_line_1[indexes[:amount]] assert_equal 'Jennifer Smith', date_line_1[indexes[:name]] break end end end # --- [ test private helpers ] --- def test_clean_parameters assert_equal ({ 'a' => '1', 'b' => '2' }), TrustCommerceGateway.clean_parameters(:a => '1', :b => '2') assert_equal ({ 'a' => '1', 'b' => '2' }), TrustCommerceGateway.clean_parameters(:a => 1, :b => 2) assert_equal ({ 'a' => '2' }), TrustCommerceGateway.clean_parameters(:a => 1, :a => 2) end end