require 'test_helper'
class PaypalExpressTest < Test::Unit::TestCase
TEST_REDIRECT_URL = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=1234567890'
LIVE_REDIRECT_URL = 'https://www.paypal.com/cgibin/webscr?cmd=_express-checkout&token=1234567890'
TEST_REDIRECT_URL_WITHOUT_REVIEW = "#{TEST_REDIRECT_URL}&useraction=commit"
LIVE_REDIRECT_URL_WITHOUT_REVIEW = "#{LIVE_REDIRECT_URL}&useraction=commit"
def setup
@gateway = PaypalExpressGateway.new(
:login => 'cody',
:password => 'test',
:pem => 'PEM'
)
@address = { :address1 => '1234 My Street',
:address2 => 'Apt 1',
:company => 'Widgets Inc',
:city => 'Ottawa',
:state => 'ON',
:zip => 'K1C2N6',
:country => 'Canada',
:phone => '(555)555-5555'
}
Base.gateway_mode = :test
end
def teardown
Base.gateway_mode = :test
end
def test_live_redirect_url
Base.gateway_mode = :production
assert_equal LIVE_REDIRECT_URL, @gateway.redirect_url_for('1234567890')
end
def test_live_redirect_url_without_review
Base.gateway_mode = :production
assert_equal LIVE_REDIRECT_URL_WITHOUT_REVIEW, @gateway.redirect_url_for('1234567890', :review => false)
end
def test_force_sandbox_redirect_url
Base.gateway_mode = :production
gateway = PaypalExpressGateway.new(
:login => 'cody',
:password => 'test',
:pem => 'PEM',
:test => true
)
assert gateway.test?
assert_equal TEST_REDIRECT_URL, gateway.redirect_url_for('1234567890')
end
def test_test_redirect_url
assert_equal :test, Base.gateway_mode
assert_equal TEST_REDIRECT_URL, @gateway.redirect_url_for('1234567890')
end
def test_test_redirect_url_without_review
assert_equal :test, Base.gateway_mode
assert_equal TEST_REDIRECT_URL_WITHOUT_REVIEW, @gateway.redirect_url_for('1234567890', :review => false)
end
def test_get_express_details
@gateway.expects(:ssl_post).returns(successful_details_response)
response = @gateway.details_for('EC-2OPN7UJGFWK9OYFV')
assert_instance_of PaypalExpressResponse, response
assert response.success?
assert response.test?
assert_equal 'EC-6WS104951Y388951L', response.token
assert_equal 'FWRVKNRRZ3WUC', response.payer_id
assert_equal 'buyer@jadedpallet.com', response.email
assert address = response.address
assert_equal 'Fred Brooks', address['name']
assert_nil address['company']
assert_equal '1234 Penny Lane', address['address1']
assert_nil address['address2']
assert_equal 'Jonsetown', address['city']
assert_equal 'NC', address['state']
assert_equal '23456', address['zip']
assert_equal 'US', address['country']
assert_nil address['phone']
end
def test_authorization
@gateway.expects(:ssl_post).returns(successful_authorization_response)
response = @gateway.authorize(300, :token => 'EC-6WS104951Y388951L', :payer_id => 'FWRVKNRRZ3WUC')
assert response.success?
assert_not_nil response.authorization
assert response.test?
end
def test_default_payflow_currency
assert_equal 'USD', PayflowExpressGateway.default_currency
end
def test_default_partner
assert_equal 'PayPal', PayflowExpressGateway.partner
end
def test_uk_partner
assert_equal 'PayPalUk', PayflowExpressUkGateway.partner
end
def test_handle_non_zero_amount
xml = REXML::Document.new(@gateway.send(:build_setup_request, 'SetExpressCheckout', 50, {}))
assert_equal '0.50', REXML::XPath.first(xml, '//n2:OrderTotal').text
end
def test_handles_zero_amount
xml = REXML::Document.new(@gateway.send(:build_setup_request, 'SetExpressCheckout', 0, {}))
assert_equal '1.00', REXML::XPath.first(xml, '//n2:OrderTotal').text
end
def test_handle_locale_code
xml = REXML::Document.new(@gateway.send(:build_setup_request, 'SetExpressCheckout', 0, { :locale => 'GB' }))
assert_equal 'GB', REXML::XPath.first(xml, '//n2:LocaleCode').text
end
def test_supported_countries
assert_equal ['US'], PaypalExpressGateway.supported_countries
end
def test_button_source
PaypalExpressGateway.application_id = 'ActiveMerchant_EC'
xml = REXML::Document.new(@gateway.send(:build_sale_or_authorization_request, 'Test', 100, {}))
assert_equal 'ActiveMerchant_EC', REXML::XPath.first(xml, '//n2:ButtonSource').text
end
def test_error_code_for_single_error
@gateway.expects(:ssl_post).returns(response_with_error)
response = @gateway.setup_authorization(100,
:return_url => 'http://example.com',
:cancel_return_url => 'http://example.com'
)
assert_equal "10736", response.params['error_codes']
end
def test_ensure_only_unique_error_codes
@gateway.expects(:ssl_post).returns(response_with_duplicate_errors)
response = @gateway.setup_authorization(100,
:return_url => 'http://example.com',
:cancel_return_url => 'http://example.com'
)
assert_equal "10736" , response.params['error_codes']
end
def test_error_codes_for_multiple_errors
@gateway.expects(:ssl_post).returns(response_with_errors)
response = @gateway.setup_authorization(100,
:return_url => 'http://example.com',
:cancel_return_url => 'http://example.com'
)
assert_equal ["10736", "10002"] , response.params['error_codes'].split(',')
end
private
def successful_details_response
<<-RESPONSE
2007-02-12T23:59:43Z
Success
c73044f11da65
2.000000
1.0006
EC-6WS104951Y388951L
buyer@jadedpallet.com
FWRVKNRRZ3WUC
verified
Fred
Brooks
US
Fred Brooks
1234 Penny Lane
Jonsetown
NC
US
United States
23456
PayPal
Confirmed
1230123
RESPONSE
end
def successful_authorization_response
<<-RESPONSE
2007-02-13T00:18:50Z
Success
62450a4266d04
2.000000
1.0006
EC-6WS104951Y388951L
8B266858CH956410C
express-checkout
instant
2007-02-13T00:18:48Z
3.00
0.00
Pending
authorization
none
RESPONSE
end
def response_with_error
<<-RESPONSE
2008-04-02T17:38:02Z
Failure
cdb720feada30
Shipping Address Invalid City State Postal Code
A match of the Shipping Address City, State, and Postal Code failed.
10736
Error
2.000000
543066
RESPONSE
end
def response_with_errors
<<-RESPONSE
2008-04-02T17:38:02Z
Failure
cdb720feada30
Shipping Address Invalid City State Postal Code
A match of the Shipping Address City, State, and Postal Code failed.
10736
Error
Authentication/Authorization Failed
You do not have permissions to make this API call
10002
Error
2.000000
543066
RESPONSE
end
def response_with_duplicate_errors
<<-RESPONSE
2008-04-02T17:38:02Z
Failure
cdb720feada30
Shipping Address Invalid City State Postal Code
A match of the Shipping Address City, State, and Postal Code failed.
10736
Error
Shipping Address Invalid City State Postal Code
A match of the Shipping Address City, State, and Postal Code failed.
10736
Error
2.000000
543066
RESPONSE
end
end