Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
refatorando para suportar o ambiente de produção da Braspag
Browse files Browse the repository at this point in the history
  • Loading branch information
lenon committed Jan 5, 2012
1 parent 74f2372 commit 6e5b1c3
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 121 deletions.
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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/
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions config/braspag.yml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion lib/generators/braspag/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def copy_config_file
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/generators/braspag/install/templates/config/braspag.yml
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 4 additions & 7 deletions lib/rbraspag/bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Braspag
class Bill < PaymentMethod

PAYMENT_METHODS = {
:bradesco => "06",
:cef => "07",
Expand All @@ -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
Expand Down Expand Up @@ -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]]
Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand Down
37 changes: 26 additions & 11 deletions lib/rbraspag/connection.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/rbraspag/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rbraspag/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions spec/bill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 6e5b1c3

Please sign in to comment.