Class: WebpayCapture
- Inherits:
-
Object
- Object
- WebpayCapture
- Defined in:
- lib/webpaycapture.rb
Instance Method Summary collapse
- #capture(authorizationCode, captureAmount, buyOrder) ⇒ Object
-
#initialize(configuration) ⇒ WebpayCapture
constructor
A new instance of WebpayCapture.
- #sign_xml(input_xml) ⇒ Object
Constructor Details
#initialize(configuration) ⇒ WebpayCapture
Returns a new instance of WebpayCapture.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/webpaycapture.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
#capture(authorizationCode, captureAmount, buyOrder) ⇒ Object
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 |
# File 'lib/webpaycapture.rb', line 36 def capture(, captureAmount, buyOrder) captureInput ={ "captureInput" => { "captureAmount" => captureAmount, "buyOrder" => buyOrder, "commerceId" => @commerce_code, "authorizationCode" => } } req = @client.build_request(:capture, message: captureInput) #Firmar documento document = sign_xml(req) puts document begin response = @client.call(:capture) do xml document.to_xml(:save_with => 0) end rescue Exception, RuntimeError => e response_array ={ "error_desc" => "Ocurrio un error en la llamada a Webpay: "+e. } 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) puts 'response capture: ' puts response_document = response_document.xpath("//authorizationcode").text = response_document.xpath("//authorizationdate").text capturedAmount = response_document.xpath("//capturedamount").text token = response_document.xpath("//token").text response_array ={ "authorizationCode" => .to_s, "authorizationDate" => .to_s, "capturedAmount" => capturedAmount.to_s, "token" => token.to_s, "error_desc" => "TRX_OK" } return response_array end |
#sign_xml(input_xml) ⇒ Object
103 104 105 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 |
# File 'lib/webpaycapture.rb', line 103 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 |