Permalink
Browse files

add code responsible for cancel the transaction

  • Loading branch information...
1 parent ab39389 commit 1501bfeda7b3dc4314cbf2c1627a2bb580f8cdcf @rafaelss committed May 14, 2012
View
@@ -19,6 +19,9 @@ module Cielo
autoload :CaptureRequest, "cielo/capture_request"
autoload :Capture, "cielo/capture"
+ autoload :CancelationRequest, "cielo/cancelation_request"
+ autoload :Cancelation, "cielo/cancelation"
+
class << self
def configuration
@configuration ||= Configuration.new
View
@@ -0,0 +1,11 @@
+module Cielo
+ class Cancelation
+ include HappyMapper
+
+ tag "cancelamento"
+ element :code, Integer, :tag => "codigo"
+ element :message, String, :tag => "mensagem"
+ element :time, Time, :tag => "data-hora"
+ element :total, Integer, :tag => "valor"
+ end
+end
@@ -0,0 +1,27 @@
+module Cielo
+ class CancelationRequest
+ include HappyMapper
+
+ tag "requisicao-cancelamento"
+ 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 cancel
+ http = Net::HTTP.new(Cielo.configuration.host, Cielo.configuration.port)
+ http.use_ssl = true
+ http.open_timeout = 10 * 1000
+ http.read_timeout = 40 * 1000
+ Cielo.logger.info(http.inspect)
+
+ post_body = "mensagem=#{to_xml}"
+ Cielo.logger.info(post_body)
+
+ response = http.request_post(Cielo.configuration.path, post_body)
+ Cielo.logger.info(response.body)
+
+ Transaction.parse(response.body, :single => true)
+ end
+ end
+end
View
@@ -10,5 +10,6 @@ class Transaction
has_one :authorization, Authorization
has_one :capture, Capture
+ has_one :cancelation, Cancelation
end
end
@@ -38,7 +38,7 @@
subject.tid = "100699306904CC7E1001"
transaction = subject.authorize
- transaction.authorization.code.should == 1
+ transaction.authorization.code.should == 6
transaction.authorization.message.should == "Transação autorizada"
transaction.authorization.time.should == Time.new(2010, 7, 14, 13, 56, 12)
transaction.authorization.total.should == 100
@@ -0,0 +1,49 @@
+# encoding: utf-8
+require "spec_helper"
+
+describe Cielo::CancelationRequest 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/cancelation_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 "#cancel" 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_3.xml", __FILE__)),
+ :headers => { "Content-Type" => "text/xml" }
+ )
+
+ subject.shop = shop
+ subject.id = 1
+ subject.tid = "100699306904CC7E1001"
+ transaction = subject.cancel
+
+ transaction.tid.should == "100699306904CC7E1001"
+ transaction.pan.should == "IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0="
+ transaction.status.should == 9
+ transaction.cancelation.code.should == 9
+ transaction.cancelation.message.should == "Transacao cancelada com sucesso"
+ transaction.cancelation.time.should == Time.new(2012, 5, 14, 9, 52, 14)
+ transaction.cancelation.total.should == 100
+ end
+end
@@ -41,7 +41,7 @@
transaction.tid.should == "100699306904CC7E1001"
transaction.pan.should == "IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0="
transaction.status.should == 6
- transaction.capture.code.should == 1
+ transaction.capture.code.should == 6
transaction.capture.message.should == "Transação capturada"
transaction.capture.time.should == Time.new(2010, 7, 14, 13, 56, 12)
transaction.capture.total.should == 100
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<requisicao-cancelamento id="1" versao="1.1.1">
+ <tid>100699306904CC7E1001</tid>
+ <dados-ec>
+ <numero>1006993069</numero>
+ <chave>25fbb99741c739dd84d7b06ec78c9bac718838630f30b112d033ce2e621b34f3</chave>
+ </dados-ec>
+</requisicao-cancelamento>
@@ -4,7 +4,7 @@
<pan>IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0=</pan>
<status>6</status>
<captura>
- <codigo>1</codigo>
+ <codigo>6</codigo>
<mensagem>Transação capturada</mensagem>
<data-hora>2010-07-14T13:56:12</data-hora>
<valor>100</valor>
@@ -4,7 +4,7 @@
<pan>IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0=</pan>
<status>6</status>
<autorizacao>
- <codigo>1</codigo>
+ <codigo>6</codigo>
<mensagem>Transação autorizada</mensagem>
<data-hora>2010-07-14T13:56:12</data-hora>
<valor>100</valor>
@@ -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>9</status>
+ <cancelamento>
+ <codigo>9</codigo>
+ <mensagem>Transacao cancelada com sucesso</mensagem>
+ <data-hora>2012-05-14T09:52:14</data-hora>
+ <valor>100</valor>
+ </cancelamento>
+</transacao>

0 comments on commit 1501bfe

Please sign in to comment.