require File.dirname(__FILE__) + '/../../test_helper'
class CyberSourceTest < Test::Unit::TestCase
LOGIN = 'YourLogin'
TRANSACTION_KEY = 'GenerateThisInTheBusinessCenter'
AMOUNT = 100
def setup
Base.gateway_mode = :test
@gateway = CyberSourceGateway.new(
:login => LOGIN,
:password => TRANSACTION_KEY , :test => true
)
@creditcard = credit_card('4111111111111111', :type => 'visa')
@declined_card = credit_card('801111111111111', :type => 'visa')
@options = { :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 => 'test1111111111111111',
:line_items => [
{
:declared_value => 100,
:quantity => 2,
:code => 'default',
:description => 'Giant Walrus',
:sku => 'WA323232323232323'
},
{
:declared_value => 100,
:quantity => 2,
:description => 'Marble Snowcone',
:sku => 'FAKE1232132113123'
}
],
:currency => 'USD'
}
end
def test_purchase_success
@creditcard.number = 1
assert response = @gateway.purchase(100, @creditcard, @options)
assert_equal Response, response.class
assert_equal '#0001', response.params['receiptid']
assert_equal true, response.success?
end
def test_purchase_error
@creditcard.number = 2
assert response = @gateway.purchase(100, @creditcard, @options)
assert_equal Response, response.class
assert_equal '#0001', response.params['receiptid']
assert_equal false, response.success?
end
def test_purchase_exceptions
@creditcard.number = 3
assert_raise(Error) do
assert response = @gateway.purchase(100, @creditcard, @options)
end
end
def test_successful_auth_request
@gateway.stubs(:ssl_post).returns(successful_authorization_response)
assert response = @gateway.authorize(AMOUNT, @creditcard, @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(@creditcard, @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, @creditcard, @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, @creditcard, @options)
assert response.success?
assert response.test?
end
def test_requires_error_on_purchase_without_order_id
assert_raise(ArgumentError){ @gateway.purchase(AMOUNT, @creditcard, @options.delete_if{|key, val| key == :order_id}) }
end
def test_requires_error_on_authorization_without_order_id
assert_raise(ArgumentError){ @gateway.purchase(AMOUNT, @creditcard, @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(@creditcard, @options.delete_if{|key, val| key == :line_items})}
end
def test_default_currency
assert_equal 'USD', CyberSourceGateway.default_currency
end
private
def successful_authorization_response
<<-XML
2007-07-12T18:31:53.838ZTEST111111111111842651133440156177166ACCEPT100AP4JY+Or4xRonEAOERAyMzQzOTEzMEM0MFZaNUZCBgDH3fgJ8AEGAMfd+AnwAwzRpAAA7RT/USD1001.00004542AI72007-07-12T18:31:53Z10023439130C40VZ2FB
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