cieloz 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -54,9 +54,24 @@ module Cieloz
54
54
  txn
55
55
  end
56
56
 
57
+ def consulta source, opts={}
58
+ tid = attrs_from source, opts, :tid
59
+ RequisicaoConsulta.new tid: tid
60
+ end
61
+
62
+ def captura source, opts={}
63
+ tid, valor = attrs_from source, opts, :tid, :valor
64
+ RequisicaoCaptura.new tid: tid, valor: valor
65
+ end
66
+
67
+ def cancelamento source, opts={}
68
+ tid, valor = attrs_from source, opts, :tid, :valor
69
+ RequisicaoCancelamento.new tid: tid, valor: valor
70
+ end
71
+
57
72
  private
58
73
  def attrs_from source, opts, *keys
59
- keys.map { |k|
74
+ attrs = keys.map { |k|
60
75
  value_or_attr_name = opts[k] || k
61
76
  if value_or_attr_name.is_a? Symbol
62
77
  source.send value_or_attr_name if source.respond_to? value_or_attr_name
@@ -64,6 +79,7 @@ module Cieloz
64
79
  value_or_attr_name
65
80
  end
66
81
  }
82
+ attrs.count == 1 ? attrs.first : attrs
67
83
  end
68
84
  end
69
85
 
@@ -18,11 +18,12 @@ module Cieloz
18
18
 
19
19
  def self.reset!
20
20
  cielo_mode!
21
- @@credenciais = nil
22
21
  end
23
22
 
24
23
  def self.store_mode!
25
24
  @@mode = :store
25
+ @@dados_ec = nil
26
+ @@credenciais = nil
26
27
  end
27
28
 
28
29
  def self.store_mode?
@@ -31,6 +32,8 @@ module Cieloz
31
32
 
32
33
  def self.cielo_mode!
33
34
  @@mode = :cielo
35
+ @@dados_ec = nil
36
+ @@credenciais = nil
34
37
  end
35
38
 
36
39
  def self.cielo_mode?
@@ -42,7 +45,7 @@ module Cieloz
42
45
  return (@@dados_ec = Requisicao::DadosEc.new @@credenciais) if @@credenciais
43
46
 
44
47
  mode = store_mode? ? :LOJA : :CIELO
45
- Homologacao::Credenciais.const_get mode
48
+ @@dados_ec = Requisicao::DadosEc.new Homologacao::Credenciais.const_get mode
46
49
  end
47
50
 
48
51
  def self.host
@@ -6,9 +6,8 @@ class Cieloz::Requisicao
6
6
 
7
7
  attr_accessor :id, :versao
8
8
  attr_reader :dados_ec
9
- hattr_writer :dados_ec
10
9
 
11
- validates :id, :versao, :dados_ec, presence: true
10
+ validates :dados_ec, presence: true
12
11
 
13
12
  def attributes
14
13
  { dados_ec: @dados_ec }
@@ -36,10 +35,12 @@ class Cieloz::Requisicao
36
35
  end
37
36
 
38
37
  def submit
39
- @id = SecureRandom.uuid if id.blank?
40
- @versao = "1.2.0" if versao.blank?
38
+ @dados_ec = Cieloz::Configuracao.credenciais
41
39
 
42
40
  if valid?
41
+ @id = SecureRandom.uuid if id.blank?
42
+ @versao = "1.2.0" if versao.blank?
43
+
43
44
  http = Net::HTTP.new Cieloz::Configuracao.host, 443
44
45
  http.use_ssl = true
45
46
  http.open_timeout = 5 * 1000
@@ -1,3 +1,3 @@
1
1
  module Cieloz
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -25,9 +25,10 @@ describe "Integration test" do
25
25
  autorizacao.nao_capturar_automaticamente
26
26
 
27
27
  txn = autorizacao.submit
28
- assert_equal _::Transacao, txn.class, "#{txn.codigo}:\n#{txn.mensagem}"
28
+
29
+ assert_equal _::Transacao, txn.class, "#{txn.status}:\n#{txn.xml}"
29
30
  assert_equal({}, autorizacao.errors.messages)
30
- assert txn.criada?
31
+ assert txn.criada?, "#{autorizacao.to_xml}\n#{txn.xml}"
31
32
 
32
33
  assert_equal "302", post_credit_card_on_cielo_page(txn.url_autenticacao).code
33
34
 
@@ -36,26 +37,28 @@ describe "Integration test" do
36
37
  consulta = Cieloz::RequisicaoConsulta.new params
37
38
  cst = consulta.submit
38
39
  assert_equal({}, consulta.errors.messages)
39
- assert cst.autorizada?
40
+ assert cst.autorizada?, "#{consulta.to_xml}\n#{cst.xml}"
40
41
 
41
42
  captura = Cieloz::RequisicaoCaptura.new params
42
43
  cap = captura.submit
43
44
  assert_equal({}, captura.errors.messages)
44
- assert cap.capturada?
45
+ assert cap.capturada?, "#{captura.to_xml}\n#{cap.xml}"
45
46
 
46
47
  cancelar = Cieloz::RequisicaoCancelamento.new params
47
48
  cnc = cancelar.submit
48
49
  assert_equal({}, cancelar.errors.messages)
49
- assert cnc.cancelada?
50
+ assert cnc.cancelada?, "#{cancelar.to_xml}\n#{cnc.xml}"
50
51
  end
51
52
 
52
53
  it "Captura Automaticamente, Consulta e Cancela" do
53
54
  autorizacao.capturar_automaticamente
54
55
 
55
56
  txn = autorizacao.submit
56
- assert_equal _::Transacao, txn.class, "#{txn.codigo}:\n#{txn.mensagem}"
57
+ p autorizacao.to_xml
58
+
59
+ assert_equal _::Transacao, txn.class, "#{txn.status}:\n#{txn.xml}"
57
60
  assert_equal({}, autorizacao.errors.messages)
58
- assert txn.criada?
61
+ assert txn.criada?, "#{autorizacao.to_xml}\n#{txn.xml}"
59
62
 
60
63
  assert_equal "302", post_credit_card_on_cielo_page(txn.url_autenticacao).code
61
64
 
@@ -64,12 +67,12 @@ describe "Integration test" do
64
67
  consulta = Cieloz::RequisicaoConsulta.new params
65
68
  cst = consulta.submit
66
69
  assert_equal({}, consulta.errors.messages)
67
- assert cst.capturada?
70
+ assert cst.capturada?, "#{consulta.to_xml}\n#{cst.xml}"
68
71
 
69
72
  cancelar = Cieloz::RequisicaoCancelamento.new params
70
73
  cnc = cancelar.submit
71
74
  assert_equal({}, cancelar.errors.messages)
72
- assert cnc.cancelada?
75
+ assert cnc.cancelada?, "#{cancelar.to_xml}\n#{cnc.xml}"
73
76
  end
74
77
 
75
78
  def post_credit_card_on_cielo_page url_cielo, bandeira=:visa,
@@ -7,6 +7,9 @@ describe Cieloz::Builder do
7
7
  @source = Object.new
8
8
  def @source.numero ; 123456 end
9
9
  def @source.valor ; 7890 end
10
+
11
+ def @source.number ; 24680 end
12
+ def @source.value ; 13579 end
10
13
  end
11
14
 
12
15
  describe "Pedido building" do
@@ -35,8 +38,6 @@ describe Cieloz::Builder do
35
38
 
36
39
  describe "opts given" do
37
40
  before do
38
- def @source.number ; 24680 end
39
- def @source.value ; 13579 end
40
41
  def @source.description ; "abc" end
41
42
  def @source.time ; Time.at 0 end
42
43
  def @source.currency ; 123 end
@@ -209,4 +210,76 @@ describe Cieloz::Builder do
209
210
  }
