Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implementing partial capture method

  • Loading branch information...
commit 47175ee3b6ee6d1a8ecab16167f07721736a90e2 1 parent e4e543f
@BrunoGrasselli BrunoGrasselli authored
Showing with 100 additions and 1 deletion.
  1. +30 −1 lib/rbraspag/credit_card.rb
  2. +70 −0 spec/credit_card_spec.rb
View
31 lib/rbraspag/credit_card.rb
@@ -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"
@@ -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
@@ -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
View
70 spec/credit_card_spec.rb
@@ -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" }
Please sign in to comment.
Something went wrong with that request. Please try again.