Class: SunatInvoice::Signature

Inherits:
Model
  • Object
show all
Includes:
Utils
Defined in:
lib/sunat_invoice/signature.rb

Constant Summary collapse

C14N_ALGORITHM =
'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'
DIGEST_ALGORITHM =
'http://www.w3.org/2000/09/xmldsig#sha1'
SIGNATURE_ALGORITHM =
'http://www.w3.org/2000/09/xmldsig#rsa-sha1'
TRANSFORMATION_ALGORITHM =
'http://www.w3.org/2000/09/xmldsig#enveloped-signature'

Constants included from Utils

Utils::COMMON_NAMESPACES, Utils::CREDIT_NOTE_NAMESPACES, Utils::DAILY_SUMMARY_NAMESPACES, Utils::DEBIT_NOTE_NAMESPACES, Utils::INVOICE_NAMESPACES, Utils::SUMMARY_NAMESPACES, Utils::TRADE_NAMESPACES, Utils::VOIDED_NAMESPACES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#amount_xml, #ubl_ext

Methods inherited from Model

#initialize, #to_hash

Constructor Details

This class inherits a constructor from SunatInvoice::Model

Instance Attribute Details

#providerObject

Returns the value of attribute provider.



17
18
19
# File 'lib/sunat_invoice/signature.rb', line 17

def provider
  @provider
end

Instance Method Details

#build_digital_attachment(xml) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/sunat_invoice/signature.rb', line 38

def build_digital_attachment(xml)
  xml['cac'].DigitalSignatureAttachment do
    xml['cac'].ExternalReference do
      xml['cbc'].URI "##{provider.signature_location_id}"
    end
  end
end

#build_signatory_party(xml) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/sunat_invoice/signature.rb', line 27

def build_signatory_party(xml)
  xml['cac'].SignatoryParty do
    xml['cac'].PartyIdentification do
      xml['cbc'].ID provider.ruc
    end
    xml['cac'].PartyName do
      xml['cbc'].Name provider.name
    end
  end
end

#build_transforms(xml) ⇒ Object



73
74
75
76
77
# File 'lib/sunat_invoice/signature.rb', line 73

def build_transforms(xml)
  xml['ds'].Transforms do
    xml['ds'].Transform Algorithm: TRANSFORMATION_ALGORITHM
  end
end

#sign(invoice_xml) ⇒ Object



46
47
48
49
50
# File 'lib/sunat_invoice/signature.rb', line 46

def sign(invoice_xml)
  options = { id_attr: provider.signature_location_id }
  doc = Xmldsig::SignedDocument.new(invoice_xml, options)
  doc.sign(private_key)
end

#signature_ext(xml) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/sunat_invoice/signature.rb', line 52

def signature_ext(xml)
  ubl_ext(xml) do
    xml['ds'].Signature(Id: provider.signature_location_id) do
      signed_info xml
      signature_value xml
    end
  end
end

#signature_value(xml) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/sunat_invoice/signature.rb', line 79

def signature_value(xml)
  xml['ds'].SignatureValue
  xml['ds'].KeyInfo do
    xml['ds'].X509Data do
      xml['ds'].X509Certificate encoded_certificate
    end
  end
end

#signed_info(xml) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sunat_invoice/signature.rb', line 61

def signed_info(xml)
  xml['ds'].SignedInfo do
    xml['ds'].CanonicalizationMethod Algorithm: C14N_ALGORITHM
    xml['ds'].SignatureMethod Algorithm: SIGNATURE_ALGORITHM
    xml['ds'].Reference URI: '' do
      build_transforms(xml)
      xml['ds'].DigestMethod Algorithm: DIGEST_ALGORITHM
      xml['ds'].DigestValue
    end
  end
end

#signer_data(xml) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/sunat_invoice/signature.rb', line 19

def signer_data(xml)
  xml['cac'].Signature do
    xml['cbc'].ID provider.signature_id
    build_signatory_party(xml)
    build_digital_attachment(xml)
  end
end