class Transbank::Oneclick::Response

Constants

RESPONSE_CODE

Attributes

action[RW]
attributes[RW]
content[RW]
errors[RW]
exception[RW]

Public Class Methods

new(content, action) click to toggle source
# File lib/transbank/oneclick/response.rb, line 27
def initialize(content, action)
  self.content = content
  self.action = action
  self.attributes = Hash[*xml_result.map{|e| [e.name.underscore.to_sym, e.text]}.flatten]
  self.errors = []
  validate!
end

Public Instance Methods

body() click to toggle source
# File lib/transbank/oneclick/response.rb, line 35
def body
  content.body
end
doc() click to toggle source
# File lib/transbank/oneclick/response.rb, line 43
def doc
  @doc ||= Nokogiri::XML body
end
errors_display() click to toggle source
# File lib/transbank/oneclick/response.rb, line 55
def errors_display
  errors.join ', '
end
exception?() click to toggle source
# File lib/transbank/oneclick/response.rb, line 90
def exception?
  false
end
http_code() click to toggle source
# File lib/transbank/oneclick/response.rb, line 39
def http_code
  content.code
end
inspect() click to toggle source
# File lib/transbank/oneclick/response.rb, line 83
def inspect
  result = ["valid: #{valid?}"]
  result << attributes_display if attributes.any?
  result << "error: \"#{errors_display}\"" if errors.any?
  "#<#{self.class} #{result.join(', ')} >"
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/transbank/oneclick/response.rb, line 98
def method_missing(method_name, *args, &block)
  attributes[method_name.to_sym] || super
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/transbank/oneclick/response.rb, line 102
def respond_to_missing?(method_name, include_private = false)
  attributes.keys.include?(method_name.to_sym) || super
end
response_code_display() click to toggle source
# File lib/transbank/oneclick/response.rb, line 75
def response_code_display
  if respond_to?(:response_code)
    # key = RESPONSE_CODE.keys.include?(action) ? action : :default
    # RESPONSE_CODE[key].fetch(response_code, response_code)
    RESPONSE_CODE[action] && RESPONSE_CODE[action][response_code] || RESPONSE_CODE[:default][response_code] || response_code
  end
end
signature_decode() click to toggle source
# File lib/transbank/oneclick/response.rb, line 71
def signature_decode
  Base64.decode64(signature_node.content)
end
signature_node() click to toggle source
# File lib/transbank/oneclick/response.rb, line 67
def signature_node
  doc.at_xpath('//ds:SignatureValue', {'ds' => 'http://www.w3.org/2000/09/xmldsig#'})
end
signed_node() click to toggle source
# File lib/transbank/oneclick/response.rb, line 63
def signed_node
  doc.at_xpath '//ds:SignedInfo', {'ds' => 'http://www.w3.org/2000/09/xmldsig#'}
end
valid?() click to toggle source
# File lib/transbank/oneclick/response.rb, line 59
def valid?
  errors.empty?
end
xml_error() click to toggle source
# File lib/transbank/oneclick/response.rb, line 51
def xml_error
  doc.xpath("//faultstring")
end
xml_result() click to toggle source
# File lib/transbank/oneclick/response.rb, line 47
def xml_result
  doc.at_xpath("//return") && doc.at_xpath("//return").children || []
end

Private Instance Methods

attributes_display() click to toggle source
# File lib/transbank/oneclick/response.rb, line 125
def attributes_display
  attributes.map{|name, value| "#{name}: \"#{value}\""}.join ', '
end
pub_key() click to toggle source
# File lib/transbank/oneclick/response.rb, line 121
def pub_key
  server_cert.public_key
end
server_cert() click to toggle source
# File lib/transbank/oneclick/response.rb, line 117
def server_cert
  @server_cert ||= OpenSSL::X509::Certificate.new File.read(Transbank::Oneclick.configuration.server_cert_path)
end
validate!() click to toggle source
# File lib/transbank/oneclick/response.rb, line 129
def validate!
  if action =~ /finishInscription|Authorize|/ and respond_to?(:response_code) and response_code != '0'
    self.errors << response_code_display
  end

  if action =~ /removeUser/ and respond_to?(:text) and text != 'true'
    self.errors << 'imposible eliminar la inscripción'
  end

  if action =~ /codeReverseOneClick/ and respond_to?(:reversed) and reversed != 'true'
    self.errors << 'imposible revertir la compra'
  end

  if content.class != Net::HTTPOK
    self.errors += xml_error.map(&:text)
    self.errors << content.message if content.respond_to?(:message)
  end

  if (self.errors.blank? || signature_node.present?) && !verify
    raise Exceptions::InvalidSignature.new("Invalid signature")
  end
end
verify() click to toggle source
# File lib/transbank/oneclick/response.rb, line 108
def verify
  return if signature_node.nil?

  signed_node.add_namespace 'soap', 'http://schemas.xmlsoap.org/soap/envelope/'
  signed_node_canonicalize = signed_node.canonicalize Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0, ["soap"], nil

  pub_key.verify OpenSSL::Digest::SHA1.new, signature_decode, signed_node_canonicalize
end