pagarme 0.15 → 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,11 +4,11 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "pagarme"
7
- spec.version = 0.15
8
- spec.authors = ["Pedro Franceschi"]
9
- spec.email = ["pedrohfranceschi@gmail.com"]
10
- spec.description = %q{Gem do pagar.me para Ruby}
11
- spec.summary = %q{Permite a integração com a API do pagar.me por Ruby.}
7
+ spec.version = 1.0
8
+ spec.authors = ["Pedro Franceschi", "Henrique Dubugras"]
9
+ spec.email = ["pedrohfranceschi@gmail.com", "henrique@pagar.me"]
10
+ spec.description = %q{Pagar.me's ruby gem}
11
+ spec.summary = %q{Allows integration with Pagar.me}
12
12
  spec.homepage = "http://pagar.me/"
13
13
 
14
14
  spec.files = `git ls-files`.split($/)
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.add_development_dependency "bundler", "~> 1.3"
20
20
  spec.add_development_dependency "rake"
21
+ spec.add_development_dependency('shoulda', '~> 3.4.0')
22
+ spec.add_development_dependency('test-unit')
21
23
 
22
24
  spec.add_dependency "rest-client"
23
25
  spec.add_dependency "multi_json"
data/pagarme.rb CHANGED
@@ -1,43 +1,3 @@
1
1
  require File.join(File.dirname(__FILE__), '.', 'lib/pagarme')
2
2
 
3
- PagarMe.api_key = "4f0907cdfaf855b83a5d4a83a247772f"
4
- PagarMe.live = true
5
3
 
6
- begin
7
- transaction = PagarMe::Transaction.new
8
- transaction.card_number = "0000000000000000"
9
- transaction.card_holder_name = "Test User"
10
- transaction.card_expiracy_month = "12"
11
- transaction.card_expiracy_year = "15"
12
- transaction.card_cvv = "314"
13
- transaction.amount = 1000
14
- transaction.charge
15
- transaction.chargeback
16
-
17
- chargebacked_transaction = PagarMe::Transaction.find_by_id(transaction.id)
18
- puts chargebacked_transaction.id == transaction.id
19
- puts chargebacked_transaction.status == transaction.status
20
- puts chargebacked_transaction.inspect
21
-
22
- puts "\n\n"
23
-
24
- hash_transaction = PagarMe::Transaction.new({
25
- :card_number => "0000000000000000",
26
- :card_holder_name => "Test User",
27
- :card_expiracy_month => "12",
28
- :card_expiracy_year => "13",
29
- :card_cvv => "314",
30
- :amount => 10000,
31
- :installments => 5
32
- })
33
- hash_transaction.charge
34
- puts hash_transaction.inspect
35
- puts hash_transaction.status
36
-
37
- puts "\n\n"
38
-
39
- transactions = PagarMe::Transaction.all
40
- puts transactions.inspect
41
- rescue PagarMe::PagarMeError => e
42
- puts "Error: #{e}"
43
- end
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+ require_relative '../test_helper'
3
+
4
+ module PagarMe
5
+ class PlanTest < Test::Unit::TestCase
6
+ should 'be able to create a plan' do
7
+ plan = test_plan
8
+ plan.create
9
+ test_plan_response(plan)
10
+ end
11
+
12
+ should 'be able to update plan' do
13
+ plan = test_plan
14
+ plan.create
15
+ plan.name = "plano silver"
16
+ plan.save
17
+ assert plan.name == 'plano silver'
18
+ end
19
+
20
+ should 'be able to create with unformatted amount' do
21
+ plan = test_plan
22
+ plan.amount = 'R$ 10.00'
23
+ plan.create
24
+ assert plan.amount == 1000
25
+ end
26
+
27
+ should 'validate plan' do
28
+ exception = assert_raises PagarMeError do
29
+ plan = Plan.new({
30
+ :amount => -1
31
+ })
32
+ plan.create
33
+ end
34
+ assert exception.errors.first.parameter_name == 'amount'
35
+ exception = assert_raises PagarMeError do
36
+ plan = Plan.new({
37
+ :amount => 1000,
38
+ :days => -1,
39
+ })
40
+ plan.create
41
+ end
42
+ assert exception.errors.first.parameter_name == 'days'
43
+
44
+ exception = assert_raises PagarMeError do
45
+ plan = Plan.new({
46
+ :amount => 1000,
47
+ :days => 30,
48
+ })
49
+ plan.create
50
+ end
51
+ assert exception.errors.first.parameter_name == 'name'
52
+ exception = assert_raises PagarMeError do
53
+ plan = Plan.new({
54
+ :amount => 1000,
55
+ :days => 30,
56
+ :name => "Plano Silver"
57
+ })
58
+ plan.create
59
+ plan.days = 'Plano gold'
60
+ plan.save
61
+ end
62
+ assert exception.errors.first.parameter_name == 'days'
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require_relative '../test_helper'
3
+
4
+ module PagarMe
5
+ class SubscriptionTest < Test::Unit::TestCase
6
+ should 'be able to create subscription with plan' do
7
+ plan = test_plan
8
+ plan.create
9
+ subscription = test_subscription
10
+ subscription.plan = plan
11
+ subscription.create
12
+ test_plan_response(subscription.plan)
13
+ test_transaction_with_customer(subscription)
14
+ end
15
+
16
+ should 'be able to create subscription without plan' do
17
+ subscription = test_subscription({:amount => 2000})
18
+ subscription.create
19
+ assert subscription.transactions.length == 1
20
+ subscription.charge(2000)
21
+ assert subscription.transactions.length == 2
22
+ assert subscription.transactions.first.kind_of?(PagarMe::Transaction)
23
+ subscription.transactions.each do |t|
24
+ test_subscription_transaction_response(t)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+ require_relative '../test_helper'
3
+
4
+ module PagarMe
5
+ class TransactionTest < Test::Unit::TestCase
6
+ should 'be able to charge' do
7
+ transaction = test_transaction
8
+ assert transaction.status == 'local'
9
+ transaction.charge
10
+ assert transaction.status == 'paid'
11
+ test_transaction_response(transaction)
12
+ end
13
+
14
+ should 'be able to refund' do
15
+ transaction = test_transaction
16
+ transaction.charge
17
+ test_transaction_response(transaction)
18
+ transaction.refund
19
+ assert transaction.status == 'refunded'
20
+ end
21
+
22
+ should 'be able to create transaciton with boleto' do
23
+ transaction = PagarMe::Transaction.new({
24
+ :payment_method => "boleto",
25
+ :amount => "1000"
26
+ })
27
+ transaction.charge
28
+
29
+ assert transaction.payment_method == 'boleto'
30
+ assert transaction.status == 'waiting_payment'
31
+ assert transaction.amount.to_s == '1000'
32
+ end
33
+
34
+ should 'be able to find a transaction' do
35
+ transaction = test_transaction
36
+ transaction.charge
37
+ test_transaction_response(transaction)
38
+
39
+ transaction_2 = PagarMe::Transaction.find_by_id(transaction.id)
40
+ assert transaction_2.id == transaction.id
41
+ end
42
+
43
+ should 'be able to create transaction with customer' do
44
+ transaction = test_transaction_with_customer
45
+ transaction.charge
46
+ test_transaction_response(transaction)
47
+ assert transaction.customer.class == Customer
48
+ test_customer_response(transaction.customer)
49
+ end
50
+
51
+ should 'be able to refund transaction with customer' do
52
+ transaction = test_transaction_with_customer
53
+ transaction.charge
54
+ test_transaction_response(transaction)
55
+ assert transaction.customer.class == Customer
56
+ test_customer_response(transaction.customer)
57
+ transaction.refund
58
+
59
+ assert transaction.status == 'refunded'
60
+ end
61
+
62
+ should 'should allow transactions with R$ amount' do
63
+ transaction = test_transaction
64
+ transaction.amount = 'R$ 10.00'
65
+ transaction.charge
66
+ assert transaction.amount == 1000
67
+ end
68
+
69
+ should 'validate invalid transaction' do
70
+
71
+ #Test invalid card_number
72
+ exception = assert_raises PagarMeError do
73
+ transaction = PagarMe::Transaction.new({
74
+ :amount => "1000",
75
+ :card_number => '123456',
76
+ :card_holder_name => "Jose da Silva",
77
+ })
78
+ transaction.charge
79
+ end
80
+ assert exception.errors.first.parameter_name == 'card_number'
81
+
82
+ #Test missing card_holder_name
83
+ exception = assert_raises PagarMeError do
84
+ transaction = PagarMe::Transaction.new({
85
+ :card_number => '4111111111111111',
86
+ :amount => "1000",
87
+ })
88
+ transaction.charge
89
+ end
90
+ assert exception.errors.first.parameter_name == 'card_holder_name'
91
+
92
+ #Test invalid expiracy month
93
+ exception = assert_raises PagarMeError do
94
+ transaction = PagarMe::Transaction.new({
95
+ :card_number => '4111111111111111',
96
+ :card_holder_name => "Jose da Silva",
97
+ :amount => "1000",
98
+ :card_expiracy_month => 15
99
+ })
100
+ transaction.charge
101
+ end
102
+ assert exception.errors.first.parameter_name == 'card_expiration_date'
103
+
104
+ #Test invalid expiracy year
105
+ exception = assert_raises PagarMeError do
106
+ transaction = PagarMe::Transaction.new({
107
+ :card_number => '4111111111111111',
108
+ :card_holder_name => "Jose da Silva",
109
+ :amount => "1000",
110
+ :card_expiration_month => 12,
111
+ :card_expiration_year => -1,
112
+ })
113
+ transaction.charge
114
+ end
115
+ assert exception.errors.first.parameter_name == 'card_expiration_date'
116
+
117
+ #Test invalid expiracy year
118
+ exception = assert_raises PagarMeError do
119
+ transaction = PagarMe::Transaction.new({
120
+ :card_number => '4111111111111111',
121
+ :card_holder_name => "Jose da Silva",
122
+ :amount => "1000",
123
+ :card_expiration_month => 12,
124
+ :card_expiration_year => 16,
125
+ })
126
+ transaction.charge
127
+ end
128
+ assert exception.errors.first.parameter_name == 'card_cvv'
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,166 @@
1
+ require 'pagarme'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ def test_transaction(params = {})
6
+ return PagarMe::Transaction.new({
7
+ :card_number => "4901720080344448",
8
+ :card_holder_name => "Jose da Silva",
9
+ :card_expiration_month => "10",
10
+ :card_expiration_year => "15",
11
+ :card_cvv => "314",
12
+ :amount => 1000
13
+ }.merge(params))
14
+ end
15
+
16
+ def test_subscription_without_plan(params = {})
17
+ return PagarMe::Subscription.new({
18
+ :payment_method => 'credit_card',
19
+ :card_number => "4901720080344448",
20
+ :card_holder_name => "Jose da Silva",
21
+ :card_expiration_month => "10",
22
+ :card_expiration_year => "15",
23
+ :card_cvv => "314",
24
+ :customer_email => 'test@test.com',
25
+ :postback_url => "http://test.com/postback"
26
+ }.merge(params))
27
+ end
28
+
29
+ def test_plan(params = {})
30
+ return PagarMe::Plan.new({
31
+ :name => "Plano gold",
32
+ :trial_days => 5,
33
+ :days => 30,
34
+ :amount => 3000,
35
+ }.merge(params))
36
+ end
37
+
38
+
39
+ def test_subscription(params = {})
40
+ return PagarMe::Subscription.new({
41
+ :payment_method => 'credit_card',
42
+ :card_number => "4901720080344448",
43
+ :card_holder_name => "Jose da Silva",
44
+ :card_expiration_month => "10",
45
+ :card_expiration_year => "15",
46
+ :card_cvv => "314",
47
+ :customer_email => 'test@test.com',
48
+ :postback_url => "http://test.com/postback",
49
+ }.merge(params))
50
+ end
51
+
52
+ def test_transaction_with_customer(params = {})
53
+ return PagarMe::Transaction.new({
54
+ :amount => 1000,
55
+ :card_number => '4901720080344448',
56
+ :card_holder_name => "Jose da Silva",
57
+ :card_expiration_month => 11,
58
+ :card_expiration_year => "13",
59
+ :card_cvv => 356,
60
+ :customer => {
61
+ :name => "Jose da Silva",
62
+ :document_number => "36433809847",
63
+ :email => "henrique@pagar.me",
64
+ :address => {
65
+ :street => 'Av. Brigadeiro Faria Lima',
66
+ :neighborhood => 'Itaim bibi',
67
+ :zipcode => '01452000',
68
+ :street_number => 2941,
69
+ },
70
+ :phone => {
71
+ :ddd => 12,
72
+ :number => '981433533',
73
+ },
74
+ :sex => 'M',
75
+ :born_at => '1970-10-11'
76
+ }
77
+ }.merge(params))
78
+ end
79
+
80
+ def test_subscription_with_customer(params = {})
81
+ return PagarMe::Subscription.new({
82
+ :amount => 1000,
83
+ :card_number => '4901720080344448',
84
+ :card_holder_name => "Jose da silva",
85
+ :card_expiration_month => 11,
86
+ :card_expiration_year => 13,
87
+ :card_cvv => 356,
88
+ :customer_email => 'teste@teste.com',
89
+ :customer => {
90
+ :name => "Jose da Silva",
91
+ :document_number => "36433809847",
92
+ :email => "henrique@pagar.me",
93
+ :address => {
94
+ :street => 'Av. Brigadeiro Faria Lima',
95
+ :neighborhood => 'Itaim bibi',
96
+ :zipcode => '01452000',
97
+ :street_number => 2941,
98
+ } ,
99
+ :phone => {
100
+ :ddd => 12,
101
+ :number => '981433533',
102
+ } ,
103
+ :sex => 'M',
104
+ :born_at => '1970-10-11'
105
+ }
106
+ }.merge(params))
107
+ end
108
+
109
+
110
+ def test_customer_response(customer)
111
+ assert customer.id
112
+ assert customer.addresses.class == Array
113
+ assert customer.phones.class == Array
114
+ assert customer.document_type == 'cpf'
115
+ assert customer.name == 'Jose da Silva'
116
+ assert customer.born_at
117
+ assert customer.id
118
+ assert customer.addresses[0].class == PagarMe::Address
119
+ assert customer.phones[0].class == PagarMe::Phone
120
+ end
121
+
122
+ def test_subscription_transaction_response(transaction)
123
+ assert transaction.id
124
+ assert !transaction.refuse_reason
125
+ assert transaction.date_created
126
+ assert transaction.amount == 2000
127
+ assert transaction.installments.to_i == 1
128
+ # assert transaction.card_brand == 'visa'
129
+ assert transaction.payment_method == 'credit_card'
130
+ assert transaction.status == 'paid'
131
+ assert !transaction.boleto_url
132
+ assert !transaction.boleto_barcode
133
+ end
134
+
135
+ def test_transaction_response(transaction)
136
+ assert transaction.id
137
+ assert transaction.card_holder_name
138
+ assert !transaction.refuse_reason
139
+ assert transaction.date_created
140
+ assert transaction.amount == 1000
141
+ assert transaction.installments.to_i == 1
142
+ assert transaction.card_holder_name == 'Jose da Silva'
143
+ # assert transaction.card_brand == 'visa'
144
+ assert transaction.payment_method == 'credit_card'
145
+ assert transaction.status == 'paid'
146
+ assert !transaction.boleto_url
147
+ assert !transaction.boleto_barcode
148
+ end
149
+
150
+ def test_plan_response(plan)
151
+ assert plan.id
152
+ assert plan.name == 'Plano gold'
153
+ assert plan.trial_days == 5
154
+ assert plan.days == 30
155
+ assert plan.amount = 3000
156
+ end
157
+
158
+ class Test::Unit::TestCase
159
+ def setup
160
+ PagarMe.api_key="ak_test_Rw4JR98FmYST2ngEHtMvVf5QJW7Eoo"
161
+ end
162
+
163
+ def teardown
164
+ PagarMe.api_key=nil
165
+ end
166
+ end