lib/adyen/api.rb in fingertips-adyen-0.3.8.20100929 vs lib/adyen/api.rb in fingertips-adyen-0.3.8.20100930

- old
+ new

@@ -20,20 +20,23 @@ # # Shortcut methods # - def self.authorise_payment(params = {}) + def self.authorise_payment(params) PaymentService.new(params).authorise_payment end - def self.authorise_recurring_payment(params = {}) + def self.authorise_recurring_payment(params) PaymentService.new(params).authorise_recurring_payment end - def self.disable_recurring_contract(params = {}) - RecurringService.new(params).disable + def self.disable_recurring_contract(shopper_reference, recurring_detail_reference = nil) + RecurringService.new({ + :shopper => { :reference => shopper_reference }, + :recurring_detail_reference => recurring_detail_reference + }).disable end # TODO: the rest # @@ -42,34 +45,43 @@ class SimpleSOAPClient # from http://curl.haxx.se/ca/cacert.pem CACERT = File.expand_path('../../../support/cacert.pem', __FILE__) - def self.endpoint - @endpoint ||= URI.parse(const_get('ENDPOINT_URI') % Adyen.environment) + class << self + attr_accessor :stubbed_response + + def endpoint + @endpoint ||= URI.parse(const_get('ENDPOINT_URI') % Adyen.environment) + end end attr_reader :params def initialize(params = {}) @params = API.default_params.merge(params) end def call_webservice_action(action, data, response_class) - endpoint = self.class.endpoint + if response = self.class.stubbed_response + self.class.stubbed_response = nil + response + else + endpoint = self.class.endpoint - post = Net::HTTP::Post.new(endpoint.path, 'Accept' => 'text/xml', 'Content-Type' => 'text/xml; charset=utf-8', 'SOAPAction' => action) - post.basic_auth(API.username, API.password) - post.body = data + post = Net::HTTP::Post.new(endpoint.path, 'Accept' => 'text/xml', 'Content-Type' => 'text/xml; charset=utf-8', 'SOAPAction' => action) + post.basic_auth(API.username, API.password) + post.body = data - request = Net::HTTP.new(endpoint.host, endpoint.port) - request.use_ssl = true - request.ca_file = CACERT - request.verify_mode = OpenSSL::SSL::VERIFY_PEER + request = Net::HTTP.new(endpoint.host, endpoint.port) + request.use_ssl = true + request.ca_file = CACERT + request.verify_mode = OpenSSL::SSL::VERIFY_PEER - request.start do |http| - response_class.new(http.request(post)) + request.start do |http| + response_class.new(http.request(post)) + end end end end class Response @@ -177,10 +189,42 @@ end class PaymentService < SimpleSOAPClient ENDPOINT_URI = 'https://pal-%s.adyen.com/pal/servlet/soap/Payment' + class << self + def success_stub + http_response = Net::HTTPOK.new('1.1', '200', 'OK') + def http_response.body; AUTHORISE_RESPONSE; end + AuthorizationResponse.new(http_response) + end + + def refused_stub + http_response = Net::HTTPOK.new('1.1', '200', 'OK') + def http_response.body; AUTHORISATION_REFUSED_RESPONSE; end + AuthorizationResponse.new(http_response) + end + + def invalid_stub + http_response = Net::HTTPOK.new('1.1', '200', 'OK') + def http_response.body; AUTHORISATION_REQUEST_INVALID_RESPONSE; end + AuthorizationResponse.new(http_response) + end + + def stub_success! + @stubbed_response = success_stub + end + + def stub_refused! + @stubbed_response = refused_stub + end + + def stub_invalid! + @stubbed_response = invalid_stub + end + end + def authorise_payment make_payment_request(authorise_payment_request_body) end def authorise_recurring_payment @@ -253,12 +297,16 @@ def invalid_request? !fault_message.nil? end - def error - ERRORS[fault_message] || [:base, fault_message] + def error(prefix = nil) + if error = ERRORS[fault_message] + prefix ? ["#{prefix}_#{error[0]}".to_sym, error[1]] : error + else + [:base, fault_message] + end end def params @params ||= xml_querier.xpath('//payment:authoriseResponse/payment:paymentResult') do |result| { @@ -271,18 +319,33 @@ end private def fault_message - @fault_message ||= xml_querier.text('//soap:Fault/faultstring') + @fault_message ||= begin + message = xml_querier.text('//soap:Fault/faultstring') + message unless message.empty? + end end end end class RecurringService < SimpleSOAPClient ENDPOINT_URI = 'https://pal-%s.adyen.com/pal/servlet/soap/Recurring' + class << self + def disabled_stub + http_response = Net::HTTPOK.new('1.1', '200', 'OK') + def http_response.body; DISABLE_RESPONSE % DisableResponse::DISABLED_RESPONSES.first; end + DisableResponse.new(http_response) + end + + def stub_disabled! + @stubbed_response = disabled_stub + end + end + # TODO: rename to list_details and make shortcut method take the only necessary param def list call_webservice_action('listRecurringDetails', list_request_body, ListResponse) end @@ -302,12 +365,20 @@ end DISABLE_LAYOUT % [@params[:merchant_account], @params[:shopper][:reference], reference || ''] end class DisableResponse < Response + DISABLED_RESPONSES = %w{ [detail-successfully-disabled] [all-details-successfully-disabled] } + response_attrs :response + def success? + super && DISABLED_RESPONSES.include?(params[:response]) + end + + alias disabled? success? + def params @params ||= { :response => xml_querier.text('//recurring:disableResponse/recurring:result/recurring:response') } end end @@ -427,10 +498,69 @@ SHOPPER_PARTIALS = { :reference => ' <payment:shopperReference>%s</payment:shopperReference>', :email => ' <payment:shopperEmail>%s</payment:shopperEmail>', :ip => ' <payment:shopperIP>%s</payment:shopperIP>', } + + # Test responses + + AUTHORISE_RESPONSE = <<EOS +<?xml version="1.0" encoding="UTF-8"?> +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <soap:Body> + <ns1:authoriseResponse xmlns:ns1="http://payment.services.adyen.com"> + <ns1:paymentResult> + <additionalData xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <authCode xmlns="http://payment.services.adyen.com">1234</authCode> + <dccAmount xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <dccSignature xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <fraudResult xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <issuerUrl xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <md xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <paRequest xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <pspReference xmlns="http://payment.services.adyen.com">9876543210987654</pspReference> + <refusalReason xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <resultCode xmlns="http://payment.services.adyen.com">Authorised</resultCode> + </ns1:paymentResult> + </ns1:authoriseResponse> + </soap:Body> +</soap:Envelope> +EOS + + AUTHORISATION_REFUSED_RESPONSE = <<EOS +<?xml version="1.0" encoding="UTF-8"?> +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <soap:Body> + <ns1:authoriseResponse xmlns:ns1="http://payment.services.adyen.com"> + <ns1:paymentResult> + <additionalData xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <authCode xmlns="http://payment.services.adyen.com">1234</authCode> + <dccAmount xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <dccSignature xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <fraudResult xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <issuerUrl xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <md xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <paRequest xmlns="http://payment.services.adyen.com" xsi:nil="true"/> + <pspReference xmlns="http://payment.services.adyen.com">9876543210987654</pspReference> + <refusalReason xmlns="http://payment.services.adyen.com">You need to actually own money.</refusalReason> + <resultCode xmlns="http://payment.services.adyen.com">Refused</resultCode> + </ns1:paymentResult> + </ns1:authoriseResponse> + </soap:Body> +</soap:Envelope> +EOS + + AUTHORISATION_REQUEST_INVALID_RESPONSE = <<EOS +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <soap:Body> + <soap:Fault> + <faultcode>soap:Server</faultcode> + <faultstring>validation 101 Invalid card number</faultstring> + </soap:Fault> + </soap:Body> +</soap:Envelope> +EOS end class RecurringService LIST_LAYOUT = <<EOS <?xml version="1.0"?> @@ -464,9 +594,26 @@ </soap:Envelope> EOS RECURRING_DETAIL_PARTIAL = <<EOS <recurring:recurringDetailReference>%s</recurring:recurringDetailReference> +EOS + + # Test responses + + DISABLE_RESPONSE = <<EOS +<?xml version="1.0"?> +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <soap:Body> + <ns1:disableResponse xmlns:ns1="http://recurring.services.adyen.com"> + <ns1:result> + <response xmlns="http://recurring.services.adyen.com"> + %s + </response> + </ns1:result> + </ns1:disableResponse> + </soap:Body> +</soap:Envelope> EOS end end end