pagarme 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +3 -2
  4. data/README.md +285 -4
  5. data/Rakefile +16 -19
  6. data/lib/pagarme.rb +17 -32
  7. data/lib/pagarme/core_ext.rb +9 -0
  8. data/lib/pagarme/errors.rb +39 -25
  9. data/lib/pagarme/model.rb +40 -45
  10. data/lib/pagarme/nested_model.rb +56 -0
  11. data/lib/pagarme/object.rb +133 -92
  12. data/lib/pagarme/request.rb +96 -46
  13. data/lib/pagarme/resources/address.rb +4 -0
  14. data/lib/pagarme/resources/antifraud_analysis.rb +4 -0
  15. data/lib/pagarme/resources/balance.rb +24 -0
  16. data/lib/pagarme/resources/balance_operation.rb +37 -0
  17. data/lib/pagarme/resources/bank_account.rb +4 -0
  18. data/lib/pagarme/resources/bulk_anticipation.rb +65 -0
  19. data/lib/pagarme/resources/card.rb +4 -0
  20. data/lib/pagarme/resources/company.rb +28 -0
  21. data/lib/pagarme/{customer.rb → resources/customer.rb} +0 -0
  22. data/lib/pagarme/resources/payable.rb +11 -0
  23. data/lib/pagarme/resources/phone.rb +4 -0
  24. data/lib/pagarme/{plan.rb → resources/plan.rb} +0 -2
  25. data/lib/pagarme/resources/postal_code.rb +15 -0
  26. data/lib/pagarme/resources/postback.rb +15 -0
  27. data/lib/pagarme/resources/recipient.rb +32 -0
  28. data/lib/pagarme/resources/split_rule.rb +4 -0
  29. data/lib/pagarme/resources/subscription.rb +43 -0
  30. data/lib/pagarme/resources/transaction.rb +41 -0
  31. data/lib/pagarme/{transfer.rb → resources/transfer.rb} +0 -3
  32. data/lib/pagarme/{phone.rb → resources/user.rb} +1 -1
  33. data/lib/pagarme/transaction_common.rb +39 -42
  34. data/lib/pagarme/version.rb +3 -0
  35. data/pagarme.gemspec +16 -13
  36. data/test/assertions.rb +195 -0
  37. data/test/fixtures.rb +222 -0
  38. data/test/pagarme/error_test.rb +13 -0
  39. data/test/pagarme/object_test.rb +19 -0
  40. data/test/pagarme/pagarme_test.rb +10 -0
  41. data/test/pagarme/resources/balance_test.rb +46 -0
  42. data/test/pagarme/resources/bank_account_test.rb +35 -0
  43. data/test/pagarme/resources/bulk_anticipation_test.rb +72 -0
  44. data/test/pagarme/resources/card_test.rb +22 -0
  45. data/test/pagarme/resources/payable_test.rb +30 -0
  46. data/test/pagarme/resources/plan_test.rb +57 -0
  47. data/test/pagarme/resources/postback_test.rb +18 -0
  48. data/test/pagarme/resources/recipient_test.rb +41 -0
  49. data/test/pagarme/resources/subscription_test.rb +100 -0
  50. data/test/pagarme/resources/transaction_test.rb +221 -0
  51. data/test/pagarme/resources/transfer_test.rb +23 -0
  52. data/test/pagarme/resources/zipcode_test.rb +15 -0
  53. data/test/test_helper.rb +52 -175
  54. metadata +102 -30
  55. data/lib/pagarme/address.rb +0 -4
  56. data/lib/pagarme/bank_account.rb +0 -9
  57. data/lib/pagarme/card.rb +0 -7
  58. data/lib/pagarme/subscription.rb +0 -43
  59. data/lib/pagarme/transaction.rb +0 -33
  60. data/lib/pagarme/util.rb +0 -78
  61. data/pagarme.rb +0 -12
  62. data/test/pagarme/bank_account.rb +0 -46
  63. data/test/pagarme/card.rb +0 -26
  64. data/test/pagarme/object.rb +0 -29
  65. data/test/pagarme/pagarme.rb +0 -10
  66. data/test/pagarme/plan.rb +0 -70
  67. data/test/pagarme/subscription.rb +0 -123
  68. data/test/pagarme/transaction.rb +0 -215
  69. data/test/pagarme/transfer.rb +0 -42
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e0e40625da7745cbbe9dde088d8b65940c1488a
4
- data.tar.gz: 09f46eb1f7a1111cb54c26d3fb4d1e0b7bb8bdd9
3
+ metadata.gz: 422b2f052212e6a80ab083f50e469ff2c42f7965
4
+ data.tar.gz: 0991a571bbf8c149abb4c7612d9f6f10174e8189
5
5
  SHA512:
6
- metadata.gz: 33084c34f78b9b799907d164c149fe7712af76daa7ff74423b97d0189bab0d6c34be295a8a72faa0b3e4d4a97013cb1ded00bff96f09e24536dfc38737fdc866
7
- data.tar.gz: 938a986ef3c6c3e5f62180e6bf0d207b3dc25db52b65ef4071a2d4ba48a3368b999e46249fb24ff621365f8fc41845466fc9a106e448772c50c40f8ced83155e
6
+ metadata.gz: b28c35d0ade1879e5ecd4665c16b0ba33625091630a81a8f014acb3f2122612d795420a3a265a58f64f353f5396e12905bd048234484699c81d7c339097f9fc1
7
+ data.tar.gz: be91ee795aeb90394646f378dbd82c8183c0d52e48c22f4197ff38c73b461efe56b58dcf71016857a726e969ed3015bd6a0b8c1de6be61c4c37d636fe714b44b
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ Gemfile.lock
19
19
  .yardoc
20
20
  _yardoc
21
21
  doc/
22
+ test/vcr_cassettes
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - "1.9.3"
4
3
  - "2.0.0"
5
- - "2.1.2"
4
+ - "2.1.8"
5
+ - "2.2.4"
6
+ - "2.3.0"
data/README.md CHANGED
@@ -1,14 +1,295 @@
1
- ## pagarme-ruby
1
+ # pagarme-ruby
2
+ [![Build Status](https://travis-ci.org/pagarme/pagarme-ruby.png)](https://travis-ci.org/pagarme/pagarme-ruby)
2
3
 
3
4
  Pagar.me Ruby library
4
5
 
5
6
  ## Documentation
6
7
 
7
- * [API Guide](http://pagar.me/docs)
8
+ * [Documentation](https://pagar.me/docs)
9
+ * [Full API Guide](https://docs.pagar.me/api)
8
10
 
9
- ## Code Status
11
+ ## Getting Started
10
12
 
11
- [![Build Status](https://travis-ci.org/pagarme/pagarme-ruby.png)](https://travis-ci.org/pagarme/pagarme-ruby)
13
+ ### Install
14
+
15
+ ```shell
16
+ gem install pagarme
17
+ ```
18
+ or add the following line to Gemfile:
19
+
20
+ ```ruby
21
+ gem 'pagarme'
22
+ ```
23
+ and run `bundle install` from your shell.
24
+
25
+ ### Configure your API key
26
+
27
+ You can set your API key in Ruby:
28
+
29
+ ```ruby
30
+ PagarMe.api_key = 'YOUR_API_KEY_HERE'
31
+ ```
32
+
33
+ or set the environment variable _PAGARME\_API\_KEY_ (**recommended**)
34
+
35
+ ### Using Pagar.me Checkout
36
+
37
+ See our [demo checkout](https://pagar.me/checkout).
38
+
39
+ More about how to use it [here](https://docs.pagar.me/checkout).
40
+
41
+ ### Transactions
42
+
43
+ #### Creating a Credit Card Transaction
44
+
45
+ To create a credit card transaction, you need a [card\_hash](https://docs.pagar.me/capturing-card-data).
46
+
47
+ ```ruby
48
+ PagarMe::Transaction.new(
49
+ amount: 1000, # in cents
50
+ card_hash: card_hash # how to get a card hash: docs.pagar.me/capturing-card-data
51
+ ).charge
52
+ ```
53
+
54
+ More about [Creating a Credit Card Transaction](https://docs.pagar.me/transactions/#realizando-uma-transacao-de-cartao-de-credito).
55
+
56
+ #### Creating a Boleto Transaction
57
+
58
+ ```ruby
59
+ transaction = PagarMe::Transaction.new(
60
+ amount: 1000, # in cents
61
+ payment_method: 'boleto'
62
+ )
63
+ transaction.charge
64
+
65
+ transaction.boleto_url # => boleto's URL
66
+ transaction.boleto_barcode # => boleto's barcode
67
+ ```
68
+
69
+ More about [Creating a Boleto Transaction](https://docs.pagar.me/transactions/#realizando-uma-transacao-de-boleto-bancario).
70
+
71
+ #### Split Rules
72
+
73
+ With split rules, received amount could be splitted between more than one recipient.
74
+ For example, splitting equally a transaction:
75
+
76
+ ```ruby
77
+ PagarMe::Transaction.new(
78
+ amount: 1000, # in cents
79
+ card_hash: card_hash, # how to get a card hash: docs.pagar.me/capturing-card-data
80
+ split\_rules: [
81
+ { recipient_id: recipient_id_1, percentage: 50 },
82
+ { recipient_id: recipient_id_2, percentage: 50 }
83
+ ]
84
+ ).charge
85
+ ```
86
+
87
+ More about [Split Rules](https://docs.pagar.me/api/#regras-do-split).
88
+
89
+ ### Plans & Subscriptions
90
+
91
+ You can use recurring charges, learn more [here](https://docs.pagar.me/plans-subscriptions).
92
+
93
+ It's important to understand the charges flow, learn more [here](https://docs.pagar.me/plans-subscriptions/#fluxo-de-cobranca)
94
+
95
+ #### Creating a Plan
96
+
97
+ ```ruby
98
+ PagarMe::Plan.new(
99
+ amount: 4990,
100
+ days: 30,
101
+ name: 'Gold Plan'
102
+ ).create
103
+ ```
104
+
105
+ More about [Creating a Plan](https://docs.pagar.me/plans-subscriptions/#criando-um-plano).
106
+
107
+ #### Creating a Subscription
108
+
109
+ ```ruby
110
+ PagarMe::Subscription.new(
111
+ plan: PagarMe::Plan.find_by_id('1234'),
112
+ card_hash: card_hash,
113
+ customer: { email: 'customer_email@pagar.me' }
114
+ ).create
115
+ ```
116
+
117
+ More about [Creating a Subscription](https://docs.pagar.me/plans-subscriptions/#criando-uma-assinatura).
118
+
119
+ ### Recipients
120
+
121
+ #### Creating a Recipient
122
+
123
+ To create a recipient, so it can receive payments through split rules or transfers:
124
+
125
+ ```ruby
126
+ PagarMe::Recipient.create(
127
+ bank_account: {
128
+ bank_code: '237',
129
+ agencia: '1935',
130
+ agencia_dv: '9',
131
+ conta: '23398',
132
+ conta_dv: '9',
133
+ legal_name: 'Fulano da Silva',
134
+ document_number: '00000000000000' # CPF or CNPJ
135
+ },
136
+ transfer_enabled: false
137
+ )
138
+ ```
139
+
140
+ More about [Creating a Recipient](https://docs.pagar.me/api/#recebedores).
141
+
142
+ #### Transfer Available Amout to Bank Account Manually
143
+
144
+ This is only needed if _transfer\_enabled_ is set to false. If set to true,
145
+ _transfer\_interval_ and _transfer\_day_ will handle it automatically.
146
+
147
+ ```ruby
148
+ PagarMe::Recipient.find(recipient_id).receive amount
149
+ ```
150
+
151
+ ### Balance And Balance Operations
152
+
153
+ #### Checking Balance
154
+
155
+ ```ruby
156
+ balance = PagarMe::Balance.balance
157
+ balance.waiting_funds.amount # money to be received in your account
158
+ balance.available.amount # in your pagarme account
159
+ balance.transferred.amount # transferred to your bank account
160
+ ```
161
+
162
+ Just that!
163
+
164
+ More about [Balance](https://docs.pagar.me/api/#saldo)
165
+
166
+ #### Checking Balance Operations
167
+
168
+ To access the history of balance operations:
169
+
170
+ ```ruby
171
+ PagarMe::BalanceOperation.balance_operations
172
+ ```
173
+
174
+ Paginating:
175
+
176
+ ```ruby
177
+ PagarMe::BalanceOperation.balance_operations 2, 50 # second page, 50 per page
178
+ ```
179
+
180
+ More about [Balance Operations](https://docs.pagar.me/api/#operacoes-de-saldo)
181
+
182
+ #### Checking Recipient Balance
183
+
184
+ ```ruby
185
+ balance = PagarMe::Recipient.find(recipient_id).balance
186
+ balance.waiting_funds.amount # money to be received in his account
187
+ balance.available.amount # in his pagarme account
188
+ balance.transferred.amount # transferred to his bank account
189
+ ```
190
+
191
+ Just that!
192
+
193
+ More about [Recipient Balance](https://docs.pagar.me/api/#saldo-de-um-recebedor)
194
+
195
+ #### Checking Recipient Balance Operations
196
+
197
+ To access the history of balance operations:
198
+
199
+ ```ruby
200
+ PagarMe::Recipient.find(recipient_id).balance_operations
201
+ ```
202
+
203
+ Paginating:
204
+
205
+ ```ruby
206
+ PagarMe::Recipient.find(recipient_id).balance_operations 2, 50 # second page, 50 per page
207
+ ```
208
+
209
+ More about [Recipient Balance Operations](https://docs.pagar.me/api/#operacoes-de-saldo-de-um-recebedor)
210
+
211
+ ### Request Bulk Anticipation
212
+
213
+ #### Checking limits
214
+
215
+ ```ruby
216
+ PagarMe::Recipient.default.bulk_anticipations_limits
217
+ ```
218
+
219
+ More about [Checking Bulk Anticipation Limits](https://docs.pagar.me/api/#obtendo-os-limites-de-antecipacao)
220
+
221
+ #### Requesting Bulk Anticipation
222
+
223
+ ```ruby
224
+ PagarMe::Recipient.default.bulk_anticipate(
225
+ timeframe: :start,
226
+ payment_date: Date.new(2016, 12, 25),
227
+ requested_amount: 10000 # in cents
228
+ )
229
+ ```
230
+
231
+ More about [Requesting Bulk Anticipation](https://docs.pagar.me/api/#criando-uma-antecipacao)
232
+
233
+ #### Getting Bulk Anticipation
234
+
235
+ ```ruby
236
+ PagarMe::BulkAnticipation.all page, count
237
+ ```
238
+
239
+ More about [Getting Bulk Anticipation](https://docs.pagar.me/api/#retornando-todas-as-antecipacoes)
240
+
241
+ ### Payables
242
+
243
+ ### Getting Payable
244
+
245
+ ```ruby
246
+ PagarMe::Payable.find 'payable_id'
247
+ ```
248
+
249
+ More about [Getting Payable](https://docs.pagar.me/api/#retornando-um-recebivel)
250
+
251
+ #### Querying Payables
252
+
253
+ ```ruby
254
+ PagarMe::Payable.all page, count
255
+ ```
256
+
257
+ ```ruby
258
+ PagarMe::Payable.find_by status: 'paid'
259
+ ```
260
+
261
+ More about [Querying Payables](https://docs.pagar.me/api/#retornando-recebiveis)
262
+
263
+ #### Querying Payables by Transaction
264
+
265
+ ```ruby
266
+ transaction = PagarMe::Transaction.find 'transaction_id'
267
+ transaction.payables
268
+ ```
269
+
270
+ More about [Payable Transactions](https://docs.pagar.me/api/#retornando-pagamentos-da-transacao)
271
+
272
+ ### Undocumented Features
273
+
274
+ This gem is stable, but in constant development.
275
+
276
+ This README is just a quick abstract of it's main features.
277
+
278
+ You can easily browse it's source code to see all [supported resources](https://github.com/pagarme/pagarme-ruby/tree/master/lib/pagarme/resources).
279
+
280
+ We will document everything while adding support to all resources listed in
281
+ [Full API Guide](https://docs.pagar.me/api).
282
+
283
+ Feel free to help us to add support to features sending pull requests.
284
+
285
+ Thanks!
286
+
287
+ ### TODO
288
+
289
+ Add support to [ElasticSearch Query DSL](https://docs.pagar.me/api/#buscas-avancadas),
290
+ so you can search your data optimally.
291
+
292
+ And document all the source code.
12
293
 
13
294
  ## License
14
295
 
data/Rakefile CHANGED
@@ -1,24 +1,21 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
2
3
 
3
- task :default => [:all]
4
+ Rake::TestTask.new :test do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/pagarme/**/*_test.rb']
8
+ end
4
9
 
5
- task :test, :test_file do |t, args|
6
- ret = true
7
- test_file = args[:test_file]
10
+ task :default => :test
8
11
 
9
- if args[:test_file]
10
- f = "test/pagarme/#{args[:test_file]}.rb"
11
- ret = ret && ruby(f, '')
12
- else
13
- Dir["test/**/*.rb"].each do |f|
14
- ret = ret && ruby(f, '')
15
- end
16
- end
12
+ task :clean_vcr do
13
+ sh 'rm -rf test/vcr_cassettes'
17
14
  end
18
15
 
19
- task :all do
20
- Rake::Task["test"].invoke
21
- require 'active_support/all'
22
- Rake::Task["test"].reenable
23
- Rake::Task["test"].invoke
24
- end
16
+ # task :all do
17
+ # Rake::Task['test'].invoke
18
+ # require 'active_support/all'
19
+ # Rake::Task['test'].reenable
20
+ # Rake::Task['test'].invoke
21
+ # end
@@ -1,46 +1,31 @@
1
+ require 'set'
2
+ require 'time'
1
3
  require 'digest/sha1'
4
+
5
+ require_relative 'pagarme/version'
6
+ require_relative 'pagarme/core_ext'
2
7
  require_relative 'pagarme/object'
3
- require_relative 'pagarme/util'
4
8
  require_relative 'pagarme/model'
9
+ require_relative 'pagarme/nested_model'
5
10
  require_relative 'pagarme/transaction_common'
6
- require_relative 'pagarme/customer'
7
- require_relative 'pagarme/phone'
8
- require_relative 'pagarme/address'
9
- require_relative 'pagarme/subscription'
10
- require_relative 'pagarme/transaction'
11
- require_relative 'pagarme/transfer'
12
- require_relative 'pagarme/card'
13
- require_relative 'pagarme/plan'
14
- require_relative 'pagarme/bank_account'
15
11
  require_relative 'pagarme/request'
16
12
  require_relative 'pagarme/errors'
17
13
 
18
- module PagarMe
19
- @@api_key = nil
20
- @@api_endpoint = 'https://api.pagar.me/1'
21
- @@live = true
22
-
23
- def self.api_endpoint=(api_endpoint)
24
- @@api_endpoint = api_endpoint
25
- end
26
-
27
- def self.api_endpoint
28
- @@api_endpoint
29
- end
30
-
31
- def self.api_key=(api_key)
32
- @@api_key = api_key
33
- end
14
+ Dir[File.expand_path('../pagarme/resources/*.rb', __FILE__)].map do |path|
15
+ require path
16
+ end
34
17
 
35
- def self.api_key
36
- @@api_key
18
+ module PagarMe
19
+ class << self
20
+ attr_accessor :api_endpoint, :open_timeout, :timeout, :api_key
37
21
  end
38
22
 
39
- def self.full_api_url(relative_path)
40
- "#{@@api_endpoint}#{relative_path}"
41
- end
23
+ self.api_endpoint = 'https://api.pagar.me/1'
24
+ self.open_timeout = 30
25
+ self.timeout = 90
26
+ self.api_key = ENV['PAGARME_API_KEY']
42
27
 
43
28
  def self.validate_fingerprint(id, fingerprint)
44
- Digest::SHA1.hexdigest(id.to_s + "#" + @@api_key) == fingerprint
29
+ PagarMe::Postback.validate id, fingerprint
45
30
  end
46
31
  end
@@ -0,0 +1,9 @@
1
+ class Object
2
+ def blank?
3
+ respond_to?(:empty?) ? !!empty? : !self
4
+ end
5
+
6
+ def present?
7
+ !blank?
8
+ end
9
+ end
@@ -1,41 +1,55 @@
1
1
  module PagarMe
2
2
  class PagarMeError < StandardError
3
- attr_accessor :message
4
- attr_accessor :parameter_name
5
- attr_accessor :type
6
- attr_accessor :url
7
- attr_accessor :errors
8
-
9
- def initialize(message = "", parameter_name = "", type = "", url = "")
10
- self.message = message
11
- self.type = type
12
- self.parameter_name = parameter_name
13
- self.errors = []
3
+ end
4
+
5
+ class ConnectionError < PagarMeError
6
+ attr_reader :error
7
+
8
+ def initialize(error)
9
+ @error = error
10
+ super error.message
14
11
  end
12
+ end
15
13
 
16
- def self.fromServerResponse(response = {})
17
- object = self.new
14
+ class RequestError < PagarMeError
15
+ end
18
16
 
19
- response['errors'].map do |error|
20
- object.message += error['message'] + ', '
21
- object.errors << PagarMeError.new(error['message'], error['parameter_name'], error['type'], response['url'])
22
- end
17
+ class ResponseError < PagarMeError
18
+ attr_reader :request_params, :error
23
19
 
24
- object
20
+ def initialize(request_params, error)
21
+ @request_params, @error = request_params, error
22
+ super @error.message
25
23
  end
24
+ end
26
25
 
27
- def to_s
28
- "#{self.class.to_s} - #{message}"
26
+ class NotFound < ResponseError
27
+ attr_reader :response
28
+ def initialize(response, request_params, error)
29
+ @response = response
30
+ super request_params, error
29
31
  end
30
32
  end
31
33
 
32
- class RequestError < PagarMeError
33
- end
34
+ class ValidationError < PagarMeError
35
+ attr_reader :response, :errors
34
36
 
35
- class ConnectionError < PagarMeError
37
+ def initialize(response)
38
+ @response = response
39
+ @errors = response['errors'].map do |error|
40
+ params = error.values_at('message', 'parameter_name', 'type', 'url')
41
+ ParamError.new *params
42
+ end
43
+ super @errors.map(&:message).join(', ')
44
+ end
36
45
  end
37
46
 
38
- class ResponseError < PagarMeError
47
+ class ParamError < PagarMeError
48
+ attr_reader :parameter_name, :type, :url
49
+
50
+ def initialize(message, parameter_name, type, url)
51
+ @parameter_name, @type, @url = parameter_name, type, url
52
+ super message
53
+ end
39
54
  end
40
55
  end
41
-