Skip to content
Browse files

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

  • Loading branch information...
1 parent 74f2372 commit 6e5b1c388adcf8567d5d152c3cd04c0185779600 @lenon lenon committed Jan 4, 2012
View
10 Gemfile.lock
@@ -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
9 config/braspag.yml
@@ -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"
View
2 lib/generators/braspag/install/install_generator.rb
@@ -12,4 +12,4 @@ def copy_config_file
end
end
end
-end
+end
View
6 lib/generators/braspag/install/templates/config/braspag.yml
@@ -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
11 lib/rbraspag/bill.rb
@@ -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
37 lib/rbraspag/connection.rb
@@ -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
View
2 lib/rbraspag/credit_card.rb
@@ -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
2 lib/rbraspag/order.rb
@@ -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
7 spec/bill_spec.rb
@@ -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
View
192 spec/connection_spec.rb
@@ -1,129 +1,155 @@
-#encoding: utf-8
+# encoding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe Braspag::Connection do
- let!(:merchant_id) { "{12345678-1234-1234-1234-123456789000}" }
- let!(:braspag_url) { "https://homologacao.pagador.com.br" }
- let!(:crypto_key) {"{84BE7E7F-698A-6C74-F820-AE359C2A07C2}"}
- let!(:crypto_url) {"http://localhost:9292"}
-
- let!(:mock_for_connection) {
- mock = {}
- mock[ENV["RACK_ENV"]] = {
+ let(:merchant_id) { "{12345678-1234-1234-1234-123456789000}" }
+ let(:crypto_key) { "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}" }
+ let(:crypto_url) { "http://localhost:9292" }
+
+ let(:braspag_environment) { "homologation" }
+
+ let(:braspag_homologation_url) { "https://homologacao.pagador.com.br" }
+ let(:braspag_production_url) { "https://transaction.pagador.com.br" }
+
+ let(:braspag_homologation_query_url) { "https://homologacao.pagador.com.br/pagador/webservice/pedido.asmx" }
+ let(:braspag_production_query_url) { "https://query.pagador.com.br/webservices/pagador/pedido.asmx" }
+
+ let(:braspag_config) do
+ config = {}
+ config[ENV["RACK_ENV"]] = {
+ "environment" => braspag_environment,
"merchant_id" => merchant_id,
- "braspag_url" => braspag_url,
- "crypto_key" => crypto_key,
- "crypto_url" => crypto_url
+ "crypto_key" => crypto_key,
+ "crypto_url" => crypto_url
}
- mock
- }
+ config
+ end
before(:all) do
@connection = Braspag::Connection.clone
end
it "should read config/braspag.yml when alloc first instance" do
- YAML.should_receive(:load_file).with("config/braspag.yml").and_return(mock_for_connection)
+ YAML.should_receive(:load_file)
+ .with("config/braspag.yml")
+ .and_return(braspag_config)
@connection.instance
end
- it "should not read config/braspag.yml when alloc second instance" do
+ it "should not read config/braspag.yml when alloc a second instance" do
YAML.should_not_receive(:load_file)
@connection.instance
end
- it "should generate exception when RACK_ENV is nil" do
- backup = ENV["RACK_ENV"].clone
- ENV["RACK_ENV"] = nil
+ it "should generate an exception when RACK_ENV is nil" do
+ ENV.should_receive(:[])
+ .with("RACK_ENV")
+ .and_return(nil)
+
expect {
- other_connection = Braspag::Connection.clone
- other_connection.instance
- }.should raise_error(Braspag::Connection::InvalidEnv)
- ENV["RACK_ENV"] = backup
+ Braspag::Connection.clone.instance
+ }.to raise_error Braspag::Connection::InvalidEnv
end
- it "should generate exception when RACK_ENV is empty" do
- backup = ENV["RACK_ENV"].clone
- ENV["RACK_ENV"] = ""
+ it "should generate an exception when RACK_ENV is empty" do
+ ENV.should_receive(:[])
+ .twice
+ .with("RACK_ENV")
+ .and_return("")
+
expect {
- other_connection = Braspag::Connection.clone
- other_connection.instance
- }.should raise_error(Braspag::Connection::InvalidEnv)
- ENV["RACK_ENV"] = backup
+ Braspag::Connection.clone.instance
+ }.to raise_error Braspag::Connection::InvalidEnv
end
+ it "should generate an exception when merchant_id is not in a correct format" do
+ braspag_config[ENV["RACK_ENV"]]["merchant_id"] = "A" * 38
- it "should generate exception when :merchant_id is more than 38 chars" do
- mock_merchant = mock_for_connection
- mock_merchant[ENV["RACK_ENV"]]["merchant_id"] = (1..100).collect{"A"}.join
- YAML.should_receive(:load_file).with("config/braspag.yml").and_return(mock_merchant)
- other_connection = Braspag::Connection.clone
+ YAML.should_receive(:load_file)
+ .with("config/braspag.yml")
+ .and_return(braspag_config)
- expect { other_connection.instance }.should raise_error(Braspag::Connection::InvalidMerchantId)
+ expect {
+ Braspag::Connection.clone.instance
+ }.to raise_error Braspag::Connection::InvalidMerchantId
end
- it "should generate exception when :merchant_id is less than 38 chars" do
- mock_merchant = mock_for_connection
- mock_merchant[ENV["RACK_ENV"]]["merchant_id"] = (1..37).collect{"B"}.join
- YAML.should_receive(:load_file).with("config/braspag.yml").and_return(mock_merchant)
- other_connection = Braspag::Connection.clone
+ it { @connection.instance.crypto_url.should == crypto_url }
+ it { @connection.instance.crypto_key.should == crypto_key }
+ it { @connection.instance.merchant_id.should == merchant_id }
+
+ [:braspag_url, :merchant_id, :crypto_url, :crypto_key,
+ :options, :environment].each do |attribute|
+
+ it { @connection.instance.should respond_to(attribute) }
- expect { other_connection.instance }.should raise_error(Braspag::Connection::InvalidMerchantId)
end
- context "should generate exception when :merchant_id not follow format {00000000-0000-0000-0000-000000000000}" do
- it "0000000-0000-0000-0000-000000000000" do
- mock_merchant = mock_for_connection
- mock_merchant[ENV["RACK_ENV"]]["merchant_id"] = "0000000-0000-0000-0000-000000000000"
- YAML.should_receive(:load_file).with("config/braspag.yml").and_return(mock_merchant)
- other_connection = Braspag::Connection.clone
- expect { other_connection.instance }.should raise_error(Braspag::Connection::InvalidMerchantId)
+ describe "#production?" do
+ it "should return true when environment is production" do
+ braspag_config[ENV["RACK_ENV"]]["environment"] = "production"
+
+ YAML.should_receive(:load_file)
+ .and_return(braspag_config)
+
+ Braspag::Connection.clone.instance.production?.should be_true
end
- it "{000000000000000000000000000000000000}" do
- mock_merchant = mock_for_connection
- mock_merchant[ENV["RACK_ENV"]]["merchant_id"] = "{000000000000000000000000000000000000}"
- YAML.should_receive(:load_file).with("config/braspag.yml").and_return(mock_merchant)
- other_connection = Braspag::Connection.clone
- expect { other_connection.instance }.should raise_error(Braspag::Connection::InvalidMerchantId)
+ it "should return false when environment is not production" do
+ braspag_config[ENV["RACK_ENV"]]["environment"] = "homologation"
+
+ YAML.should_receive(:load_file)
+ .and_return(braspag_config)
+
+ Braspag::Connection.clone.instance.production?.should be_false
end
end
- it "should :merchant_id has correct in instance" do
- new_connection = @connection.instance
- new_connection.merchant_id.should == merchant_id
- end
+ describe "#homologation?" do
+ it "should return true when environment is homologation" do
+ braspag_config[ENV["RACK_ENV"]]["environment"] = "homologation"
- it "should generate exception when :braspag_url is nil" do
- mock_merchant = mock_for_connection
- mock_merchant[ENV["RACK_ENV"]]["braspag_url"] = nil
- YAML.should_receive(:load_file).with("config/braspag.yml").and_return(mock_merchant)
- other_connection = Braspag::Connection.clone
+ YAML.should_receive(:load_file)
+ .and_return(braspag_config)
- expect { other_connection.instance }.should raise_error(Braspag::Connection::InvalidBraspagUrl)
- end
+ Braspag::Connection.clone.instance.homologation?.should be_true
+ end
- it "should generate exception when :braspag_url is empty" do
- mock_merchant = mock_for_connection
- mock_merchant[ENV["RACK_ENV"]]["braspag_url"] = ""
- YAML.should_receive(:load_file).with("config/braspag.yml").and_return(mock_merchant)
- other_connection = Braspag::Connection.clone
+ it "should return false when environment is not homologation" do
+ braspag_config[ENV["RACK_ENV"]]["environment"] = "production"
- expect { other_connection.instance }.should raise_error(Braspag::Connection::InvalidBraspagUrl)
- end
+ YAML.should_receive(:load_file)
+ .and_return(braspag_config)
- it "should :braspag_url has correct in instance" do
- new_connection = @connection.instance
- new_connection.braspag_url.should == braspag_url
+ Braspag::Connection.clone.instance.homologation?.should be_false
+ end
end
- it "should :crypto_url has correct in instance" do
- new_connection = @connection.instance
- new_connection.crypto_url.should == crypto_url
- end
+ describe "#braspag_url" do
+ context "when environment is homologation" do
+ it "should return the Braspag homologation url" do
+ braspag_config[ENV["RACK_ENV"]]["environment"] = "homologation"
- it "should :crypto_key has correct in instance" do
- new_connection = @connection.instance
- new_connection.crypto_key.should == crypto_key
+ YAML.should_receive(:load_file)
+ .and_return(braspag_config)
+
+ connection = Braspag::Connection.clone.instance
+ connection.braspag_url.should == braspag_homologation_url
+ connection.braspag_query_url.should == braspag_homologation_query_url
+ end
+ end
+
+ context "when environment is production" do
+ it "should return the Braspag production url" do
+ braspag_config[ENV["RACK_ENV"]]["environment"] = "production"
+
+ YAML.should_receive(:load_file)
+ .and_return(braspag_config)
+
+ connection = Braspag::Connection.clone.instance
+ connection.braspag_url.should == braspag_production_url
+ connection.braspag_query_url.should == braspag_production_query_url
+ end
+ end
end
end
View
5 spec/credit_card_spec.rb
@@ -3,6 +3,7 @@
describe Braspag::CreditCard do
let!(:braspag_url) { "https://homologacao.pagador.com.br" }
+ let!(:braspag_query_url) { "https://homologacao.pagador.com.br/pagador/webservice/pedido.asmx" }
describe ".authorize" do
it "should raise an error when :order_id is not present" do
@@ -487,7 +488,7 @@
xmlns="http://www.pagador.com.br/" />
EOXML
- FakeWeb.register_uri(:post, "#{braspag_url}/pagador/webservice/pedido.asmx/GetDadosCartao",
+ FakeWeb.register_uri(:post, "#{braspag_query_url}/GetDadosCartao",
:body => xml)
expect {
@@ -515,7 +516,7 @@
</DadosCartao>
EOXML
- FakeWeb.register_uri(:post, "#{braspag_url}/pagador/webservice/pedido.asmx/GetDadosCartao",
+ FakeWeb.register_uri(:post, "#{braspag_query_url}/GetDadosCartao",
:body => xml)
order_info = Braspag::CreditCard.info("12345")
FakeWeb.clean_registry
View
5 spec/order_spec.rb
@@ -3,6 +3,7 @@
describe Braspag::Order do
let!(:braspag_url) { "https://homologacao.pagador.com.br" }
+ let!(:braspag_query_url) { "https://homologacao.pagador.com.br/pagador/webservice/pedido.asmx" }
describe "#status" do
it "should raise an error when no order_id is given" do
@@ -32,7 +33,7 @@
xmlns="http://www.pagador.com.br/" />
EOXML
- FakeWeb.register_uri(:post, "#{braspag_url}/pagador/webservice/pedido.asmx/GetDadosPedido",
+ FakeWeb.register_uri(:post, "#{braspag_query_url}/GetDadosPedido",
:body => xml)
expect {
@@ -68,7 +69,7 @@
</DadosPedido>
EOXML
- FakeWeb.register_uri(:post, "#{braspag_url}/pagador/webservice/pedido.asmx/GetDadosPedido",
+ FakeWeb.register_uri(:post, "#{braspag_query_url}/GetDadosPedido",
:body => xml)
order_info = Braspag::Order.status("12345")
FakeWeb.clean_registry

0 comments on commit 6e5b1c3

Please sign in to comment.
Something went wrong with that request. Please try again.