pagseguro-oficial 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -1
  3. data/README.md +54 -2
  4. data/examples/{authorization.rb → authorization/authorization.rb} +1 -1
  5. data/examples/{authorization_by_code.rb → authorization/authorization_by_code.rb} +1 -1
  6. data/examples/{authorization_by_notification_code.rb → authorization/authorization_by_notification_code.rb} +1 -1
  7. data/examples/refund.rb +25 -0
  8. data/examples/{abandoned_transactions.rb → transaction/abandoned_transactions.rb} +1 -1
  9. data/examples/{boleto_transaction_request.rb → transaction/boleto_transaction_request.rb} +1 -1
  10. data/examples/{credit_card_transaction_request.rb → transaction/credit_card_transaction_request.rb} +1 -1
  11. data/examples/{invalid_transaction_by_notification_code.rb → transaction/invalid_transaction_by_notification_code.rb} +1 -1
  12. data/examples/{online_debit_transaction.rb → transaction/online_debit_transaction.rb} +1 -1
  13. data/examples/{transaction_by_code.rb → transaction/transaction_by_code.rb} +1 -1
  14. data/examples/{transaction_by_notification_code.rb → transaction/transaction_by_notification_code.rb} +1 -1
  15. data/examples/{transaction_by_reference.rb → transaction/transaction_by_reference.rb} +1 -1
  16. data/examples/transaction/transaction_cancellation.rb +19 -0
  17. data/examples/transaction/transaction_status_history.rb +22 -0
  18. data/examples/{transactions_by_date.rb → transaction/transactions_by_date.rb} +1 -1
  19. data/lib/pagseguro/installment.rb +14 -0
  20. data/lib/pagseguro/payment_request/serializer.rb +48 -55
  21. data/lib/pagseguro/refund/request_serializer.rb +24 -0
  22. data/lib/pagseguro/refund/response.rb +33 -0
  23. data/lib/pagseguro/refund/response_serializer.rb +17 -0
  24. data/lib/pagseguro/refund/serializer.rb +24 -0
  25. data/lib/pagseguro/refund.rb +44 -0
  26. data/lib/pagseguro/transaction/collection.rb +20 -0
  27. data/lib/pagseguro/transaction/response.rb +37 -4
  28. data/lib/pagseguro/transaction/search.rb +1 -1
  29. data/lib/pagseguro/transaction/serializer.rb +14 -3
  30. data/lib/pagseguro/transaction/status_collection.rb +20 -0
  31. data/lib/pagseguro/transaction.rb +34 -14
  32. data/lib/pagseguro/transaction_cancellation/request_serializer.rb +18 -0
  33. data/lib/pagseguro/transaction_cancellation/response.rb +32 -0
  34. data/lib/pagseguro/transaction_cancellation/response_serializer.rb +17 -0
  35. data/lib/pagseguro/transaction_cancellation/serializer.rb +18 -0
  36. data/lib/pagseguro/transaction_cancellation.rb +40 -0
  37. data/lib/pagseguro/transaction_status.rb +14 -0
  38. data/lib/pagseguro/version.rb +1 -1
  39. data/lib/pagseguro.rb +13 -0
  40. data/spec/fixtures/refund/success.xml +2 -0
  41. data/spec/fixtures/transaction_cancellation/success.xml +2 -0
  42. data/spec/fixtures/transactions/status_history.xml +12 -0
  43. data/spec/pagseguro/refund/request_serializer_spec.rb +15 -0
  44. data/spec/pagseguro/refund/response_serializer_spec.rb +12 -0
  45. data/spec/pagseguro/refund/response_spec.rb +39 -0
  46. data/spec/pagseguro/refund/serializer_spec.rb +15 -0
  47. data/spec/pagseguro/refund_spec.rb +63 -0
  48. data/spec/pagseguro/request_spec.rb +0 -4
  49. data/spec/pagseguro/transaction/collection_spec.rb +43 -0
  50. data/spec/pagseguro/transaction/response_spec.rb +96 -0
  51. data/spec/pagseguro/transaction/search/search_abandoned_spec.rb +41 -28
  52. data/spec/pagseguro/transaction/search/search_by_date_spec.rb +20 -18
  53. data/spec/pagseguro/transaction/search/search_by_reference_spec.rb +13 -18
  54. data/spec/pagseguro/transaction/search_spec.rb +1 -6
  55. data/spec/pagseguro/transaction/serializer_spec.rb +11 -0
  56. data/spec/pagseguro/transaction/status_collection_spec.rb +43 -0
  57. data/spec/pagseguro/transaction_cancellation/request_serializer_spec.rb +13 -0
  58. data/spec/pagseguro/transaction_cancellation/response_serializer_spec.rb +9 -0
  59. data/spec/pagseguro/transaction_cancellation/response_spec.rb +42 -0
  60. data/spec/pagseguro/transaction_cancellation/serializer_spec.rb +13 -0
  61. data/spec/pagseguro/transaction_cancellation_spec.rb +58 -0
  62. data/spec/pagseguro/transaction_spec.rb +159 -13
  63. data/spec/pagseguro/transaction_status_spec.rb +7 -0
  64. data/spec/spec_helper.rb +9 -0
  65. metadata +64 -14
