Class: WebpayNullify

Inherits:
Object
  • Object
show all
Defined in:
lib/webpaynullify.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ WebpayNullify

Returns a new instance of WebpayNullify.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/webpaynullify.rb', line 8

def initialize(configuration)

  @wsdl_path = ''
  @ambient = configuration.environment

  case @ambient
    when 'INTEGRACION'
      @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSCommerceIntegrationService?wsdl'
    when 'CERTIFICACION'
      @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSCommerceIntegrationService?wsdl'
    when 'PRODUCCION'
      @wsdl_path='https://webpay3g.transbank.cl/WSWebpayTransaction/cxf/WSCommerceIntegrationService?wsdl'
    else
      #Por defecto esta el ambiente de INTEGRACION
      @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSCommerceIntegrationService?wsdl'
  end

  @commerce_code = configuration.commerce_code
  @private_key = OpenSSL::PKey::RSA.new(configuration.private_key)
  @public_cert = OpenSSL::X509::Certificate.new(configuration.public_cert)
  @webpay_cert = OpenSSL::X509::Certificate.new(configuration.webpay_cert)
  @client = Savon.client(wsdl: @wsdl_path)

end

Instance Method Details

#nullify(authorizationCode, authorizedAmount, buyOrder, nullifyAmount, commercecode) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/webpaynullify.rb', line 35

def nullify(authorizationCode, authorizedAmount, buyOrder, nullifyAmount, commercecode)


  nullifyInput ={
      "nullificationInput" => {
          "authorizationCode" => authorizationCode,
          "authorizedAmount" => authorizedAmount,
          "buyOrder" => buyOrder,
          "commerceId" => commercecode,
          "nullifyAmount" => nullifyAmount
      }
  }

  req = @client.build_request(:nullify, message: nullifyInput)

  #Firmar documento
  document = sign_xml(req)
  puts document

  begin
    response = @client.call(:nullify) do
      xml document.to_xml(:save_with => 0)
    end
  rescue Exception ,RuntimeError => e
    puts "Ocurrio un error en la llamada a Webpay: "+e.message
    response_array ={
        "error_desc" => "Ocurrio un error en la llamada a Webpay: "+e.message
    }

    return response_array
  end

  #Verificacion de certificado respuesta
  tbk_cert = OpenSSL::X509::Certificate.new(@webpay_cert)

  if !Verifier.verify(response, tbk_cert)
    puts "El Certificado de respuesta es Invalido."
    response_array ={
        "error_desc" => 'El Certificado de respuesta es Invalido'
    }
    return response_array
  else
    puts "El Certificado de respuesta es Valido."
  end


  response_document = Nokogiri::HTML(response.to_s)

  authorizationCode = response_document.xpath("//authorizationcode").text
  authorizationDate = response_document.xpath("//authorizationdate").text
  balance 				  = response_document.xpath("//balance").text
  nullifiedAmount 	= response_document.xpath("//nullifiedamount").text
  token 						= response_document.xpath("//token").text

  response_array ={
      "authorizationCode" => authorizationCode.to_s,
      "authorizationDate" => authorizationDate.to_s,
      "balance" 				  => balance.to_s,
      "nullifiedAmount" 	=> nullifiedAmount.to_s,
      "token" 						=> token.to_s,
      "error_desc"        => 'TRX_OK'
  }

  puts 'respuesta: '
  puts response_document

  return response_array
end

#sign_xml(input_xml) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/webpaynullify.rb', line 106

def sign_xml (input_xml)

  document = Nokogiri::XML(input_xml.body)
  envelope = document.at_xpath("//env:Envelope")
  envelope.prepend_child("<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>")
  xml = document.to_s

  signer = Signer.new(xml)

  signer.cert = OpenSSL::X509::Certificate.new(@public_cert)
  signer.private_key = OpenSSL::PKey::RSA.new(@private_key)

  signer.document.xpath("//soapenv:Body", { "soapenv" => "http://schemas.xmlsoap.org/soap/envelope/" }).each do |node|
    signer.digest!(node)
  end

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

  document = Nokogiri::XML(signed_xml)
  x509data = 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', document)
  n.add_child(new_data)
  x509data.add_next_sibling(n)

  return document
end