module Transbank::Webpay::Validations

Attributes

errors[R]

Public Instance Methods

errors_display() click to toggle source
# File lib/transbank/webpay/validations.rb, line 6
def errors_display
  errors.join ', '
end
valid?() click to toggle source
# File lib/transbank/webpay/validations.rb, line 10
def valid?
  errors.empty?
end
validate_response!() click to toggle source
# File lib/transbank/webpay/validations.rb, line 14
def validate_response!
  validate_response_code!
  validate_http_response!

  return if errors.any? || validate_document

  raise Transbank::Webpay::Exceptions::InvalidSignature, 'Invalid signature'
end

Private Instance Methods

calculated_digest() click to toggle source
# File lib/transbank/webpay/validations.rb, line 25
def calculated_digest
  node = doc.at_xpath('//soap:Body')
  node_canonicalize = node.canonicalize(Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0, nil, nil)
  digest = OpenSSL::Digest::SHA1.new.reset.digest(node_canonicalize)
  Base64.encode64(digest).strip
end
validate_digest() click to toggle source
# File lib/transbank/webpay/validations.rb, line 73
def validate_digest
  calculated_digest == digest
end
validate_document() click to toggle source
# File lib/transbank/webpay/validations.rb, line 56
def validate_document
  return false if signature_node.nil?
  validate_digest && validate_signature
end
validate_http_response!() click to toggle source
# File lib/transbank/webpay/validations.rb, line 49
def validate_http_response!
  return if content.class == Net::HTTPOK

  @errors += xml_error_display
  @errors << content.message if content.respond_to?(:message) && @errors.empty?
end
validate_response_code!() click to toggle source

Validations

# File lib/transbank/webpay/validations.rb, line 44
def validate_response_code!
  return if xml_response_code.empty?
  @errors << response_code_display if xml_response_code != '0'
end
validate_signature() click to toggle source
# File lib/transbank/webpay/validations.rb, line 61
def validate_signature
  signed_node_canonicalize = signed_node.canonicalize(
    Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0, ["soap"], nil
  )

  Transbank::Webpay::Vault.pub_key.verify(
    OpenSSL::Digest::SHA1.new,
    signature_decode,
    signed_node_canonicalize
  )
end