moip-transparente 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in moip-transparente.gemspec
4
+ gemspec
5
+ libxml-ruby
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Bruno Frank
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,71 @@
1
+ # Moip::Transparente
2
+
3
+ Gem para integração com checkout transparente do MOIP
4
+
5
+ ## Instalação
6
+
7
+ Coloque no seu Gemfile
8
+
9
+ ```ruby
10
+ gem 'moip-transparente'
11
+ ```
12
+ Rode:
13
+
14
+ ```sh
15
+ $ bundle
16
+ ```
17
+
18
+ Ou instale manualmente
19
+
20
+ ```sh
21
+ $ gem install moip-transparente
22
+ ```
23
+
24
+ ## Como usar
25
+
26
+ Crie uma instancia de Moip::Checkout no controller
27
+
28
+ ```ruby
29
+ @moip = Moip::Checkout.new
30
+
31
+ invoice = {
32
+ :razao => 'Fatura XXX',
33
+ :id => 1,
34
+ :total => '10.55',
35
+ :acrescimo => '0.00',
36
+ :desconto => '0.00',
37
+ :cliente => {
38
+ :id => 1,
39
+ :nome => 'Bob Sponja Calça Quadrada',
40
+ :email => 'calquadrada@gmail.com',
41
+ :logradouro => 'Rua X',
42
+ :complemento => 'Qd 11 Lt 2323',
43
+ :bairro => 'Solta Gato',
44
+ :cidade => 'Caixa Prego',
45
+ :uf => 'BA',
46
+ :cep => '00124-11',
47
+ :telefone => '(55) 5555-5555'
48
+ },
49
+ :parcelamentos => [
50
+ {:minimo => 1, :maximo => 10, :repassar => true}
51
+ ]
52
+ }
53
+
54
+ # requisite o token
55
+ @moip.get_token(invoice)
56
+
57
+ # Na view
58
+
59
+ <%= @moip.javascript_tag %> # Vai gerar o include do Javascript
60
+
61
+ <%= @moip.widget_tag('paymentSucess', 'paymentFail') %> # Vai gerar o a div do javascript com as funções
62
+ ```
63
+
64
+
65
+ ## Contribuições
66
+
67
+ 1. Faça um fork
68
+ 2. Crie um novo branch (`git checkout -b my-new-feature`)
69
+ 3. Commit suas alterações (`git commit -am 'Added some feature'`)
70
+ 4. Faça um push (`git push origin my-new-feature`)
71
+ 5. Solicite o pull
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ moip-transparente
2
+ =================
3
+
4
+ Integração do MOIP com checkout transparente.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib'
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = true
11
+ end
12
+
13
+ desc "Run tests"
14
+ task :default => :test
data/lib/moip.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "net/https"
2
+ require "uri"
3
+ require "moip/version"
4
+ require "moip/checkout"
5
+
6
+ module Moip
7
+ class Config
8
+ def self.access_token
9
+ @access_token
10
+ end
11
+
12
+ def self.access_token=(value)
13
+ @access_token = value
14
+ end
15
+
16
+ def self.access_key
17
+ @access_key
18
+ end
19
+
20
+ def self.access_key=(value)
21
+ @access_key = value
22
+ end
23
+
24
+ def self.test?
25
+ @test || false
26
+ end
27
+
28
+ def self.test=(test)
29
+ @test = test
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,211 @@
1
+ require 'xml'
2
+ require 'base64'
3
+
4
+ class Moip::Checkout
5
+
6
+ def javascript_tag
7
+ "<script type='text/javascript' src='#{get_javascript_url}' charset='ISO-8859-1'></script>"
8
+ end
9
+
10
+ def widget_tag(success, fail)
11
+ "<div id='MoipWidget'
12
+ data-token='#{@token}'
13
+ callback-method-success='#{success}'
14
+ callback-method-error='#{fail}'>
15
+ </div>"
16
+ end
17
+
18
+ def get_token(invoice)
19
+ doc = XML::Document.new
20
+ doc.root = XML::Node.new('EnviarInstrucao')
21
+
22
+ unica = XML::Node.new('InstrucaoUnica')
23
+ unica['TipoValidacao'] = 'Transparente'
24
+
25
+
26
+ description = XML::Node.new('Razao')
27
+ description << invoice[:razao]
28
+ unica << description
29
+
30
+ id_invoice = XML::Node.new('IdProprio')
31
+ id_invoice << invoice[:id]
32
+ unica << id_invoice
33
+
34
+ valores = XML::Node.new('Valores')
35
+
36
+ valor = XML::Node.new('Valor')
37
+ valor['moeda'] = 'BRL'
38
+ valor << invoice[:total]
39
+
40
+ valores << valor
41
+
42
+
43
+ valor = XML::Node.new('Acrescimo')
44
+ valor['moeda'] = 'BRL'
45
+ valores << invoice[:acrescimo] || '0.00'
46
+
47
+
48
+ valor = XML::Node.new('Deducao')
49
+ valor['moeda'] = 'BRL'
50
+ valores << invoice[:desconto] || '0.00'
51
+
52
+ unica << valores
53
+
54
+ pagador = XML::Node.new('Pagador')
55
+ nome = XML::Node.new('Nome')
56
+ nome << invoice[:cliente][:nome]
57
+ pagador << nome
58
+
59
+ email = XML::Node.new('Email')
60
+ email << invoice[:cliente][:email]
61
+ pagador << email
62
+
63
+ id = XML::Node.new('IdPagador')
64
+ id << invoice[:cliente][:id]
65
+ pagador << id
66
+
67
+ endereco = XML::Node.new('EnderecoCobranca')
68
+
69
+ logradouro = XML::Node.new('Logradouro')
70
+ logradouro << invoice[:cliente][:logradouro]
71
+ endereco << logradouro
72
+
73
+ numero = XML::Node.new('Numero')
74
+ numero_or_default = invoice[:cliente][:numero] || '0'
75
+ numero << numero_or_default
76
+ endereco << numero
77
+
78
+ complemento = XML::Node.new('Complemento')
79
+ complemento << invoice[:cliente][:complemento]
80
+ endereco << complemento
81
+
82
+ bairro = XML::Node.new('Bairro')
83
+ bairro << invoice[:cliente][:bairro]
84
+ endereco << bairro
85
+
86
+ cidade = XML::Node.new('Cidade')
87
+ cidade << invoice[:cliente][:cidade]
88
+ endereco << cidade
89
+
90
+ estado = XML::Node.new('Estado')
91
+ estado << invoice[:cliente][:uf]
92
+ endereco << estado
93
+
94
+ pais = XML::Node.new('Pais')
95
+ pais_or_default = invoice[:cliente][:pais] || 'BRA'
96
+ pais << pais_or_default
97
+ endereco << pais
98
+
99
+ cep = XML::Node.new('CEP')
100
+ cep << invoice[:cliente][:cep]
101
+ endereco << cep
102
+
103
+ telefone = XML::Node.new('TelefoneFixo')
104
+ telefone << invoice[:cliente][:telefone]
105
+ endereco << telefone
106
+
107
+ pagador << endereco
108
+ unica << pagador
109
+
110
+ parcelamentos = XML::Node.new('Parcelamentos')
111
+ invoice[:parcelamentos].each do |parcelamento_item|
112
+ parcelamento = XML::Node.new('Parcelamento')
113
+ minimo = XML::Node.new('MinimoParcelas')
114
+ minimo << parcelamento_item[:minimo]
115
+ parcelamento << minimo
116
+
117
+ maximo = XML::Node.new('MaximoParcelas')
118
+ maximo << parcelamento_item[:maximo]
119
+ parcelamento << maximo
120
+
121
+ if parcelamento_item[:repassar]
122
+ repassar = XML::Node.new('Repassar')
123
+ repassar << '1'
124
+ parcelamento << repassar
125
+ else
126
+ juros = XML::Node.new('Juros')
127
+ juros << parcelamento_item[:juros]
128
+ parcelamento << juros
129
+ end
130
+ parcelamentos << parcelamento
131
+ end
132
+
133
+ unica << parcelamentos
134
+
135
+ comissoes = XML::Node.new('Comissoes')
136
+ if invoice[:comissoes]
137
+ invoice[:comissoes].each do |comissao_item|
138
+ comissionamento = XML::Node.new('Comissionamento')
139
+ razao = XML::Node.new('Razao')
140
+ razao << comissao_item[:razao] || invoice[:razao]
141
+ comissionamento << razao
142
+
143
+ comissionado = XML::Node.new("Comissionado")
144
+ login_moip = XML::Node.new("LoginMoIP")
145
+ login_moip << comissao_item[:login_moip]
146
+ comissionado << login_moip
147
+ comissionamento << comissionado
148
+
149
+ valor = XML::Node.new("ValorFixo")
150
+ valor << comissao_item[:valor]
151
+ comissionamento << valor
152
+ comissoes << comissionamento
153
+ end
154
+ end
155
+
156
+ if invoice[:pagador_taxas]
157
+ taxas = XML::Node.new('PagadorTaxa')
158
+ login_moip = XML::Node.new("LoginMoIP")
159
+ login_moip << invoice[:pagador_taxas]
160
+ taxas << login_moip
161
+ comissoes << taxas
162
+ end
163
+
164
+ unica << comissoes
165
+
166
+ doc.root << unica
167
+
168
+ parser = XML::Parser.string(post_data(doc.to_s))
169
+ dom = parser.parse
170
+ resposta = dom.find('./Resposta').first
171
+ if resposta.find('Status')[0].content == 'Sucesso'
172
+ @token = resposta.find('Token')[0].content
173
+ return {:status => :ok, :token => resposta.find('Token')[0].content, :id => resposta.find('ID')[0].content}
174
+ elsif resposta.find('Status')[0].content == 'Falha'
175
+ return {:status => :fail, :code => resposta.find('Erro')[0]['Codigo'], :message => resposta.find('Erro')[0].content, :id => resposta.find('ID')[0].content }
176
+ end
177
+ end
178
+
179
+ private
180
+
181
+ def get_token_url
182
+ if Moip::Config.test?
183
+ return "https://desenvolvedor.moip.com.br/sandbox/ws/alpha/EnviarInstrucao/Unica"
184
+ else
185
+ return "https://www.moip.com.br/ws/alpha/EnviarInstrucao/Unica"
186
+ end
187
+ end
188
+
189
+ def get_javascript_url
190
+ if Moip::Config.test?
191
+ return "https://desenvolvedor.moip.com.br/sandbox/transparente/MoipWidget-v2.js"
192
+ else
193
+ return "https://www.moip.com.br/transparente/MoipWidget-v2.js"
194
+ end
195
+ end
196
+
197
+ def post_data(xml)
198
+ uri = URI.parse(get_token_url)
199
+ http = Net::HTTP.new(uri.host, uri.port)
200
+ http.use_ssl = true
201
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
202
+ #http.set_debug_output $stderr if Moip::Config.test?
203
+
204
+ request = Net::HTTP::Post.new(uri.path)
205
+ request.basic_auth Moip::Config.access_token, Moip::Config.access_key
206
+
207
+ request.body = xml
208
+ response = http.start {|r| r.request request }
209
+ response.body
210
+ end
211
+ end
@@ -0,0 +1,3 @@
1
+ module Moip
2
+ VERSION = "0.0.1"
3
+ end
data/moip.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/moip/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Bruno Frank"]
6
+ gem.email = ["bfscordeiro@gmail.com"]
7
+ gem.description = %q{Gem para integração com checkout transparente do MOIP}
8
+ gem.summary = %q{Gem para integração com checkout transparente do MOIP}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "moip-transparente"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Moip::VERSION
17
+ end
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class CheckoutTest < TestHelper
5
+
6
+ def setup
7
+ @moip = Moip::Checkout.new
8
+ end
9
+
10
+ def test_get_token
11
+ invoice = {
12
+ :razao => 'Cobrança so site XXX',
13
+ :id => rand(999),
14
+ :total => '10.55',
15
+ :acrescimo => '0.00',
16
+ :desconto => '0.00',
17
+ :cliente => {
18
+ :id => 111,
19
+ :nome => 'Bruno Frank Silva Cordeiro',
20
+ :email => 'bfscordeiro@gmail.com',
21
+ :logradouro => 'Rua 3-E',
22
+ :complemento => 'Qd 40 Lt 01 Cs 4',
23
+ :bairro => 'Garavelo',
24
+ :cidade => 'Aparecida de Goiânia',
25
+ :uf => 'GO',
26
+ :cep => '74932-180',
27
+ :telefone => '(62) 3288-5334'
28
+ },
29
+ :parcelamentos => [
30
+ {:minimo => 1, :maximo => 10, :repassar => true}
31
+ ]
32
+ }
33
+
34
+ response = @moip.get_token(invoice)
35
+ @token = response[:token]
36
+ assert_equal response[:status], :ok
37
+ end
38
+
39
+ def test_widget
40
+ expected = "<div id='MoipWidget'
41
+ data-token='#{@token}'
42
+ callback-method-success='success'
43
+ callback-method-error='fail'>
44
+ </div>"
45
+
46
+ widget = @moip.widget_tag('success', 'fail')
47
+
48
+ assert_equal widget, expected
49
+ end
50
+
51
+ def test_script
52
+ assert_equal @moip.javascript_tag, "<script type='text/javascript' src='https://desenvolvedor.moip.com.br/sandbox/transparente/MoipWidget-v2.js' charset='ISO-8859-1'></script>"
53
+ end
54
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class ConfigTest < TestHelper
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_test_flag
9
+ assert Moip::Config.test?
10
+ end
11
+
12
+ def test_access_token_flag
13
+ assert_equal Moip::Config.access_token, 'UFTWIIZJQUNTIV97KIDYRV8GK37X8LKM'
14
+ end
15
+
16
+ def test_key_flag
17
+ assert_equal Moip::Config.access_key, 'OXPO54ITJ6DOF91DJEWV3YUQQMU7QVYU719RAYGQ'
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require "moip"
4
+
5
+ class TestHelper < Test::Unit::TestCase
6
+ end
7
+
8
+ Moip::Config.access_token = 'UFTWIIZJQUNTIV97KIDYRV8GK37X8LKM'
9
+ Moip::Config.access_key = 'OXPO54ITJ6DOF91DJEWV3YUQQMU7QVYU719RAYGQ'
10
+ Moip::Config.test = true
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moip-transparente
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bruno Frank
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-03 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Gem para integração com checkout transparente do MOIP
15
+ email:
16
+ - bfscordeiro@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.markdown
25
+ - README.md
26
+ - Rakefile
27
+ - lib/moip.rb
28
+ - lib/moip/checkout.rb
29
+ - lib/moip/version.rb
30
+ - moip.gemspec
31
+ - test/checkout_test.rb
32
+ - test/config_test.rb
33
+ - test/test_helper.rb
34
+ homepage: ''
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.17
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Gem para integração com checkout transparente do MOIP
58
+ test_files:
59
+ - test/checkout_test.rb
60
+ - test/config_test.rb
61
+ - test/test_helper.rb