Permalink
Browse files

refatorando para suportar o ambiente de produção da Braspag

  • Loading branch information...
lenon committed Jan 4, 2012
1 parent 74f2372 commit 6e5b1c388adcf8567d5d152c3cd04c0185779600
View
@@ -1,10 +1,10 @@
PATH
remote: .
specs:
- rbraspag (0.0.16)
+ rbraspag (0.0.18)
cs-httpi (>= 0.9.5.2)
- json
- nokogiri
+ json (>= 1.6.1)
+ nokogiri (>= 1.5.0)
GEM
remote: http://rubygems.org/
@@ -22,11 +22,11 @@ GEM
guard (>= 0.2.2)
guard-rspec (0.4.0)
guard (>= 0.4.0)
- json (1.5.3)
+ json (1.6.4)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
nokogiri (1.5.0)
- rack (1.3.0)
+ rack (1.4.0)
rake (0.9.2)
rspec (2.6.0)
rspec-core (~> 2.6.0)
View
@@ -1,12 +1,17 @@
development:
+ environment: "homologation"
merchant_id: "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"
- braspag_url: "https://homologacao.pagador.com.br"
crypto_url: "http://localhost:9292"
crypto_key: "1234561246"
test:
+ environment: "homologation"
merchant_id: "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"
- braspag_url: "https://homologacao.pagador.com.br"
crypto_url: "http://localhost:9292"
crypto_key: "1234561246"
+production:
+ environment: "production"
+ merchant_id: "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"
+ crypto_url: "http://localhost:9292"
+ crypto_key: "1234561246"
@@ -12,4 +12,4 @@ def copy_config_file
end
end
end
-end
+end
@@ -1,17 +1,17 @@
development:
+ environment: "homologation"
merchant_id: "{YOUR_MERCHANT_ID_ON_BRASPAG}"
- braspag_url: "https://homologacao.pagador.com.br"
crypto_url: "http://localhost:9292"
crypto_key: "1234561246"
test:
+ environment: "homologation"
merchant_id: "{YOUR_MERCHANT_ID_ON_BRASPAG}"
- braspag_url: "https://homologacao.pagador.com.br"
crypto_url: "http://localhost:9292"
crypto_key: "1234561246"
production:
+ environment: "production"
merchant_id: "{YOUR_MERCHANT_ID_ON_BRASPAG}"
- braspag_url: "https://www.pagador.com.br"
crypto_url: "http://localhost:9292"
crypto_key: "1234561246"
View
@@ -2,7 +2,7 @@
module Braspag
class Bill < PaymentMethod
-
+
PAYMENT_METHODS = {
:bradesco => "06",
:cef => "07",
@@ -27,7 +27,7 @@ class Bill < PaymentMethod
:emails => "emails"
}
-
+ # (my face when I saw this method ---------> D:)
def self.generate(params)
connection = Braspag::Connection.instance
params = params
@@ -71,7 +71,6 @@ def self.generate(params)
raise InvalidPaymentMethod
end
-
data = MAPPING.inject({}) do |memo, k|
if k[0] == :payment_method
memo[k[1]] = PAYMENT_METHODS[params[:payment_method]]
@@ -117,14 +116,13 @@ def self.generate(params)
response
end
-
def self.info(order_id)
connection = Braspag::Connection.instance
raise InvalidOrderId unless order_id.is_a?(String) || order_id.is_a?(Fixnum)
raise InvalidOrderId unless (1..50).include?(order_id.to_s.size)
- request = ::HTTPI::Request.new("#{connection.braspag_url}/pagador/webservice/pedido.asmx/GetDadosBoleto")
+ request = ::HTTPI::Request.new("#{connection.braspag_query_url}/GetDadosBoleto")
request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
response = ::HTTPI.post(request)
@@ -148,11 +146,10 @@ def self.info(order_id)
raise UnknownError if response[:document_number].nil?
response
-
end
protected
-
+
def self.uri
connection = Braspag::Connection.instance
"#{connection.braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto"
View
@@ -1,27 +1,42 @@
module Braspag
class Connection
include Singleton
+
class InvalidMerchantId < Exception ; end
class InvalidEnv < Exception ; end
class InvalidBraspagUrl < Exception ; end
- attr_reader :braspag_url, :merchant_id, :crypto_url, :crypto_key
+ PRODUCTION_URL = "https://transaction.pagador.com.br"
+ HOMOLOGATION_URL = "https://homologacao.pagador.com.br"
+
+ PRODUCTION_QUERY_URL = "https://query.pagador.com.br/webservices/pagador/pedido.asmx"
+ HOMOLOGATION_QUERY_URL = "https://homologacao.pagador.com.br/pagador/webservice/pedido.asmx"
+
+ attr_reader :braspag_url, :braspag_query_url, :merchant_id, :crypto_url, :crypto_key, :options, :environment
def initialize
raise InvalidEnv if ENV["RACK_ENV"].nil? || ENV["RACK_ENV"].empty?
- options = YAML.load_file("config/braspag.yml")
- options = options[ENV["RACK_ENV"]]
- merchant_id = options["merchant_id"]
+ @options = YAML.load_file('config/braspag.yml')[ ENV['RACK_ENV'] ]
+ @merchant_id = @options['merchant_id']
+
+ raise InvalidMerchantId unless @merchant_id =~ /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i
+
+ @environment = @options["environment"] == 'production' ? 'production' : 'homologation'
- raise InvalidMerchantId unless merchant_id.length == 38
- raise InvalidMerchantId unless merchant_id.match /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i
- raise InvalidBraspagUrl if options["braspag_url"].nil? || options["braspag_url"].empty?
+ @braspag_url = self.production? ? PRODUCTION_URL : HOMOLOGATION_URL
+ @braspag_query_url = self.production? ? PRODUCTION_QUERY_URL : HOMOLOGATION_QUERY_URL
+
+ @crypto_key = @options["crypto_key"]
+ @crypto_url = @options["crypto_url"]
+ end
+
+ def production?
+ @environment == 'production'
+ end
- @crypto_key = options["crypto_key"]
- @crypto_url = options["crypto_url"]
- @braspag_url = options["braspag_url"]
- @merchant_id = merchant_id
+ def homologation?
+ @environment == 'homologation'
end
end
end
@@ -123,7 +123,7 @@ def self.info(order_id)
raise InvalidOrderId unless order_id.is_a?(String) || order_id.is_a?(Fixnum)
raise InvalidOrderId unless (1..50).include?(order_id.to_s.size)
- request = ::HTTPI::Request.new("#{connection.braspag_url}/pagador/webservice/pedido.asmx/GetDadosCartao")
+ request = ::HTTPI::Request.new("#{connection.braspag_query_url}/GetDadosCartao")
request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
response = ::HTTPI.post(request)
View
@@ -8,7 +8,7 @@ def self.status(order_id)
raise InvalidOrderId unless order_id.is_a?(String) || order_id.is_a?(Fixnum)
raise InvalidOrderId unless (1..50).include?(order_id.to_s.size)
- request = ::HTTPI::Request.new("#{connection.braspag_url}/pagador/webservice/pedido.asmx/GetDadosPedido")
+ request = ::HTTPI::Request.new("#{connection.braspag_query_url}/GetDadosPedido")
request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
response = ::HTTPI.post(request)
View
@@ -3,7 +3,8 @@
describe Braspag::Bill do
let!(:braspag_url) { "https://homologacao.pagador.com.br" }
-
+ let!(:braspag_query_url) { "https://homologacao.pagador.com.br/pagador/webservice/pedido.asmx" }
+
describe ".generate" do
context "consitencies" do
before do
@@ -496,7 +497,7 @@
xmlns="http://www.pagador.com.br/" />
EOXML
- FakeWeb.register_uri(:post, "#{braspag_url}/pagador/webservice/pedido.asmx/GetDadosBoleto",
+ FakeWeb.register_uri(:post, "#{braspag_query_url}/GetDadosBoleto",
:body => xml)
expect {
@@ -534,7 +535,7 @@
</DadosBoleto>
EOXML
- FakeWeb.register_uri(:post, "#{braspag_url}/pagador/webservice/pedido.asmx/GetDadosBoleto",
+ FakeWeb.register_uri(:post, "#{braspag_query_url}/GetDadosBoleto",
:body => xml)
status = Braspag::Bill.info("12345")
FakeWeb.clean_registry
Oops, something went wrong.

0 comments on commit 6e5b1c3

Please sign in to comment.