@@ -20,12 +20,23 @@ module PagSeguro
20
20
  end
21
21
  end
22
22
 
23
+ def serialize_status_history
24
+ xml.css("status").map do |node|
25
+ PagSeguro::TransactionStatus.new(
26
+ code: node.css("code").text,
27
+ date: Time.parse(node.css("date").text),
28
+ notification_code: node.css("notificationCode").text
29
+ )
30
+ end
31
+ end
32
+
33
+ private
23
34
  def serialize_general(data)
24
- data[:code] = xml.css("> code").text
35
+ data[:code] = xml.at_css("code").text
25
36
  data[:reference] = xml.css("reference").text
26
- data[:type_id] = xml.css("> type").text
37
+ data[:type_id] = xml.at_css("type").text
27
38
  data[:payment_link] = xml.css("paymentLink").text
28
- data[:status] = xml.css("> status").text
39
+ data[:status] = xml.at_css("status").text
29
40
 
30
41
  cancellation_source = xml.css("cancellationSource")
31
42
  data[:cancellation_source] = cancellation_source.text if cancellation_source.any?
@@ -0,0 +1,20 @@
1
+ module PagSeguro
2
+ class Transaction
3
+ class StatusCollection
4
+ extend Forwardable
5
+
6
+ def_delegators :@statuses, :each, :empty?, :any?
7
+
8
+ def statuses=(objects)
9
+ @statuses = objects
10
+ end
11
+
12
+ def errors
13
+ @errors ||= Errors.new
14
+ end
15
+
16
+ # PagSeguro::TransactionStatus instances.
17
+ attr_reader :statuses
18
+ end
19
+ end
20
+ end
@@ -76,6 +76,17 @@ module PagSeguro
76
76
  load_from_response send_request("transactions/notifications/#{code}", options)
77
77
  end
78
78
 
79
+ # Find a transaction by status.
80
+ # Return a PagSeguro::Transaction::Colletion.
81
+ def self.find_status_history(code)
82
+ request = send_request("transactions/#{code}/statusHistory")
83
+ collection = StatusCollection.new
84
+ response = Response.new(request, collection)
85
+ response.serialize_statuses
86
+
87
+ collection
88
+ end
89
+
79
90
  # Search transactions within a date range.
80
91
  # Return a PagSeguro::SearchByDate instance
81
92
  #
@@ -171,6 +182,21 @@ module PagSeguro
171
182
  @status = ensure_type(PaymentStatus, status)
172
183
  end
173
184
 
185
+ # Set errors.
186
+ def errors
187
+ @errors ||= Errors.new
188
+ end
189
+
190
+ # Update all attributes
191
+ def update_attributes(attrs)
192
+ attrs.each { |name, value| send("#{name}=", value) }
193
+ end
194
+
195
+ # Serialize the XML object.
196
+ def self.load_from_xml(xml) # :nodoc:
197
+ new Serializer.new(xml).serialize
198
+ end
199
+
174
200
  private
175
201
  def self.api_version
176
202
  'v3'
@@ -180,24 +206,18 @@ module PagSeguro
180
206
  @errors = Errors.new
181
207
  end
182
208
 
183
- #
184
209
  # Serialize the HTTP response into data.
