pagseguro-oficial 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +7 -5
  3. data/CHANGELOG.md +0 -1
  4. data/README.md +6 -1
  5. data/docs/transparent_checkout.md +201 -0
  6. data/examples/boleto_transaction_request.rb +94 -0
  7. data/examples/create_session.rb +13 -0
  8. data/examples/credit_card_transaction_request.rb +122 -0
  9. data/examples/installment.rb +20 -9
  10. data/examples/online_debit_transaction.rb +98 -0
  11. data/examples/transaction_by_code.rb +62 -0
  12. data/examples/transaction_by_notification_code.rb +55 -51
  13. data/lib/pagseguro.rb +17 -1
  14. data/lib/pagseguro/bank.rb +8 -0
  15. data/lib/pagseguro/boleto_transaction_request.rb +8 -0
  16. data/lib/pagseguro/credit_card_transaction_request.rb +39 -0
  17. data/lib/pagseguro/document.rb +2 -2
  18. data/lib/pagseguro/errors.rb +11 -5
  19. data/lib/pagseguro/holder.rb +28 -0
  20. data/lib/pagseguro/installment.rb +11 -16
  21. data/lib/pagseguro/installment/collection.rb +25 -0
  22. data/lib/pagseguro/installment/request_serializer.rb +24 -0
  23. data/lib/pagseguro/installment/response.rb +29 -4
  24. data/lib/pagseguro/installment/{serializer.rb → response_serializer.rb} +1 -1
  25. data/lib/pagseguro/items.rb +3 -0
  26. data/lib/pagseguro/online_debit_transaction_request.rb +17 -0
  27. data/lib/pagseguro/payment_method.rb +1 -0
  28. data/lib/pagseguro/payment_releases.rb +3 -0
  29. data/lib/pagseguro/sender.rb +4 -0
  30. data/lib/pagseguro/session.rb +34 -0
  31. data/lib/pagseguro/session/response.rb +32 -0
  32. data/lib/pagseguro/session/response_serializer.rb +18 -0
  33. data/lib/pagseguro/transaction.rb +6 -3
  34. data/lib/pagseguro/transaction/response.rb +1 -1
  35. data/lib/pagseguro/transaction_installment.rb +11 -0
  36. data/lib/pagseguro/transaction_request.rb +143 -0
  37. data/lib/pagseguro/transaction_request/request_serializer.rb +148 -0
  38. data/lib/pagseguro/transaction_request/response.rb +32 -0
  39. data/lib/pagseguro/transaction_request/response_serializer.rb +117 -0
  40. data/lib/pagseguro/version.rb +1 -1
  41. data/spec/fixtures/session/success.xml +4 -0
  42. data/spec/fixtures/transaction_request/success.xml +58 -0
  43. data/spec/fixtures/transactions/search.xml +40 -0
  44. data/spec/pagseguro/bank_spec.rb +5 -0
  45. data/spec/pagseguro/boleto_transaction_request_spec.rb +9 -0
  46. data/spec/pagseguro/credit_card_transaction_request_spec.rb +36 -0
  47. data/spec/pagseguro/document_spec.rb +1 -1
  48. data/spec/pagseguro/errors_spec.rb +5 -2
  49. data/spec/pagseguro/features/create_session_spec.rb +52 -0
  50. data/spec/pagseguro/features/create_transaction_request_spec.rb +58 -0
  51. data/spec/pagseguro/holder_spec.rb +9 -0
  52. data/spec/pagseguro/installment/collection_spec.rb +43 -0
  53. data/spec/pagseguro/installment/request_serializer_spec.rb +16 -0
  54. data/spec/pagseguro/installment/{serializer_spec.rb → response_serializer_spec.rb} +1 -1
  55. data/spec/pagseguro/installment/response_spec.rb +52 -0
  56. data/spec/pagseguro/installment_spec.rb +22 -73
  57. data/spec/pagseguro/online_debit_transaction_request_spec.rb +18 -0
  58. data/spec/pagseguro/payment_method_spec.rb +1 -0
  59. data/spec/pagseguro/payment_request/response_spec.rb +0 -11
  60. data/spec/pagseguro/sender_spec.rb +1 -0
  61. data/spec/pagseguro/session/response_serializer_spec.rb +12 -0
  62. data/spec/pagseguro/session/response_spec.rb +52 -0
  63. data/spec/pagseguro/session_spec.rb +41 -0
  64. data/spec/pagseguro/transaction/search/search_abandoned_spec.rb +27 -0
  65. data/spec/pagseguro/transaction/search/search_by_date_spec.rb +26 -0
  66. data/spec/pagseguro/transaction/search/search_by_reference_spec.rb +26 -0
  67. data/spec/pagseguro/transaction/search_spec.rb +131 -0
  68. data/spec/pagseguro/transaction_installment_spec.rb +6 -0
  69. data/spec/pagseguro/transaction_request/request_serializer_spec.rb +213 -0
  70. data/spec/pagseguro/transaction_request/response_serializer_spec.rb +52 -0
  71. data/spec/pagseguro/transaction_request/response_spec.rb +60 -0
  72. data/spec/pagseguro/transaction_request_spec.rb +125 -0
  73. data/spec/support/helpers.rb +14 -0
  74. metadata +79 -6
@@ -0,0 +1,28 @@
1
+ module PagSeguro
2
+ class Holder
3
+ include Extensions::MassAssignment
4
+ include Extensions::EnsureType
5
+
6
+ # Set the name.
7
+ attr_accessor :name
8
+
9
+ # Set the birth date.
10
+ attr_accessor :birth_date
11
+
12
+ # Get document info.
13
+ attr_reader :document
14
+
15
+ # Get the phone.
16
+ attr_reader :phone
17
+
18
+ # Set the document.
19
+ def document=(document)
20
+ @document = ensure_type(Document, document)
21
+ end
22
+
23
+ # Set the phone.
24
+ def phone=(phone)
25
+ @phone = ensure_type(Phone, phone)
26
+ end
27
+ end
28
+ end
@@ -20,27 +20,22 @@ module PagSeguro
20
20
 
21
21
  # Find installment options by a given amount
22
22
  # Optional. Credit card brand
23
- # Return an Array of PagSeguro::Installment instances
23
+ # Return a PagSeguro::Installment::Collection instance
24
24
  def self.find(amount, card_brand = nil)
25
- string = "installments?amount=#{amount}"
26
- string += "&cardBrand=#{card_brand}" if card_brand
27
- load_from_response Request.get(string, 'v2')
25
+ request = Request.get("installments", api_version, params(amount: amount, card_brand: card_brand))
26
+ collection = Collection.new
27
+ Response.new(request, collection).serialize
28
+
29
+ collection
28
30
  end
29
31
 
30
- # Serialize the HTTP response into data.
31
- def self.load_from_response(response) # :nodoc:
32
- if response.success? and response.xml?
33
- Nokogiri::XML(response.body).css("installments > installment").map do |node|
34
- load_from_xml(node)
35
- end
36
- else
37
- Response.new Errors.new(response)
38
- end
32
+ private
33
+ def self.params(options)
34
+ RequestSerializer.new(options).to_params
39
35
  end
40
36
 
41
- # Serialize the XML object.
42
- def self.load_from_xml(xml) # :nodoc:
43
- new Serializer.new(xml).serialize
37
+ def self.api_version
38
+ 'v2'
44
39
  end
45
40
  end
46
41
  end
@@ -0,0 +1,25 @@
1
+ module PagSeguro
2
+ class Installment
3
+ class Collection
4
+ extend Forwardable
5
+
6
+ def_delegators :@installments, :each, :empty?, :any?
7
+
8
+ def installments=(objects)
9
+ @installments = instantiate_installments(objects)
10
+ end
11
+
12
+ def errors
13
+ @errors ||= Errors.new
14
+ end
15
+
16
+ private
17
+ def instantiate_installments(installments)
18
+ return [] unless installments
19
+ installments.map do |installment|
20
+ Installment.new(installment)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ module PagSeguro
2
+ class Installment
3
+ class RequestSerializer
4
+ # The data that will be serialized.
5
+ attr_reader :data
6
+
7
+ def initialize(data)
8
+ @data = data
9
+ end
10
+
11
+ def to_params
12
+ params[:amount] = data[:amount]
13
+ params[:cardBrand] = data[:card_brand] if data[:card_brand]
14
+
15
+ params
16
+ end
17
+
18
+ private
19
+ def params
20
+ @params ||= {}
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,11 +1,36 @@
1
1
  module PagSeguro
2
2
  class Installment
3
3
  class Response
4
- # Set the response errors.
5
- attr_reader :errors
4
+ def initialize(response, collection)
5
+ @response = response
6
+ @collection = collection
7
+ end
8
+
9
+ def serialize
10
+ if success?
11
+ collection.installments = serialize_installments
12
+ else
13
+ collection.errors.add(response)
14
+ end
15
+
16
+ collection
17
+ end
18
+
19
+ def success?
20
+ response.success? && response.xml?
21
+ end
22
+
23
+ private
24
+ # The request response.
25
+ attr_reader :response
26
+
27
+ # The PagSeguro::Installment::Collection instance.
28
+ attr_reader :collection
6
29
 
7
- def initialize(errors = Errors.new)
8
- @errors = errors
30
+ def serialize_installments
31
+ Nokogiri::XML(response.body).css("installments > installment").map do |node|
32
+ ResponseSerializer.new(node).serialize
33
+ end
9
34
  end
10
35
  end
11
36
  end
@@ -1,6 +1,6 @@
1
1
  module PagSeguro
2
2
  class Installment
3
- class Serializer
3
+ class ResponseSerializer
4
4
  # The installment that will be serialized
5
5
  attr_reader :xml
6
6
 
@@ -10,6 +10,7 @@ module PagSeguro
10
10
  @store = []
11
11
  end
12
12
 
13
+ # Adds a new item to item list.
13
14
  def <<(item)
14
15
  item = ensure_type(Item, item)
15
16
 
@@ -20,6 +21,8 @@ module PagSeguro
20
21
  end
21
22
  end
22
23
 
24
+ # Verify if the item is already included to item list.
25
+ # Returns boolean.
23
26
  def include?(item)
24
27
  @store.find {|stored_item| stored_item.id == ensure_type(Item, item).id }
25
28
  end
@@ -0,0 +1,17 @@
1
+ module PagSeguro
2
+ class OnlineDebitTransactionRequest < TransactionRequest
3
+ # Get the bank info.
4
+ attr_reader :bank
5
+
6
+ # Get the payment_method.
7
+ def payment_method
8
+ "online_debit"
9
+ end
10
+
11
+ # Set the bank.
12
+ # Required for online debit payment method.
13
+ def bank=(bank)
14
+ @bank = ensure_type(Bank, bank)
15
+ end
16
+ end
17
+ end
@@ -4,6 +4,7 @@ module PagSeguro
4
4
  include Extensions::MassAssignment
5
5
 
6
6
  TYPES = {
7
+ "" => :not_set,
7
8
  "1" => :credit_card,
8
9
  "2" => :boleto,
9
10
  "3" => :online_transfer,
@@ -10,12 +10,15 @@ module PagSeguro
10
10
  @payments = []
11
11
  end
12
12
 
13
+ # Adds payment to payment list.
13
14
  def <<(payment)
14
15
  payment = ensure_type(PaymentRelease, payment)
15
16
 
16
17
  @payments << payment unless @payments.include? payment
17
18
  end
18
19
 
20
+ # Verify if a payment is already included to payment list.
21
+ # Returns Boolean.
19
22
  def include?(payment)
20
23
  self.find do |included_payment|
21
24
  included_payment.installment == ensure_type(PaymentRelease, payment).installment
@@ -18,6 +18,10 @@ module PagSeguro
18
18
  # Set the CPF document.
19
19
  attr_accessor :cpf
20
20
 
21
+ # Set sender hash.
22
+ # It's used to identify the sender.
23
+ attr_accessor :hash
24
+
21
25
  # Set the sender phone.
22
26
  def phone=(phone)
23
27
  @phone = ensure_type(Phone, phone)
@@ -0,0 +1,34 @@
1
+ module PagSeguro
2
+ class Session
3
+ include Extensions::MassAssignment
4
+
5
+ # The session id.
6
+ attr_accessor :id
7
+
8
+ # The PageSeguro::Errors object.
9
+ attr_writer :errors
10
+
11
+ def errors
12
+ @errors ||= Errors.new
13
+ end
14
+
15
+ # Create a payment session.
16
+ # Return a PagSeguro::Session instance.
17
+ def self.create
18
+ response = Request.post("sessions", api_version)
19
+ session = Session.new
20
+ response = Response.new(response, session).serialize
21
+
22
+ session
23
+ end
24
+
25
+ def update_attributes(attrs)
26
+ attrs.map { |name, value| send("#{name}=", value) }
27
+ end
28
+
29
+ private
30
+ def self.api_version
31
+ 'v2'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ module PagSeguro
2
+ class Session
3
+ class Response
4
+ def initialize(response, session)
5
+ @response = response
6
+ @session = session
7
+ end
8
+
9
+ def serialize
10
+ if success?
11
+ xml = Nokogiri::XML(response.body).css("session").first
12
+ session.update_attributes(ResponseSerializer.new(xml).serialize)
13
+ else
14
+ session.errors.add(response)
15
+ end
16
+
17
+ session
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 Session instance.
29
+ attr_reader :session
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ module PagSeguro
2
+ class Session
3
+ class ResponseSerializer
4
+ # The session that will be serialized
5
+ attr_reader :xml
6
+
7
+ def initialize(xml)
8
+ @xml = xml
9
+ end
10
+
11
+ def serialize
12
+ {}.tap do |data|
13
+ data[:id] = xml.css("id").text
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -102,8 +102,7 @@ module PagSeguro
102
102
  # # +reference+: the transaction reference code
103
103
  #
104
104
  def self.find_by_reference(reference)
105
- options = { reference: reference }
106
- SearchByReference.new("transactions", options)
105
+ SearchByReference.new("transactions", { reference: reference })
107
106
  end
108
107
 
109
108
  # Get abandoned transactions.
@@ -137,7 +136,7 @@ module PagSeguro
137
136
 
138
137
  # Send a get request to v3 API version, with the path given
139
138
  def self.send_request(path)
140
- Request.get(path, 'v3')
139
+ Request.get(path, api_version)
141
140
  end
142
141
 
143
142
  # Serialize the XML object.
@@ -191,6 +190,10 @@ module PagSeguro
191
190
  end
192
191
 
193
192
  private
193
+ def self.api_version
194
+ 'v3'
195
+ end
196
+
194
197
  def after_initialize
195
198
  @errors = Errors.new
196
199
  end
@@ -1,7 +1,7 @@
1
1
  module PagSeguro
2
2
  class Transaction
3
3
  class Response
4
- # Set the response errors.
4
+ # The response errors.
5
5
  attr_reader :errors
6
6
 
7
7
  def initialize(errors = Errors.new)
@@ -0,0 +1,11 @@
1
+ module PagSeguro
2
+ class TransactionInstallment
3
+ include Extensions::MassAssignment
4
+
5
+ # Set the installments value.
6
+ attr_accessor :value
7
+
8
+ # Set the installments quantity.
9
+ attr_accessor :quantity
10
+ end
11
+ end
@@ -0,0 +1,143 @@
1
+ module PagSeguro
2
+ class TransactionRequest
3
+ include Extensions::MassAssignment
4
+ include Extensions::EnsureType
5
+
6
+ # Set the payment currency.
7
+ # Defaults to BRL.
8
+ attr_accessor :currency
9
+
10
+ # Get the payment sender.
11
+ attr_reader :sender
12
+
13
+ # Get the shipping info.
14
+ attr_reader :shipping
15
+
16
+ # Set the extra amount to be applied to the transaction's total.
17
+ # This value can be used to add an extra charge to the transaction
18
+ # or provide a discount, if negative.
19
+ attr_accessor :extra_amount
20
+
21
+ # Set the reference code.
22
+ # Optional. You can use the reference code to store an identifier so you can
23
+ # associate the PagSeguro transaction to a transaction in your system.
24
+ # Tipically this is the order id.
25
+ attr_accessor :reference
26
+
27
+ # Determines for which url PagSeguro will send the order related
28
+ # notifications codes.
29
+ # Optional. Any change happens in the transaction status, a new notification
30
+ # request will be send to this url. You can use that for update the related
31
+ # order.
32
+ attr_accessor :notification_url
33
+
34
+ # Set the payment mode.
35
+ attr_accessor :payment_mode
36
+
37
+ # The extra parameters for payment request.
38
+ attr_accessor :extra_params
39
+
40
+ # The transaction code returned from api.
41
+ attr_reader :code
42
+
43
+ # The transaction type returned from api.
44
+ attr_reader :type_id
45
+
46
+ # The payment link returned from api.
47
+ attr_reader :payment_link
48
+
49
+ # The transaction status returned from api.
50
+ attr_reader :status
51
+
52
+ # The payment method returned from api.
53
+ attr_reader :payment_method
54
+
55
+ # The gross amount returned from api.
56
+ attr_reader :gross_amount
57
+
58
+ # The discount amount returned from api.
59
+ attr_reader :discount_amount
60
+
61
+ # The net amount returned from api.
62
+ attr_reader :net_amount
63
+
64
+ # The installments number returned from api.
65
+ attr_reader :installment_count
66
+
67
+ # The created at date returned from api
68
+ attr_reader :created_at
69
+
70
+ # The updated at date returned from api
71
+ attr_reader :updated_at
72
+
73
+ attr_writer :errors
74
+
75
+ # Products/items in this payment request.
76
+ def items
77
+ @items ||= Items.new
78
+ end
79
+
80
+ def errors
81
+ @errors ||= Errors.new
82
+ end
83
+
84
+ # Subclasses must implement payment_method
85
+ def payment_method
86
+ raise NotImplementedError.new("'#payment_method' must be implemented in specific class")
87
+ end
88
+
89
+ # Set the payment sender.
90
+ def sender=(sender)
91
+ @sender = ensure_type(Sender, sender)
92
+ end
93
+
94
+ # Set the shipping info.
95
+ def shipping=(shipping)
96
+ @shipping = ensure_type(Shipping, shipping)
97
+ end
98
+
99
+ # Calls the PagSeguro web service and create this request for payment.
100
+ # Return boolean.
101
+ def create
102
+ request = Request.post("transactions", api_version, params)
103
+ response = Response.new(request, self)
104
+ response.serialize
105
+
106
+ response.success?
107
+ end
108
+
109
+ def update_attributes(attrs)
110
+ attrs.map { |name, value| send("#{name}=", value) }
111
+ end
112
+
113
+ private
114
+ attr_writer :code, :type_id, :payment_link, :status, :payment_method,
115
+ :gross_amount, :discount_amount, :net_amount, :installment_count,
116
+ :created_at, :updated_at
117
+
118
+ def before_initialize
119
+ self.currency = "BRL"
120
+ self.extra_params = []
121
+ end
122
+
123
+ def params
124
+ RequestSerializer.new(self).to_params
125
+ end
126
+
127
+ # Used to set response items from api.
128
+ def items=(items)
129
+ @items = Items.new
130
+ items.map { |item| @items << item }
131
+ end
132
+
133
+ # Used to set the payment method from api.
134
+ def payment_method=(payment_method)
135
+ @payment_method = ensure_type(PaymentMethod, payment_method)
136
+ end
137
+
138
+ private
139
+ def api_version
140
+ 'v2'
141
+ end
142
+ end
143
+ end