cieloz 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cieloz/builder.rb +9 -65
- data/lib/cieloz/helpers.rb +38 -1
- data/lib/cieloz/requisicao_tid.rb +22 -0
- data/lib/cieloz/requisicao_transacao/dados_pedido.rb +17 -0
- data/lib/cieloz/requisicao_transacao/dados_portador.rb +8 -0
- data/lib/cieloz/requisicao_transacao/forma_pagamento.rb +16 -0
- data/lib/cieloz/requisicao_transacao.rb +24 -0
- data/lib/cieloz/version.rb +1 -1
- data/test/minitest_helper.rb +1 -1
- data/test/unit/mapper_test.rb +37 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e1982472d4621f9f7283be9cd04767f7d72a7df
|
4
|
+
data.tar.gz: a1c912e103164cde71684125669f9fd1c1ac8cb4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 893b0252c1fc78c9816c04dad698926fef565f5c86f627c2bb1018c40e3c31f4e2ab5d2aed76db5b6210938ed3d98a0ae5a27e1d01c8726114b5fe85b3c6ee01
|
7
|
+
data.tar.gz: 4c23e5013b12ab5b749100545a5d4cb73a345a07d4140cc4c94f513e19de919505c6d9a7e653fb0864f19e404b9c407f41d1725c569f974bcfd458e78c01668c
|
data/lib/cieloz/builder.rb
CHANGED
@@ -1,95 +1,39 @@
|
|
1
1
|
module Cieloz
|
2
2
|
module Builder
|
3
3
|
def portador source, opts={}
|
4
|
-
|
5
|
-
:numero, :validade, :codigo_seguranca, :nome_portador
|
6
|
-
|
7
|
-
RequisicaoTransacao::DadosPortador.new numero: num,
|
8
|
-
validade: val, codigo_seguranca: cod, nome_portador: nome
|
4
|
+
RequisicaoTransacao::DadosPortador.map source, opts
|
9
5
|
end
|
10
6
|
|
11
7
|
def pedido source, opts={}
|
12
|
-
|
13
|
-
:descricao, :data_hora, :moeda, :idioma, :soft_descriptor
|
14
|
-
|
15
|
-
num, val, desc, time, cur, lang, soft = mappings
|
16
|
-
val = (val * 100).round unless val.nil? or val.integer?
|
17
|
-
|
18
|
-
time ||= Time.now
|
19
|
-
cur ||= Cieloz::Configuracao.moeda
|
20
|
-
lang ||= Cieloz::Configuracao.idioma
|
21
|
-
soft ||= Cieloz::Configuracao.soft_descriptor
|
22
|
-
|
23
|
-
RequisicaoTransacao::DadosPedido.new data_hora: time,
|
24
|
-
numero: num, valor: val, moeda: cur, idioma: lang,
|
25
|
-
descricao: desc, soft_descriptor: soft
|
8
|
+
RequisicaoTransacao::DadosPedido.map source, opts
|
26
9
|
end
|
27
10
|
|
28
11
|
def debito source, opts={}
|
29
|
-
|
30
|
-
RequisicaoTransacao::FormaPagamento.new.credito bandeira
|
12
|
+
RequisicaoTransacao::FormaPagamento.map_debito source, opts
|
31
13
|
end
|
32
14
|
|
33
15
|
def credito source, opts={}
|
34
|
-
|
35
|
-
RequisicaoTransacao::FormaPagamento.new.credito bandeira
|
16
|
+
RequisicaoTransacao::FormaPagamento.map_credito source, opts
|
36
17
|
end
|
37
18
|
|
38
19
|
def parcelado source, opts={}
|
39
|
-
|
40
|
-
RequisicaoTransacao::FormaPagamento.new.parcelado bandeira, parcelas
|
20
|
+
RequisicaoTransacao::FormaPagamento.map_parcelado source, opts
|
41
21
|
end
|
42
22
|
|
43
23
|
def transacao source, opts={}
|
44
|
-
|
45
|
-
attrs_from source, opts, :dados_portador, :dados_pedido,
|
46
|
-
:forma_pagamento, :url_retorno, :capturar, :campo_livre
|
47
|
-
|
48
|
-
url ||= Cieloz::Configuracao.url_retorno
|
49
|
-
|
50
|
-
txn = RequisicaoTransacao.new dados_portador: portador,
|
51
|
-
dados_pedido: pedido, forma_pagamento: pagamento,
|
52
|
-
campo_livre: campo_livre, url_retorno: url,
|
53
|
-
dados_ec: Cieloz::Configuracao.credenciais
|
54
|
-
|
55
|
-
capturar ||= Cieloz::Configuracao.captura_automatica
|
56
|
-
|
57
|
-
case capturar.to_s
|
58
|
-
when 'true' then txn.capturar_automaticamente
|
59
|
-
else txn.nao_capturar_automaticamente
|
60
|
-
end
|
61
|
-
|
62
|
-
txn.send pagamento.metodo_autorizacao
|
63
|
-
|
64
|
-
txn
|
24
|
+
RequisicaoTransacao.map source, opts
|
65
25
|
end
|
66
26
|
|
67
27
|
def consulta source, opts={}
|
68
|
-
|
69
|
-
RequisicaoConsulta.new tid: tid
|
28
|
+
RequisicaoConsulta.map source, opts
|
70
29
|
end
|
71
30
|
|
72
31
|
def captura source, opts={}
|
73
|
-
|
74
|
-
RequisicaoCaptura.new tid: tid, valor: valor
|
32
|
+
RequisicaoCaptura.map source, opts
|
75
33
|
end
|
76
34
|
|
77
35
|
def cancelamento source, opts={}
|
78
|
-
|
79
|
-
RequisicaoCancelamento.new tid: tid, valor: valor
|
80
|
-
end
|
81
|
-
|
82
|
-
private
|
83
|
-
def attrs_from source, opts, *keys
|
84
|
-
attrs = keys.map { |k|
|
85
|
-
value_or_attr_name = opts[k] || k
|
86
|
-
if value_or_attr_name.is_a? Symbol
|
87
|
-
source.send value_or_attr_name if source.respond_to? value_or_attr_name
|
88
|
-
else
|
89
|
-
value_or_attr_name
|
90
|
-
end
|
91
|
-
}
|
92
|
-
attrs.count == 1 ? attrs.first : attrs
|
36
|
+
RequisicaoCancelamento.map source, opts
|
93
37
|
end
|
94
38
|
end
|
95
39
|
|
data/lib/cieloz/helpers.rb
CHANGED
@@ -14,11 +14,48 @@ module Cieloz
|
|
14
14
|
end
|
15
15
|
}
|
16
16
|
end
|
17
|
+
|
18
|
+
def attrs_from source, opts, *keys
|
19
|
+
attrs = keys.map { |k|
|
20
|
+
value_or_attr_name = opts[k] || k
|
21
|
+
if value_or_attr_name.is_a? Symbol
|
22
|
+
source.send value_or_attr_name if source.respond_to? value_or_attr_name
|
23
|
+
else
|
24
|
+
value_or_attr_name
|
25
|
+
end
|
26
|
+
}
|
27
|
+
attrs.count == 1 ? attrs.first : attrs
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module InstanceMethods
|
32
|
+
def valid?
|
33
|
+
valid = _valid?
|
34
|
+
unless @source.nil?
|
35
|
+
unless valid
|
36
|
+
errors.messages.each { |attr,attr_errors|
|
37
|
+
source_attr = @opts[attr]
|
38
|
+
if source_attr.is_a?(Symbol) and @source.respond_to?(source_attr)
|
39
|
+
attr_errors.each {|e| @source.errors.add source_attr, e }
|
40
|
+
else
|
41
|
+
attr_errors.each {|e| @source.errors.add :base, "#{attr}: #{e}" if e.is_a? String }
|
42
|
+
end
|
43
|
+
@source.errors.messages.each {|attr,attr_errors| attr_errors.uniq! }
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
valid
|
48
|
+
end
|
17
49
|
end
|
18
50
|
|
19
51
|
def self.included base
|
20
|
-
base.extend ClassMethods
|
21
52
|
base.send :include, ActiveModel::Validations
|
53
|
+
base.extend ClassMethods
|
54
|
+
base.class_eval do
|
55
|
+
alias :_valid? :valid?
|
56
|
+
attr_accessor :source, :opts
|
57
|
+
end
|
58
|
+
base.send :include, InstanceMethods
|
22
59
|
end
|
23
60
|
|
24
61
|
def initialize attrs={}
|
@@ -1,5 +1,16 @@
|
|
1
1
|
module Cieloz
|
2
2
|
class RequisicaoTid < Requisicao
|
3
|
+
module ClassMethods
|
4
|
+
def map source, opts={}
|
5
|
+
tid = attrs_from source, opts, :tid
|
6
|
+
new source: source, opts: opts, tid: tid
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.inherited(target)
|
11
|
+
target.extend ClassMethods
|
12
|
+
end
|
13
|
+
|
3
14
|
attr_accessor :tid
|
4
15
|
|
5
16
|
def attributes
|
@@ -8,6 +19,17 @@ module Cieloz
|
|
8
19
|
end
|
9
20
|
|
10
21
|
class RequisicaoTidValor < RequisicaoTid
|
22
|
+
module ClassMethods
|
23
|
+
def map source, opts={}
|
24
|
+
tid, valor = attrs_from source, opts, :tid, :valor
|
25
|
+
new source: source, opts: opts, tid: tid, valor: valor
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.inherited(target)
|
30
|
+
target.extend ClassMethods
|
31
|
+
end
|
32
|
+
|
11
33
|
attr_accessor :valor
|
12
34
|
|
13
35
|
def attributes
|
@@ -17,6 +17,23 @@ class Cieloz::RequisicaoTransacao
|
|
17
17
|
validates :idioma, inclusion: { in: IDIOMAS }
|
18
18
|
validates :soft_descriptor, length: { maximum: 13 }
|
19
19
|
|
20
|
+
def self.map(source, opts={})
|
21
|
+
mappings = attrs_from source, opts, :numero, :valor,
|
22
|
+
:descricao, :data_hora, :moeda, :idioma, :soft_descriptor
|
23
|
+
|
24
|
+
num, val, desc, time, cur, lang, soft = mappings
|
25
|
+
val = (val * 100).round unless val.nil? or val.integer?
|
26
|
+
|
27
|
+
time ||= Time.now
|
28
|
+
cur ||= Cieloz::Configuracao.moeda
|
29
|
+
lang ||= Cieloz::Configuracao.idioma
|
30
|
+
soft ||= Cieloz::Configuracao.soft_descriptor
|
31
|
+
|
32
|
+
new source: source, opts: opts,
|
33
|
+
data_hora: time, numero: num, valor: val, moeda: cur,
|
34
|
+
idioma: lang, descricao: desc, soft_descriptor: soft
|
35
|
+
end
|
36
|
+
|
20
37
|
def attributes
|
21
38
|
{
|
22
39
|
numero: @numero,
|
@@ -28,6 +28,14 @@ class Cieloz::RequisicaoTransacao
|
|
28
28
|
|
29
29
|
validates :indicador, presence: true
|
30
30
|
|
31
|
+
def self.map(source, opts={})
|
32
|
+
num, val, cod, nome = attrs_from source, opts,
|
33
|
+
:numero, :validade, :codigo_seguranca, :nome_portador
|
34
|
+
|
35
|
+
new source: source, opts: opts,
|
36
|
+
numero: num, validade: val, codigo_seguranca: cod, nome_portador: nome
|
37
|
+
end
|
38
|
+
|
31
39
|
def initialize attrs={}
|
32
40
|
super
|
33
41
|
indicador_nao_informado! if codigo_seguranca.blank?
|
@@ -32,6 +32,22 @@ class Cieloz::RequisicaoTransacao
|
|
32
32
|
validates :bandeira, inclusion: { in: BANDEIRAS_PARCELAMENTO },
|
33
33
|
if: "[ PARCELADO_LOJA, PARCELADO_ADM ].include? @produto"
|
34
34
|
|
35
|
+
|
36
|
+
def self.map_debito(source, opts={})
|
37
|
+
bandeira = attrs_from source, opts, :bandeira
|
38
|
+
new(source: source, opts: opts).debito bandeira
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.map_credito(source, opts={})
|
42
|
+
bandeira = attrs_from source, opts, :bandeira
|
43
|
+
new(source: source, opts: opts).credito bandeira
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.map_parcelado source, opts={}
|
47
|
+
bandeira, parcelas = attrs_from source, opts, :bandeira, :parcelas
|
48
|
+
new(source: source, opts: opts).parcelado bandeira, parcelas
|
49
|
+
end
|
50
|
+
|
35
51
|
def attributes
|
36
52
|
{
|
37
53
|
bandeira: @bandeira,
|
@@ -39,6 +39,30 @@ class Cieloz::RequisicaoTransacao < Cieloz::Requisicao
|
|
39
39
|
|
40
40
|
validates :campo_livre, length: { maximum: 128 }
|
41
41
|
|
42
|
+
def self.map(source, opts={})
|
43
|
+
portador, pedido, pagamento, url, capturar, campo_livre =
|
44
|
+
attrs_from source, opts, :dados_portador, :dados_pedido,
|
45
|
+
:forma_pagamento, :url_retorno, :capturar, :campo_livre
|
46
|
+
|
47
|
+
url ||= Cieloz::Configuracao.url_retorno
|
48
|
+
|
49
|
+
txn = new source: source, opts: opts, dados_portador: portador,
|
50
|
+
dados_pedido: pedido, forma_pagamento: pagamento,
|
51
|
+
campo_livre: campo_livre, url_retorno: url,
|
52
|
+
dados_ec: Cieloz::Configuracao.credenciais
|
53
|
+
|
54
|
+
capturar ||= Cieloz::Configuracao.captura_automatica
|
55
|
+
|
56
|
+
case capturar.to_s
|
57
|
+
when 'true' then txn.capturar_automaticamente
|
58
|
+
else txn.nao_capturar_automaticamente
|
59
|
+
end
|
60
|
+
|
61
|
+
txn.send pagamento.metodo_autorizacao if pagamento
|
62
|
+
|
63
|
+
txn
|
64
|
+
end
|
65
|
+
|
42
66
|
def nested_validations
|
43
67
|
nested_attrs = [ :dados_ec, :dados_pedido, :forma_pagamento ]
|
44
68
|
nested_attrs << :dados_portador if Cieloz::Configuracao.store_mode?
|
data/lib/cieloz/version.rb
CHANGED
data/test/minitest_helper.rb
CHANGED
@@ -10,7 +10,7 @@ require 'erb'
|
|
10
10
|
VCR.configure do |c|
|
11
11
|
c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
|
12
12
|
c.hook_into :webmock
|
13
|
-
c.debug_logger = File.open('test/tmp/vcr.log', 'w')
|
13
|
+
# c.debug_logger = File.open('test/tmp/vcr.log', 'w')
|
14
14
|
end
|
15
15
|
|
16
16
|
class MiniTest::Spec
|
@@ -0,0 +1,37 @@
|
|
1
|
+
describe Cieloz::Helpers do
|
2
|
+
class Source
|
3
|
+
include ActiveModel::Validations
|
4
|
+
|
5
|
+
def number ; nil end
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:order) { Source.new }
|
9
|
+
let(:pedido) { Cieloz.pedido order, numero: :number, valor: :value }
|
10
|
+
|
11
|
+
it "recognizes error for attribute" do
|
12
|
+
pedido.wont_be :valid?
|
13
|
+
errors = order.errors.messages[:number]
|
14
|
+
errors.wont_be_empty
|
15
|
+
errors.must_equal pedido.errors[:numero]
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:txn) { Source.new }
|
19
|
+
let(:transacao) { Cieloz.transacao txn, dados_pedido: pedido }
|
20
|
+
|
21
|
+
it "recognizes errors for dependent mappers" do
|
22
|
+
transacao.wont_be :valid?
|
23
|
+
errors = order.errors.messages[:number]
|
24
|
+
errors.wont_be_empty
|
25
|
+
errors.must_equal pedido.errors[:numero]
|
26
|
+
end
|
27
|
+
|
28
|
+
it "appends non-dependent errors to root aggregate base" do
|
29
|
+
transacao.wont_be :valid?
|
30
|
+
transacao.errors.delete :dados_pedido # ignores dependent errors
|
31
|
+
expected_errors = transacao.errors.messages.map { |attr,errors|
|
32
|
+
# errors have theit attributes identified when put on base
|
33
|
+
errors.map { |e| "#{attr}: #{e}" }
|
34
|
+
}.flatten
|
35
|
+
txn.errors.messages[:base].must_equal expected_errors
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cieloz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fábio Luiz Nery de Miranda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -173,6 +173,7 @@ files:
|
|
173
173
|
- test/unit/bandeiras_operacoes_test.rb
|
174
174
|
- test/unit/builder_test.rb
|
175
175
|
- test/unit/configuracao_test.rb
|
176
|
+
- test/unit/mapper_test.rb
|
176
177
|
- test/unit/requisicao_autorizacao_tid_test.rb
|
177
178
|
- test/unit/requisicao_cancelamento_test.rb
|
178
179
|
- test/unit/requisicao_captura_test.rb
|
@@ -218,6 +219,7 @@ test_files:
|
|
218
219
|
- test/unit/bandeiras_operacoes_test.rb
|
219
220
|
- test/unit/builder_test.rb
|
220
221
|
- test/unit/configuracao_test.rb
|
222
|
+
- test/unit/mapper_test.rb
|
221
223
|
- test/unit/requisicao_autorizacao_tid_test.rb
|
222
224
|
- test/unit/requisicao_cancelamento_test.rb
|
223
225
|
- test/unit/requisicao_captura_test.rb
|