210
211
  end
211
212
  end
213
+
214
+ describe "TID ops Building" do
215
+ before do
216
+ def @source.tid ; "A1B2C3" end
217
+ def @source.transaction_id ; "Z3Y2X1" end
218
+ end
219
+
220
+ describe "Consulta" do
221
+ it "from tid when missing opts" do
222
+ _.consulta(@source).tid.must_equal @source.tid
223
+ end
224
+
225
+ it "from mapped attributes" do
226
+ _.consulta(@source, tid: :transaction_id).tid.must_equal @source.transaction_id
227
+ end
228
+
229
+ it "from values" do
230
+ transaction_id = "ABC123"
231
+ _.consulta(@source, tid: transaction_id).tid.must_equal transaction_id
232
+ end
233
+ end
234
+
235
+ describe "Captura" do
236
+ it "is a RequisicaoCaptura" do
237
+ _.captura(@source).is_a? _::RequisicaoCaptura
238
+ end
239
+
240
+ it "from tid and valor when missing opts" do
241
+ captura = _.captura @source
242
+ captura.tid.must_equal @source.tid
243
+ captura.valor.must_equal @source.valor
244
+ end
245
+
246
+ it "from mapped attributes" do
247
+ captura = _.captura @source, tid: :transaction_id, valor: :value
248
+ captura.tid.must_equal @source.transaction_id
249
+ captura.valor.must_equal @source.value
250
+ end
251
+
252
+ it "from values" do
253
+ tid, valor = "ABC123", 34567
254
+ captura = _.captura @source, tid: tid, valor: valor
255
+ captura.tid.must_equal tid
256
+ captura.valor.must_equal valor
257
+ end
258
+ end
259
+
260
+ describe "Cancelamento" do
261
+ it "is a RequisicaoCaptura" do
262
+ _.cancelamento(@source).is_a? _::RequisicaoCancelamento
263
+ end
264
+
265
+ it "from tid and valor when missing opts" do
266
+ cancelamento = _.cancelamento @source
267
+ cancelamento.tid.must_equal @source.tid
268
+ cancelamento.valor.must_equal @source.valor
269
+ end
270
+
271
+ it "from mapped attributes" do
272
+ cancelamento = _.cancelamento @source, tid: :transaction_id, valor: :value
273
+ cancelamento.tid.must_equal @source.transaction_id
274
+ cancelamento.valor.must_equal @source.value
275
+ end
276
+
277
+ it "from values" do
278
+ tid, valor = "ABC123", 34567
279
+ cancelamento = _.cancelamento @source, tid: tid, valor: valor
280
+ cancelamento.tid.must_equal tid
281
+ cancelamento.valor.must_equal valor
282
+ end
283
+ end
284
+ end
212
285
  end