185
- def self.load_from_response(response) # :nodoc:
186
- if response.success? and response.xml?
187
- load_from_xml Nokogiri::XML(response.body).css("transaction").first
188
- else
189
- Response.new Errors.new(response)
190
- end
191
- end
210
+ def self.load_from_response(request) # :nodoc:
211
+ transaction = new
212
+ response = Response.new(request, transaction)
213
+ response.serialize
192
214
 
193
- # Send a get request to v3 API version, with the path given
194
- def self.send_request(path, options)
195
- Request.get(path, 'v3', options)
215
+ transaction
196
216
  end
197
217
 
198
- # Serialize the XML object.
199
- def self.load_from_xml(xml) # :nodoc:
200
- new Serializer.new(xml).serialize
218
+ # Send a get request to v3 API version, with the path given
219
+ def self.send_request(path, options = {})
220
+ Request.get(path, api_version, options)
201
221
  end
202
222
  end
203
223
  end
@@ -0,0 +1,18 @@
1
+ module PagSeguro
2
+ class TransactionCancellation
3
+ class RequestSerializer
4
+ # The transaction_cancellation that will be serialized.
5
+ attr_reader :transaction_cancellation
6
+
7
+ def initialize(transaction_cancellation)
8
+ @transaction_cancellation = transaction_cancellation
9
+ end
10
+
11
+ def to_params
12
+ {}.tap do |data|
13
+ data[:transactionCode] = transaction_cancellation.transaction_code
14
+ end.delete_if { |_, value| value.nil? }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ module PagSeguro
2
+ class TransactionCancellation
3
+ class Response
4
+ def initialize(response, cancellation)
5
+ @response = response
6
+ @cancellation = cancellation
7
+ end
8
+
9
+ def serialize
10
+ if success?
11
+ xml = Nokogiri::XML(response.body)
12
+ cancellation.update_attributes(ResponseSerializer.new(xml).serialize)
13
+ else
14
+ cancellation.errors.add(response)
15
+ end
16
+
17
+ cancellation
18
+ end
19
+
20
+ def success?
21
+ response.success? && response.xml?
22
+ end
23
+
24
+ private
25
+ # The request response.
26
+ attr_reader :response
27
+
28
+ # The PagSeguro::TransactionCancellation instance.
29
+ attr_reader :cancellation
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module PagSeguro
2
+ class TransactionCancellation
3
+ class ResponseSerializer
4
+ attr_reader :xml
5
+
6
+ def initialize(xml)
7
+ @xml = xml
8
+ end
9
+
10
+ def serialize
11
+ {}.tap do |data|
12
+ data[:result] = xml.css("> result").text
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module PagSeguro
2
+ class TransactionCancellation
3
+ class Serializer
4
+ # The refund that will be serialized.
5
+ attr_reader :refund
6
+
7
+ def initialize(refund)
8
+ @refund = refund
9
+ end
10
+
11
+ def to_params
12
+ {}.tap do |data|
13
+ data[:transactionCode] = refund.transaction_code
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ module PagSeguro
2
+ class TransactionCancellation
3
+ include Extensions::MassAssignment
4
+
5
+ # Set the transaction code.
6
+ # The transaction status must be: Aguardando pagamento or Em análise.
7
+ attr_accessor :transaction_code
8
+
9
+ # Result from http request.
10
+ attr_accessor :result
11
+
12
+ # Calls the PagSeguro webservice and register the cancellation.
13
+ # Return boolean.
14
+ def register
15
+ request = Request.post("transactions/cancels", api_version, params)
16
+ response = Response.new(request, self)
17
+ response.serialize
18
+
19
+ response.success?
20
+ end
21
+
22
+ # Errors object.
23
+ def errors
24
+ @errors ||= Errors.new
25
+ end
26
+
27
+ def update_attributes(attrs)
28
+ attrs.each { |name, value| send("#{name}=", value) }
29
+ end
30
+
31
+ private
32
+ def api_version
33
+ "v2"
34
+ end
35
+
36
+ def params
37
+ RequestSerializer.new(self).to_params
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ module PagSeguro
2
+ class TransactionStatus
3
+ include Extensions::MassAssignment
4
+
5
+ # The transaction status code
6
+ attr_accessor :code
7
+
8
+ # The date of the status change
9
+ attr_accessor :date
10
+
11
+ # The code of the notification sent when the status changed
12
+ attr_accessor :notification_code
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module PagSeguro
2
- VERSION = "2.3.0"
2
+ VERSION = "2.4.0"
3
3
  end
data/lib/pagseguro.rb CHANGED
@@ -50,6 +50,11 @@ require "pagseguro/payment_request/response"
50
50
  require "pagseguro/payment_status"
51
51
  require "pagseguro/permission"
52
52
  require "pagseguro/request"
53
+ require "pagseguro/refund"
54
+ require "pagseguro/refund/request_serializer"
55
+ require "pagseguro/refund/response"
56
+ require "pagseguro/refund/response_serializer"
57
+ require "pagseguro/refund/serializer"
53
58
  require "pagseguro/sender"
54
59
  require "pagseguro/session"
55
60
  require "pagseguro/session/response"
@@ -58,8 +63,16 @@ require "pagseguro/notification"
58
63
  require "pagseguro/notification/authorization"
59
64
  require "pagseguro/notification/transaction"
60
65
  require "pagseguro/transaction"
66
+ require "pagseguro/transaction_status"
61
67
  require "pagseguro/transaction/response"
62
68
  require "pagseguro/transaction/serializer"
69
+ require "pagseguro/transaction/collection"
70
+ require "pagseguro/transaction/status_collection"
71
+ require "pagseguro/transaction_cancellation"
72
+ require "pagseguro/transaction_cancellation/request_serializer"
73
+ require "pagseguro/transaction_cancellation/response"
74
+ require "pagseguro/transaction_cancellation/response_serializer"
75
+ require "pagseguro/transaction_cancellation/serializer"
63
76
  require "pagseguro/transaction/search"
64
77
  require "pagseguro/transaction/search/search_by_date"
65
78
  require "pagseguro/transaction/search/search_by_reference"
@@ -0,0 +1,2 @@
1
+ <?xml version=”1.0” encoding=”ISO-8859-1” standalone=”yes”?>
2
+ <result>OK</result>
@@ -0,0 +1,2 @@
1
+ <?xml version=”1.0” encoding=”ISO-8859-1” standalone=”yes”?>
2
+ <result>OK</result>
@@ -0,0 +1,12 @@
1
+ <statusHistory>
2
+ <status>
3
+ <code>1</code>
4
+ <date>2015-02-24T10:23:34.000-03:00</date>
5
+ <notificationCode>B7C381-7AADE5ADE576-8CC4159F8FBB-25C7D6</notificationCode>
6
+ </status>
7
+ <status>
8
+ <code>3</code>
9
+ <date>2015-02-24T10:23:36.000-03:00</date>
10
+ <notificationCode>0EF4E7-1F96B496B41B-CFF4ABAFABF3-47CEF2</notificationCode>
11
+ </status>
12
+ </statusHistory>
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Refund::RequestSerializer do
4
+ subject(:serializer) { PagSeguro::Refund::RequestSerializer.new(refund) }
5
+
6
+ let(:refund) do
7
+ PagSeguro::Refund.new transaction_code: "1234",
8
+ value: "100.50"
9
+ end
10
+
11
+ let(:params) { serializer.to_params }
12
+
13
+ it { expect(params).to include(transactionCode: "1234") }
14
+ it { expect(params).to include(refundValue: "100.50") }
15
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Refund::ResponseSerializer do
4
+ context "for existing refund response" do
5
+ let(:source) { File.read("./spec/fixtures/refund/success.xml") }
6
+ let(:xml) { Nokogiri::XML(source) }
7
+ let(:serializer) { described_class.new(xml) }
8
+ subject(:data) { serializer.serialize }
9
+
10
+ it { expect(data).to include(result: "OK") }
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Refund::Response do
4
+ let(:refund) do
5
+ PagSeguro::Refund.new
6
+ end
7
+
8
+ subject { PagSeguro::Refund::Response.new(http_response, refund) }
9
+
10
+ context '#success?' do
11
+ let(:http_response) do
12
+ double(:HttpResponse, xml?: true)
13
+ end
14
+
15
+ it 'delegate to response' do
16
+ allow(http_response).to receive(:success?).and_return(true)
17
+ expect(subject).to be_success
18
+
19
+ allow(http_response).to receive(:success?).and_return(false)
20
+ expect(subject).not_to be_success
21
+ end
22
+ end
23
+
24
+ context '#serialize' do
25
+ let(:http_response) do
26
+ double(:HttpResponse, body: raw_xml, success?: true, xml?: true, unauthorized?: false, bad_request?: false)
27
+ end
28
+
29
+ let(:raw_xml) { File.read("./spec/fixtures/refund/success.xml") }
30
+
31
+ it 'return refund instance' do
32
+ expect(subject.serialize).to eq refund
33
+ end
34
+
35
+ it 'change result' do
36
+ expect { subject.serialize }.to change { refund.result }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Refund::Serializer do
4
+ let(:refund) { PagSeguro::Refund.new }
5
+ let(:params) { serializer.to_params }
6
+ subject(:serializer) { described_class.new(refund) }
7
+
8
+ before do
9
+ refund.transaction_code = "1234"
10
+ refund.value = 100.50
11
+ end
12
+
13
+ it { expect(params).to include(transactionCode: "1234") }
14
+ it { expect(params).to include(refundValue: "100.50") }
15
+ end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Refund do
4
+ let(:xml_parsed) { Nokogiri::XML(raw_xml) }
5
+
6
+ it_assigns_attribute :transaction_code
7
+ it_assigns_attribute :value
8
+
9
+ it "errors must be a instante of PagSeguro::Errors" do
10
+ expect(subject.errors).to be_a(PagSeguro::Errors)
11
+ end
12
+
13
+ describe "#register" do
14
+ let(:refund) { PagSeguro::Refund.new }
15
+
16
+ context 'a correct response' do
17
+ before { FakeWeb.register_uri :any, %r[.*?], body: raw_xml }
18
+
19
+ let(:raw_xml) { File.read("./spec/fixtures/refund/success.xml") }
20
+
21
+ let :response_request do
22
+ double(:ResponseRequest, success?: true, unauthorized?: false, bad_request?: false, data: xml_parsed, body: raw_xml, :xml? => true)
23
+ end
24
+
25
+ it "performs request" do
26
+ expect(PagSeguro::Request).to receive(:post)
27
+ .with("transactions/refunds", "v2", {})
28
+ .and_return(response_request)
29
+
30
+ refund.register
31
+ end
32
+
33
+ it "returns a PagSeguro::Refund" do
34
+ expect(refund.register).to be_a(PagSeguro::Refund)
35
+ end
36
+ end
37
+
38
+ context "a failure response" do
39
+ before do
40
+ allow(PagSeguro::Request).to receive(:post)
41
+ .and_return(response_request)
42
+ end
43
+
44
+ let(:raw_xml) { File.read("./spec/fixtures/invalid_code.xml") }
45
+
46
+ let :response_request do
47
+ double(:ResponseRequest, success?: false, unauthorized?: false, not_found?: false, bad_request?: true, data: xml_parsed, body: raw_xml, :xml? => true)
48
+ end
49
+
50
+ it "returns a PagSeguro::Refund with errors" do
51
+ expect(refund.register.errors).not_to be_empty
52
+ end
53
+ end
54
+ end
55
+
56
+ it '#update_attributes' do
57
+ refund = PagSeguro::Refund.new
58
+
59
+ expect(refund).to receive(:result=).with("OK")
60
+
61
+ refund.update_attributes(result: "OK")
62
+ end
63
+ end
@@ -11,10 +11,6 @@ describe PagSeguro::Request do
11
11
  context "POST request" do
12
12
  before do
13
13
  FakeWeb.register_uri :post, %r[.+], body: "BODY"
14
- PagSeguro.configure do |config|
15
- config.app_id = nil
16
- config.app_key = nil
17
- end
18
14
  end
19
15
 
