/
charge_spec.rb
172 lines (155 loc) · 5.83 KB
/
charge_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require_relative '../rspec_helper'
describe Smartcoin::Charge do
before(:each) do
Smartcoin.api_key('pk_test_3ac0794848c339')
Smartcoin.api_secret('sk_test_8bec997b7a0ea1')
end
it 'should create a charge' do
token_params = {number: 4242424242424242,
exp_month: 11,
exp_year: 2017,
cvc: '111',
name: 'Doctor Who'
}
token = Smartcoin::Token.create(token_params)
charge_params = {
amount: 1000,
currency: 'brl',
card: token.id,
}
charge = Smartcoin::Charge.create(charge_params)
expect(charge.id).to match(/ch_(.*)/)
expect(charge.amount).to eq(charge_params[:amount])
expect(charge.paid).to be_truthy
expect(charge.captured).to be_truthy
expect(charge.card.id).to match(/card_(.*)/)
expect(charge.card.type).to eq('Visa')
expect(charge.fees.size).to be >= 2
expect(charge.fees.first.type).to eq('Smartcoin fee: flat')
expect(charge.fees.first.class).to eq(Smartcoin::Fee)
expect(charge.installments.size).to be >= 1
expect(charge.installments.first.class).to eq(Smartcoin::Installment)
end
it 'should create a bank_slip charge types' do
charge_params = {amount: 1000, currency: 'brl', type: 'bank_slip'}
charge = Smartcoin::Charge.create(charge_params)
expect(charge.id).to match(/ch_(.*)/)
expect(charge.amount).to eq(charge_params[:amount])
expect(charge.paid).to be_falsey
expect(charge.card).to be_nil
expect(charge.bank_slip).to_not be_nil
expect(charge.bank_slip.link).to match(/https:\/\/api\.smartcoin\.com\.br\/v1\/charges\/ch_(.*)\/bank_slip\/test/)
end
it 'should retrieve a charge that has already created' do
token_params = {number: 4242424242424242,
exp_month: 11,
exp_year: 2017,
cvc: '111',
name: 'Doctor Who'
}
token = Smartcoin::Token.create(token_params)
charge_params = {
amount: 1000,
currency: 'brl',
card: token.id,
}
charge_created = Smartcoin::Charge.create(charge_params)
charge_retrieved = Smartcoin::Charge.retrieve(charge_created.id)
expect(charge_retrieved.id).to eq(charge_created.id)
expect(charge_retrieved.amount).to eq(charge_created.amount)
expect(charge_retrieved.card.fingerprint).to eq(charge_created.card.fingerprint)
end
it 'should capture charge that has already created' do
token_params = {number: 4242424242424242,
exp_month: 11,
exp_year: 2017,
cvc: '111',
name: 'Doctor Who'
}
token = Smartcoin::Token.create(token_params)
charge_params = {
amount: 1000,
currency: 'brl',
card: token.id,
capture: false
}
charge_created = Smartcoin::Charge.create(charge_params)
expect(charge_created.captured).to be_falsey
charge_created.capture()
expect(charge_created.captured).to be_truthy
end
it 'should partial capture charge that has already created' do
token_params = {number: 4242424242424242,
exp_month: 11,
exp_year: 2017,
cvc: '111',
name: 'Doctor Who'
}
token = Smartcoin::Token.create(token_params)
charge_params = {
amount: 1000,
currency: 'brl',
card: token.id,
capture: false
}
charge_created = Smartcoin::Charge.create(charge_params)
expect(charge_created.captured).to be_falsey
amount_captured = 300
charge_created.capture(amount_captured)
expect(charge_created.captured).to be_truthy
expect(charge_created.amount).to eq(charge_params[:amount])
expect(charge_created.amount_refunded).to eq(charge_params[:amount] - amount_captured)
end
it 'should refund charge that has already created' do
token_params = {number: 4242424242424242,
exp_month: 11,
exp_year: 2017,
cvc: '111',
name: 'Doctor Who'
}
token = Smartcoin::Token.create(token_params)
charge_params = {
amount: 1000,
currency: 'brl',
card: token.id
}
charge_created = Smartcoin::Charge.create(charge_params)
expect(charge_created.refunded).to be_falsey
charge_created.refund()
expect(charge_created.refunded).to be_truthy
expect(charge_created.amount).to eq(charge_params[:amount])
expect(charge_created.amount_refunded).to eq(charge_params[:amount])
expect(charge_created.refunds.size).to eq(1)
end
it 'should partial refund charge that has already created' do
token_params = {number: 4242424242424242,
exp_month: 11,
exp_year: 2017,
cvc: '111',
name: 'Doctor Who'
}
token = Smartcoin::Token.create(token_params)
charge_params = {
amount: 1000,
currency: 'brl',
card: token.id
}
charge_created = Smartcoin::Charge.create(charge_params)
expect(charge_created.refunded).to be_falsey
amount_refunded = 600
charge_created.refund(amount_refunded)
expect(charge_created.refunded).to be_falsey
expect(charge_created.amount).to eq(charge_params[:amount])
expect(charge_created.amount_refunded).to eq(amount_refunded)
expect(charge_created.refunds.size).to eq(1)
expect(charge_created.refunds[0].amount).to eq(amount_refunded)
end
it 'should list charges that have already created' do
params = {count: 3}
charge_list = Smartcoin::Charge.list_all(params)
expect(charge_list.object).to eq('list')
expect(charge_list.count).to eq(3)
expect(charge_list.data.size).to eq(3)
expect(charge_list.data[0].class).to eq(Smartcoin::Charge)
end
end