rbraspag 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +57 -0
- data/Guardfile +9 -0
- data/README.md +46 -0
- data/Rakefile +6 -0
- data/lib/rbraspag/bill.rb +133 -0
- data/lib/rbraspag/connection.rb +17 -0
- data/lib/rbraspag/credit_card.rb +41 -0
- data/lib/rbraspag/crypto/jar_webservice.rb +93 -0
- data/lib/rbraspag/crypto/webservice.rb +103 -0
- data/lib/rbraspag/eft.rb +96 -0
- data/lib/rbraspag/errors.rb +22 -0
- data/lib/rbraspag/version.rb +3 -0
- data/lib/rbraspag.rb +31 -0
- data/rbraspag.gemspec +30 -0
- data/spec/bill_spec.rb +394 -0
- data/spec/connection_spec.rb +54 -0
- data/spec/credit_card_spec.rb +68 -0
- data/spec/crypto/jar_webservice_spec.rb +190 -0
- data/spec/crypto/webservice_spec.rb +165 -0
- data/spec/eft_spec.rb +238 -0
- data/spec/spec_helper.rb +9 -0
- metadata +165 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rbraspag (0.0.1)
|
5
|
+
cs-httpi (= 0.9.5.2)
|
6
|
+
json
|
7
|
+
nokogiri
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
archive-tar-minitar (0.5.2)
|
13
|
+
columnize (0.3.3)
|
14
|
+
cs-httpi (0.9.5.2)
|
15
|
+
rack
|
16
|
+
diff-lcs (1.1.2)
|
17
|
+
fakeweb (1.3.0)
|
18
|
+
guard (0.4.2)
|
19
|
+
thor (~> 0.14.6)
|
20
|
+
guard-rspec (0.4.0)
|
21
|
+
guard (>= 0.4.0)
|
22
|
+
json (1.5.3)
|
23
|
+
linecache19 (0.5.12)
|
24
|
+
ruby_core_source (>= 0.1.4)
|
25
|
+
nokogiri (1.5.0)
|
26
|
+
rack (1.3.0)
|
27
|
+
rspec (2.6.0)
|
28
|
+
rspec-core (~> 2.6.0)
|
29
|
+
rspec-expectations (~> 2.6.0)
|
30
|
+
rspec-mocks (~> 2.6.0)
|
31
|
+
rspec-core (2.6.4)
|
32
|
+
rspec-expectations (2.6.0)
|
33
|
+
diff-lcs (~> 1.1.2)
|
34
|
+
rspec-mocks (2.6.0)
|
35
|
+
ruby-debug-base19 (0.11.25)
|
36
|
+
columnize (>= 0.3.1)
|
37
|
+
linecache19 (>= 0.5.11)
|
38
|
+
ruby_core_source (>= 0.1.4)
|
39
|
+
ruby-debug19 (0.11.6)
|
40
|
+
columnize (>= 0.3.1)
|
41
|
+
linecache19 (>= 0.5.11)
|
42
|
+
ruby-debug-base19 (>= 0.11.19)
|
43
|
+
ruby_core_source (0.1.5)
|
44
|
+
archive-tar-minitar (>= 0.5.2)
|
45
|
+
shoulda-matchers (1.0.0.beta2)
|
46
|
+
thor (0.14.6)
|
47
|
+
|
48
|
+
PLATFORMS
|
49
|
+
ruby
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
fakeweb
|
53
|
+
guard-rspec
|
54
|
+
rbraspag!
|
55
|
+
rspec
|
56
|
+
ruby-debug19
|
57
|
+
shoulda-matchers
|
data/Guardfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# rBraspag
|
2
|
+
|
3
|
+
rbraspag gem to use Braspag gateway
|
4
|
+
|
5
|
+
# Instalation
|
6
|
+
|
7
|
+
gem install rbraspag
|
8
|
+
|
9
|
+
# Example
|
10
|
+
|
11
|
+
## Create a Bill (Boleto/Bloqueto for brazilian guys)
|
12
|
+
your_merchant_id = '{12341234-1234-1234-1234-123412341234}'
|
13
|
+
connection = Braspag::Connection.new your_merchant_id
|
14
|
+
Braspag::Bill.new(connection , {
|
15
|
+
:order_id => 1,
|
16
|
+
:amount => 3,
|
17
|
+
:payment_method => 10
|
18
|
+
})
|
19
|
+
@response = @bill.generate
|
20
|
+
|
21
|
+
|
22
|
+
# License
|
23
|
+
|
24
|
+
(The MIT License)
|
25
|
+
|
26
|
+
Copyright (c) 2010
|
27
|
+
|
28
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
29
|
+
a copy of this software and associated documentation files (the
|
30
|
+
'Software'), to deal in the Software without restriction, including
|
31
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
32
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
33
|
+
permit persons to whom the Software is furnished to do so, subject to
|
34
|
+
the following conditions:
|
35
|
+
|
36
|
+
The above copyright notice and this permission notice shall be
|
37
|
+
included in all copies or substantial portions of the Software.
|
38
|
+
|
39
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
40
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
41
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
42
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
43
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
44
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
45
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
46
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
module Braspag
|
2
|
+
class Bill
|
3
|
+
|
4
|
+
MAPPING = {
|
5
|
+
:merchant_id => "merchantId",
|
6
|
+
:order_id => "orderId",
|
7
|
+
:customer_name => "customerName",
|
8
|
+
:customer_id => "customerIdNumber",
|
9
|
+
:amount => "amount",
|
10
|
+
:payment_method => "paymentMethod",
|
11
|
+
:number => "boletoNumber",
|
12
|
+
:instructions => "instructions",
|
13
|
+
:expiration_date => "expirationDate"
|
14
|
+
}
|
15
|
+
|
16
|
+
def initialize(connection, params)
|
17
|
+
raise InvalidConnection unless connection.is_a?(Braspag::Connection)
|
18
|
+
|
19
|
+
@connection = connection
|
20
|
+
@params = params
|
21
|
+
@params[:merchant_id] = connection.merchant_id
|
22
|
+
|
23
|
+
if @params[:expiration_date].is_a?(Date)
|
24
|
+
@params[:expiration_date] = @params[:expiration_date].strftime("%d/%m/%y")
|
25
|
+
end
|
26
|
+
|
27
|
+
ok?
|
28
|
+
end
|
29
|
+
|
30
|
+
def ok?
|
31
|
+
raise IncompleteParams if @params[:order_id].nil? || @params[:amount].nil? || @params[:payment_method].nil?
|
32
|
+
|
33
|
+
raise InvalidOrderId unless @params[:order_id].is_a?(String) || @params[:order_id].is_a?(Fixnum)
|
34
|
+
raise InvalidOrderId unless (1..50).include?(@params[:order_id].to_s.size)
|
35
|
+
|
36
|
+
unless @params[:customer_name].nil?
|
37
|
+
raise InvalidCustomerName unless (1..255).include?(@params[:customer_name].to_s.size)
|
38
|
+
end
|
39
|
+
|
40
|
+
unless @params[:customer_id].nil?
|
41
|
+
raise InvalidCustomerId unless (11..18).include?(@params[:customer_id].to_s.size)
|
42
|
+
end
|
43
|
+
|
44
|
+
unless @params[:number].nil?
|
45
|
+
raise InvalidNumber unless (1..255).include?(@params[:number].to_s.size)
|
46
|
+
end
|
47
|
+
|
48
|
+
unless @params[:instructions].nil?
|
49
|
+
raise InvalidInstructions unless (1..512).include?(@params[:instructions].to_s.size)
|
50
|
+
end
|
51
|
+
|
52
|
+
unless @params[:expiration_date].nil?
|
53
|
+
raise InvalidExpirationDate unless @params[:expiration_date].to_s.size == 8
|
54
|
+
end
|
55
|
+
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def generate
|
60
|
+
data = {}
|
61
|
+
@params.each {|name, value|
|
62
|
+
if MAPPING[name].nil?
|
63
|
+
data[name] = value
|
64
|
+
else
|
65
|
+
data[MAPPING[name]] = value
|
66
|
+
end
|
67
|
+
}
|
68
|
+
|
69
|
+
request = ::HTTPI::Request.new uri
|
70
|
+
request.body = data
|
71
|
+
|
72
|
+
response = ::HTTPI.post request
|
73
|
+
response = convert_to_map response.body
|
74
|
+
|
75
|
+
raise InvalidAmount if response[:message] == "Invalid purchase amount"
|
76
|
+
raise InvalidMerchantId if response[:message] == "Invalid merchantId"
|
77
|
+
raise InvalidPaymentMethod if response[:message] == "Invalid payment method"
|
78
|
+
raise InvalidStringFormat if response[:message] == "Input string was not in a correct format."
|
79
|
+
raise UnknownError if response[:status].nil?
|
80
|
+
|
81
|
+
response
|
82
|
+
end
|
83
|
+
|
84
|
+
protected
|
85
|
+
|
86
|
+
def uri
|
87
|
+
"#{@connection.base_url}/webservices/pagador/Boleto.asmx/CreateBoleto"
|
88
|
+
end
|
89
|
+
|
90
|
+
def convert_to_map(document)
|
91
|
+
document = Nokogiri::XML(document)
|
92
|
+
|
93
|
+
map = {
|
94
|
+
:url => nil,\
|
95
|
+
:amount => nil,
|
96
|
+
:number => "boletoNumber",
|
97
|
+
:expiration_date => Proc.new {
|
98
|
+
begin
|
99
|
+
Date.parse(document.search("expirationDate").first.to_s)
|
100
|
+
rescue
|
101
|
+
nil
|
102
|
+
end
|
103
|
+
},
|
104
|
+
:return_code => "returnCode",
|
105
|
+
:status => nil,
|
106
|
+
:message => nil
|
107
|
+
}
|
108
|
+
|
109
|
+
map.each do |keyForMap , keyValue|
|
110
|
+
|
111
|
+
if keyValue.is_a?(String) || keyValue.nil?
|
112
|
+
keyValue = keyForMap if keyValue.nil?
|
113
|
+
|
114
|
+
value = document.search(keyValue).first
|
115
|
+
if !value.nil?
|
116
|
+
value = value.content.to_s
|
117
|
+
map[keyForMap] = value unless value == ""
|
118
|
+
end
|
119
|
+
|
120
|
+
elsif keyValue.is_a?(Proc)
|
121
|
+
map[keyForMap] = keyValue.call
|
122
|
+
end
|
123
|
+
|
124
|
+
map[keyForMap]
|
125
|
+
end
|
126
|
+
|
127
|
+
map
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Braspag
|
2
|
+
class Connection
|
3
|
+
class InvalidMerchantId < Exception ; end
|
4
|
+
|
5
|
+
attr_reader :base_url, :environment, :merchant_id
|
6
|
+
|
7
|
+
def initialize(merchant_id, environment = :production)
|
8
|
+
raise InvalidMerchantId unless merchant_id.length == 38
|
9
|
+
raise InvalidMerchantId unless merchant_id.match /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i
|
10
|
+
|
11
|
+
environment = :test unless environment.eql? :production
|
12
|
+
@environment = eval(environment.to_s.capitalize)
|
13
|
+
@base_url = @environment::BASE_URL
|
14
|
+
@merchant_id = merchant_id
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Braspag
|
2
|
+
class CreditCard
|
3
|
+
|
4
|
+
%w(authorize capture).each do |method|
|
5
|
+
eval <<-METHOD
|
6
|
+
def #{method}!(map)
|
7
|
+
invoke! "#{method.capitalize}", map
|
8
|
+
end
|
9
|
+
METHOD
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def invoke!(method, map)
|
15
|
+
document = invoke_and_parse(method, "#{base_action_url}/#{method}") do |message|
|
16
|
+
map.each do |key, value|
|
17
|
+
message.add("tns:#{key}", "#{value}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
convert_to_map document
|
21
|
+
end
|
22
|
+
|
23
|
+
def base_action_url
|
24
|
+
"https://www.pagador.com.br/webservice/pagador"
|
25
|
+
end
|
26
|
+
|
27
|
+
def uri
|
28
|
+
"#{@connection.base_url}/webservices/pagador/Pagador.asmx"
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_to_map(document)
|
32
|
+
map = { "amount" => "", "authorisationNumber" => "", "message" => "", "returnCode" => "", "status" => "", "transactionId" => "" }
|
33
|
+
map.each_key do |key|
|
34
|
+
document.xpath("//ns:#{key}").each do |text|
|
35
|
+
map[key] = text.to_s
|
36
|
+
end
|
37
|
+
end
|
38
|
+
map
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Braspag
|
2
|
+
module Crypto
|
3
|
+
class JarWebservice
|
4
|
+
|
5
|
+
def initialize(crypto_key, connection_uri)
|
6
|
+
@crypto_key = crypto_key
|
7
|
+
@connection_uri = connection_uri
|
8
|
+
end
|
9
|
+
|
10
|
+
def encrypt(map)
|
11
|
+
raise Braspag::IncompleteParams if map.nil?
|
12
|
+
raise Braspag::IncompleteParams unless map.is_a?(Hash)
|
13
|
+
|
14
|
+
request = ::HTTPI::Request.new encrypt_uri
|
15
|
+
|
16
|
+
data = {:key => @crypto_key, :fields => map}
|
17
|
+
|
18
|
+
request.headers["Content-Type"] = "application/json"
|
19
|
+
|
20
|
+
request.body = data.to_json
|
21
|
+
|
22
|
+
response = ::HTTPI.post request
|
23
|
+
|
24
|
+
begin
|
25
|
+
response = JSON.parse(response.body)
|
26
|
+
rescue Exception => e
|
27
|
+
raise UnknownError
|
28
|
+
end
|
29
|
+
|
30
|
+
raise IncompleteParams if (
|
31
|
+
response["msg"] == "INVALID FORMAT" ||
|
32
|
+
response["msg"] == "INVALID FIELDS"
|
33
|
+
)
|
34
|
+
|
35
|
+
raise InvalidEncryptedKey if response["msg"] == "INVALID ENCRYPTED STRING"
|
36
|
+
raise InvalidCryptKey if response["msg"] == "INVALID KEY"
|
37
|
+
|
38
|
+
response["encrypt"]
|
39
|
+
end
|
40
|
+
|
41
|
+
def decrypt(encrypted, fields)
|
42
|
+
raise Braspag::InvalidEncryptedKey if encrypted.nil?
|
43
|
+
raise Braspag::InvalidEncryptedKey unless encrypted.is_a?(String)
|
44
|
+
|
45
|
+
raise Braspag::IncompleteParams if fields.nil?
|
46
|
+
raise Braspag::IncompleteParams unless fields.is_a?(Array)
|
47
|
+
|
48
|
+
|
49
|
+
request = ::HTTPI::Request.new decrypt_uri
|
50
|
+
request.body = {
|
51
|
+
"key" => @crypto_key,
|
52
|
+
"encrypted" => encrypted,
|
53
|
+
"fields" => fields
|
54
|
+
}.to_json
|
55
|
+
|
56
|
+
request.headers["Content-Type"] = "application/json"
|
57
|
+
|
58
|
+
response = ::HTTPI.post request
|
59
|
+
|
60
|
+
begin
|
61
|
+
response = JSON.parse(response.body)
|
62
|
+
rescue Exception => e
|
63
|
+
raise UnknownError
|
64
|
+
end
|
65
|
+
|
66
|
+
raise IncompleteParams if (
|
67
|
+
response["msg"] == "INVALID FORMAT" ||
|
68
|
+
response["msg"] == "INVALID FIELDS"
|
69
|
+
)
|
70
|
+
|
71
|
+
raise InvalidEncryptedKey if response["msg"] == "INVALID ENCRYPTED STRING"
|
72
|
+
|
73
|
+
raise InvalidCryptKey if response["msg"] == "INVALID KEY"
|
74
|
+
|
75
|
+
map = {}
|
76
|
+
response["fields"].each do |key,value|
|
77
|
+
map[key.downcase.to_sym] = value
|
78
|
+
end
|
79
|
+
map
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
def encrypt_uri
|
84
|
+
"#{@connection_uri}/v1/encrypt.json"
|
85
|
+
end
|
86
|
+
|
87
|
+
def decrypt_uri
|
88
|
+
"#{@connection_uri}/v1/decrypt.json"
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module Braspag
|
2
|
+
module Crypto
|
3
|
+
class Webservice
|
4
|
+
def initialize(connection)
|
5
|
+
@connection = connection
|
6
|
+
end
|
7
|
+
|
8
|
+
def encrypt(map)
|
9
|
+
raise Braspag::IncompleteParams if map.nil?
|
10
|
+
raise Braspag::IncompleteParams unless map.is_a?(Hash)
|
11
|
+
|
12
|
+
request = ::HTTPI::Request.new uri
|
13
|
+
|
14
|
+
fields = "\n"
|
15
|
+
map.each do |key, value|
|
16
|
+
fields.concat(" <tns:string>#{key}=#{value}</tns:string>\n")
|
17
|
+
end
|
18
|
+
|
19
|
+
request.body = <<-STRING
|
20
|
+
<?xml version="1.0" encoding="utf-8"?>
|
21
|
+
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
|
22
|
+
<env:Header />
|
23
|
+
<env:Body>
|
24
|
+
<tns:EncryptRequest xmlns:tns="https://www.pagador.com.br/webservice/BraspagGeneralService">
|
25
|
+
<tns:merchantId>#{@connection.merchant_id}</tns:merchantId>
|
26
|
+
<tns:request>
|
27
|
+
#{fields}
|
28
|
+
</tns:request>
|
29
|
+
</tns:EncryptRequest>
|
30
|
+
</env:Body>
|
31
|
+
</env:Envelope>
|
32
|
+
STRING
|
33
|
+
|
34
|
+
request.headers["Content-Type"] = "text/xml"
|
35
|
+
|
36
|
+
response = ::HTTPI.post request
|
37
|
+
|
38
|
+
document = Nokogiri::XML(response.body)
|
39
|
+
|
40
|
+
raise Braspag::UnknownError if document.children.empty?
|
41
|
+
|
42
|
+
#melhorar este parser cof cof
|
43
|
+
response = document.children.children.children.children.children.to_s
|
44
|
+
|
45
|
+
raise Braspag::InvalidMerchantId if (response == 'Erro BP 011' || response == 'Erro BP 012')
|
46
|
+
raise Braspag::InvalidIP if (response == 'Erro BP 067' || response == 'Erro BP 068')
|
47
|
+
|
48
|
+
response
|
49
|
+
end
|
50
|
+
|
51
|
+
def decrypt(encripted_text)
|
52
|
+
|
53
|
+
raise Braspag::IncompleteParams if encripted_text.nil?
|
54
|
+
raise Braspag::IncompleteParams unless encripted_text.is_a?(String)
|
55
|
+
|
56
|
+
request = ::HTTPI::Request.new uri
|
57
|
+
|
58
|
+
request.body = <<-STRING
|
59
|
+
<?xml version="1.0" encoding="utf-8"?>
|
60
|
+
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
|
61
|
+
<env:Header />
|
62
|
+
<env:Body>
|
63
|
+
<tns:DecryptRequest xmlns:tns="https://www.pagador.com.br/webservice/BraspagGeneralService">
|
64
|
+
<tns:merchantId>#{@connection.merchant_id}</tns:merchantId>
|
65
|
+
<tns:cryptString>#{encripted_text}</tns:cryptString>
|
66
|
+
</tns:DecryptRequest>
|
67
|
+
</env:Body>
|
68
|
+
</env:Envelope>
|
69
|
+
STRING
|
70
|
+
|
71
|
+
request.headers["Content-Type"] = "text/xml"
|
72
|
+
|
73
|
+
response = ::HTTPI.post request
|
74
|
+
|
75
|
+
document = Nokogiri::XML(response.body)
|
76
|
+
raise Braspag::UnknownError if document.children.empty?
|
77
|
+
|
78
|
+
result_error = document.children.children.children.children.children.first.content.to_s
|
79
|
+
|
80
|
+
raise Braspag::InvalidMerchantId if (result_error == 'Erro BP 011' || result_error == 'Erro BP 012')
|
81
|
+
raise Braspag::InvalidIP if (result_error == 'Erro BP 067' || result_error == 'Erro BP 068')
|
82
|
+
|
83
|
+
convert_request_to_map document
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
protected
|
88
|
+
def uri
|
89
|
+
"#{@connection.base_url}/BraspagGeneralService/BraspagGeneralService.asmx"
|
90
|
+
end
|
91
|
+
|
92
|
+
def convert_request_to_map(document)
|
93
|
+
map = {}
|
94
|
+
document.children.children.children.children.children.each do |n|
|
95
|
+
values = n.content.to_s.split("=")
|
96
|
+
map[values[0].downcase.to_sym] = values[1]
|
97
|
+
end
|
98
|
+
map
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/lib/rbraspag/eft.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
module Braspag
|
2
|
+
class Eft
|
3
|
+
MAPPING = {
|
4
|
+
:merchant_id => "Id_Loja",
|
5
|
+
:order_id => "VendaId",
|
6
|
+
:customer_name => "Nome",
|
7
|
+
:customer_id => "Cpf",
|
8
|
+
:amount => "Valor",
|
9
|
+
:payment_method => "codpagamento",
|
10
|
+
:installments => "PARCELAS",
|
11
|
+
:has_interest => "TIPOPARCELADO"
|
12
|
+
}
|
13
|
+
|
14
|
+
def initialize(connection, params, crypto_strategy = nil)
|
15
|
+
raise InvalidConnection unless connection.is_a?(Braspag::Connection)
|
16
|
+
|
17
|
+
@connection = connection
|
18
|
+
@params = params
|
19
|
+
@params[:merchant_id] = connection.merchant_id
|
20
|
+
@crypto_strategy = crypto_strategy
|
21
|
+
ok?
|
22
|
+
end
|
23
|
+
|
24
|
+
def ok?
|
25
|
+
raise IncompleteParams if @params[:order_id].nil? || @params[:amount].nil? || @params[:payment_method].nil?
|
26
|
+
|
27
|
+
raise InvalidOrderId unless @params[:order_id].is_a?(String) || @params[:order_id].is_a?(Fixnum)
|
28
|
+
raise InvalidOrderId unless (1..50).include?(@params[:order_id].to_s.size)
|
29
|
+
|
30
|
+
unless @params[:customer_name].nil?
|
31
|
+
raise InvalidCustomerName unless (1..255).include?(@params[:customer_name].to_s.size)
|
32
|
+
end
|
33
|
+
|
34
|
+
unless @params[:customer_id].nil?
|
35
|
+
raise InvalidCustomerId unless (11..18).include?(@params[:customer_id].to_s.size)
|
36
|
+
end
|
37
|
+
|
38
|
+
unless @params[:installments].nil?
|
39
|
+
raise InvalidInstallments unless (1..2).include?(@params[:installments].to_s.size)
|
40
|
+
begin
|
41
|
+
@params[:installments] = Integer(@params[:installments]) unless @params[:installments].is_a?(Integer)
|
42
|
+
rescue Exception => e
|
43
|
+
raise InvalidInstallments
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
unless @params[:has_interest].nil?
|
48
|
+
raise InvalidHasInterest unless (@params[:has_interest].is_a?(TrueClass) || @params[:has_interest].is_a?(FalseClass))
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
true
|
53
|
+
end
|
54
|
+
|
55
|
+
def generate
|
56
|
+
data = {}
|
57
|
+
@params.each {|name, value|
|
58
|
+
if MAPPING[name].nil?
|
59
|
+
data[name] = value
|
60
|
+
else
|
61
|
+
data[MAPPING[name]] = value
|
62
|
+
end
|
63
|
+
}
|
64
|
+
|
65
|
+
html = "<form id=\"form_tef_#{@params[:order_id]}\" name=\"form_tef_#{@params[:order_id]}\" action=\"#{self.uri}\" method=\"post\">\n"
|
66
|
+
|
67
|
+
if @crypto_strategy.nil?
|
68
|
+
data.each do |key, value|
|
69
|
+
html.concat "<input type=\"text\" name=\"#{key}\" value=\"#{value}\" />\n"
|
70
|
+
end
|
71
|
+
else
|
72
|
+
data.delete("Id_Loja")
|
73
|
+
html.concat "<input type=\"text\" name=\"crypt\" value=\"#{@crypto_strategy.encrypt(data)}\" />\n"
|
74
|
+
html.concat "<input type=\"text\" name=\"Id_Loja\" value=\"#{@params[:merchant_id]}\" />\n"
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
html.concat <<-EOHTML
|
79
|
+
</form>
|
80
|
+
<script type="text/javascript" charset="utf-8">
|
81
|
+
document.forms["form_tef_#{@params[:order_id]}"].submit();
|
82
|
+
</script>
|
83
|
+
EOHTML
|
84
|
+
|
85
|
+
html
|
86
|
+
end
|
87
|
+
|
88
|
+
protected
|
89
|
+
|
90
|
+
def uri
|
91
|
+
"#{@connection.base_url}/pagador/passthru.asp"
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Braspag
|
2
|
+
class InvalidConnection < Exception ; end
|
3
|
+
class InvalidMerchantId < Exception ; end
|
4
|
+
class InvalidConnection < Exception ; end
|
5
|
+
class IncompleteParams < Exception ; end
|
6
|
+
class InvalidOrderId < Exception ; end
|
7
|
+
class InvalidCustomerName < Exception ; end
|
8
|
+
class InvalidCustomerId < Exception ; end
|
9
|
+
class InvalidNumber < Exception ; end
|
10
|
+
class InvalidInstructions < Exception ; end
|
11
|
+
class InvalidExpirationDate < Exception ; end
|
12
|
+
class InvalidStringFormat < Exception ; end
|
13
|
+
class InvalidPost < Exception ; end
|
14
|
+
class InvalidPaymentMethod < Exception ; end
|
15
|
+
class InvalidAmount < Exception ; end
|
16
|
+
class InvalidInstallments < Exception ; end
|
17
|
+
class InvalidHasInterest < Exception ; end
|
18
|
+
class InvalidIP < Exception; end
|
19
|
+
class InvalidCryptKey < Exception; end
|
20
|
+
class InvalidEncryptedKey < Exception; end
|
21
|
+
class UnknownError < Exception ; end
|
22
|
+
end
|
data/lib/rbraspag.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "rbraspag/version"
|
2
|
+
|
3
|
+
ENV["RACK_ENV"] ||= "development"
|
4
|
+
|
5
|
+
require 'bundler'
|
6
|
+
|
7
|
+
Bundler.setup
|
8
|
+
|
9
|
+
require "cs-httpi"
|
10
|
+
require "json"
|
11
|
+
require "nokogiri"
|
12
|
+
|
13
|
+
Bundler.require(:default, ENV["RACK_ENV"].to_sym)
|
14
|
+
|
15
|
+
require 'rbraspag/connection'
|
16
|
+
require 'rbraspag/crypto/jar_webservice'
|
17
|
+
require 'rbraspag/crypto/webservice'
|
18
|
+
require 'rbraspag/bill'
|
19
|
+
require 'rbraspag/credit_card'
|
20
|
+
require 'rbraspag/eft'
|
21
|
+
require 'rbraspag/errors'
|
22
|
+
|
23
|
+
module Braspag
|
24
|
+
class Production
|
25
|
+
BASE_URL = 'https://www.pagador.com.br'
|
26
|
+
end
|
27
|
+
|
28
|
+
class Test
|
29
|
+
BASE_URL = 'https://homologacao.pagador.com.br'
|
30
|
+
end
|
31
|
+
end
|