Skip to content
Browse files

Merge pull request #6 from maizsofta/master

Merging
  • Loading branch information...
2 parents 6d80cc7 + 4982c9e commit 63faf99ea2a94a9d66e250d0ae224069dac533dd @herberthamaral herberthamaral committed May 14, 2011
Showing with 177 additions and 60 deletions.
  1. +2 −5 Gemfile.lock
  2. +9 −0 lib/moip/client.rb
  3. +1 −1 lib/moip/direct_payment.rb
  4. +165 −54 spec/moip_spec.rb
View
7 Gemfile.lock
@@ -9,12 +9,12 @@ PATH
GEM
remote: http://rubygems.org/
specs:
- activesupport (3.0.1)
+ activesupport (3.0.7)
crack (0.1.8)
diff-lcs (1.1.2)
httparty (0.6.1)
crack (= 0.1.8)
- nokogiri (1.4.3.1)
+ nokogiri (1.4.4)
rspec (2.1.0)
rspec-core (~> 2.1.0)
rspec-expectations (~> 2.1.0)
@@ -28,8 +28,5 @@ PLATFORMS
ruby
DEPENDENCIES
- activesupport (>= 2.3.2)
- httparty (~> 0.6.1)
moip!
- nokogiri (~> 1.4.3)
rspec (~> 2.1.0)
View
9 lib/moip/client.rb
@@ -3,7 +3,9 @@
module MoIP
class WebServerResponseError < StandardError ; end
+ class MissingConfigError < StandardError ; end
class MissingTokenError < StandardError ; end
+ class MissingKeyError < StandardError ; end
class Client
include HTTParty
@@ -48,6 +50,13 @@ def notification(params)
private
def peform_action!(action_name, url, options = {})
+
+ raise(MissingConfigError, "É necessário criar um arquivo de configuração para o moip. Veja mais em: https://github.com/moiplabs/moip-ruby") if MoIP.token.nil? && MoIP.key.nil?
+
+ raise(MissingTokenError, "É necessário informar um token na configuração") if MoIP.token.nil? || MoIP.token.empty?
+
+ raise(MissingKeyError, "É necessário informar um key na configuração") if MoIP.key.nil? || MoIP.key.empty?
+
response = self.send(action_name, url, options)
raise(WebServerResponseError, "Ocorreu um erro ao chamar o webservice") if response.nil?
response
View
2 lib/moip/direct_payment.rb
@@ -12,7 +12,7 @@ class << self
# Cria uma instrução de pagamento direto
def body(attributes = {})
- raise(MissinPaymentTypeError, "É necessário informar a razão do pagamento") if attributes[:razao].nil?
+ raise(MissingPaymentTypeError, "É necessário informar a razão do pagamento") if attributes[:razao].nil?
raise(MissingPayerError, "É obrigatório passar as informações do pagador") if attributes[:pagador].nil?
builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
View
219 spec/moip_spec.rb
@@ -6,11 +6,6 @@
describe "Make payments with the MoIP API" do
before :all do
- MoIP.setup do |config|
- config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
- config.token = 'token'
- config.key = 'key'
- end
@pagador = { :nome => "Luiz Inácio Lula da Silva",
:login_moip => "lula",
@@ -47,8 +42,68 @@
:pagador => @pagador, :razao => "Pagamento"}
end
+ context "misconfigured" do
+ it "should raise a missing config error " do
+ MoIP::Client # for autoload
+ lambda { MoIP::Client.checkout(@billet) }.should raise_error(MoIP::MissingConfigError)
+ end
+
+ it "should raise a missing token error when token is nil" do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = nil
+ config.key = 'key'
+ end
+
+ MoIP::Client # for autoload
+ lambda { MoIP::Client.checkout(@billet) }.should raise_error(MoIP::MissingTokenError)
+ end
+
+ it "should raise a missing key error when key is nil" do
+
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = nil
+ end
+
+ MoIP::Client # for autoload
+ lambda { MoIP::Client.checkout(@billet) }.should raise_error(MoIP::MissingKeyError)
+ end
+
+
+
+ it "should raise a missing token error when token is empty" do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = ''
+ config.key = 'key'
+ end
+
+ MoIP::Client # for autoload
+ lambda { MoIP::Client.checkout(@billet) }.should raise_error(MoIP::MissingTokenError)
+ end
+
+ it "should raise a missing key error when key is empty" do
+
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = ''
+ end
+
+ MoIP::Client # for autoload
+ lambda { MoIP::Client.checkout(@billet) }.should raise_error(MoIP::MissingKeyError)
+ end
+ end
+
context "checkout" do
before(:each) do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
MoIP::Client.stub!(:post).
and_return("ns1:EnviarInstrucaoUnicaResponse"=>
{ "Resposta"=>
@@ -58,43 +113,55 @@
}
})
end
-
+
it "with old api should be deprecated" do
deprecations = collect_deprecations{ MoIP.checkout(@billet) }
-
+
deprecations.should_not be_empty
deprecations.any? {|w| w =~ /MoIP.checkout has been deprecated/ }.should be_true
end
-
+
context "when it is a billet checkout" do
+ before(:each) do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
+ end
it "should raise an exception when razao parameter is not passed" do
error = "É necessário informar a razão do pagamento"
-
- lambda { MoIP::Client.checkout(@billet_without_razao) }.should
- raise_error(MoIP::MissingPaymentTypeError,error)
+
+ lambda { MoIP::Client.checkout(@billet_without_razao) }.should raise_error(MoIP::MissingPaymentTypeError,error)
end
-
+
it "should have status 'Sucesso'" do
response = MoIP::Client.checkout(@billet)
response["Status"].should == "Sucesso"
end
end
-
+
context "when it is a debit checkout" do
+ before(:each) do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
+ end
it "should have status 'Sucesso' with valid arguments" do
response = MoIP::Client.checkout(@debit)
response["Status"].should == "Sucesso"
end
-
+
it "should have status 'Falha' when a instituition is not passed as argument" do
@incorrect_debit = { :value => "37.90", :id_proprio => id,
:forma => "DebitoBancario", :pagador => @pagador,
:razao => "Pagamento"}
-
+
error = "Pagamento direto não é possível com a instituição de pagamento enviada"
-
- MoIP::Client.stub!(:post)
- .and_return("ns1:EnviarInstrucaoUnicaResponse"=>
+
+ MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>
{ "Resposta"=>
{
"Status"=>"Falha",
@@ -105,96 +172,120 @@
lambda { MoIP::Client.checkout(@incorrect_debit) }.should
raise_error(MoIP::WebServerResponseError, error)
end
-
+
it "should raise an exception if payer informations were not passed" do
@incorrect_debit = { :value => "37.90", :id_proprio => id,
:forma => "DebitoBancario",
:instituicao => "BancoDoBrasil",
:razao => "Pagamento"
}
-
+
lambda { MoIP::Client.checkout(@incorrect_debit) }.should
raise_error(MoIP::MissingPayerError, "É obrigatório passar as informações do pagador")
end
end
-
+
context "when it is a credit card checkout" do
+ before(:each) do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
+ end
it "should have status 'Sucesso' with valid arguments" do
response = MoIP::Client.checkout(@credit)
response["Status"].should == "Sucesso"
end
-
+
it "should have status 'Falha' when the card informations were not passed as argument" do
@incorrect_credit = { :value => "8.90", :id_proprio => id,
:forma => "CartaoCredito", :pagador => @pagador,
:razao => "Pagamento"
}
-
+
error = "Pagamento direto não é possível com a instituição de pagamento enviada"
- MoIP::Client.stub!(:post)
- .and_return("ns1:EnviarInstrucaoUnicaResponse"=>
+ MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>
{
"Resposta"=>
{
"Status"=>"Falha",
"Erro"=>error
}
})
-
+
error = "Pagamento direto não é possível com a instituição de pagamento enviada"
lambda { MoIP::Client.checkout(@incorrect_credit) }.should
raise_error(MoIP::WebServerResponseError, error)
end
end
-
+
context "in error scenario" do
+ before(:each) do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
+ end
it "should raise an exception if response is nil" do
MoIP::Client.stub!(:post).and_return(nil)
lambda { MoIP::Client.checkout(@billet) }.should
raise_error(StandardError,"Ocorreu um erro ao chamar o webservice")
end
-
+
it "should raise an exception if status is fail" do
- MoIP::Client.stub!(:post)
- .and_return("ns1:EnviarInstrucaoUnicaResponse"=>
+ MoIP::Client.stub!(:post).and_return("ns1:EnviarInstrucaoUnicaResponse"=>
{ "Resposta"=>
{"Status"=>"Falha",
"Erro"=>"O status da resposta é Falha"
}
})
-
+
lambda { MoIP::Client.checkout(@billet) }.should raise_error(StandardError, "O status da resposta é Falha")
end
end
end
-
+
context "query a transaction token" do
+
before(:each) do
- MoIP::Client.stub!(:get)
- .and_return("ns1:ConsultarTokenResponse"=>
+
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
+ MoIP::Client.stub!(:get).and_return("ns1:ConsultarTokenResponse"=>
{ "RespostaConsultar"=>
{"Status"=>"Sucesso",
"ID"=>"201010291031001210000000046760"
}
})
end
-
+
it "with old api should be deprecated" do
deprecations = collect_deprecations{ MoIP.query(token) }
-
+
deprecations.should_not be_empty
deprecations.any? {|w| w =~ /MoIP.query has been deprecated/ }.should be_true
end
-
+
it "should retrieve the transaction" do
response = MoIP::Client.query(token)
response["Status"].should == "Sucesso"
end
-
+
context "in a error scenario" do
+ before(:each) do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
+ end
it "should retrieve status 'Falha'" do
- MoIP::Client.stub!(:get)
- .and_return("ns1:ConsultarTokenResponse"=>
+ MoIP::Client.stub!(:get).and_return("ns1:ConsultarTokenResponse"=>
{ "RespostaConsultar"=>
{"Status"=>"Falha",
"Erro"=>"Instrução não encontrada",
@@ -206,57 +297,77 @@
end
end
end
-
+
context "build the MoIP URL" do
+ before(:each) do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
+ end
it "with old api should be deprecated" do
deprecations = collect_deprecations{ MoIP.moip_page(token) }
-
+
deprecations.should_not be_empty
deprecations.any? {|w| w =~ /MoIP.moip_page has been deprecated/ }.should be_true
end
-
+
it "should build the correct URL" do
page = "https://desenvolvedor.moip.com.br/sandbox/Instrucao.do?token=#{token}"
MoIP::Client.moip_page(token).should == page
end
-
+
it "should raise an error if the token is not informed" do
error = "É necessário informar um token para retornar os dados da transação"
lambda { MoIP::Client.moip_page("").should
raise_error(ArgumentError, error) }
end
-
+
it "should raise an error if nil is passed as the token" do
error = "É necessário informar um token para retornar os dados da transação"
lambda { MoIP::Client.moip_page(nil).should
raise_error(ArgumentError, error) }
end
+
+ it "should raise a missing token error if nil is passed as the token" do
+ lambda { MoIP::Client.moip_page(nil).should raise_error(MissingTokenError) }
+ end
+
+ it "should raise a missing token error if an empty string is passed as the token" do
+ lambda { MoIP::Client.moip_page("").should raise_error(MissingTokenError) }
+ end
end
-
+
context "when receive notification" do
before(:each) do
+ MoIP.setup do |config|
+ config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
+ config.token = 'token'
+ config.key = 'key'
+ end
@params = { "id_transacao" => "Pag62", "valor" => "8.90",
"status_pagamento" => "3", "cod_moip" => "001",
"forma_pagamento" => "73", "tipo_pagamento" => "BoletoBancario",
"email_consumidor" => "presidente@planalto.gov.br" }
end
-
+
it "with old api should be deprecated" do
deprecations = collect_deprecations{ MoIP.notification(@param) }
-
+
deprecations.should_not be_empty
deprecations.any? {|w| w =~ /MoIP.notification has been deprecated/ }.should be_true
end
-
+
it "should return a hash with the params extracted from NASP" do
response = { :transaction_id => "Pag62", :amount => "8.90",
:status => "printed", :code => "001",
:payment_type => "BoletoBancario",
:email => "presidente@planalto.gov.br" }
-
+
MoIP::Client.notification(@params).should == response
end
-
+
it "should return valid status based on status code" do
MoIP::STATUS[1].should == "authorized"
MoIP::STATUS[2].should == "started"
@@ -266,15 +377,15 @@
MoIP::STATUS[6].should == "analysing"
end
end
-
+
def id
"transaction_" + Digest::SHA1.hexdigest([Time.now, rand].join)
end
-
+
def token
"T2X0Q1N021E0B2S9U1P0V3Y0G1F570Y2P4M0P000M0Z0F0J0G0U4N6C7W5T9"
end
-
+
def collect_deprecations
old_behavior = ActiveSupport::Deprecation.behavior
deprecations = []

0 comments on commit 63faf99

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