locomotiva-braspag 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Guardfile +10 -0
- data/README.md +59 -0
- data/RELEASES.md +20 -0
- data/Rakefile +6 -0
- data/config/braspag.yml +17 -0
- data/lib/generators/braspag/install/install_generator.rb +15 -0
- data/lib/generators/braspag/install/templates/config/braspag.yml +17 -0
- data/lib/locomotiva-braspag.rb +45 -0
- data/lib/locomotiva-braspag/bill.rb +164 -0
- data/lib/locomotiva-braspag/connection.rb +50 -0
- data/lib/locomotiva-braspag/credit_card.rb +210 -0
- data/lib/locomotiva-braspag/crypto/jar_webservice.rb +91 -0
- data/lib/locomotiva-braspag/crypto/webservice.rb +100 -0
- data/lib/locomotiva-braspag/eft.rb +90 -0
- data/lib/locomotiva-braspag/errors.rb +40 -0
- data/lib/locomotiva-braspag/order.rb +42 -0
- data/lib/locomotiva-braspag/payment_method.rb +73 -0
- data/lib/locomotiva-braspag/poster.rb +36 -0
- data/lib/locomotiva-braspag/protected_credit_card.rb +160 -0
- data/lib/locomotiva-braspag/utils.rb +32 -0
- data/lib/locomotiva-braspag/version.rb +3 -0
- data/locomotiva-braspag.gemspec +33 -0
- data/spec/bill_spec.rb +427 -0
- data/spec/connection_spec.rb +188 -0
- data/spec/credit_card_spec.rb +517 -0
- data/spec/crypto/jar_webservice_spec.rb +187 -0
- data/spec/crypto/webservice_spec.rb +143 -0
- data/spec/eft_spec.rb +209 -0
- data/spec/order_spec.rb +118 -0
- data/spec/poster_spec.rb +53 -0
- data/spec/protected_credit_card_spec.rb +296 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/utils_spec.rb +47 -0
- metadata +244 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b328bd3b8324a13c1607b3df909b0a6ebfae7c27
|
4
|
+
data.tar.gz: c3df45262ecd9eba10eecb5677321e03dbb08a96
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7fe820a0e29f668b030e3d33aed4897dc772f8a16a04c7ada5f917d6449f84756f80dfed8770aa5e6be0bb246d934577ceef9d26ffe03752c0c084f3f0fea753
|
7
|
+
data.tar.gz: 9dded58c47931707874a5a2b094979ea50cc0bec3201595b881a4584b359c652e7fd582cd285e385a4878710fc7da8479545b963d2c0e585e5fb1f572af8ffd2
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
guard 'bundler' do
|
2
|
+
watch('Gemfile')
|
3
|
+
end
|
4
|
+
|
5
|
+
guard 'rspec', :version => 2, :bundler => false do
|
6
|
+
watch(%r{^spec/(.*)_spec\.rb$})
|
7
|
+
watch(%r{^lib/rbraspag.rb$}) { "spec" }
|
8
|
+
watch(%r{^lib/rbraspag/(.*)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
9
|
+
watch('spec/spec_helper.rb') { "spec" }
|
10
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Locomotiva.pro Braspag
|
2
|
+
|
3
|
+
locomotiva-braspag gem to use Braspag gateway
|
4
|
+
|
5
|
+
* This gem need RACK_ENV environment variable to identify the environment
|
6
|
+
|
7
|
+
# How to install
|
8
|
+
|
9
|
+
## for Rails 3 app
|
10
|
+
|
11
|
+
### Add on your Gemfile
|
12
|
+
|
13
|
+
gem "locomotiva-braspag"
|
14
|
+
|
15
|
+
### Create a config/braspag.yml file
|
16
|
+
|
17
|
+
$ rails generate braspag:install
|
18
|
+
|
19
|
+
### Set RACK_ENV (our suggest)
|
20
|
+
|
21
|
+
# add last line in config/environment.rb
|
22
|
+
# ...
|
23
|
+
# ENV["RACK_ENV"] ||= ENV["RAILS_ENV"]
|
24
|
+
|
25
|
+
### Edit config/braspag.yml with your Braspag merchant_id
|
26
|
+
|
27
|
+
# Examples
|
28
|
+
|
29
|
+
## to create a Bill (Boleto/Bloqueto for brazilian guys)
|
30
|
+
@bill = Braspag::Bill.generate({
|
31
|
+
:order_id => 1,
|
32
|
+
:amount => 3,
|
33
|
+
:payment_method => 10
|
34
|
+
})
|
35
|
+
|
36
|
+
# License
|
37
|
+
|
38
|
+
(The MIT License)
|
39
|
+
|
40
|
+
Copyright (c) 2010
|
41
|
+
|
42
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
43
|
+
a copy of this software and associated documentation files (the
|
44
|
+
'Software'), to deal in the Software without restriction, including
|
45
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
46
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
47
|
+
permit persons to whom the Software is furnished to do so, subject to
|
48
|
+
the following conditions:
|
49
|
+
|
50
|
+
The above copyright notice and this permission notice shall be
|
51
|
+
included in all copies or substantial portions of the Software.
|
52
|
+
|
53
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
54
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
55
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
56
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
57
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
58
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
59
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/RELEASES.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# RELEASES
|
2
|
+
|
3
|
+
## For next release
|
4
|
+
|
5
|
+
d2b9a7a Voltando a usar o HTTPI
|
6
|
+
|
7
|
+
## 0.1.1 - 09/01/2012
|
8
|
+
|
9
|
+
9ffbcbd adicionando novos códigos de métodos de pagamento
|
10
|
+
|
11
|
+
|
12
|
+
## 0.1.0 - 09/01/2012
|
13
|
+
|
14
|
+
5cf697e adicionando Gemfile.lock ao .gitignore
|
15
|
+
a6b1f54 removendo Gemfile.lock; modificando verificação do argumento has_interest
|
16
|
+
a663e0a baixando a versão mínima requerida do nokogiri
|
17
|
+
628d108 refatorando testes e implementação
|
18
|
+
087549a moving Connection exceptions to errors.rb; tests for Utils class
|
19
|
+
9a727eb bump up version
|
20
|
+
6e5b1c3 refatorando para suportar o ambiente de produção da Braspag
|
data/Rakefile
ADDED
data/config/braspag.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
development:
|
2
|
+
environment: "homologation"
|
3
|
+
merchant_id: "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"
|
4
|
+
crypto_url: "http://localhost:9292"
|
5
|
+
crypto_key: "1234561246"
|
6
|
+
|
7
|
+
test:
|
8
|
+
environment: "homologation"
|
9
|
+
merchant_id: "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"
|
10
|
+
crypto_url: "http://localhost:9292"
|
11
|
+
crypto_key: "1234561246"
|
12
|
+
|
13
|
+
production:
|
14
|
+
environment: "production"
|
15
|
+
merchant_id: "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"
|
16
|
+
crypto_url: "http://localhost:9292"
|
17
|
+
crypto_key: "1234561246"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Braspag
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
desc "Copy braspag.yml file to your application config directory."
|
5
|
+
|
6
|
+
def self.source_root
|
7
|
+
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
8
|
+
end
|
9
|
+
|
10
|
+
def copy_config_file
|
11
|
+
template "config/braspag.yml"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
development:
|
2
|
+
environment: "homologation"
|
3
|
+
merchant_id: "{YOUR_MERCHANT_ID_ON_BRASPAG}"
|
4
|
+
crypto_url: "http://localhost:9292"
|
5
|
+
crypto_key: "1234561246"
|
6
|
+
|
7
|
+
test:
|
8
|
+
environment: "homologation"
|
9
|
+
merchant_id: "{YOUR_MERCHANT_ID_ON_BRASPAG}"
|
10
|
+
crypto_url: "http://localhost:9292"
|
11
|
+
crypto_key: "1234561246"
|
12
|
+
|
13
|
+
production:
|
14
|
+
environment: "production"
|
15
|
+
merchant_id: "{YOUR_MERCHANT_ID_ON_BRASPAG}"
|
16
|
+
crypto_url: "http://localhost:9292"
|
17
|
+
crypto_key: "1234561246"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'httpi'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'json'
|
5
|
+
require 'savon'
|
6
|
+
|
7
|
+
require "locomotiva-braspag/version"
|
8
|
+
require 'locomotiva-braspag/connection'
|
9
|
+
require 'locomotiva-braspag/payment_method'
|
10
|
+
require 'locomotiva-braspag/crypto/jar_webservice'
|
11
|
+
require 'locomotiva-braspag/crypto/webservice'
|
12
|
+
require 'locomotiva-braspag/bill'
|
13
|
+
require 'locomotiva-braspag/poster'
|
14
|
+
require 'locomotiva-braspag/credit_card'
|
15
|
+
require 'locomotiva-braspag/protected_credit_card'
|
16
|
+
require 'locomotiva-braspag/eft'
|
17
|
+
require 'locomotiva-braspag/errors'
|
18
|
+
require 'locomotiva-braspag/utils'
|
19
|
+
require 'locomotiva-braspag/order'
|
20
|
+
|
21
|
+
module Braspag
|
22
|
+
def self.logger=(value)
|
23
|
+
@logger = value
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.logger
|
27
|
+
@logger
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.config_file_path=(path)
|
31
|
+
@config_path = path
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.config_file_path
|
35
|
+
@config_path || 'config/braspag.yml'
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.proxy_address=(value)
|
39
|
+
@proxy_address = value
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.proxy_address
|
43
|
+
@proxy_address
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require "bigdecimal"
|
2
|
+
|
3
|
+
module Braspag
|
4
|
+
class Bill < PaymentMethod
|
5
|
+
|
6
|
+
PAYMENT_METHODS = {
|
7
|
+
:bradesco => "06",
|
8
|
+
:cef => "07",
|
9
|
+
:hsbc => "08",
|
10
|
+
:bb => "09",
|
11
|
+
:real => "10",
|
12
|
+
:citibank => "13",
|
13
|
+
:itau => "14",
|
14
|
+
:unibanco => "26",
|
15
|
+
:bank_boston => "33",
|
16
|
+
:brb => "38",
|
17
|
+
:safra => "40",
|
18
|
+
:santander => "124"
|
19
|
+
}
|
20
|
+
|
21
|
+
MAPPING = {
|
22
|
+
:merchant_id => "merchantId",
|
23
|
+
:order_id => "orderId",
|
24
|
+
:customer_name => "customerName",
|
25
|
+
:customer_id => "customerIdNumber",
|
26
|
+
:amount => "amount",
|
27
|
+
:payment_method => "paymentMethod",
|
28
|
+
:number => "boletoNumber",
|
29
|
+
:instructions => "instructions",
|
30
|
+
:expiration_date => "expirationDate",
|
31
|
+
:emails => "emails"
|
32
|
+
}
|
33
|
+
|
34
|
+
PRODUCTION_INFO_URI = "/webservices/pagador/pedido.asmx/GetDadosBoleto"
|
35
|
+
HOMOLOGATION_INFO_URI = "/pagador/webservice/pedido.asmx/GetDadosBoleto"
|
36
|
+
CREATION_URI = "/webservices/pagador/Boleto.asmx/CreateBoleto"
|
37
|
+
|
38
|
+
def self.generate(params)
|
39
|
+
connection = Braspag::Connection.instance
|
40
|
+
params[:merchant_id] = connection.merchant_id
|
41
|
+
|
42
|
+
params = self.normalize_params(params)
|
43
|
+
self.check_params(params)
|
44
|
+
|
45
|
+
data = {}
|
46
|
+
|
47
|
+
MAPPING.each do |k, v|
|
48
|
+
case k
|
49
|
+
when :payment_method
|
50
|
+
data[v] = PAYMENT_METHODS[params[:payment_method]]
|
51
|
+
when :amount
|
52
|
+
data[v] = Utils.convert_decimal_to_string(params[:amount])
|
53
|
+
else
|
54
|
+
data[v] = params[k] || ""
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
request = ::HTTPI::Request.new(self.creation_url)
|
59
|
+
request.body = data
|
60
|
+
|
61
|
+
response = Utils::convert_to_map(::HTTPI.post(request).body,
|
62
|
+
{
|
63
|
+
:url => nil,
|
64
|
+
:amount => nil,
|
65
|
+
:number => "boletoNumber",
|
66
|
+
:expiration_date => Proc.new { |document|
|
67
|
+
begin
|
68
|
+
Date.parse(document.search("expirationDate").first.to_s)
|
69
|
+
rescue
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
},
|
73
|
+
:return_code => "returnCode",
|
74
|
+
:status => nil,
|
75
|
+
:message => nil
|
76
|
+
})
|
77
|
+
|
78
|
+
raise InvalidMerchantId if response[:message] == "Invalid merchantId"
|
79
|
+
raise InvalidAmount if response[:message] == "Invalid purchase amount"
|
80
|
+
raise InvalidPaymentMethod if response[:message] == "Invalid payment method"
|
81
|
+
raise InvalidStringFormat if response[:message] == "Input string was not in a correct format."
|
82
|
+
raise UnknownError if response[:status].nil?
|
83
|
+
|
84
|
+
response[:amount] = BigDecimal.new(response[:amount])
|
85
|
+
|
86
|
+
response
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.normalize_params(params)
|
90
|
+
params = super
|
91
|
+
|
92
|
+
if params[:expiration_date].respond_to?(:strftime)
|
93
|
+
params[:expiration_date] = params[:expiration_date].strftime("%d/%m/%y")
|
94
|
+
end
|
95
|
+
|
96
|
+
params
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.check_params(params)
|
100
|
+
super
|
101
|
+
|
102
|
+
if params[:number]
|
103
|
+
raise InvalidNumber unless (1..255).include?(params[:number].to_s.size)
|
104
|
+
end
|
105
|
+
|
106
|
+
if params[:instructions]
|
107
|
+
raise InvalidInstructions unless (1..512).include?(params[:instructions].to_s.size)
|
108
|
+
end
|
109
|
+
|
110
|
+
if params[:expiration_date]
|
111
|
+
matches = params[:expiration_date].to_s.match /(\d{2})\/(\d{2})\/(\d{2})/
|
112
|
+
raise InvalidExpirationDate unless matches
|
113
|
+
begin
|
114
|
+
Date.new(matches[3].to_i, matches[2].to_i, matches[1].to_i)
|
115
|
+
rescue ArgumentError
|
116
|
+
raise InvalidExpirationDate
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.info_url
|
122
|
+
connection = Braspag::Connection.instance
|
123
|
+
connection.braspag_url + (connection.production? ? PRODUCTION_INFO_URI : HOMOLOGATION_INFO_URI)
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.creation_url
|
127
|
+
Braspag::Connection.instance.braspag_url + CREATION_URI
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.info(order_id)
|
131
|
+
connection = Braspag::Connection.instance
|
132
|
+
|
133
|
+
raise InvalidOrderId unless self.valid_order_id?(order_id)
|
134
|
+
|
135
|
+
request = ::HTTPI::Request.new(self.info_url)
|
136
|
+
request.body = {
|
137
|
+
:loja => connection.merchant_id,
|
138
|
+
:numeroPedido => order_id.to_s
|
139
|
+
}
|
140
|
+
|
141
|
+
response = ::HTTPI.post(request)
|
142
|
+
|
143
|
+
response = Utils::convert_to_map(response.body, {
|
144
|
+
:document_number => "NumeroDocumento",
|
145
|
+
:payer => "Sacado",
|
146
|
+
:our_number => "NossoNumero",
|
147
|
+
:bill_line => "LinhaDigitavel",
|
148
|
+
:document_date => "DataDocumento",
|
149
|
+
:expiration_date => "DataVencimento",
|
150
|
+
:receiver => "Cedente",
|
151
|
+
:bank => "Banco",
|
152
|
+
:agency => "Agencia",
|
153
|
+
:account => "Conta",
|
154
|
+
:wallet => "Carteira",
|
155
|
+
:amount => "ValorDocumento",
|
156
|
+
:amount_invoice => "ValorPago",
|
157
|
+
:invoice_date => "DataCredito"
|
158
|
+
})
|
159
|
+
|
160
|
+
raise UnknownError if response[:document_number].nil?
|
161
|
+
response
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Braspag
|
2
|
+
class Connection
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
PRODUCTION_URL = "https://transaction.pagador.com.br"
|
6
|
+
HOMOLOGATION_URL = "https://homologacao.pagador.com.br"
|
7
|
+
|
8
|
+
PROTECTED_CARD_PRODUCTION_URL = "https://cartaoprotegido.braspag.com.br/Services"
|
9
|
+
PROTECTED_CARD_HOMOLOGATION_URL = "https://homologacao.braspag.com.br/services/testenvironment"
|
10
|
+
|
11
|
+
attr_reader :braspag_url, :protected_card_url, :merchant_id, :crypto_url, :crypto_key, :options, :environment
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
raise InvalidEnv if ENV["BRASPAG_ENV"].nil? || ENV["BRASPAG_ENV"].empty?
|
15
|
+
|
16
|
+
@options = YAML.load_file(Braspag.config_file_path)[ ENV['BRASPAG_ENV'] ]
|
17
|
+
@merchant_id = @options['merchant_id']
|
18
|
+
|
19
|
+
raise InvalidMerchantId unless @merchant_id =~ /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i
|
20
|
+
|
21
|
+
@crypto_key = @options["crypto_key"]
|
22
|
+
@crypto_url = @options["crypto_url"]
|
23
|
+
@environment = @options["environment"]
|
24
|
+
|
25
|
+
@braspag_url = @options["pagador_url"] || default_env_configuration[:pagador][@environment]
|
26
|
+
@protected_card_url = @options["protected_card_url"] || default_env_configuration[:protected_card][@environment]
|
27
|
+
end
|
28
|
+
|
29
|
+
def default_env_configuration
|
30
|
+
{
|
31
|
+
:pagador => {
|
32
|
+
"production" => PRODUCTION_URL,
|
33
|
+
"homologation" => HOMOLOGATION_URL
|
34
|
+
},
|
35
|
+
:protected_card => {
|
36
|
+
"production" => PROTECTED_CARD_PRODUCTION_URL,
|
37
|
+
"homologation" => PROTECTED_CARD_HOMOLOGATION_URL
|
38
|
+
}
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def production?
|
43
|
+
@environment == 'production'
|
44
|
+
end
|
45
|
+
|
46
|
+
def homologation?
|
47
|
+
@environment == 'homologation'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|