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
@@ -0,0 +1,13 @@
1
+ require_relative '../test_helper'
2
+
3
+ module PagarMe
4
+ class ErrorTest < Test::Unit::TestCase
5
+ should 'be able to handle single error messages' do
6
+ assert_transaction_errors card_number: nil
7
+ end
8
+
9
+ should 'be able to handle multiple error messages' do
10
+ assert_transaction_errors amount: 10, card_expiration_year: 12
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../test_helper'
2
+
3
+ module PagarMe
4
+ class ObjectTest < Test::Unit::TestCase
5
+ should 'be able to create object and add any attribute' do
6
+ object = PagarMe::PagarMeObject.new attr1: 2
7
+ assert_equal object.attr1, 2
8
+
9
+ object = PagarMe::PagarMeObject.new
10
+ object.attr1 = 2
11
+ assert_equal object.attr1, 2
12
+ end
13
+
14
+ should 'be able to add nested attributes' do
15
+ object = PagarMe::PagarMeObject.new nested: { attrib: 2 }
16
+ assert_equal object.nested.attrib, 2
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ require_relative '../test_helper'
2
+
3
+ module PagarMe
4
+ class PagarMeTest < Test::Unit::TestCase
5
+ should 'validate fingerprint correctly' do
6
+ finderprint = Digest::SHA1.hexdigest "123##{PagarMe.api_key}"
7
+ assert PagarMe.validate_fingerprint(123, finderprint)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,46 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class BalanceTest < Test::Unit::TestCase
5
+
6
+ should 'change amount amount after transaction being paid' do
7
+ transaction = PagarMe::Transaction.charge transaction_with_boleto_params
8
+ previous_balance = PagarMe::Balance.balance
9
+
10
+ transaction.status = :paid
11
+ transaction.save
12
+
13
+ balance = PagarMe::Balance.balance
14
+ assert_increased_available_amount previous_balance, balance
15
+ end
16
+
17
+ should 'change recipient amount after transaction being paid' do
18
+ recipient = PagarMe::Recipient.create recipient_with_nested_bank_account_params
19
+ split_rules = [ { recipient_id: recipient.id, percentage: 100 } ]
20
+ transaction = PagarMe::Transaction.charge transaction_with_boleto_params(split_rules: split_rules)
21
+
22
+ assert_empty_balance recipient.balance
23
+
24
+ transaction.status = :paid
25
+ transaction.save
26
+
27
+ assert_available_balance recipient.balance
28
+ end
29
+
30
+ should 'change recipient amount after recipient receive money' do
31
+ recipient = PagarMe::Recipient.create recipient_with_nested_bank_account_params
32
+ split_rules = [ { recipient_id: recipient.id, percentage: 100 } ]
33
+ transaction = PagarMe::Transaction.charge transaction_with_boleto_params(split_rules: split_rules)
34
+
35
+ assert_empty_balance recipient.balance
36
+
37
+ transaction.status = :paid
38
+ transaction.save
39
+
40
+ recipient.receive recipient.balance.available.amount
41
+
42
+ assert_transfered_balance recipient.balance
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class BankAccountTest < Test::Unit::TestCase
5
+ should 'be able to create a bank_account' do
6
+ bank_account = PagarMe::BankAccount.create bank_account_params
7
+ assert_equal bank_account.bank_code, '237'
8
+ end
9
+
10
+ should 'be able to search by anything' do
11
+ bank_account = PagarMe::BankAccount.create bank_account_params
12
+ bank_accounts = PagarMe::BankAccount.find_by bank_code: '237'
13
+
14
+ assert bank_accounts.size > 0
15
+ bank_accounts.each do |b|
16
+ assert_equal b.bank_code, '237'
17
+ end
18
+ end
19
+
20
+ {
21
+ bank_code: 'foo',
22
+ agencia: 'abcd',
23
+ agencia_dv: 'Y',
24
+ conta: 'ABCD',
25
+ conta_dv: '',
26
+ legal_name: '',
27
+ document_number: 'foooo'
28
+ }.each do |key, value|
29
+ should "validate bank_account - #{key}" do
30
+ exception = assert_raises(PagarMe::ValidationError){ BankAccount.create key => value }
31
+ assert_has_error_param exception, key.to_s
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,72 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class BulkAnticipationTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ super
8
+ ensure_waiting_funds
9
+ end
10
+
11
+ should 'be able to calculate anticipations limits' do
12
+ assert_anticipation_limits PagarMe::Recipient.default.bulk_anticipations_limits(anticipations_limits_params)
13
+ end
14
+
15
+ should 'be able to request anticipation and cancel it' do
16
+ recipient = PagarMe::Recipient.default
17
+ anticipation = recipient.bulk_anticipate anticipation_params
18
+
19
+ assert_anticipation anticipation
20
+ assert_anticipation BulkAnticipation.find(recipient.id, anticipation.id)
21
+
22
+ anticipation.cancel
23
+ assert_canceled_anticipation anticipation
24
+
25
+ anticipation = recipient.bulk_anticipate anticipation_params
26
+ assert_canceled_anticipation PagarMe::BulkAnticipation.cancel(recipient.id, anticipation.id)
27
+ end
28
+
29
+ should 'have bulk anticipations on default recipient ' do
30
+ recipient = PagarMe::Recipient.default
31
+ assert recipient.bulk_anticipations.count > 0
32
+ end
33
+
34
+ should 'create a building anticipation an later confirm it' do
35
+ recipient = PagarMe::Recipient.default
36
+ anticipation = recipient.bulk_anticipate anticipation_params(build: true)
37
+ assert_equal anticipation.id, BulkAnticipation.confirm(recipient.id, anticipation.id).id
38
+
39
+ anticipation = recipient.bulk_anticipate anticipation_params(build: true)
40
+ assert_equal anticipation.id, anticipation.confirm.id
41
+ end
42
+
43
+ should 'create a building anticipation an later delete it' do
44
+ recipient = PagarMe::Recipient.default
45
+ anticipation = recipient.bulk_anticipate anticipation_params(build: true)
46
+
47
+ assert_equal anticipation.id, PagarMe::BulkAnticipation.find(recipient.id, anticipation.id).id
48
+ PagarMe::BulkAnticipation.delete(recipient.id, anticipation.id).id
49
+ assert_raises PagarMe::NotFound do
50
+ PagarMe::BulkAnticipation.find recipient.id, anticipation.id
51
+ end
52
+
53
+ anticipation = recipient.bulk_anticipate anticipation_params(build: true)
54
+ assert_equal anticipation.id, PagarMe::BulkAnticipation.find(recipient.id, anticipation.id).id
55
+ anticipation.delete
56
+ assert_raises PagarMe::NotFound do
57
+ PagarMe::BulkAnticipation.find recipient.id, anticipation.id
58
+ end
59
+ end
60
+
61
+ protected
62
+ def anticipation_params(params = Hash.new)
63
+ anticipations_limits_params(reasonable_amount).merge params
64
+ end
65
+
66
+ def reasonable_amount
67
+ limits = PagarMe::Recipient.default.bulk_anticipations_limits(anticipations_limits_params)
68
+ { requested_amount: (limits.maximum.amount + 9*limits.minimum.amount)/10 + 1 }
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,22 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class CardTest < Test::Unit::TestCase
5
+ should 'be able to create' do
6
+ card = PagarMe::Card.create card_params
7
+
8
+ assert card.id
9
+ assert_equal card.first_digits, '490172'
10
+ assert_equal card.last_digits, '4448'
11
+ end
12
+
13
+ should 'be able to find by id' do
14
+ card = PagarMe::Card.create card_params
15
+ found_card = PagarMe::Card.find_by_id card.id
16
+
17
+ assert_equal card.id, found_card.id
18
+ assert_equal card.first_digits, found_card.first_digits
19
+ assert_equal card.last_digits, found_card.last_digits
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class PayableTest < Test::Unit::TestCase
5
+ should 'be created setting default recipient on payable' do
6
+ transaction = PagarMe::Transaction.create transaction_with_customer_with_card_params
7
+ payable = transaction.payables.first
8
+
9
+ assert_equal transaction.payables.count, 1
10
+ assert_equal payable.recipient_id, PagarMe::Recipient.default.id
11
+ end
12
+
13
+ should 'create one per split rule' do
14
+ transaction = PagarMe::Transaction.create transaction_with_customer_with_card_with_split_rules_params
15
+ payable = transaction.payables.first
16
+
17
+ assert_equal transaction.payables.count, 4
18
+ assert_equal transaction.payables.map(&:recipient_id).sort, fixtures.persistent_recipient_ids.sort
19
+ end
20
+
21
+ should 'create be found' do
22
+ payables = PagarMe::Payable.find_by type: 'refund'
23
+
24
+ assert payables.count > 0
25
+ payables.each do |payable|
26
+ assert_equal payable.type, 'refund'
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,57 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class PlanTest < Test::Unit::TestCase
5
+ should 'be able to create a plan' do
6
+ plan = PagarMe::Plan.create plan_params
7
+ assert_plan_created plan
8
+ end
9
+
10
+ should 'be able to update plan' do
11
+ plan = PagarMe::Plan.create plan_params
12
+ assert_plan_created plan
13
+
14
+ plan.name = 'plano bronze'
15
+ plan.save
16
+ assert_equal plan.name, 'plano bronze'
17
+ end
18
+
19
+ should 'be able to search by anything' do
20
+ plan = PagarMe::Plan.create plan_params
21
+ assert_plan_created plan
22
+
23
+ # find_by_hash is possibly consistent, wait to try to ensure!!!
24
+ sleep 1
25
+ found_plans = PagarMe::Plan.find_by trial_days: 5
26
+
27
+ assert found_plans.size > 0
28
+ found_plans.each do |plan|
29
+ assert_equal plan.trial_days, 5
30
+ end
31
+ end
32
+
33
+ should 'validate plan amount' do
34
+ exception = assert_raises(PagarMe::ValidationError){ Plan.create amount: -1 }
35
+ assert_has_error_param exception, 'amount'
36
+ end
37
+
38
+ should 'validate plan days' do
39
+ exception = assert_raises(PagarMe::ValidationError){ Plan.create amount: 1000, days: -1 }
40
+ assert_has_error_param exception, 'days'
41
+ end
42
+
43
+ should 'validate plan with missing name' do
44
+ exception = assert_raises(PagarMe::ValidationError){ Plan.create amount: 1000, days: 20 }
45
+ assert_has_error_param exception, 'name'
46
+ end
47
+
48
+ should 'not be possible to edit days' do
49
+ plan = Plan.create amount: 1000, days: 20, name: 'Plano Platinum'
50
+ exception = assert_raises(PagarMe::ValidationError) do
51
+ plan.days = 30
52
+ plan.save
53
+ end
54
+ assert_has_error_param exception, 'days'
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,18 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class TransactionTest < Test::Unit::TestCase
5
+ should 'be valid when has valid fingerprint' do
6
+ fixed_api_key do
7
+ postback = PagarMe::Postback.new postback_response_params
8
+ assert postback.valid?
9
+ end
10
+ end
11
+
12
+ should 'be valid when has invalid fingerprint' do
13
+ invalid_fingerprint = Digest::SHA1.hexdigest 'Invalid Fingerprint!'
14
+ postback = PagarMe::Postback.new postback_response_params(fingerprint: invalid_fingerprint)
15
+ assert !postback.valid?
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,41 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class RecipientTest < Test::Unit::TestCase
5
+
6
+ should 'be able to create a recipient with bank_account data' do
7
+ recipient = PagarMe::Recipient.create recipient_with_nested_bank_account_params
8
+ assert_not_nil recipient.date_created
9
+ end
10
+
11
+ should 'be able to create a recipient with bank_account id' do
12
+ bank_account = PagarMe::BankAccount.create bank_account_params
13
+ recipient = PagarMe::Recipient.create recipient_params(bank_account_id: bank_account.id)
14
+
15
+ assert_not_nil recipient.date_created
16
+ end
17
+
18
+ should 'not be able to create a recipient without bank_account data' do
19
+ recipient = PagarMe::Recipient.new recipient_params
20
+ exception = assert_raises(PagarMe::ValidationError){ recipient.create }
21
+
22
+ [:bank_code, :agencia, :conta_dv, :conta, :document_number, :legal_name].each do |missing_attr|
23
+ assert_has_error_param exception, "bank_account[#{missing_attr}]"
24
+ end
25
+ assert_nil recipient.date_created
26
+ end
27
+
28
+ should 'be able to search' do
29
+ recipient = PagarMe::Recipient.create recipient_with_nested_bank_account_params
30
+
31
+ bank_account_doc_number = bank_account_params[:document_number]
32
+ recipients = PagarMe::Recipient.find_by 'bank_account[document_number]' => bank_account_doc_number
33
+
34
+ assert recipients.size > 0
35
+ recipients.each do |recipient|
36
+ assert_equal recipient.bank_account.document_number, bank_account_doc_number
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,100 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module PagarMe
4
+ class SubscriptionTest < Test::Unit::TestCase
5
+ should 'be able to create subscription with plan' do
6
+ plan = PagarMe::Plan.create plan_params
7
+ assert_plan_created plan
8
+
9
+ subscription = PagarMe::Subscription.create subscription_with_customer_params(plan: plan)
10
+ assert_subscription_created subscription, plan
11
+ end
12
+
13
+ should 'be able to see subscription transactions' do
14
+ plan = PagarMe::Plan.create no_trial_plan_params
15
+ subscription = PagarMe::Subscription.create subscription_with_customer_params(plan: plan)
16
+ assert_no_trial_subscription_created subscription, plan
17
+
18
+ sleep 1
19
+ assert subscription.transactions.count > 0
20
+ assert_equal subscription.transactions.first.status, 'paid'
21
+ end
22
+
23
+ should 'be able to create subscription with plan and unsaved card' do
24
+ plan = PagarMe::Plan.create plan_params
25
+ card = PagarMe::Card.new card_params
26
+ subscription = PagarMe::Subscription.create subscription_with_customer_params(plan: plan, card: card)
27
+ assert_subscription_created subscription, plan
28
+ end
29
+
30
+ should 'be able to create subscription with plan and saved card' do
31
+ plan = PagarMe::Plan.create plan_params
32
+ card = PagarMe::Card.create card_params
33
+ subscription = PagarMe::Subscription.create subscription_with_customer_params(plan: plan, card: card)
34
+ assert_subscription_created subscription, plan
35
+ end
36
+
37
+ should 'be able to create subscription without plan' do
38
+ subscription = PagarMe::Subscription.create subscription_with_customer_with_card_params(amount: 2000)
39
+ assert_subscription_successfully_paid subscription, 2000
40
+
41
+ found_subscription = PagarMe::Subscription.find_by_id subscription.id
42
+ assert_subscription_successfully_paid subscription, 2000
43
+ end
44
+
45
+ should 'be able to create subscription without plan and charge with installments' do
46
+ subscription = PagarMe::Subscription.create subscription_with_customer_with_card_params(amount: 2000, installments: 6)
47
+ assert_subscription_successfully_paid subscription, 2000, 6
48
+
49
+ id = subscription.id
50
+
51
+ subscription.charge 1500, 3
52
+ assert_equal id, subscription.id
53
+ assert_subscription_successfully_paid subscription, 1500, 3
54
+
55
+ found_subscription = PagarMe::Subscription.find_by_id subscription.id
56
+ assert_subscription_successfully_paid subscription, 1500, 3
57
+ end
58
+
59
+ should 'be able to update subscription' do
60
+ subscription = PagarMe::Subscription.create subscription_with_customer_with_card_params
61
+
62
+ subscription.payment_method = 'boleto'
63
+ subscription.save
64
+
65
+ found_subscription = PagarMe::Subscription.find_by_id subscription.id
66
+ assert_equal found_subscription.payment_method, 'boleto'
67
+ end
68
+
69
+ should 'raise an error when nil or empty string as ID' do
70
+ assert_raises RequestError do
71
+ PagarMe::Subscription.find_by_id nil
72
+ end
73
+
74
+ assert_raises RequestError do
75
+ PagarMe::Subscription.find_by_id ''
76
+ end
77
+ end
78
+
79
+ should 'be able to change plans' do
80
+ plan = PagarMe::Plan.create plan_params
81
+ other_plan = PagarMe::Plan.create other_plan_params
82
+
83
+ subscription = PagarMe::Subscription.create subscription_with_customer_with_card_params(plan: plan)
84
+ assert_equal subscription.plan.id, plan.id
85
+
86
+ subscription.plan = other_plan
87
+ subscription.save
88
+ assert_equal subscription.plan.id, other_plan.id
89
+ end
90
+
91
+ should 'be able to cancel a subscription' do
92
+ plan = PagarMe::Plan.create plan_params
93
+ subscription = PagarMe::Subscription.create subscription_with_customer_with_card_params(plan: plan)
94
+ assert_equal subscription.status, 'trialing'
95
+
96
+ subscription.cancel
97
+ assert_equal subscription.status, 'canceled'
98
+ end
99
+ end
100
+ end