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

Commit

Permalink
adding get credit card method
Browse files Browse the repository at this point in the history
  • Loading branch information
Weverton do Couto Timoteo committed Jun 20, 2012
1 parent 69d69f6 commit ba10f66
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/rbraspag/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def self.void(order_id)
})
end

# saves credit card in Braspag PCI Compliant
def self.save(params = {})
connection = Braspag::Connection.instance
params[:merchant_id] = connection.merchant_id
Expand All @@ -173,6 +174,27 @@ def self.save(params = {})

end

# request the credit card info in Braspag PCI Compliant
def self.get(just_click_key)
connection = Braspag::Connection.instance

raise InvalidJustClickKey unless valid_just_click_key?(just_click_key)

request = ::HTTPI::Request.new(self.get_protected_card_url)
request.body = { 'getCreditCardRequestWS' => {:loja => connection.merchant_id, :justClickKey => just_click_key} }

response = ::HTTPI.post(request)

response = Utils::convert_to_map(response.body, {
:holder => "CardHolder",
:card_number => "CardNumber",
:expiration => "CardExpiration",
:masked_card_number => "MaskedCardNumber"
})

raise UnknownError if response[:card_number].nil?
response
end

def self.info(order_id)
connection = Braspag::Connection.instance
Expand Down Expand Up @@ -240,6 +262,9 @@ def self.check_protected_card_params(params)
end
end

def self.valid_just_click_key?(just_click_key)
(just_click_key.is_a?(String) && just_click_key.size == 36)
end

def self.info_url
connection = Braspag::Connection.instance
Expand Down
1 change: 1 addition & 0 deletions lib/rbraspag/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class InvalidExpirationDate < Exception ; end
class InvalidSecurityCode < Exception ; end
class InvalidType < Exception ; end
class InvalidNumberPayments < Exception ; end
class InvalidJustClickKey < Exception ; end
class UnknownError < Exception ; end

class Connection
Expand Down
69 changes: 69 additions & 0 deletions spec/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,75 @@
end
end

describe ".get" do
let(:get_protected_card_url) { "http://braspag/bla" }

let(:invalid_xml) do
<<-EOXML
<DadosCartao xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.pagador.com.br/">
<CardHolder>Joao Maria Souza</CardHolder>
<CardNumber></CardNumber>
<CardExpiration>10/12</CardExpiration>
<MaskedCardNumber>******9999</MaskedCardNumber>
</DadosCartao>
EOXML
end

let(:valid_xml) do
<<-EOXML
<DadosCartao xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.pagador.com.br/">
<CardHolder>Joao Maria Souza</CardHolder>
<CardNumber>9999999999</CardNumber>
<CardExpiration>10/12</CardExpiration>
<MaskedCardNumber>******9999</MaskedCardNumber>
</DadosCartao>
EOXML
end

it "should raise an error when just click key is not valid" do
Braspag::CreditCard.should_receive(:valid_just_click_key?)
.with("bla")
.and_return(false)

expect {
Braspag::CreditCard.get "bla"
}.to raise_error(Braspag::InvalidJustClickKey)
end

it "should raise an error when Braspag returned an invalid xml as response" do
FakeWeb.register_uri(:post, get_protected_card_url, :body => invalid_xml)

Braspag::CreditCard.should_receive(:get_protected_card_url)
.and_return(get_protected_card_url)

expect {
Braspag::CreditCard.get("b0b0b0b0-bbbb-4d4d-bd27-f1f1f1ededed")
}.to raise_error(Braspag::UnknownError)
end

it "should return a Hash when Braspag returned a valid xml as response" do
FakeWeb.register_uri(:post, get_protected_card_url, :body => valid_xml)

Braspag::CreditCard.should_receive(:get_protected_card_url)
.and_return(get_protected_card_url)

response = Braspag::CreditCard.get("b0b0b0b0-bbbb-4d4d-bd27-f1f1f1ededed")
response.should be_kind_of Hash

response.should == {
:holder => "Joao Maria Souza",
:expiration => "10/12",
:card_number => "9" * 10,
:masked_card_number => "*" * 6 + "9" * 4
}
end

end


describe ".info" do
let(:info_url) { "http://braspag/bla" }
Expand Down

0 comments on commit ba10f66

Please sign in to comment.