libwebpayplus 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,312 @@
1
+ require 'signer'
2
+ require 'savon'
3
+ require_relative "verifier"
4
+
5
+
6
+ class WebpayMallNormal
7
+
8
+ def initialize(configuration)
9
+
10
+ @wsdl_path = ''
11
+ @ambient = configuration.environment
12
+
13
+ case @ambient
14
+ when 'INTEGRACION'
15
+ @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSWebpayService?wsdl'
16
+ when 'CERTIFICACION'
17
+ @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSWebpayService?wsdl'
18
+ when 'PRODUCCION'
19
+ @wsdl_path='https://webpay3g.transbank.cl/WSWebpayTransaction/cxf/WSWebpayService?wsdl'
20
+ else
21
+ #Por defecto esta el ambiente de INTEGRACION
22
+ @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSWebpayService?wsdl'
23
+ end
24
+
25
+ @commerce_code = configuration.commerce_code
26
+ @private_key = OpenSSL::PKey::RSA.new(configuration.private_key)
27
+ @public_cert = OpenSSL::X509::Certificate.new(configuration.public_cert)
28
+ @webpay_cert = OpenSSL::X509::Certificate.new(configuration.webpay_cert)
29
+ @store_codes = configuration.store_codes
30
+ @client = Savon.client(wsdl: @wsdl_path)
31
+
32
+ end
33
+
34
+ #######################################################
35
+ def initTransaction(buyOrder, sessionId, urlReturn, urlFinal, stores)
36
+
37
+ detailArray = Array.new
38
+
39
+ stores.each do |store|
40
+ wsTransactionDetail = {
41
+ "commerceCode" => store['storeCode'],
42
+ "amount" => store['amount'],
43
+ "buyOrder" => store['buyOrder']
44
+ }
45
+ detailArray.push(wsTransactionDetail)
46
+ end
47
+
48
+
49
+ inputComplete ={
50
+ "wsInitTransactionInput" => {
51
+ "wSTransactionType" => 'TR_MALL_WS',
52
+ "commerceId" => @commerce_code,
53
+ "sessionId" => sessionId,
54
+ "buyOrder" => buyOrder,
55
+ "returnURL" => urlReturn,
56
+ "finalURL" => urlFinal,
57
+ "transactionDetails" => detailArray
58
+ }
59
+ }
60
+
61
+ req = @client.build_request(:init_transaction, message: inputComplete)
62
+
63
+ #Firmar documento
64
+ document = sign_xml(req)
65
+ #document = Util.signXml(req)
66
+
67
+ puts document
68
+
69
+ begin
70
+ puts 'iniciando initMall...'
71
+ response = @client.call(:init_transaction) do
72
+ xml document.to_xml(:save_with => 0)
73
+ end
74
+ rescue Exception, RuntimeError => e
75
+ puts "Ocurrio un error en la llamada a Webpay: "+e.message
76
+ response_array ={
77
+ "error_desc" => "Ocurrio un error en la llamada a Webpay: "+e.message
78
+ }
79
+ return response_array
80
+ end
81
+
82
+ token=''
83
+ puts 'response: '+response.to_s
84
+
85
+ #Verificacion de certificado respuesta
86
+ tbk_cert = OpenSSL::X509::Certificate.new(@webpay_cert)
87
+
88
+ if !Verifier.verify(response, tbk_cert)
89
+ puts "El Certificado de respuesta es Invalido"
90
+ response_array ={
91
+ "error_desc" => 'El Certificado de respuesta es Invalido'
92
+ }
93
+ return response_array
94
+ else
95
+ puts "El Certificado de respuesta es Valido."
96
+ end
97
+
98
+
99
+ response_document = Nokogiri::HTML(response.to_s)
100
+ response_document.xpath("//token").each do |token_value|
101
+ token = token_value.text
102
+ end
103
+ url=''
104
+ response_document.xpath("//url").each do |url_value|
105
+ url = url_value.text
106
+ end
107
+
108
+ puts 'token: '+token
109
+ puts 'url: '+url
110
+
111
+
112
+ response_array ={
113
+ "token" => token.to_s,
114
+ "url" => url.to_s,
115
+ "error_desc" => 'TRX_OK'
116
+ }
117
+
118
+ return response_array
119
+ end
120
+
121
+
122
+
123
+ ##############################################
124
+ def getTransactionResult(token)
125
+
126
+ getResultInput ={
127
+ "tokenInput" => token
128
+ }
129
+
130
+ #Preparacion firma
131
+ req = @client.build_request(:get_transaction_result, message: getResultInput)
132
+
133
+ #firmar la peticion
134
+ document = sign_xml(req)
135
+
136
+ #Se realiza el getResult
137
+ begin
138
+ puts "Iniciando getTransactionResult Mall..."
139
+ response = @client.call(:get_transaction_result) do
140
+ xml document.to_xml(:save_with => 0)
141
+ end
142
+
143
+ rescue Exception, RuntimeError => e
144
+ puts "Ocurrio un error en la llamada a Webpay: "+e.message
145
+ response_array ={
146
+ "error_desc" => "Ocurrio un error en la llamada a Webpay: "+e.message
147
+ }
148
+ return response_array
149
+ end
150
+
151
+ #Se revisa que respuesta no sea nula.
152
+ if response
153
+ puts 'Respuesta getResult: '+ response.to_s
154
+ else
155
+ puts 'Webservice Webpay responde con null'
156
+ response_array ={
157
+ "error_desc" => 'Webservice Webpay responde con null'
158
+ }
159
+ return response_array
160
+ end
161
+
162
+ puts response
163
+
164
+ #Verificacion de certificado respuesta
165
+ tbk_cert = OpenSSL::X509::Certificate.new(@webpay_cert)
166
+
167
+ if !Verifier.verify(response, tbk_cert)
168
+ puts "El Certificado de respuesta es Invalido."
169
+ response_array ={
170
+ "error_desc" => 'Webservice Webpay responde con null'
171
+ }
172
+ return response_array
173
+ else
174
+ puts "El Certificado de respuesta es Valido."
175
+ end
176
+
177
+
178
+ token_obtenido=''
179
+ response = Nokogiri::HTML(response.to_s)
180
+
181
+
182
+ accountingDate = response.xpath("//accountingdate").text
183
+ buyOrder = response.xpath("//buyorder").text
184
+ cardNumber = response.xpath("//cardnumber").text
185
+
186
+ #ciclo
187
+ detailOutput = response.xpath("//detailoutput")
188
+
189
+ sessionId = response.xpath("//sessionid").text
190
+ transactionDate = response.xpath("//transactiondate").text
191
+ urlRedirection = response.xpath("//urlredirection").text
192
+ vci = response.xpath("//vci").text
193
+
194
+ response_array ={
195
+ "accountingDate" => accountingDate.to_s,
196
+ "buyOrder" => buyOrder.to_s,
197
+ "cardNumber" => cardNumber.to_s,
198
+ "detailOutput1" => detailOutput[0].to_s,
199
+ "detailOutput2" => detailOutput[1].to_s,
200
+ "sessionId" => sessionId.to_s,
201
+ "transactionDate" => transactionDate.to_s,
202
+ "urlRedirection" => urlRedirection.to_s,
203
+ "vci" => vci.to_s,
204
+ "error_desc" => 'TRX_OK'
205
+ }
206
+
207
+ puts 'detailOutput: '
208
+ puts detailOutput[0]
209
+
210
+ #Realizar el acknowledge
211
+ puts 'Iniciando acknowledge... '
212
+ acknowledgeTransaction(token)
213
+
214
+ return response_array
215
+ end
216
+
217
+
218
+
219
+ ################################
220
+ def acknowledgeTransaction(token)
221
+ acknowledgeInput ={
222
+ "tokenInput" => token
223
+ }
224
+
225
+ #Preparacion firma
226
+ req = @client.build_request(:acknowledge_transaction, message: acknowledgeInput)
227
+
228
+ #Se firma el body de la peticion
229
+ document = sign_xml(req)
230
+
231
+ #Se realiza el acknowledge_transaction
232
+ begin
233
+ puts "Iniciando acknowledge_transaction..."
234
+ response = @client.call(:acknowledge_transaction, message: acknowledgeInput) do
235
+ xml document.to_xml(:save_with => 0)
236
+ end
237
+
238
+ rescue Exception, RuntimeError => e
239
+ puts "Ocurrio un error en la llamada a Webpay: "+e.message
240
+ response_array ={
241
+ "error_desc" => "Ocurrio un error en la llamada a Webpay: "+e.message
242
+ }
243
+ return response_array
244
+ end
245
+
246
+ #Se revisa que respuesta no sea nula.
247
+ if response
248
+ puts 'Respuesta acknowledge_transaction: '+ response.to_s
249
+ else
250
+ puts 'Webservice Webpay responde con null'
251
+ response_array ={
252
+ "error_desc" => 'Webservice Webpay responde con null'
253
+ }
254
+ return response_array
255
+ end
256
+
257
+ #Verificacion de certificado respuesta
258
+ tbk_cert = OpenSSL::X509::Certificate.new(@webpay_cert)
259
+
260
+ if !Verifier.verify(response, tbk_cert)
261
+ puts "El Certificado de respuesta es Invalido."
262
+ response_array ={
263
+ "error_desc" => 'El Certificado de respuesta es Invalido'
264
+ }
265
+ return response_array
266
+ else
267
+ puts "El Certificado de respuesta es Valido."
268
+ end
269
+
270
+ response_array ={
271
+ "error_desc" => 'TRX_OK'
272
+ }
273
+ return response_array
274
+
275
+ end
276
+
277
+
278
+
279
+ def sign_xml (input_xml)
280
+
281
+ document = Nokogiri::XML(input_xml.body)
282
+ envelope = document.at_xpath("//env:Envelope")
283
+ 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>")
284
+ xml = document.to_s
285
+
286
+ signer = Signer.new(xml)
287
+
288
+ signer.cert = OpenSSL::X509::Certificate.new(@public_cert)
289
+ signer.private_key = OpenSSL::PKey::RSA.new(@private_key)
290
+
291
+ signer.document.xpath("//soapenv:Body", { "soapenv" => "http://schemas.xmlsoap.org/soap/envelope/" }).each do |node|
292
+ signer.digest!(node)
293
+ end
294
+
295
+ signer.sign!(:issuer_serial => true)
296
+ signed_xml = signer.to_xml
297
+
298
+ document = Nokogiri::XML(signed_xml)
299
+ x509data = document.at_xpath("//*[local-name()='X509Data']")
300
+ new_data = x509data.clone()
301
+ new_data.set_attribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#")
302
+
303
+ n = Nokogiri::XML::Node.new('wsse:SecurityTokenReference', document)
304
+ n.add_child(new_data)
305
+ x509data.add_next_sibling(n)
306
+
307
+ return document
308
+ end
309
+
310
+
311
+
312
+ end
@@ -0,0 +1,310 @@
1
+ require 'signer'
2
+ require 'savon'
3
+ require_relative "verifier"
4
+
5
+
6
+ class WebpayNormal
7
+
8
+
9
+ def initialize(configuration)
10
+
11
+ @wsdl_path = ''
12
+ @ambient = configuration.environment
13
+
14
+ case @ambient
15
+ when 'INTEGRACION'
16
+ @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSWebpayService?wsdl'
17
+ when 'CERTIFICACION'
18
+ @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSWebpayService?wsdl'
19
+ when 'PRODUCCION'
20
+ @wsdl_path='https://webpay3g.transbank.cl/WSWebpayTransaction/cxf/WSWebpayService?wsdl'
21
+ else
22
+ #Por defecto esta el ambiente de INTEGRACION
23
+ @wsdl_path='https://webpay3gint.transbank.cl/WSWebpayTransaction/cxf/WSWebpayService?wsdl'
24
+ end
25
+
26
+
27
+ @commerce_code = configuration.commerce_code
28
+ @private_key = OpenSSL::PKey::RSA.new(configuration.private_key)
29
+ @public_cert = OpenSSL::X509::Certificate.new(configuration.public_cert)
30
+ @webpay_cert = OpenSSL::X509::Certificate.new(configuration.webpay_cert)
31
+ @client = Savon.client(wsdl: @wsdl_path)
32
+
33
+ end
34
+
35
+
36
+ #######################################################
37
+ def initTransaction(amount, buyOrder, sessionId, urlReturn, urlFinal)
38
+
39
+
40
+ initInput ={
41
+ "wsInitTransactionInput" => {
42
+ "wSTransactionType" => "TR_NORMAL_WS",
43
+ "buyOrder" => buyOrder,
44
+ "sessionId" => sessionId,
45
+ "returnURL" => urlReturn,
46
+ "finalURL" => urlFinal,
47
+ "transactionDetails" => {
48
+ "sharesAmount" => nil,
49
+ "sharesNumber" => nil,
50
+ "amount" => amount,
51
+ "commerceCode" => @commerce_code,
52
+ "buyOrder" => buyOrder
53
+ }
54
+ }
55
+ }
56
+
57
+ req = @client.build_request(:init_transaction, message: initInput)
58
+
59
+ #Firmar documento
60
+ document = sign_xml(req)
61
+ puts document
62
+
63
+ begin
64
+ response = @client.call(:init_transaction) do
65
+ xml document.to_xml(:save_with => 0)
66
+ end
67
+ rescue Exception, RuntimeError => e
68
+ puts "Ocurrio un error en la llamada a Webpay: "+e.message
69
+ response_array ={
70
+ "error_desc" => "Ocurrio un error en la llamada a Webpay: "+e.message
71
+ }
72
+ return response_array
73
+ end
74
+
75
+ #Verificacion de certificado respuesta
76
+ tbk_cert = OpenSSL::X509::Certificate.new(@webpay_cert)
77
+
78
+ if !Verifier.verify(response, tbk_cert)
79
+ puts "El Certificado de respuesta es Invalido."
80
+ response_array ={
81
+ "error_desc" => 'El Certificado de respuesta es Invalido'
82
+ }
83
+ return response_array
84
+ else
85
+ puts "El Certificado de respuesta es Valido."
86
+ end
87
+
88
+
89
+ token=''
90
+ response_document = Nokogiri::HTML(response.to_s)
91
+ response_document.xpath("//token").each do |token_value|
92
+ token = token_value.text
93
+ end
94
+ url=''
95
+ response_document.xpath("//url").each do |url_value|
96
+ url = url_value.text
97
+ end
98
+
99
+ puts 'token: '+token
100
+ puts 'url: '+url
101
+
102
+
103
+
104
+ response_array ={
105
+ "token" => token.to_s,
106
+ "url" => url.to_s,
107
+ "error_desc" => "TRX_OK"
108
+ }
109
+
110
+ return_array ={
111
+ "request" => initInput['wsInitTransactionInput'],
112
+ "response" => response_array
113
+
114
+ }
115
+
116
+ return return_array
117
+ end
118
+
119
+
120
+ ##############################################
121
+ def getTransactionResult(token)
122
+
123
+ getResultInput ={
124
+ "tokenInput" => token
125
+ }
126
+
127
+ #Preparacion firma
128
+ req = @client.build_request(:get_transaction_result, message: getResultInput)
129
+ #firmar la peticion
130
+ document = sign_xml(req)
131
+
132
+ #Se realiza el getResult
133
+ begin
134
+ puts "Iniciando GetResult..."
135
+ response = @client.call(:get_transaction_result) do
136
+ xml document.to_xml(:save_with => 0)
137
+ end
138
+
139
+ rescue Exception, RuntimeError => e
140
+ puts "Ocurrio un error en la llamada a Webpay: "+e.message
141
+ response_array ={
142
+ "error_desc" => "Ocurrio un error en la llamada a Webpay: "+e.message
143
+ }
144
+ return response_array
145
+ end
146
+
147
+ #Se revisa que respuesta no sea nula.
148
+ if response
149
+ puts 'Respuesta getResult: '+ response.to_s
150
+ else
151
+ puts 'Webservice Webpay responde con null'
152
+ response_array ={
153
+ "error_desc" => 'Webservice Webpay responde con null'
154
+ }
155
+ return response_array
156
+ end
157
+
158
+ #Verificacion de certificado respuesta
159
+ tbk_cert = OpenSSL::X509::Certificate.new(@webpay_cert)
160
+
161
+ if !Verifier.verify(response, tbk_cert)
162
+ puts "El Certificado de respuesta es Invalido"
163
+ response_array ={
164
+ "error_desc" => 'El Certificado de respuesta es Invalido'
165
+ }
166
+ return response_array
167
+ else
168
+ puts "El Certificado de respuesta es Valido."
169
+ end
170
+
171
+
172
+ response_document = Nokogiri::HTML(response.to_s)
173
+
174
+ puts response_document
175
+
176
+ accountingdate = response_document.xpath("//accountingdate").text
177
+ buyorder = response_document.at_xpath("//buyorder").text
178
+ cardnumber = response_document.xpath("//cardnumber").text
179
+ amount = response_document.xpath("//amount").text
180
+ commercecode = response_document.xpath("//commercecode").text
181
+ authorizationcode = response_document.xpath("//authorizationcode").text
182
+ paymenttypecode = response_document.xpath("//paymenttypecode").text
183
+ responsecode = response_document.xpath("//responsecode").text
184
+ transactiondate = response_document.xpath("//transactiondate").text
185
+ urlredirection = response_document.xpath("//urlredirection").text
186
+ vci = response_document.xpath("//vci").text
187
+ sharesnumber = response_document.xpath("//sharesnumber").text
188
+ sessionid = response_document.xpath("///sessionid").text
189
+
190
+ response_array = {
191
+ "accountingDate" => accountingdate.to_s,
192
+ "buyOrder" => buyorder.to_s,
193
+ "cardNumber" => cardnumber.to_s,
194
+ "cardExpirationDate" => nil,
195
+ "authorizationcode" => authorizationcode.to_s,
196
+ "paymenttypecode" => paymenttypecode.to_s,
197
+ "responsecode" => responsecode.to_s,
198
+ "sharesnumber" => sharesnumber.to_s,
199
+ "amount" => amount.to_s,
200
+ "commercecode" => commercecode.to_s,
201
+ "sessionid" => sessionid.to_s,
202
+ "transactiondate" => transactiondate.to_s,
203
+ "urlredirection" => urlredirection.to_s,
204
+ "vci" => vci.to_s,
205
+ "error_desc" => 'TRX_OK'
206
+ }
207
+
208
+ # No se realiza el acknoledge en el transactionresult, debido a que para hacer pruebas de duplicidad
209
+ # no se debe ejecutar el acknoledge.
210
+
211
+ # Realizar el acknowledge
212
+ # puts 'Se inicia acknowledgeTransaction...'
213
+ # acknowledgeTransaction(token)
214
+
215
+ puts 'response normal:...'
216
+ return response_array
217
+ end
218
+
219
+
220
+ ################################
221
+ def acknowledgeTransaction(token)
222
+ acknowledgeInput ={
223
+ "tokenInput" => token
224
+ }
225
+
226
+ #Preparacion firma
227
+ req = @client.build_request(:acknowledge_transaction, message: acknowledgeInput)
228
+
229
+ #Se firma el body de la peticion
230
+ document = sign_xml(req)
231
+
232
+ #Se realiza el acknowledge_transaction
233
+ begin
234
+ puts "Iniciando acknowledge_transaction..."
235
+ response = @client.call(:acknowledge_transaction, message: acknowledgeInput) do
236
+ xml document.to_xml(:save_with => 0)
237
+ end
238
+
239
+ rescue Exception, RuntimeError => e
240
+ puts "Ocurrio un error en la llamada a Webpay: "+e.message
241
+ response_array ={
242
+ "error_desc" => "Ocurrio un error en la llamada a Webpay: "+e.message
243
+ }
244
+ return response_array
245
+ end
246
+
247
+ #Se revisa que respuesta no sea nula.
248
+ if response
249
+ puts 'Respuesta acknowledge_transaction: '+ response.to_s
250
+ else
251
+ puts 'Webservice Webpay responde con null'
252
+ response_array ={
253
+ "error_desc" => 'Webservice Webpay responde con null'
254
+ }
255
+ return response_array
256
+ end
257
+
258
+ #Verificacion de certificado respuesta
259
+ tbk_cert = OpenSSL::X509::Certificate.new(@webpay_cert)
260
+
261
+ if !Verifier.verify(response, tbk_cert)
262
+ puts "El Certificado de respuesta es Invalido."
263
+ response_array ={
264
+ "error_desc" => 'El Certificado de respuesta es Invalido'
265
+ }
266
+ return response_array
267
+ else
268
+ puts "El Certificado de respuesta es Valido."
269
+ end
270
+
271
+ response_array ={
272
+ "error_desc" => 'TRX_OK'
273
+ }
274
+ return response_array
275
+
276
+ end
277
+
278
+
279
+ def sign_xml (input_xml)
280
+
281
+ document = Nokogiri::XML(input_xml.body)
282
+ envelope = document.at_xpath("//env:Envelope")
283
+ 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>")
284
+ xml = document.to_s
285
+
286
+ signer = Signer.new(xml)
287
+
288
+ signer.cert = OpenSSL::X509::Certificate.new(@public_cert)
289
+ signer.private_key = OpenSSL::PKey::RSA.new(@private_key)
290
+
291
+ signer.document.xpath("//soapenv:Body", { "soapenv" => "http://schemas.xmlsoap.org/soap/envelope/" }).each do |node|
292
+ signer.digest!(node)
293
+ end
294
+
295
+ signer.sign!(:issuer_serial => true)
296
+ signed_xml = signer.to_xml
297
+
298
+ document = Nokogiri::XML(signed_xml)
299
+ x509data = document.at_xpath("//*[local-name()='X509Data']")
300
+ new_data = x509data.clone()
301
+ new_data.set_attribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#")
302
+
303
+ n = Nokogiri::XML::Node.new('wsse:SecurityTokenReference', document)
304
+ n.add_child(new_data)
305
+ x509data.add_next_sibling(n)
306
+
307
+ return document
308
+ end
309
+
310
+ end