Class: Transbank::Webpay::Document

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Document.



8
9
10
11
12
13
# File 'lib/transbank/webpay/document.rb', line 8

def initialize(wsdl_url, action, params = {})
  client = Savon.client(wsdl: wsdl_url)
  xml = client.build_request(action, message: params).body

  @unsigned_document = Nokogiri::XML(xml)
end

Instance Attribute Details

#unsigned_documentObject (readonly)

Returns the value of attribute unsigned_document.



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

def unsigned_document
  @unsigned_document
end

#unsigned_xmlObject (readonly)

Returns the value of attribute unsigned_xml.



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

def unsigned_xml
  @unsigned_xml
end

Instance Method Details

#certObject



50
51
52
# File 'lib/transbank/webpay/document.rb', line 50

def cert
  @cert ||= OpenSSL::X509::Certificate.new open(Transbank::Webpay.configuration.cert_path)
end

#documentObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/transbank/webpay/document.rb', line 23

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



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

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

#private_keyObject



54
55
56
# File 'lib/transbank/webpay/document.rb', line 54

def private_key
  @private_key ||= OpenSSL::PKey::RSA.new open(Transbank::Webpay.configuration.key_path)
end

#signed_xmlObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/transbank/webpay/document.rb', line 34

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

  signer = Signer.new(unsigned_xml)
  signer.cert = cert
  signer.private_key = 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



19
20
21
# File 'lib/transbank/webpay/document.rb', line 19

def to_xml
  document.to_xml(save_with: 0)
end