@@ -43,18 +43,24 @@ describe Cieloz::Configuracao do
43
43
 
44
44
  describe "credenciais" do
45
45
  describe "not set" do
46
+ let(:loja_ec) { Cieloz::Requisicao::DadosEc.new Cieloz::Homologacao::Credenciais::LOJA }
47
+ let(:cielo_ec) { Cieloz::Requisicao::DadosEc.new Cieloz::Homologacao::Credenciais::CIELO }
48
+
46
49
  it "defaults to Homologacao::Credenciais::CIELO" do
47
- _.credenciais.must_equal Cieloz::Homologacao::Credenciais::CIELO
50
+ _.credenciais.numero.must_equal cielo_ec.numero
51
+ _.credenciais.chave.must_equal cielo_ec.chave
48
52
  end
49
53
 
50
54
  it "returns Homologacao::LOJA at store_mode" do
51
55
  _.store_mode!
52
- _.credenciais.must_equal Cieloz::Homologacao::Credenciais::LOJA
56
+ _.credenciais.numero.must_equal loja_ec.numero
57
+ _.credenciais.chave.must_equal loja_ec.chave
53
58
  end
54
59
 
55
60
  it "returns Homologacao::CIELO at cielo_mode" do
56
61
  _.cielo_mode!
57
- _.credenciais.must_equal Cieloz::Homologacao::Credenciais::CIELO
62
+ _.credenciais.numero.must_equal cielo_ec.numero
63
+ _.credenciais.chave.must_equal cielo_ec.chave
58
64
  end
59
65
  end
60
66
 
@@ -4,16 +4,11 @@ describe Cieloz::RequisicaoAutorizacaoTid do
4
4
  let(:_) { subject.class }
5
5
  let(:dir) { File.dirname __FILE__ }
6
6
  let(:opts) { { root: "requisicao-autorizacao-tid" } }
