test/unit/gateways/paypal_test.rb in activemerchant-1.2.1 vs test/unit/gateways/paypal_test.rb in activemerchant-1.3.0

- old
+ new

@@ -1,72 +1,60 @@ require File.dirname(__FILE__) + '/../../test_helper' class PaypalTest < Test::Unit::TestCase def setup + Base.mode = :test + PaypalGateway.pem_file = nil + + @amount = 100 @gateway = PaypalGateway.new( :login => 'cody', :password => 'test', - :pem => '' + :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' - } - - @creditcard = credit_card('4242424242424242') - Base.gateway_mode = :test + @credit_card = credit_card('4242424242424242') + @options = { :billing_address => address, :ip => '127.0.0.1' } end - - def teardown - Base.gateway_mode = :test - PaypalGateway.pem_file = nil - end def test_no_ip_address - assert_raise(ArgumentError){ @gateway.purchase(100, @creditcard, :address => @address)} + assert_raise(ArgumentError){ @gateway.purchase(@amount, @credit_card, :billing_address => address)} end - - def test_purchase_success - @creditcard.number = 1 - - assert response = @gateway.purchase(100, @creditcard, :address => @address, :ip => '127.0.0.1') - assert_equal Response, response.class - assert_equal '#0001', response.params['receiptid'] - assert_equal true, response.success? + + def test_successful_purchase + @gateway.expects(:ssl_post).returns(successful_purchase_response) + + assert response = @gateway.purchase(@amount, @credit_card, @options) + assert_instance_of Response, response + assert_success response + assert_equal '62U664727W5914806', response.authorization + assert response.test? end - - def test_purchase_error - @creditcard.number = 2 - - assert response = @gateway.purchase(100, @creditcard, :order_id => 1, :address => @address, :ip => '127.0.0.1') - assert_equal Response, response.class - assert_equal '#0001', response.params['receiptid'] - assert_equal false, response.success? - + + def test_failed_purchase + @gateway.expects(:ssl_post).returns(failed_purchase_response) + + assert response = @gateway.purchase(@amount, @credit_card, @options) + assert_instance_of Response, response + assert_failure response + assert response.test? end def test_reauthorization @gateway.expects(:ssl_post).returns(successful_reauthorization_response) - response = @gateway.reauthorize(1000, '32J876265E528623B') + response = @gateway.reauthorize(@amount, '32J876265E528623B') assert response.success? assert_equal('1TX27389GX108740X', response.authorization) assert response.test? end - def test_purchase_exceptions - @creditcard.number = 3 - - assert_raise(Error) do - assert response = @gateway.purchase(100, @creditcard, :order_id => 1, :address => @address, :ip => '127.0.0.1') - end + def test_reauthorization_with_warning + @gateway.expects(:ssl_post).returns(successful_with_warning_reauthorization_response) + response = @gateway.reauthorize(@amount, '32J876265E528623B') + assert response.success? + assert_equal('1TX27389GX108740X', response.authorization) + assert response.test? end def test_amount_style assert_equal '10.34', @gateway.send(:amount, 1034) @@ -75,11 +63,11 @@ end end def test_paypal_timeout_error @gateway.stubs(:ssl_post).returns(paypal_timeout_error_response) - response = @gateway.purchase(100, @creditcard, :order_id => 1, :address => @address, :ip => '127.0.0.1') + response = @gateway.purchase(@amount, @credit_card, @options) assert_equal "SOAP-ENV:Server", response.params['faultcode'] assert_equal "Internal error", response.params['faultstring'] assert_equal "Timeout processing request", response.params['detail'] assert_equal "SOAP-ENV:Server: Internal error - Timeout processing request", response.message end @@ -135,38 +123,160 @@ end def test_button_source PaypalGateway.application_id = 'ActiveMerchant_DC' - xml = REXML::Document.new(@gateway.send(:build_sale_or_authorization_request, 'Test', 100, @creditcard, {})) + xml = REXML::Document.new(@gateway.send(:build_sale_or_authorization_request, 'Test', @amount, @credit_card, {})) assert_equal 'ActiveMerchant_DC', REXML::XPath.first(xml, '//n2:ButtonSource').text end def test_item_total_shipping_handling_and_tax_not_included_unless_all_are_present - xml = @gateway.send(:build_sale_or_authorization_request, 'Authorization', 100, @creditcard, - :tax => 100, - :shipping => 100, - :handling => 100 + xml = @gateway.send(:build_sale_or_authorization_request, 'Authorization', @amount, @credit_card, + :tax => @amount, + :shipping => @amount, + :handling => @amount ) doc = REXML::Document.new(xml) assert_nil REXML::XPath.first(doc, '//n2:PaymentDetails/n2:TaxTotal') end def test_item_total_shipping_handling_and_tax - xml = @gateway.send(:build_sale_or_authorization_request, 'Authorization', 100, @creditcard, - :tax => 100, - :shipping => 100, - :handling => 100, + xml = @gateway.send(:build_sale_or_authorization_request, 'Authorization', @amount, @credit_card, + :tax => @amount, + :shipping => @amount, + :handling => @amount, :subtotal => 200 ) doc = REXML::Document.new(xml) assert_equal '1.00', REXML::XPath.first(doc, '//n2:PaymentDetails/n2:TaxTotal').text end + def test_should_use_test_certificate_endpoint + gateway = PaypalGateway.new( + :login => 'cody', + :password => 'test', + :pem => 'PEM' + ) + assert_equal PaypalGateway::URLS[:test][:certificate], gateway.send(:endpoint_url) + end + + def test_should_use_live_certificate_endpoint + gateway = PaypalGateway.new( + :login => 'cody', + :password => 'test', + :pem => 'PEM' + ) + gateway.expects(:test?).returns(false) + + assert_equal PaypalGateway::URLS[:live][:certificate], gateway.send(:endpoint_url) + end + + def test_should_use_test_signature_endpoint + gateway = PaypalGateway.new( + :login => 'cody', + :password => 'test', + :signature => 'SIG' + ) + + assert_equal PaypalGateway::URLS[:test][:signature], gateway.send(:endpoint_url) + end + + def test_should_use_live_signature_endpoint + gateway = PaypalGateway.new( + :login => 'cody', + :password => 'test', + :signature => 'SIG' + ) + gateway.expects(:test?).returns(false) + + assert_equal PaypalGateway::URLS[:live][:signature], gateway.send(:endpoint_url) + end + + def test_should_raise_argument_when_credentials_not_present + assert_raises(ArgumentError) do + PaypalGateway.new(:login => 'cody', :password => 'test') + end + end + + def test_avs_result + @gateway.expects(:ssl_post).returns(successful_purchase_response) + + response = @gateway.purchase(@amount, @credit_card, @options) + assert_equal 'X', 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 + <<-RESPONSE +<?xml version="1.0" encoding="UTF-8"?> +<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:market="urn:ebay:apis:Market" xmlns:auction="urn:ebay:apis:Auction" xmlns:sizeship="urn:ebay:api:PayPalAPI/sizeship.xsd" xmlns:ship="urn:ebay:apis:ship" xmlns:skype="urn:ebay:apis:skype" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:ns="urn:ebay:api:PayPalAPI"> + <SOAP-ENV:Header> + <Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext" xsi:type="wsse:SecurityType"/> + <RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType"> + <Credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType"> + <Username xsi:type="xs:string"/> + <Password xsi:type="xs:string"/> + <Subject xsi:type="xs:string"/> + </Credentials> + </RequesterCredentials> + </SOAP-ENV:Header> + <SOAP-ENV:Body id="_0"> + <DoDirectPaymentResponse xmlns="urn:ebay:api:PayPalAPI"> + <Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2008-01-06T23:41:25Z</Timestamp> + <Ack xmlns="urn:ebay:apis:eBLBaseComponents">Success</Ack> + <CorrelationID xmlns="urn:ebay:apis:eBLBaseComponents">fee61882e6f47</CorrelationID> + <Version xmlns="urn:ebay:apis:eBLBaseComponents">2.000000</Version> + <Build xmlns="urn:ebay:apis:eBLBaseComponents">1.0006</Build> + <Amount xsi:type="cc:BasicAmountType" currencyID="USD">3.00</Amount> + <AVSCode xsi:type="xs:string">X</AVSCode> + <CVV2Code xsi:type="xs:string">M</CVV2Code> + <TransactionID>62U664727W5914806</TransactionID> + </DoDirectPaymentResponse> + </SOAP-ENV:Body> +</SOAP-ENV:Envelope> + RESPONSE + end + + def failed_purchase_response + <<-RESPONSE +<?xml version="1.0" encoding="UTF-8"?> +<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:market="urn:ebay:apis:Market" xmlns:auction="urn:ebay:apis:Auction" xmlns:sizeship="urn:ebay:api:PayPalAPI/sizeship.xsd" xmlns:ship="urn:ebay:apis:ship" xmlns:skype="urn:ebay:apis:skype" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:ns="urn:ebay:api:PayPalAPI"> + <SOAP-ENV:Header> + <Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext" xsi:type="wsse:SecurityType"/> + <RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType"> + <Credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType"> + <Username xsi:type="xs:string"/> + <Password xsi:type="xs:string"/> + <Subject xsi:type="xs:string"/> + </Credentials> + </RequesterCredentials> + </SOAP-ENV:Header> + <SOAP-ENV:Body id="_0"> + <DoDirectPaymentResponse xmlns="urn:ebay:api:PayPalAPI"> + <Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2008-01-06T23:41:25Z</Timestamp> + <Ack xmlns="urn:ebay:apis:eBLBaseComponents">Failure</Ack> + <CorrelationID xmlns="urn:ebay:apis:eBLBaseComponents">fee61882e6f47</CorrelationID> + <Version xmlns="urn:ebay:apis:eBLBaseComponents">2.000000</Version> + <Build xmlns="urn:ebay:apis:eBLBaseComponents">1.0006</Build> + <Amount xsi:type="cc:BasicAmountType" currencyID="USD">3.00</Amount> + <AVSCode xsi:type="xs:string">X</AVSCode> + <CVV2Code xsi:type="xs:string">M</CVV2Code> + </DoDirectPaymentResponse> + </SOAP-ENV:Body> +</SOAP-ENV:Envelope> + RESPONSE + end + def paypal_timeout_error_response <<-RESPONSE <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:cc='urn:ebay:apis:CoreComponentTypes' xmlns:sizeship='urn:ebay:api:PayPalAPI/sizeship.xsd' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' xmlns:saml='urn:oasis:names:tc:SAML:1.0:assertion' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:wsu='http://schemas.xmlsoap.org/ws/2002/07/utility' xmlns:ebl='urn:ebay:apis:eBLBaseComponents' xmlns:ds='http://www.w3.org/2000/09/xmldsig#' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:ns='urn:ebay:api:PayPalAPI' xmlns:market='urn:ebay:apis:Market' xmlns:ship='urn:ebay:apis:ship' xmlns:auction='urn:ebay:apis:Auction' xmlns:wsse='http://schemas.xmlsoap.org/ws/2002/12/secext' xmlns:xsd='http://www.w3.org/2001/XMLSchema'> <SOAP-ENV:Header> @@ -227,9 +337,58 @@ </SOAP-ENV:Header> <SOAP-ENV:Body id="_0"> <DoReauthorizationResponse xmlns="urn:ebay:api:PayPalAPI"> <Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2007-03-04T23:34:42Z</Timestamp> <Ack xmlns="urn:ebay:apis:eBLBaseComponents">Success</Ack> + <CorrelationID xmlns="urn:ebay:apis:eBLBaseComponents">e444ddb7b3ed9</CorrelationID> + <Version xmlns="urn:ebay:apis:eBLBaseComponents">2.000000</Version> + <Build xmlns="urn:ebay:apis:eBLBaseComponents">1.0006</Build> + <AuthorizationID xsi:type="ebl:AuthorizationId">1TX27389GX108740X</AuthorizationID> + </DoReauthorizationResponse> + </SOAP-ENV:Body> +</SOAP-ENV:Envelope> + RESPONSE + end + + def successful_with_warning_reauthorization_response + <<-RESPONSE +<?xml version="1.0" encoding="UTF-8"?> +<SOAP-ENV:Envelope + xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" + xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xs="http://www.w3.org/2001/XMLSchema" + xmlns:cc="urn:ebay:apis:CoreComponentTypes" + xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" + xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" + xmlns:ds="http://www.w3.org/2000/09/xmldsig#" + xmlns:market="urn:ebay:apis:Market" + xmlns:auction="urn:ebay:apis:Auction" + xmlns:sizeship="urn:ebay:api:PayPalAPI/sizeship.xsd" + xmlns:ship="urn:ebay:apis:ship" + xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" + xmlns:ebl="urn:ebay:apis:eBLBaseComponents" + xmlns:ns="urn:ebay:api:PayPalAPI"> + <SOAP-ENV:Header> + <Security + xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext" + xsi:type="wsse:SecurityType"> + </Security> + <RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" + xsi:type="ebl:CustomSecurityHeaderType"> + <Credentials xmlns="urn:ebay:apis:eBLBaseComponents" + xsi:type="ebl:UserIdPasswordType"> + <Username xsi:type="xs:string"></Username> + <Password xsi:type="xs:string"></Password> + <Subject xsi:type="xs:string"></Subject> + </Credentials> + </RequesterCredentials> + </SOAP-ENV:Header> + <SOAP-ENV:Body id="_0"> + <DoReauthorizationResponse xmlns="urn:ebay:api:PayPalAPI"> + <Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2007-03-04T23:34:42Z</Timestamp> + <Ack xmlns="urn:ebay:apis:eBLBaseComponents">SuccessWithWarning</Ack> <CorrelationID xmlns="urn:ebay:apis:eBLBaseComponents">e444ddb7b3ed9</CorrelationID> <Version xmlns="urn:ebay:apis:eBLBaseComponents">2.000000</Version> <Build xmlns="urn:ebay:apis:eBLBaseComponents">1.0006</Build> <AuthorizationID xsi:type="ebl:AuthorizationId">1TX27389GX108740X</AuthorizationID> </DoReauthorizationResponse>