Module: Adyen::Matchers::XPathPaymentFormCheck

Defined in:
lib/adyen/matchers.rb

Class Method Summary collapse

Class Method Details

.build_xpath_query(checks) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/adyen/matchers.rb', line 9

def self.build_xpath_query(checks)
  # Start by finding the check for the Adyen form tag
  xpath_query =  "//form[@action='#{Adyen::Form.url}']"

  # Add recurring/single check if specified
  recurring =  checks.delete(:recurring)
  unless recurring.nil?
    if recurring
      xpath_query << "[descendant::input[@type='hidden'][@name='recurringContract']]"
    else
      xpath_query << "[not(descendant::input[@type='hidden'][@name='recurringContract'])]"
    end
  end

  # Add a check for all the other fields specified
  checks.each do |key, value|
    condition  = "descendant::input[@type='hidden'][@name='#{key.to_s.camelize(:lower)}']"
    condition << "[@value='#{value}']" unless value == :anything
    xpath_query << "[#{condition}]"
  end

  return xpath_query
end

.check(subject, checks = {}) ⇒ Object



45
46
47
# File 'lib/adyen/matchers.rb', line 45

def self.check(subject, checks = {})
  !!REXML::XPath.first(document(subject), build_xpath_query(checks))
end

.document(subject) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/adyen/matchers.rb', line 33

def self.document(subject)
  if String === subject
    REXML::Document.new(subject.to_s)
  elsif subject.respond_to?(:body)
    REXML::Document.new(subject.body.to_s)
  elsif REXML::Document === subject
    subject
  else
    raise "Cannot handle this XML input type"
  end
end