7
- let(:ec) { _::DadosEc.new Cieloz::Homologacao::Credenciais::CIELO }
7
+ let(:ec) { Cieloz::Configuracao.credenciais }
8
8
 
9
9
  it "serializes tid" do
10
10
  tid = 12345
11
11
  subject.tid = tid
12
12
  assert_equal expected_xml(opts) { "<tid>#{tid}</tid>" }, subject.to_xml
13
13
  end
14
-
15
- it "serializes dados-ec" do
16
- subject.dados_ec = ec
17
- assert_equal expected_xml(opts) { xml_for :ec, dir, binding }, subject.to_xml
18
- end
19
14
  end
@@ -4,7 +4,7 @@ describe Cieloz::RequisicaoCancelamento do
4
4
  let(:_) { subject.class }
5
5
  let(:dir) { File.dirname __FILE__ }
6
6
  let(:opts) { { root: "requisicao-cancelamento" } }
7
- let(:ec) { _::DadosEc.new Cieloz::Homologacao::Credenciais::CIELO }
7
+ let(:ec) { Cieloz::Configuracao.credenciais }
8
8
 
9
9
  it "serializes tid" do
10
10
  tid = 12345
@@ -12,11 +12,6 @@ describe Cieloz::RequisicaoCancelamento do
12
12
  assert_equal expected_xml(opts) { "<tid>#{tid}</tid>" }, subject.to_xml
13
13
  end
14
14
 
15
- it "serializes dados-ec" do
16
- subject.dados_ec = ec
17
- assert_equal expected_xml(opts) { xml_for :ec, dir, binding }, subject.to_xml
18
- end
19
-
20
15
  it "serializes valor" do
21
16
  val = 123
22
17
  subject.valor = val
@@ -4,7 +4,7 @@ describe Cieloz::RequisicaoCaptura do
4
4
  let(:_) { subject.class }
5
5
  let(:dir) { File.dirname __FILE__ }
6
6
  let(:opts) { { root: "requisicao-captura" } }
7
- let(:ec) { _::DadosEc.new Cieloz::Homologacao::Credenciais::CIELO }
7
+ let(:ec) { Cieloz::Configuracao.credenciais }
8
8
 
9
9
  it "serializes tid" do
10
10
  tid = 12345
@@ -12,11 +12,6 @@ describe Cieloz::RequisicaoCaptura do
12
12
  assert_equal expected_xml(opts) { "<tid>#{tid}</tid>" }, subject.to_xml
13
13
  end
14
14
 
15
- it "serializes dados-ec" do
16
- subject.dados_ec = ec
17
- assert_equal expected_xml(opts) { xml_for :ec, dir, binding }, subject.to_xml
18
- end
19
-
20
15
  it "serializes valor" do
21
16
  val = 123
22
17
  subject.valor = val
@@ -4,16 +4,11 @@ describe Cieloz::RequisicaoConsulta do
4
4
  let(:_) { subject.class }
5
5
  let(:dir) { File.dirname __FILE__ }
6
6
  let(:opts) { { root: "requisicao-consulta" } }
7
- let(:ec) { _::DadosEc.new Cieloz::Homologacao::Credenciais::CIELO }
7
+ let(:ec) { Cieloz::Configuracao.credenciais }
8
8
 
9
9
  it "serializes tid" do
10
10
  tid = 12345
11
11
  subject.tid = tid
12
12
  assert_equal expected_xml(opts) { "<tid>#{tid}</tid>" }, subject.to_xml
13
13
  end
14
-
15
- it "serializes dados-ec" do
16
- subject.dados_ec = ec
17
- assert_equal expected_xml(opts) { xml_for :ec, dir, binding }, subject.to_xml
18
- end
19
14
  end
@@ -13,10 +13,6 @@ describe Cieloz::Requisicao do
13
13
  subject.versao = versao
14
14
  end
15
15
 
16
- it "serializes" do
17
- assert_equal expected_xml(opts), subject.to_xml
18
- end
19
-
20
16
  describe "value attributes" do
21
17
  before do
22
18
  subject.class_eval do
@@ -44,18 +40,11 @@ describe Cieloz::Requisicao do
44
40
  end
