# encoding: UTF-8 require 'spec_helper' require 'adyen/api' require 'nokogiri' require 'rexml/document' Adyen.configuration.default_api_params = { :merchant_account => 'SuperShopper' } Adyen.configuration.api_username = 'SuperShopper' Adyen.configuration.api_password = 'secret' module Net class HTTP class Post attr_reader :header attr_reader :assigned_basic_auth alias old_basic_auth basic_auth def basic_auth(username, password) if Net::HTTP.stubbing_enabled @assigned_basic_auth = [username, password] else old_basic_auth(username, password) end end def soap_action header['soapaction'].first end end class << self attr_accessor :stubbing_enabled, :posted, :stubbed_response def stubbing_enabled=(enabled) reset! if @stubbing_enabled = enabled end def reset! @posted = nil @stubbed_response = nil end end def host @address end alias old_start start def start(&block) Net::HTTP.stubbing_enabled ? yield(self) : old_start(&block) end alias old_request request def request(request) if Net::HTTP.stubbing_enabled self.class.posted = [self, request] self.class.stubbed_response else old_request(request) end end end end module Adyen module API class PaymentService public :authorise_payment_request_body, :authorise_recurring_payment_request_body, :authorise_one_click_payment_request_body, :capture_request_body, :refund_request_body, :cancel_or_refund_request_body, :cancel_request_body end class RecurringService public :list_request_body, :disable_request_body end end end module APISpecHelper def node_for_current_object_and_method Adyen::API::XMLQuerier.xml(@object.send(@method)) end def xpath(query, &block) node_for_current_method.xpath(query, &block) end def text(query) node_for_current_method.text(query) end def stub_net_http(response_body) Net::HTTP.stubbing_enabled = true response = Net::HTTPOK.new('1.1', '200', 'OK') response.stub(:body).and_return(response_body) Net::HTTP.stubbed_response = response end def self.included(klass) klass.extend ClassMethods end module ClassMethods def for_each_xml_backend(&block) backends = [Adyen::API::XMLQuerier::NokogiriBackend, Adyen::API::XMLQuerier::REXMLBackend] backends.each do |xml_backend| describe "with a #{xml_backend} backend" do before { Adyen::API::XMLQuerier.stub(:default_backend => xml_backend.new) } instance_eval(&block) end end end def it_should_have_shortcut_methods_for_params_on_the_response it "provides shortcut methods, on the response object, for all entries in the #params hash" do @response.params.each do |key, value| @response.send(key).should == value end end end def it_should_return_params_for_each_xml_backend(params) for_each_xml_backend do it "returns a hash with parsed response details" do @object.send(@method).params.should == params end end end def it_should_validate_request_parameters(*params) params.each do |param| case param when Symbol it_should_validate_request_param(param) { @object.params[param] = '' } when Hash param.each do |name, attrs| it_should_validate_request_param(name) { @object.params[name] = nil } attrs.each do |attr| it_should_validate_request_param("#{name} => :#{attr}") { @object.params[name][attr] = nil } end end end end end def it_should_validate_request_param(name, &block) it "validates the `#{name}' request parameter" do instance_eval &block lambda { @object.send(@method) }.should raise_error(ArgumentError) end end def describe_response_from(method, response, soap_action = 'authorise', &block) describe(method) do before do stub_net_http(response) @method = method @object.params.merge!(:psp_reference => '9876543210987654') @response = @object.send(@method) @request, @post = Net::HTTP.posted end after do Net::HTTP.stubbing_enabled = false end it "posts the body generated for the given parameters" do @post.body.should == Adyen::API::SimpleSOAPClient::ENVELOPE % @object.send("#{@method}_request_body") end it "posts to the correct SOAP action" do @post.soap_action.should == soap_action end it_should_have_shortcut_methods_for_params_on_the_response instance_eval(&block) end end def describe_request_body_of(method, xpath = nil, &block) method = "#{method}_request_body" describe(method) do before { @method = method } if xpath define_method(:node_for_current_method) do node_for_current_object_and_method.xpath(xpath) end end instance_eval(&block) end end def describe_modification_request_body_of(method, camelized_method = nil, &block) describe_request_body_of method, "//payment:#{camelized_method || method}/payment:modificationRequest" do before do @payment.params[:psp_reference] = 'original-psp-reference' end it "includes the merchant account" do text('./payment:merchantAccount').should == 'SuperShopper' end it "includes the payment (PSP) reference of the payment to refund" do text('./payment:originalReference').should == 'original-psp-reference' end instance_eval(&block) if block_given? end end end end AUTHORISE_RESPONSE = < cardSummary 1111 1234 9876543210987654 Authorised EOS AUTHORISATION_DECLINED_RESPONSE = < 1234 9876543210987654 You need to actually own money. Refused EOS AUTHORISE_REQUEST_INVALID_RESPONSE = < soap:Server %s EOS AUTHORISE_REQUEST_REFUSED_RESPONSE = < You need to actually own money. Refused EOS LIST_RESPONSE = < 2009-10-27T11:26:22.203+01:00
12 2012 S. Hopper 1111 2009-10-27T11:50:12.178+01:00 RecurringDetailReference1 mc 123456789 bank-location-id AnyBank BBBBCCLLbbb NL NL69PSTB0001234567 S. Hopper 2009-10-27T11:26:22.216+01:00 RecurringDetailReference2 IDEAL S. Hopper 1234567890 Berlin 12345678 TestBank 2009-10-27T11:26:22.216+01:00 RecurringDetailReference3 elv
s.hopper@example.com user-id
EOS LIST_EMPTY_RESPONSE = <
EOS DISABLE_RESPONSE = < %s EOS REFUND_RESPONSE = < 8512865475512126 %s EOS CANCEL_OR_REFUND_RESPONSE = < 8512865521218306 %s EOS CANCEL_RESPONSE = < 8612865544848013 %s EOS CAPTURE_RESPONSE = < 8512867956198946 %s EOS BILLET_RECEIVED_RESPONSE = < boletobancario.urlhttps://test.adyen.com/hpp/generationBoleto.shtml?data=AgABAQBdYDe9OqdseA79Rfexm2Lz8fRQ1bWqkLhBCf1fHhQEif7bsRKi0otq%2B1ekMAdMIZUiVXeR3QFrAOA8Zy4tpiNLhkMq6f7W2zFqYhVWrByqxQnbQTYuX2FWI7tsu7Vb0MnyOvFfFdFtaxzImZYCli%2BMrqaAJ5HI9ap3egeqBQIsRI%2Fj0zWsu2EGN16lGbwFOLyxl%2By0Pc5jazTo8rnBA7OVPGDIu7Qt%2F2DYIMcB6PXou5W3aJoTC4SldhNdobVqgWUtES8NsWdOYbLGa6I%2BjSwEFXvxyTXwtw4J2E%2BE7ux1UhBiZRj66lMbcvaYlfnR2xWbA%2BKmdLrVvuXTroEHKQ%2B1C%2FuyGuiOk3SmGq6TMgOyCEt%2BmG%2Bq6z5jDi%2BnYLtlLQU4ccMOujgWMfGkViC%2FXDUlqYjKbn8NHwPwoPcelpf1zCDCe%2Fvu6NBTVQbEXbE0oV0j2MT1tLlMdf08iUsDThuQ3MlJbE8VbTMlttOFqoyXhBjepQ42C1eXfswSz1gsZlHanBCTiw1pB69vkvfWPf5IdUSx1cpEr9LJ9PSz%2FeHxEhq%2B8ZdWzrybXqRbEl2mUjLeyhMNuiE%3Dboletobancario.dataAgABAQBdYDe9OqdseA79Rfexm2Lz8fRQ1bWqkLhBCf1fHhQEif7bsRKi0otq+1ekMAdMIZUiVXeR3QFrAOA8Zy4tpiNLhkMq6f7W2zFqYhVWrByqxQnbQTYuX2FWI7tsu7Vb0MnyOvFfFdFtaxzImZYCli+MrqaAJ5HI9ap3egeqBQIsRI/j0zWsu2EGN16lGbwFOLyxl+y0Pc5jazTo8rnBA7OVPGDIu7Qt/2DYIMcB6PXou5W3aJoTC4SldhNdobVqgWUtES8NsWdOYbLGa6I+jSwEFXvxyTXwtw4J2E+E7ux1UhBiZRj66lMbcvaYlfnR2xWbA+KmdLrVvuXTroEHKQ+1C/uyGuiOk3SmGq6TMgOyCEt+mG+q6z5jDi+nYLtlLQU4ccMOujgWMfGkViC/XDUlqYjKbn8NHwPwoPcelpf1zCDCe/vu6NBTVQbEXbE0oV0j2MT1tLlMdf08iUsDThuQ3MlJbE8VbTMlttOFqoyXhBjepQ42C1eXfswSz1gsZlHanBCTiw1pB69vkvfWPf5IdUSx1cpEr9LJ9PSz/eHxEhq+8ZdWzrybXqRbEl2mUjLeyhMNuiE=boletobancario.expirationDate2014-07-17boletobancario.dueDate2014-07-028814038837489129Received EOS BILLET_REFUSED_RESPONSE = < 8514038928235061102 Unable to determine variantRefused EOS