Class: Transbank::Webpay::Document

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/transbank/webpay/document.rb

Constant Summary collapse

XML_HEADER =

rubocop:disable LineLength

"<env:Header><wsse:Security xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' wsse:mustUnderstand='1'/></env:Header>".freeze
SOAPENV =
'http://schemas.xmlsoap.org/soap/envelope/'.freeze
NAMESPACES =
{
  'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
  'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  'xmlns:tns' => 'http://service.wswebpay.webpay.transbank.com/',
  'xmlns:env' => 'http://schemas.xmlsoap.org/soap/envelope/'
}.freeze

Constants included from Helper

Helper::XS_DATE_TIME, Helper::XS_INTEGER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#camelcase, #typecasting, #underscore, #xml_to_hash

Constructor Details

#initialize(action, params = {}) ⇒ Document

Returns a new instance of Document.



16
17
18
19
# File 'lib/transbank/webpay/document.rb', line 16

def initialize(action, params = {})
  camelcase_action = camelcase(action).to_sym
  @unsigned_xml = build_xml camelcase_action, params
end

Instance Attribute Details

#unsigned_xmlObject (readonly)

Returns the value of attribute unsigned_xml.



5
6
7
# File 'lib/transbank/webpay/document.rb', line 5

def unsigned_xml
  @unsigned_xml
end

Instance Method Details

#documentObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/transbank/webpay/document.rb', line 33

def document
  @document ||= Nokogiri::XML(signed_xml).tap do |signed_document|
    x509data = signed_document.at_xpath("//*[local-name()='X509Data']")
    new_data = x509data.clone
    new_data.set_attribute('xmlns:ds', 'http://www.w3.org/2000/09/xmldsig#')
    n = Nokogiri::XML::Node.new('wsse:SecurityTokenReference', signed_document)
    n.add_child(new_data)
    x509data.add_next_sibling(n)
  end
end

#envelopeObject



25
26
27
# File 'lib/transbank/webpay/document.rb', line 25

def envelope
  @envelope ||= unsigned_document.at_xpath("//env:Envelope")
end

#signed_xmlObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/transbank/webpay/document.rb', line 44

def signed_xml
  envelope.prepend_child(XML_HEADER)
  unsigned_xml = unsigned_document.to_s

  signer = Signer.new(unsigned_xml)
  signer.cert = Transbank::Webpay::Vault.cert
  signer.private_key = Transbank::Webpay::Vault.private_key

  signer.document.xpath('//soapenv:Body', soapenv: SOAPENV).each do |node|
    signer.digest!(node)
  end

  signer.sign!(:issuer_serial => true)
  signer.to_xml
end

#to_xmlObject



29
30
31
# File 'lib/transbank/webpay/document.rb', line 29

def to_xml
  document.to_xml(save_with: 0)
end

#unsigned_documentObject



21
22
23
# File 'lib/transbank/webpay/document.rb', line 21

def unsigned_document
  @unsigned_document ||= Nokogiri::XML(unsigned_xml)
end