Skip to content

Commit

Permalink
implementing partial capture method
Browse files Browse the repository at this point in the history
  • Loading branch information
brunograsselli committed Nov 12, 2012
1 parent e4e543f commit 47175ee
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/rbraspag/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class CreditCard < PaymentMethod
:expiration => "expiration",
:security_code => "securityCode",
:number_payments => "numberPayments",
:type => "typePayment",
:type => "typePayment"
}

AUTHORIZE_URI = "/webservices/pagador/Pagador.asmx/Authorize"
CAPTURE_URI = "/webservices/pagador/Pagador.asmx/Capture"
PARTIAL_CAPTURE_URI = "/webservices/pagador/Pagador.asmx/CapturePartial"
CANCELLATION_URI = "/webservices/pagador/Pagador.asmx/VoidTransaction"
PRODUCTION_INFO_URI = "/webservices/pagador/pedido.asmx/GetDadosCartao"
HOMOLOGATION_INFO_URI = "/pagador/webservice/pedido.asmx/GetDadosCartao"
Expand Down Expand Up @@ -75,6 +76,30 @@ def self.capture(order_id)
})
end

def self.partial_capture(order_id, amount)
connection = Braspag::Connection.instance
merchant_id = connection.merchant_id

raise InvalidOrderId unless self.valid_order_id?(order_id)

data = {
MAPPING[:order_id] => order_id,
MAPPING[:merchant_id] => merchant_id,
"captureAmount" => amount
}

response = Braspag::Poster.new(self.partial_capture_url).do_post(:partial_capture, data)

Utils::convert_to_map(response.body, {
:amount => nil,
:number => "authorisationNumber",
:message => 'message',
:return_code => 'returnCode',
:status => 'status',
:transaction_id => "transactionId"
})
end

def self.void(order_id)
connection = Braspag::Connection.instance
merchant_id = connection.merchant_id
Expand Down Expand Up @@ -174,6 +199,10 @@ def self.capture_url
Braspag::Connection.instance.braspag_url + CAPTURE_URI
end

def self.partial_capture_url
Braspag::Connection.instance.braspag_url + PARTIAL_CAPTURE_URI
end

def self.cancellation_url
Braspag::Connection.instance.braspag_url + CANCELLATION_URI
end
Expand Down
70 changes: 70 additions & 0 deletions spec/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,76 @@
end
end

describe ".partial_capture" do
let(:partial_capture_url) { "http://foo.bar/bar/partial" }
let(:order_id) { "um id qualquer" }

before do
@connection.should_receive(:merchant_id)
end

context "invalid order id" do
it "should raise an error" do
Braspag::CreditCard.should_receive(:valid_order_id?)
.with(order_id)
.and_return(false)

expect {
Braspag::CreditCard.partial_capture(order_id, 10.0)
}.to raise_error(Braspag::InvalidOrderId)
end
end

context "valid order id" do
let(:valid_xml) do
<<-EOXML
<?xml version="1.0" encoding="utf-8"?>
<PagadorReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="https://www.pagador.com.br/webservice/pagador">
<amount>2</amount>
<message>Approved</message>
<returnCode>0</returnCode>
<status>0</status>
</PagadorReturn>
EOXML
end

let(:request) { OpenStruct.new :url => partial_capture_url }

before do
Braspag::CreditCard.should_receive(:partial_capture_url)
.and_return(partial_capture_url)

::HTTPI::Request.should_receive(:new)
.with(partial_capture_url)
.and_return(request)

::HTTPI.should_receive(:post)
.with(request)
.and_return(mock(:body => valid_xml))
end

it "should return a Hash" do
response = Braspag::CreditCard.partial_capture("order id qualquer", 10.0)
response.should be_kind_of Hash
response.should == {
:amount => "2",
:number => nil,
:message => "Approved",
:return_code => "0",
:status => "0",
:transaction_id => nil
}
end

it "should post capture info" do
Braspag::CreditCard.partial_capture("order id qualquer", 10.0)
request.body.should == {"orderId"=>"order id qualquer", "captureAmount"=>10.0, "merchantId"=>"um id qualquer"}
end
end
end

describe ".void" do
let(:cancellation_url) { "http://foo.bar/bar/baz" }
let(:order_id) { "um id qualquer" }
Expand Down

0 comments on commit 47175ee

Please sign in to comment.