45
41
 
46
42
  describe "complex attributes" do
47
- let(:attributes) { { numero: 123, chave: "M3str4" } }
48
- let(:ec) { _::DadosEc.new attributes }
43
+ let(:ec) { Cieloz::Configuracao.credenciais }
49
44
  let(:xml) { expected_xml(opts) { xml_for :ec, dir, binding } }
50
45
 
51
46
  it "serializes" do
52
- subject.dados_ec = ec
53
- assert_equal xml, subject.to_xml
54
- end
55
-
56
- it "ignores nils" do
57
- attributes.merge! ignore_me: nil
58
- subject.dados_ec = ec
47
+ subject.submit # @dados_ec is set on submission
59
48
  assert_equal xml, subject.to_xml
60
49
  end
61
50
  end
@@ -70,7 +59,6 @@ describe Cieloz::Requisicao do
70
59
  end
71
60
 
72
61
  it "sends to test web service" do
73
- subject.dados_ec = _::DadosEc.new Cieloz::Homologacao::Credenciais::CIELO
74
62
  erro = subject.submit
75
63
  assert_equal({}, subject.errors.messages)
76
64
  assert_equal err, erro.codigo
@@ -3,7 +3,7 @@ describe Cieloz::RequisicaoTransacao do
3
3
  let(:dir) { File.dirname __FILE__ }
4
4
  let(:opts) { { root: "requisicao-transacao" } }
5
5
 
6
- let(:ec) { _::DadosEc.new Cieloz::Homologacao::Credenciais::CIELO }
6
+ let(:ec) { Cieloz::Configuracao.credenciais }
7
7
  let(:portador) { _::DadosPortador::TEST::VISA }
8
8
 
9
9
  let(:now) { Time.now }
@@ -14,7 +14,7 @@ describe Cieloz::RequisicaoTransacao do
14
14
  let(:pagamento) { _::FormaPagamento.new.credito "visa" }
15
15
 
16
16
  it "serializes dados-ec" do
17
- subject.dados_ec = ec
17
+ subject.submit # @dados_ec is set on submission
18
18
  assert_equal expected_xml(opts) { xml_for :ec, dir, binding }, subject.to_xml
19
19
  end
20
20
 
@@ -62,7 +62,6 @@ describe Cieloz::RequisicaoTransacao do
62
62
  Cieloz::Configuracao.reset!
63
63
  subject.id = SecureRandom.uuid
64
64
  subject.versao = "1.2.0"
65
- subject.dados_ec = ec
66
65
  # txn.dados_portador = portador # buy page loja only!
67
66
  subject.dados_pedido = pedido
68
67
  subject.forma_pagamento = pagamento
@@ -1,12 +1,10 @@
1
+ describe Cieloz::Requisicao do
2
+ it { must validate_presence_of :dados_ec }
3
+ end
4
+
1
5
  describe Cieloz::Requisicao::DadosEc do
2
6
  it { must validate_presence_of :numero }
3
7
  it { must validate_presence_of :chave }
4
-
5
- it "is validated inside RequisicaoTransacao" do
6
- txn = Cieloz::RequisicaoTransacao.new dados_ec: subject
7
- refute txn.valid?
8
- refute txn.errors[:dados_ec].empty?
9
- end
10
8
  end
11
9
 
12
10
  describe Cieloz::RequisicaoTransacao::DadosPortador do
@@ -209,16 +207,9 @@ describe Cieloz::RequisicaoTransacao::FormaPagamento do
209
207
  end
210
208
  end
211
209
 
212
- describe Cieloz::Requisicao do
213
- it { must validate_presence_of :id }
214
- it { must validate_presence_of :versao }
215
- it { must validate_presence_of :dados_ec }
216
- end
217
-
218
210
  describe Cieloz::RequisicaoTransacao do
219
211
  let(:_) { subject.class }
220
212
 
221
- it { must validate_presence_of :dados_ec }
222
213
  it { must validate_presence_of :dados_pedido }
223
214
  it { must validate_presence_of :forma_pagamento }
224
215
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cieloz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-19 00:00:00.000000000 Z
12
+ date: 2013-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri