pagseguro-oficial 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -1
  3. data/CHANGELOG.md +17 -1
  4. data/Gemfile +0 -2
  5. data/README.md +1 -1
  6. data/docs/transparent_checkout.md +3 -3
  7. data/examples/authorization/create_authorization.rb +32 -0
  8. data/examples/authorization/create_authorization_with_account.rb +56 -0
  9. data/examples/authorization/{authorization_by_code.rb → search/search_authorization_by_code.rb} +4 -4
  10. data/examples/authorization/search/search_authorization_by_date.rb +35 -0
  11. data/examples/authorization/{authorization_by_notification_code.rb → search/search_authorization_by_notification_code.rb} +4 -5
  12. data/examples/authorization/search/search_authorization_by_reference.rb +31 -0
  13. data/examples/{payment_request.rb → checkout/create_payment_request.rb} +5 -7
  14. data/examples/{transaction/abandoned_transactions.rb → checkout/search/search_abandoned_transactions.rb} +6 -3
  15. data/examples/{transaction/transaction_by_code.rb → checkout/search/search_transaction_by_code.rb} +3 -3
  16. data/examples/{transaction/transaction_by_notification_code.rb → checkout/search/search_transaction_by_notification_code.rb} +3 -3
  17. data/examples/{transaction/transaction_by_reference.rb → checkout/search/search_transaction_by_reference.rb} +3 -3
  18. data/examples/{transaction/transaction_status_history.rb → checkout/search/search_transaction_status_history.rb} +2 -2
  19. data/examples/{transaction/transactions_by_date.rb → checkout/search/search_transactions_by_date.rb} +3 -3
  20. data/examples/{transaction/boleto_transaction_request.rb → direct/create_transaction_using_boleto.rb} +1 -1
  21. data/examples/{transaction/credit_card_transaction_request.rb → direct/create_transaction_using_credit_card.rb} +1 -1
  22. data/examples/{transaction/online_debit_transaction.rb → direct/create_transaction_using_online_debit.rb} +1 -1
  23. data/examples/{installment.rb → get_installments.rb} +3 -3
  24. data/examples/{transaction/transaction_cancellation.rb → request_transaction_cancellation.rb} +2 -2
  25. data/examples/{authorization/authorization.rb → split_payment/create_authorization.rb} +5 -4
  26. data/examples/split_payment/create_payment_request.rb +71 -0
  27. data/lib/pagseguro.rb +14 -8
  28. data/lib/pagseguro/account.rb +29 -0
  29. data/lib/pagseguro/authorization.rb +10 -4
  30. data/lib/pagseguro/authorization/collection.rb +3 -1
  31. data/lib/pagseguro/authorization/request_serializer.rb +6 -6
  32. data/lib/pagseguro/authorization/response.rb +2 -2
  33. data/lib/pagseguro/authorization_request.rb +16 -4
  34. data/lib/pagseguro/authorization_request/request_serializer.rb +127 -15
  35. data/lib/pagseguro/authorization_request/response.rb +2 -2
  36. data/lib/pagseguro/company.rb +41 -0
  37. data/lib/pagseguro/document.rb +4 -0
  38. data/lib/pagseguro/documents.rb +5 -0
  39. data/lib/pagseguro/errors.rb +27 -5
  40. data/lib/pagseguro/extensions/collection_object.rb +34 -0
  41. data/lib/pagseguro/item.rb +4 -0
  42. data/lib/pagseguro/items.rb +10 -17
  43. data/lib/pagseguro/partner.rb +20 -0
  44. data/lib/pagseguro/payment_request.rb +30 -2
  45. data/lib/pagseguro/payment_request/{serializer.rb → request_serializer.rb} +86 -1
  46. data/lib/pagseguro/person.rb +40 -0
  47. data/lib/pagseguro/phone.rb +8 -0
  48. data/lib/pagseguro/phones.rb +5 -0
  49. data/lib/pagseguro/receiver.rb +17 -0
  50. data/lib/pagseguro/receiver_split.rb +15 -0
  51. data/lib/pagseguro/request.rb +25 -1
  52. data/lib/pagseguro/transaction.rb +7 -5
  53. data/lib/pagseguro/transaction_cancellation.rb +3 -6
  54. data/lib/pagseguro/transaction_cancellation/response.rb +7 -6
  55. data/lib/pagseguro/{refund.rb → transaction_refund.rb} +2 -2
  56. data/lib/pagseguro/{refund → transaction_refund}/request_serializer.rb +1 -1
  57. data/lib/pagseguro/{refund → transaction_refund}/response.rb +9 -9
  58. data/lib/pagseguro/{refund → transaction_refund}/response_serializer.rb +1 -1
  59. data/lib/pagseguro/transaction_request.rb +1 -4
  60. data/lib/pagseguro/transaction_request/response.rb +8 -7
  61. data/lib/pagseguro/version.rb +1 -1
  62. data/locales/pt-BR.yml +1 -0
  63. data/pagseguro-oficial.gemspec +13 -13
  64. data/spec/fixtures/authorization/search_authorization.xml +47 -0
  65. data/spec/fixtures/authorization_request/authorization_request.xml +11 -0
  66. data/spec/fixtures/authorization_request/authorization_request_with_account.xml +47 -0
  67. data/spec/pagseguro/account_spec.rb +27 -0
  68. data/spec/pagseguro/authorization/collection_spec.rb +3 -3
  69. data/spec/pagseguro/authorization/request_serializer_spec.rb +1 -2
  70. data/spec/pagseguro/authorization/response_spec.rb +70 -15
  71. data/spec/pagseguro/authorization_request/request_serializer_spec.rb +314 -14
  72. data/spec/pagseguro/authorization_request_spec.rb +96 -31
  73. data/spec/pagseguro/authorization_spec.rb +52 -10
  74. data/spec/pagseguro/company_spec.rb +12 -0
  75. data/spec/pagseguro/documents_spec.rb +37 -0
  76. data/spec/pagseguro/errors_spec.rb +45 -6
  77. data/spec/pagseguro/extensions/collection_object_spec.rb +77 -0
  78. data/spec/pagseguro/features/create_transaction_request_spec.rb +2 -2
  79. data/spec/pagseguro/installment/response_spec.rb +5 -3
  80. data/spec/pagseguro/installment_spec.rb +5 -2
  81. data/spec/pagseguro/items_spec.rb +104 -12
  82. data/spec/pagseguro/pagseguro_spec.rb +8 -8
  83. data/spec/pagseguro/partner_spec.rb +8 -0
  84. data/spec/pagseguro/payment_request/request_serializer_spec.rb +251 -0
  85. data/spec/pagseguro/payment_request_spec.rb +67 -11
  86. data/spec/pagseguro/person_spec.rb +10 -0
  87. data/spec/pagseguro/phones_spec.rb +38 -0
  88. data/spec/pagseguro/receiver_spec.rb +6 -0
  89. data/spec/pagseguro/receiver_split_spec.rb +7 -0
  90. data/spec/pagseguro/request_spec.rb +27 -4
  91. data/spec/pagseguro/session/response_spec.rb +6 -2
  92. data/spec/pagseguro/session_spec.rb +5 -2
  93. data/spec/pagseguro/transaction/response_spec.rb +14 -8
  94. data/spec/pagseguro/transaction/search_spec.rb +15 -8
  95. data/spec/pagseguro/transaction_cancellation/response_spec.rb +6 -3
  96. data/spec/pagseguro/transaction_cancellation_spec.rb +7 -5
  97. data/spec/pagseguro/{refund → transaction_refund}/request_serializer_spec.rb +3 -3
  98. data/spec/pagseguro/{refund → transaction_refund}/response_serializer_spec.rb +1 -1
  99. data/spec/pagseguro/{refund → transaction_refund}/response_spec.rb +3 -3
  100. data/spec/pagseguro/{refund_spec.rb → transaction_refund_spec.rb} +15 -7
  101. data/spec/pagseguro/transaction_request/request_serializer_spec.rb +9 -9
  102. data/spec/pagseguro/transaction_request/response_spec.rb +5 -2
  103. data/spec/pagseguro/transaction_request_spec.rb +14 -9
  104. data/spec/pagseguro/transaction_spec.rb +37 -30
  105. data/spec/spec_helper.rb +0 -3
  106. data/spec/support/ensure_type_macro.rb +20 -2
  107. data/{examples/refund.rb → transaction/transaction_refund.rb} +1 -1
  108. metadata +112 -96
  109. data/examples/transaction/invalid_transaction_by_notification_code.rb +0 -22
  110. data/lib/pagseguro/refund/serializer.rb +0 -24
  111. data/lib/pagseguro/transaction_cancellation/serializer.rb +0 -18
  112. data/spec/pagseguro/payment_request/serializer_spec.rb +0 -166
  113. data/spec/pagseguro/refund/serializer_spec.rb +0 -15
  114. data/spec/pagseguro/transaction_cancellation/serializer_spec.rb +0 -13
@@ -6,7 +6,7 @@ module PagSeguro
6
6
  end
7
7
 
8
8
  def serialize
9
- if response.success? && response.xml?
9
+ if success?
10
10
  xml = Nokogiri::XML(response.body).css('authorizationRequest').first
11
11
  ResponseSerializer.new(xml).serialize
12
12
  else
@@ -15,7 +15,7 @@ module PagSeguro
15
15
  end
16
16
 
17
17
  def success?
18
- (response.success? && response.xml?) ? true : false
18
+ response.success? && response.xml?
19
19
  end
20
20
 
21
21
  private
@@ -0,0 +1,41 @@
1
+ module PagSeguro
2
+ class Company
3
+ include Extensions::MassAssignment
4
+ include Extensions::EnsureType
5
+
6
+ attr_accessor :name
7
+ attr_accessor :display_name
8
+ attr_accessor :website_url
9
+
10
+ attr_reader :address
11
+ attr_reader :partner
12
+
13
+ def address=(address)
14
+ @address = ensure_type(Address, address)
15
+ end
16
+
17
+ def partner=(partner)
18
+ @partner = ensure_type(Partner, partner)
19
+ end
20
+
21
+ def phones
22
+ @phones ||= Phones.new
23
+ end
24
+
25
+ def phones=(_phones=[])
26
+ _phones.each do |phone|
27
+ phones << ensure_type(Phone, phone)
28
+ end
29
+ end
30
+
31
+ def documents
32
+ @documents ||= Documents.new
33
+ end
34
+
35
+ def documents=(_documents=[])
36
+ _documents.each do |document|
37
+ documents << ensure_type(Document, document)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -7,5 +7,9 @@ module PagSeguro
7
7
 
8
8
  # Set the value.
9
9
  attr_accessor :value
10
+
11
+ def ==(other)
12
+ [type, value] == [other.type, other.value]
13
+ end
10
14
  end
11
15
  end
@@ -0,0 +1,5 @@
1
+ module PagSeguro
2
+ class Documents
3
+ include Extensions::CollectionObject
4
+ end
5
+ end
@@ -20,16 +20,38 @@ module PagSeguro
20
20
 
21
21
  private
22
22
  def process(response)
23
- @messages << error_message(:unauthorized, "Unauthorized") if response.unauthorized?
24
- @messages << error_message(:not_found, "Not found") if response.not_found?
23
+ return unless response.error?
24
+ @messages << error_message(
25
+ error_to_sym(response.error),
26
+ error_to_human(response.error)
27
+ )
25
28
 
26
- response.data.css("errors > error").each do |error|
27
- @messages << error_message(error.css("code").text, error.css("message").text)
28
- end if response.bad_request?
29
+ if response.error == Aitch::BadRequestError
30
+ response.data.css("errors > error").each do |error|
31
+ @messages << error_message(error.css("code").text, error.css("message").text)
32
+ end
33
+ end
29
34
  end
30
35
 
31
36
  def error_message(code, message)
32
37
  I18n.t(code, scope: "pagseguro.errors", default: message)
33
38
  end
39
+
40
+
41
+ # Error formats
42
+ def error_to_sym(error)
43
+ error.to_s.split(/::/)[-1]
44
+ .gsub(/Error$/, '')
45
+ .gsub(/[[:upper:]]/)
46
+ .with_index {|k, i| i == 0 ? k : ('_' + k)}
47
+ .downcase
48
+ .to_sym
49
+ end
50
+
51
+ def error_to_human(error)
52
+ error_to_sym(error).to_s
53
+ .capitalize
54
+ .gsub('_', ' ')
55
+ end
34
56
  end
35
57
  end
@@ -0,0 +1,34 @@
1
+ module PagSeguro
2
+ module Extensions
3
+ module CollectionObject
4
+ extend Forwardable
5
+ include Enumerable
6
+ include EnsureType
7
+
8
+ def_delegators :@store, :size, :clear, :empty?, :any?, :each, :include?
9
+
10
+ attr_accessor :store
11
+
12
+ def initialize
13
+ @store = []
14
+ end
15
+
16
+ def collection_type
17
+ PagSeguro.const_get(class_name_singularized)
18
+ end
19
+
20
+ # Adds a new object to the collection.
21
+ def <<(param)
22
+ object = ensure_type(collection_type, param)
23
+ @store << object unless include?(object)
24
+ end
25
+
26
+ private
27
+
28
+ def class_name_singularized
29
+ class_name = self.class.to_s.split('::').last
30
+ class_name[0...-1] if class_name.end_with? 's'
31
+ end
32
+ end
33
+ end
34
+ end
@@ -21,6 +21,10 @@ module PagSeguro
21
21
  # Set the shipping cost per unit.
22
22
  attr_accessor :shipping_cost
23
23
 
24
+ def ==(other)
25
+ [id, description, amount] == [other.id, other.description, other.amount]
26
+ end
27
+
24
28
  private
25
29
  def before_initialize
26
30
  self.quantity = 1
@@ -1,30 +1,23 @@
1
1
  module PagSeguro
2
2
  class Items
3
- extend Forwardable
4
- include Enumerable
5
- include Extensions::EnsureType
3
+ include Extensions::CollectionObject
6
4
 
7
- def_delegators :@store, :size, :clear, :empty?, :any?, :each
8
-
9
- def initialize
10
- @store = []
11
- end
12
-
13
- # Adds a new item to item list.
5
+ # Overriding standard method to add new objects
14
6
  def <<(item)
15
7
  item = ensure_type(Item, item)
16
8
 
17
- if include?(item)
18
- item.quantity += 1
9
+ original_item = find_item(item)
10
+
11
+ if original_item
12
+ original_item.quantity += (item.quantity || 1)
19
13
  else
20
- @store << item
14
+ store << item
21
15
  end
22
16
  end
23
17
 
24
- # Verify if the item is already included to item list.
25
- # Returns boolean.
26
- def include?(item)
27
- @store.find {|stored_item| stored_item.id == ensure_type(Item, item).id }
18
+ private
19
+ def find_item(item)
20
+ store.detect {|stored_item| stored_item == item }
28
21
  end
29
22
  end
30
23
  end
@@ -0,0 +1,20 @@
1
+ module PagSeguro
2
+ class Partner
3
+ include Extensions::MassAssignment
4
+
5
+ attr_accessor :name
6
+
7
+ # Pattern: aaaa-MM-dd
8
+ attr_accessor :birth_date
9
+
10
+ def documents
11
+ @documents ||= Documents.new
12
+ end
13
+
14
+ def documents=(_documents=[])
15
+ _documents.each do |document|
16
+ documents << document
17
+ end
18
+ end
19
+ end
20
+ end
@@ -11,6 +11,12 @@ module PagSeguro
11
11
  # Get the payment sender.
12
12
  attr_reader :sender
13
13
 
14
+ # Set and get primary receiver email.
15
+ attr_accessor :primary_receiver
16
+
17
+ # Get the payment receivers.
18
+ attr_reader :receivers
19
+
14
20
  # Get the shipping info.
15
21
  attr_reader :shipping
16
22
 
@@ -65,6 +71,13 @@ module PagSeguro
65
71
  @sender = ensure_type(Sender, sender)
66
72
  end
67
73
 
74
+ # Set the receivers.
75
+ def receivers=(receivers)
76
+ receivers.each do |receiver|
77
+ @receivers << ensure_type(Receiver, receiver)
78
+ end
79
+ end
80
+
68
81
  # Set the shipping info.
69
82
  def shipping=(shipping)
70
83
  @shipping = ensure_type(Shipping, shipping)
@@ -72,14 +85,29 @@ module PagSeguro
72
85
 
73
86
  # Calls the PagSeguro web service and register this request for payment.
74
87
  def register
75
- params = Serializer.new(self).to_params
76
- Response.new Request.post("checkout", api_version, params)
88
+ request = if @receivers.empty?
89
+ Request.post('checkout', api_version, params)
90
+ else
91
+ Request.post_xml('checkouts', api_version, credentials, xml_params)
92
+ end
93
+
94
+ Response.new(request)
77
95
  end
78
96
 
79
97
  private
98
+
99
+ def xml_params
100
+ RequestSerializer.new(self).to_xml_params
101
+ end
102
+
103
+ def params
104
+ RequestSerializer.new(self).to_params
105
+ end
106
+
80
107
  def before_initialize
81
108
  self.extra_params = []
82
109
  self.currency = "BRL"
110
+ @receivers = []
83
111
  end
84
112
 
85
113
  def api_version
@@ -1,6 +1,6 @@
1
1
  module PagSeguro
2
2
  class PaymentRequest
3
- class Serializer
3
+ class RequestSerializer
4
4
  # The payment request that will be serialized.
5
5
  attr_reader :payment_request
6
6
 
@@ -8,6 +8,14 @@ module PagSeguro
8
8
  @payment_request = payment_request
9
9
  end
10
10
 
11
+ def to_xml_params
12
+ xml_builder.to_xml(
13
+ save_with:
14
+ Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS |
15
+ Nokogiri::XML::Node::SaveOptions::FORMAT
16
+ )
17
+ end
18
+
11
19
  def to_params
12
20
  {}.tap do |data|
13
21
  data[:receiverEmail] = PagSeguro.receiver_email
@@ -19,17 +27,33 @@ module PagSeguro
19
27
  data[:abandonURL] = payment_request.abandon_url
20
28
  data[:maxUses] = payment_request.max_uses
21
29
  data[:maxAge] = payment_request.max_age
30
+ data[:credentials] = payment_request.credentials
22
31
  payment_request.items.each_with_index do |item, index|
23
32
  serialize_item(data, item, index.succ)
24
33
  end
25
34
 
26
35
  serialize_sender(data, payment_request.sender)
27
36
  serialize_shipping(data, payment_request.shipping)
37
+ serialize_receivers(data, payment_request.receivers)
28
38
  serialize_extra_params(data, payment_request.extra_params)
29
39
  end.delete_if { |_, value| value.nil? }
30
40
  end
31
41
 
32
42
  private
43
+
44
+ def serialize_receivers(data, receivers)
45
+ receivers.each.with_index(1) do |receiver, idx|
46
+ data["receiver[#{idx}].email"] = receiver.email
47
+ serialize_receiver_split(data, receiver, idx, receiver.split)
48
+ end
49
+ end
50
+
51
+ def serialize_receiver_split(data, receiver, idx, split)
52
+ data["receiver[#{idx}].split.amount"] = receiver.split.amount
53
+ data["receiver[#{idx}].split.feePercent"] = receiver.split.fee_percent
54
+ data["receiver[#{idx}].split.ratePercent"] = receiver.split.rate_percent
55
+ end
56
+
33
57
  def serialize_item(data, item, index)
34
58
  data["itemId#{index}"] = item.id
35
59
  data["itemDescription#{index}"] = item.description
@@ -89,6 +113,67 @@ module PagSeguro
89
113
  def to_amount(amount)
90
114
  "%.2f" % BigDecimal(amount.to_s).round(2).to_s("F") if amount
91
115
  end
116
+
117
+ def xml_builder
118
+ Nokogiri::XML::Builder.new(encoding: PagSeguro.encoding) do |xml|
119
+ xml.send(:checkout) {
120
+ xml_serialize_receivers(xml)
121
+ xml_serialize_sender(xml, payment_request.sender)
122
+ xml.send(:currency, payment_request.currency)
123
+ xml.send(:reference, payment_request.reference)
124
+ xml.send(:redirectURL, payment_request.redirect_url)
125
+ xml.send(:notificationURL, payment_request.notification_url)
126
+ xml_serialize_items(xml, payment_request.items)
127
+ }
128
+ end
129
+ end
130
+
131
+ def xml_serialize_items(xml, items)
132
+ xml.send(:items) {
133
+ items.each do |item|
134
+ xml.send(:item) {
135
+ xml.send(:id, item.id)
136
+ xml.send(:description, item.description)
137
+ xml.send(:quantity, item.quantity)
138
+ xml.send(:amount, to_amount(item.amount))
139
+ xml.send(:weight, item.weight) if item.weight
140
+ xml.send(:shippingCost, to_amount(item.shipping_cost))
141
+ }
142
+ end
143
+ }
144
+ end
145
+
146
+ def xml_serialize_sender(xml, sender)
147
+ return unless sender
148
+
149
+ xml.send(:sender) {
150
+ xml.send(:name, sender.name)
151
+ xml.send(:email, sender.email)
152
+ if sender.phone
153
+ xml.send(:phone) {
154
+ xml.send(:areaCode, sender.phone.area_code)
155
+ xml.send(:number, sender.phone.number)
156
+ }
157
+ end
158
+ }
159
+ end
160
+
161
+ def xml_serialize_receivers(xml)
162
+ xml.send(:primaryReceiver) {
163
+ xml.send(:email, payment_request.primary_receiver)
164
+ }
165
+
166
+ xml.send(:receivers) {
167
+ payment_request.receivers.each do |receiver|
168
+ xml.send(:receiver) {
169
+ xml.send(:email, receiver.email)
170
+ xml.send(:split) {
171
+ xml.send(:amount, receiver.split.amount)
172
+ }
173
+ }
174
+ end
175
+ }
176
+ end
92
177
  end
93
178
  end
94
179
  end
@@ -0,0 +1,40 @@
1
+ module PagSeguro
2
+ class Person
3
+ include Extensions::MassAssignment
4
+ include Extensions::EnsureType
5
+
6
+ # Set the account name.
7
+ attr_accessor :name
8
+
9
+ # Set the account birth_date.
10
+ # Pattern: aaaa-MM-dd
11
+ attr_accessor :birth_date
12
+
13
+ # Set the account address.
14
+ attr_reader :address
15
+
16
+ def address=(address)
17
+ @address = ensure_type(Address, address)
18
+ end
19
+
20
+ def documents
21
+ @documents ||= Documents.new
22
+ end
23
+
24
+ def documents=(_documents=[])
25
+ _documents.each do |document|
26
+ documents << ensure_type(Document, document)
27
+ end
28
+ end
29
+
30
+ def phones
31
+ @phones ||= Phones.new
32
+ end
33
+
34
+ def phones=(_phones=[])
35
+ _phones.each do |phone|
36
+ phones << ensure_type(Phone, phone)
37
+ end
38
+ end
39
+ end
40
+ end