Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add ability to capture a transaction

  • Loading branch information...
commit 0fcd17ec6ee7db88fb993b2e5bf8f47b522c40c5 1 parent c28216e
@rafaelss authored
View
2  lib/cielo.rb
@@ -8,6 +8,8 @@ module Cielo
autoload :Card, "cielo/card"
autoload :TransactionRequest, "cielo/transaction_request"
autoload :Transaction, "cielo/transaction"
+ autoload :CaptureRequest, "cielo/capture_request"
+ autoload :Capture, "cielo/capture"
autoload :Configuration, "cielo/configuration"
class << self
View
11 lib/cielo/capture.rb
@@ -0,0 +1,11 @@
+module Cielo
+ class Capture
+ include HappyMapper
+
+ tag "captura"
+ element :code, Integer, :tag => "codigo"
+ element :message, String, :tag => "mensagem"
+ element :time, Time, :tag => "data-hora"
+ element :total, Integer, :tag => "valor"
+ end
+end
View
20 lib/cielo/capture_request.rb
@@ -0,0 +1,20 @@
+module Cielo
+ class CaptureRequest
+ include HappyMapper
+
+ tag "requisicao-captura"
+ attribute :id, Integer, :on_save => proc { |value| value.to_s }
+ attribute :version, String, :tag => "versao", :on_save => proc { |value| value ? value : "1.1.1" }
+ element :tid, String
+ has_one :shop, Shop
+
+ def capture
+ http = Net::HTTP.new("qasecommerce.cielo.com.br", 443)
+ http.use_ssl = true
+ http.open_timeout = 10 * 1000
+ http.read_timeout = 40 * 1000
+ response = http.request_post("/servicos/ecommwsec.do", "mensagem=#{to_xml}")
+ Transaction.parse(response.body, :single => true)
+ end
+ end
+end
View
2  lib/cielo/transaction.rb
@@ -7,5 +7,7 @@ class Transaction
element :pan, String
element :status, Integer
element :authentication_url, String, :tag => "url-autenticacao"
+
+ has_one :capture, Capture
end
end
View
49 spec/cielo/capture_request_spec.rb
@@ -0,0 +1,49 @@
+# encoding: utf-8
+require "spec_helper"
+
+describe Cielo::CaptureRequest do
+ subject { described_class.new }
+
+ let(:shop) do
+ Cielo::Shop.new.tap do |s|
+ s.number = 1006993069
+ s.key = "25fbb99741c739dd84d7b06ec78c9bac718838630f30b112d033ce2e621b34f3"
+ end
+ end
+
+ let(:request_xml) { File.read(File.expand_path("../../fixtures/capture_request.xml", __FILE__)) }
+
+ it "#to_xml" do
+ subject.shop = shop
+ subject.id = 1
+ subject.tid = "100699306904CC7E1001"
+ xml = subject.to_xml
+
+ xml.should == request_xml
+ end
+
+ it "#capture" do
+ stub_request(:post, "https://qasecommerce.cielo.com.br/servicos/ecommwsec.do").
+ with(
+ :body => "mensagem=#{request_xml}"
+ ).
+ to_return(
+ :status => 200,
+ :body => File.read(File.expand_path("../../fixtures/transaction_1.xml", __FILE__)),
+ :headers => { "Content-Type" => "text/xml" }
+ )
+
+ subject.shop = shop
+ subject.id = 1
+ subject.tid = "100699306904CC7E1001"
+ transaction = subject.capture
+
+ transaction.tid.should == "100699306904CC7E1001"
+ transaction.pan.should == "IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0="
+ transaction.status.should == 6
+ transaction.capture.code.should == 1
+ transaction.capture.message.should == "Transação capturada"
+ transaction.capture.time.should == Time.new(2010, 7, 14, 13, 56, 12)
+ transaction.capture.total.should == 100
+ end
+end
View
8 spec/fixtures/capture_request.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<requisicao-captura id="1" versao="1.1.1">
+ <tid>100699306904CC7E1001</tid>
+ <dados-ec>
+ <numero>1006993069</numero>
+ <chave>25fbb99741c739dd84d7b06ec78c9bac718838630f30b112d033ce2e621b34f3</chave>
+ </dados-ec>
+</requisicao-captura>
View
12 spec/fixtures/transaction_1.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<transacao versao="1.1.1" id="1" xmlns="http://ecommerce.cbmp.com.br">
+ <tid>100699306904CC7E1001</tid>
+ <pan>IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0=</pan>
+ <status>6</status>
+ <captura>
+ <codigo>1</codigo>
+ <mensagem>Transação capturada</mensagem>
+ <data-hora>2010-07-14T13:56:12</data-hora>
+ <valor>100</valor>
+ </captura>
+</transacao>
Please sign in to comment.
Something went wrong with that request. Please try again.