require File.dirname(__FILE__) + '/../../test_helper' class CyberSourceTest < Test::Unit::TestCase def setup Base.gateway_mode = :test @gateway = CyberSourceGateway.new( :login => 'l', :password => 'p' ) @amount = 100 @credit_card = credit_card('4111111111111111', :type => 'visa') @declined_card = credit_card('801111111111111', :type => 'visa') @options = { :billing_address => { :address1 => '1234 My Street', :address2 => 'Apt 1', :company => 'Widgets Inc', :city => 'Ottawa', :state => 'ON', :zip => 'K1C2N6', :country => 'Canada', :phone => '(555)555-5555' }, :email => 'someguy1232@fakeemail.net', :order_id => '1000', :line_items => [ { :declared_value => @amount, :quantity => 2, :code => 'default', :description => 'Giant Walrus', :sku => 'WA323232323232323' }, { :declared_value => @amount, :quantity => 2, :description => 'Marble Snowcone', :sku => 'FAKE1232132113123' } ], :currency => 'USD' } end def test_successful_purchase @gateway.expects(:ssl_post).returns(successful_purchase_response) assert response = @gateway.purchase(@amount, @credit_card, @options) assert_equal 'Successful transaction', response.message assert_success response assert_equal "#{@options[:order_id]};#{response.params['requestID']};#{response.params['requestToken']}", response.authorization assert response.test? end def test_unsuccessful_authorization @gateway.expects(:ssl_post).returns(unsuccessful_authorization_response) assert response = @gateway.purchase(@amount, @credit_card, @options) assert_instance_of Response, response assert_failure response end def test_successful_auth_request @gateway.stubs(:ssl_post).returns(successful_authorization_response) assert response = @gateway.authorize(@amount, @credit_card, @options) assert_equal Response, response.class assert response.success? assert response.test? end def test_successful_tax_request @gateway.stubs(:ssl_post).returns(successful_tax_response) assert response = @gateway.calculate_tax(@credit_card, @options) assert_equal Response, response.class assert response.success? assert response.test? end def test_successful_capture_request @gateway.stubs(:ssl_post).returns(successful_authorization_response, successful_capture_response) assert response = @gateway.authorize(@amount, @credit_card, @options) assert response.success? assert response.test? assert response_capture = @gateway.capture(@amount, response.authorization) assert response_capture.success? assert response_capture.test? end def test_successful_purchase_request @gateway.stubs(:ssl_post).returns(successful_capture_response) assert response = @gateway.purchase(@amount, @credit_card, @options) assert response.success? assert response.test? end def test_requires_error_on_purchase_without_order_id assert_raise(ArgumentError){ @gateway.purchase(@amount, @credit_card, @options.delete_if{|key, val| key == :order_id}) } end def test_requires_error_on_authorization_without_order_id assert_raise(ArgumentError){ @gateway.purchase(@amount, @credit_card, @options.delete_if{|key, val| key == :order_id}) } end def test_requires_error_on_tax_calculation_without_line_items assert_raise(ArgumentError){ @gateway.calculate_tax(@credit_card, @options.delete_if{|key, val| key == :line_items})} end def test_default_currency assert_equal 'USD', CyberSourceGateway.default_currency end def test_avs_result @gateway.expects(:ssl_post).returns(successful_purchase_response) response = @gateway.purchase(@amount, @credit_card, @options) assert_equal 'Y', response.avs_result['code'] end def test_cvv_result @gateway.expects(:ssl_post).returns(successful_purchase_response) response = @gateway.purchase(@amount, @credit_card, @options) assert_equal 'M', response.cvv_result['code'] end private def successful_purchase_response <<-XML 2008-01-15T21:42:03.343Zb0a6cf9aa07f1a8495f89c364bbd6a9a2004333231260008401927ACCEPT100Afvvj7Ke2Fmsbq0wHFE2sM6R4GAptYZ0jwPSA+R9PhkyhFTb0KRjoE4+ynthZrG6tMBwjAtTUSD1001.00123456YYMM2008-01-15T21:42:03Z00U XML end def successful_authorization_response <<-XML 2007-07-12T18:31:53.838ZTEST111111111111842651133440156177166ACCEPT100AP4JY+Or4xRonEAOERAyMzQzOTEzMEM0MFZaNUZCBgDH3fgJ8AEGAMfd+AnwAwzRpAAA7RT/USD1001.00004542AI72007-07-12T18:31:53Z10023439130C40VZ2FB XML end def unsuccessful_authorization_response <<-XML 2008-01-15T21:50:41.580Za1efca956703a2a5037178a8a28f73572004338415330008402434REJECT231Afvvj7KfIgU12gooCFE2/DanQIApt+G1OgTSA+R9PTnyhFTb0KRjgFY+ynyIFNdoKKAghwgx231 XML end def successful_tax_response <<-XML 2007-07-11T18:27:56.314ZTEST111111111111841784762620176127166ACCEPT100AMYJY9fl62i+vx2OEQYAx9zv/9UBZAAA5h5D1001.000Madison000WI0537170 XML end def successful_capture_response <<-XML 2007-07-17T17:15:32.642Ztest11111111111111111846925324700976124593ACCEPT100AP4JZB883WKS/34BEZAzMTE1OTI5MVQzWE0wQjEzBTUt3wbOAQUy3D7oDgMMmvQAnQglGBP1002007-07-17T17:15:32Z1.0031159291T3XM2B13 XML end end