20
16
  it "includes credentials" do
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Transaction::Collection do
4
+ let(:transactions) { [PagSeguro::Transaction.new] }
5
+
6
+ it "should have a PagSeguro::Errors instance" do
7
+ expect(subject.errors).to be_a(PagSeguro::Errors)
8
+ end
9
+
10
+ context "when there are no transactions" do
11
+ before do
12
+ subject.transactions = []
13
+ end
14
+
15
+ it "is blank" do
16
+ expect(subject).to be_empty
17
+ end
18
+
19
+ it "doesn't have any transaction" do
20
+ expect(subject).not_to be_any
21
+ end
22
+ end
23
+
24
+ context "when there are transactions" do
25
+ before do
26
+ subject.transactions = transactions
27
+ end
28
+
29
+ it "is not blank" do
30
+ expect(subject).not_to be_empty
31
+ end
32
+
33
+ it "has any transaction" do
34
+ expect(subject).to be_any
35
+ end
36
+
37
+ it "has PagSeguro::Transaction instances" do
38
+ subject.each do |transaction|
39
+ expect(transaction).to be_a(PagSeguro::Transaction)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,96 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Transaction::Response do
4
+ subject { PagSeguro::Transaction::Response.new(http_response, object) }
5
+
6
+ context "#success?" do
7
+ let(:object) { double(:Object) }
8
+ let(:http_response) { double(:HttpResponse, xml?: true) }
9
+
10
+ it "delegates to response" do
11
+ allow(http_response).to receive(:success?).and_return(true)
12
+ expect(subject).to be_success
13
+
14
+ allow(http_response).to receive(:success?).and_return(false)
15
+ expect(subject).not_to be_success
16
+ end
17
+ end
18
+
19
+ describe "#serialize_statuses" do
20
+ let(:object) { PagSeguro::Transaction::StatusCollection.new }
21
+ let(:http_response) do
22
+ double(:Response, xml?: true, success?: true, unauthorized?: false,
23
+ bad_request?: false, body: raw_xml, data: parsed_xml)
24
+ end
25
+ let(:parsed_xml) { Nokogiri::XML(raw_xml) }
26
+
27
+ context "when request succeeds" do
28
+ let(:raw_xml) { File.read("./spec/fixtures/transactions/status_history.xml") }
29
+
30
+ it "returns a collection" do
31
+ expect(subject.serialize_statuses).to be_a(PagSeguro::Transaction::StatusCollection)
32
+ end
33
+
34
+ it "not change errors" do
35
+ expect { subject.serialize_statuses }.not_to change { object.errors.empty? }
36
+ end
37
+ end
38
+
39
+ context "when request fails" do
40
+ before do
41
+ allow(http_response).to receive(:success?).and_return(false)
42
+ allow(http_response).to receive(:bad_request?).and_return(true)
43
+ allow(http_response).to receive(:not_found?).and_return(true)
44
+ allow(http_response).to receive(:body).and_return(raw_xml)
45
+ end
46
+ let(:raw_xml) { File.read("./spec/fixtures/invalid_code.xml") }
47
+
48
+ it "returns PagSeguro::Transaction::StatusCollection instance" do
49
+ expect(subject.serialize_statuses).to be_a(PagSeguro::Transaction::StatusCollection)
50
+ end
51
+
52
+ it "change collection errors" do
53
+ expect { subject.serialize_statuses }.to change { object.errors.empty? }
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "#serialize" do
59
+ let(:object) { PagSeguro::Transaction.new }
60
+ let(:http_response) do
61
+ double(:Response, xml?: true, success?: true, unauthorized?: false,
62
+ bad_request?: false, body: raw_xml, data: parsed_xml)
63
+ end
64
+ let(:parsed_xml) { Nokogiri::XML(raw_xml) }
65
+
66
+ context "when request succeeds" do
67
+ let(:raw_xml) { File.read("./spec/fixtures/transactions/success.xml") }
68
+
69
+ it "returns a transaction" do
70
+ expect(subject.serialize).to be_a(PagSeguro::Transaction)
71
+ end
72
+
73
+ it "not change errors" do
74
+ expect { subject.serialize }.not_to change { object.errors.empty? }
75
+ end
76
+ end
77
+
78
+ context "when request fails" do
79
+ before do
80
+ allow(http_response).to receive(:success?).and_return(false)
81
+ allow(http_response).to receive(:bad_request?).and_return(true)
82
+ allow(http_response).to receive(:not_found?).and_return(false)
83
+ allow(http_response).to receive(:body).and_return(raw_xml)
84
+ end
85
+ let(:raw_xml) { File.read("./spec/fixtures/invalid_code.xml") }
86
+
87
+ it "returns the transaction" do
88
+ expect(subject.serialize).to be_a(PagSeguro::Transaction)
89
+ end
90
+
91
+ it "change transaction errors" do
92
+ expect { subject.serialize }.to change { object.errors.empty? }
93
+ end
94
+ end
95
+ end
96
+ end