catarse_pagarme 2.5.1 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -257
- data/app/controllers/catarse_pagarme/application_controller.rb +6 -7
- data/app/controllers/catarse_pagarme/credit_cards_controller.rb +5 -5
- data/app/controllers/catarse_pagarme/notifications_controller.rb +6 -6
- data/app/controllers/catarse_pagarme/slip_controller.rb +6 -6
- data/app/helpers/catarse_pagarme/application_helper.rb +3 -3
- data/app/models/catarse_pagarme/credit_card_transaction.rb +1 -1
- data/app/models/catarse_pagarme/fee_calculator_concern.rb +12 -12
- data/app/models/catarse_pagarme/payment_concern.rb +9 -0
- data/app/models/catarse_pagarme/{contribution_delegator.rb → payment_delegator.rb} +17 -17
- data/app/models/catarse_pagarme/slip_transaction.rb +3 -2
- data/app/models/catarse_pagarme/transaction_base.rb +14 -13
- data/lib/catarse_pagarme/engine.rb +1 -1
- data/lib/catarse_pagarme/version.rb +1 -1
- data/spec/controllers/catarse_pagarme/credit_cards_controller_spec.rb +5 -5
- data/spec/controllers/catarse_pagarme/notifications_controller_spec.rb +11 -6
- data/spec/controllers/catarse_pagarme/slip_controller_spec.rb +6 -6
- data/spec/dummy/app/models/contribution.rb +1 -24
- data/spec/dummy/app/models/payment.rb +55 -0
- data/spec/dummy/app/models/payment_engines.rb +7 -0
- data/spec/helpers/catarse_pagarme/application_helper_spec.rb +2 -2
- data/spec/models/catarse_pagarme/credit_card_transaction_spec.rb +23 -23
- data/spec/models/catarse_pagarme/{contribution_delegator_spec.rb → payment_delegator_spec.rb} +28 -55
- data/spec/models/catarse_pagarme/slip_transaction_spec.rb +35 -29
- data/spec/support/factories.rb +16 -6
- metadata +8 -6
- data/app/models/catarse_pagarme/contribution_concern.rb +0 -9
@@ -1,45 +1,45 @@
|
|
1
1
|
module CatarsePagarme
|
2
|
-
class
|
3
|
-
attr_accessor :
|
2
|
+
class PaymentDelegator
|
3
|
+
attr_accessor :payment, :transaction
|
4
4
|
include FeeCalculatorConcern
|
5
5
|
|
6
|
-
def initialize(
|
6
|
+
def initialize(payment)
|
7
7
|
configure_pagarme
|
8
|
-
self.
|
8
|
+
self.payment = payment
|
9
9
|
end
|
10
10
|
|
11
11
|
def change_status_by_transaction(transactin_status)
|
12
12
|
case transactin_status
|
13
13
|
when 'paid', 'authorized' then
|
14
|
-
self.
|
14
|
+
self.payment.pay unless self.payment.paid?
|
15
15
|
when 'refunded' then
|
16
|
-
self.
|
16
|
+
self.payment.refund unless self.payment.refunded?
|
17
17
|
when 'refused' then
|
18
|
-
self.
|
19
|
-
when 'waiting_payment', 'processing' then
|
20
|
-
self.contribution.waiting unless self.contribution.waiting_confirmation?
|
18
|
+
self.payment.refuse unless self.payment.refused?
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
24
22
|
def update_fee
|
25
23
|
fill_acquirer_data
|
26
|
-
|
27
|
-
|
24
|
+
payment.update_attributes({
|
25
|
+
gateway_fee: get_fee,
|
28
26
|
})
|
29
27
|
end
|
30
28
|
|
31
29
|
def fill_acquirer_data
|
32
|
-
if
|
33
|
-
|
30
|
+
if payment.gateway_data.nil? || payment.gateway_data["acquirer_name"].nil? || payment.gateway_data["acquirer_tid"].nil?
|
31
|
+
data = payment.gateway_data || {}
|
32
|
+
payment.gateway_data = data.merge({
|
34
33
|
acquirer_name: transaction.acquirer_name,
|
35
34
|
acquirer_tid: transaction.tid,
|
36
35
|
card_brand: transaction.try(:card_brand)
|
37
36
|
})
|
37
|
+
payment.save
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
def refund
|
42
|
-
if
|
42
|
+
if payment.is_credit_card?
|
43
43
|
transaction.refund
|
44
44
|
else
|
45
45
|
transaction.refund(bank_account_attributes)
|
@@ -47,7 +47,7 @@ module CatarsePagarme
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def value_for_transaction
|
50
|
-
(self.
|
50
|
+
(self.payment.value * 100).to_i
|
51
51
|
end
|
52
52
|
|
53
53
|
def value_with_installment_tax(installment)
|
@@ -65,7 +65,7 @@ module CatarsePagarme
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def transaction
|
68
|
-
@transaction ||= ::PagarMe::Transaction.find_by_id(self.
|
68
|
+
@transaction ||= ::PagarMe::Transaction.find_by_id(self.payment.gateway_id)
|
69
69
|
end
|
70
70
|
|
71
71
|
def get_installment(installment_number)
|
@@ -86,7 +86,7 @@ module CatarsePagarme
|
|
86
86
|
protected
|
87
87
|
|
88
88
|
def bank_account_attributes
|
89
|
-
bank =
|
89
|
+
bank = payment.user.bank_account
|
90
90
|
|
91
91
|
{
|
92
92
|
bank_account: {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module CatarsePagarme
|
2
2
|
class SlipTransaction < TransactionBase
|
3
|
-
def initialize(attributes,
|
3
|
+
def initialize(attributes, payment)
|
4
4
|
super
|
5
5
|
build_default_bank_account
|
6
6
|
end
|
@@ -9,9 +9,10 @@ module CatarsePagarme
|
|
9
9
|
update_user_bank_account
|
10
10
|
|
11
11
|
self.transaction = PagarMe::Transaction.new(self.attributes)
|
12
|
+
|
12
13
|
self.transaction.charge
|
13
14
|
|
14
|
-
|
15
|
+
change_payment_state
|
15
16
|
|
16
17
|
self.transaction
|
17
18
|
end
|
@@ -1,18 +1,19 @@
|
|
1
1
|
module CatarsePagarme
|
2
2
|
class TransactionBase
|
3
|
-
attr_accessor :attributes, :
|
3
|
+
attr_accessor :attributes, :payment,
|
4
4
|
:transaction, :user
|
5
5
|
|
6
|
-
def initialize(attributes,
|
6
|
+
def initialize(attributes, payment)
|
7
7
|
self.attributes = attributes
|
8
|
-
self.
|
9
|
-
self.user =
|
8
|
+
self.payment = payment
|
9
|
+
self.user = payment.user
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
self.
|
12
|
+
def change_payment_state
|
13
|
+
self.payment.update_attributes(attributes_to_payment)
|
14
|
+
self.payment.save!
|
14
15
|
delegator.update_fee
|
15
|
-
self.
|
16
|
+
self.payment.payment_notifications.create(contribution_id: self.payment.contribution_id, extra_data: self.transaction.to_json)
|
16
17
|
delegator.change_status_by_transaction(self.transaction.status)
|
17
18
|
end
|
18
19
|
|
@@ -20,19 +21,19 @@ module CatarsePagarme
|
|
20
21
|
PaymentType::CREDIT_CARD
|
21
22
|
end
|
22
23
|
|
23
|
-
def
|
24
|
+
def attributes_to_payment
|
24
25
|
{
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
payment_method: payment_method,
|
27
|
+
gateway_id: self.transaction.id,
|
28
|
+
gateway: 'Pagarme',
|
29
|
+
gateway_data: self.transaction.to_json,
|
29
30
|
installments: (self.transaction.installments || 1),
|
30
31
|
installment_value: (delegator.value_for_installment(self.transaction.installments || 0) / 100.0).to_f
|
31
32
|
}
|
32
33
|
end
|
33
34
|
|
34
35
|
def delegator
|
35
|
-
self.
|
36
|
+
self.payment.pagarme_delegator
|
36
37
|
end
|
37
38
|
|
38
39
|
end
|
@@ -2,12 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe CatarsePagarme::CreditCardsController do
|
4
4
|
before do
|
5
|
-
PaymentEngines.stub(:find_payment).and_return(contribution)
|
6
5
|
controller.stub(:current_user).and_return(user)
|
7
6
|
end
|
8
7
|
|
9
8
|
let(:project) { create(:project, goal: 10_000, state: 'online') }
|
10
9
|
let(:contribution) { create(:contribution, value: 10, project: project) }
|
10
|
+
let(:payment) { contribution.payments.first }
|
11
11
|
|
12
12
|
describe 'pay with credit card' do
|
13
13
|
context 'without an user' do
|
@@ -15,17 +15,17 @@ describe CatarsePagarme::CreditCardsController do
|
|
15
15
|
|
16
16
|
it 'should raise a error' do
|
17
17
|
expect {
|
18
|
-
post :create, { locale: :pt,
|
18
|
+
post :create, { locale: :pt, id: contribution.id, use_route: 'catarse_pagarme' }
|
19
19
|
}.to raise_error('invalid user')
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
context 'with an user' do
|
24
|
-
let(:user) {
|
24
|
+
let(:user) { payment.user }
|
25
25
|
context "with valid card data" do
|
26
26
|
before do
|
27
27
|
post :create, {
|
28
|
-
locale: :pt,
|
28
|
+
locale: :pt, id: contribution.id, use_route: 'catarse_pagarme',
|
29
29
|
card_hash: sample_card_hash }
|
30
30
|
end
|
31
31
|
|
@@ -37,7 +37,7 @@ describe CatarsePagarme::CreditCardsController do
|
|
37
37
|
context 'with invalid card data' do
|
38
38
|
before do
|
39
39
|
post :create, {
|
40
|
-
locale: :pt,
|
40
|
+
locale: :pt, id: contribution.id, use_route: 'catarse_pagarme', card_hash: "abcd" }
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'payment_status should be failed' do
|
@@ -14,29 +14,34 @@ describe CatarsePagarme::NotificationsController do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
let(:project) { create(:project, goal: 10_000, state: 'online') }
|
17
|
-
let(:contribution) { create(:contribution, value: 10, project: project
|
17
|
+
let(:contribution) { create(:contribution, value: 10, project: project) }
|
18
|
+
let(:payment) {
|
19
|
+
p = contribution.payments.first
|
20
|
+
p.update_attributes gateway_id: 'abcd'
|
21
|
+
p
|
22
|
+
}
|
18
23
|
let(:credit_card) { create(:credit_card, subscription_id: '1542')}
|
19
24
|
|
20
25
|
describe 'CREATE' do
|
21
|
-
context "with invalid
|
26
|
+
context "with invalid payment" do
|
22
27
|
before do
|
23
28
|
PaymentEngines.stub(:find_payment).and_return(nil)
|
24
29
|
post :create, { locale: :pt, id: 'abcdfg', use_route: 'catarse_pagarme' }
|
25
30
|
end
|
26
31
|
|
27
|
-
it "should not found the
|
32
|
+
it "should not found the payment" do
|
28
33
|
expect(response.code.to_i).to eq(404)
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
32
|
-
context "with valid
|
37
|
+
context "with valid payment" do
|
33
38
|
before do
|
34
|
-
PaymentEngines.stub(:find_payment).and_return(
|
39
|
+
PaymentEngines.stub(:find_payment).and_return(payment)
|
35
40
|
post :create, { locale: :pt, id: 'abcd', use_route: 'catarse_pagarme' }
|
36
41
|
end
|
37
42
|
|
38
43
|
it "should save an extra_data into payment_notifications" do
|
39
|
-
expect(
|
44
|
+
expect(payment.payment_notifications.size).to eq(1)
|
40
45
|
end
|
41
46
|
|
42
47
|
it "should return 200 status" do
|
@@ -2,13 +2,13 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe CatarsePagarme::SlipController do
|
4
4
|
before do
|
5
|
-
PaymentEngines.stub(:find_payment).and_return(contribution)
|
6
5
|
controller.stub(:current_user).and_return(user)
|
7
6
|
Bank.create(name: 'foo', code: '123')
|
8
7
|
end
|
9
8
|
|
10
9
|
let(:project) { create(:project, goal: 10_000, state: 'online') }
|
11
10
|
let(:contribution) { create(:contribution, value: 10, project: project) }
|
11
|
+
let(:payment) { contribution.payments.first }
|
12
12
|
let(:credit_card) { create(:credit_card, subscription_id: '1542')}
|
13
13
|
|
14
14
|
describe 'pay with slip' do
|
@@ -17,18 +17,18 @@ describe CatarsePagarme::SlipController do
|
|
17
17
|
|
18
18
|
it 'should raise a error' do
|
19
19
|
expect {
|
20
|
-
post :create, { locale: :pt,
|
20
|
+
post :create, { locale: :pt, id: contribution.id, use_route: 'catarse_pagarme' }
|
21
21
|
}.to raise_error('invalid user')
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
context 'with an user' do
|
26
|
-
let(:user) {
|
26
|
+
let(:user) { payment.user }
|
27
27
|
|
28
28
|
context 'with valid bank account data' do
|
29
29
|
before do
|
30
30
|
post :create, {
|
31
|
-
locale: :pt,
|
31
|
+
locale: :pt, id: contribution.id, use_route: 'catarse_pagarme',
|
32
32
|
user: { bank_account_attributes: {
|
33
33
|
bank_id: Bank.first.id, agency: '1', agency_digit: '1', account: '1', account_digit: '1', owner_name: 'foo', owner_document: '1'
|
34
34
|
} } }
|
@@ -44,10 +44,10 @@ describe CatarsePagarme::SlipController do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
context 'with invalid bank account data' do
|
47
|
-
let(:user) {
|
47
|
+
let(:user) { payment.user }
|
48
48
|
|
49
49
|
before do
|
50
|
-
post :create, { locale: :pt,
|
50
|
+
post :create, { locale: :pt, id: contribution.id, use_route: 'catarse_pagarme', user: { bank_account_attributes: { owner_name: '' } } }
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'boleto_url should be nil' do
|
@@ -2,28 +2,5 @@ class Contribution < ActiveRecord::Base
|
|
2
2
|
belongs_to :user
|
3
3
|
belongs_to :project
|
4
4
|
has_many :payment_notifications
|
5
|
-
|
6
|
-
def confirmed?
|
7
|
-
false
|
8
|
-
end
|
9
|
-
|
10
|
-
def update_current_billing_info
|
11
|
-
end
|
12
|
-
|
13
|
-
def confirm!
|
14
|
-
true
|
15
|
-
end
|
16
|
-
alias :confirm :confirm!
|
17
|
-
|
18
|
-
def waiting_confirmation?
|
19
|
-
end
|
20
|
-
|
21
|
-
def waiting
|
22
|
-
end
|
23
|
-
|
24
|
-
def cancel
|
25
|
-
end
|
26
|
-
|
27
|
-
def canceled?
|
28
|
-
end
|
5
|
+
has_many :payments
|
29
6
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class Payment < ActiveRecord::Base
|
2
|
+
has_many :payment_notifications
|
3
|
+
belongs_to :contribution
|
4
|
+
delegate :user, :project, to: :contribution
|
5
|
+
|
6
|
+
validates_presence_of :state, :key, :gateway, :payment_method, :value, :installments, :installment_value
|
7
|
+
validate :value_should_be_equal_or_greater_than_pledge
|
8
|
+
|
9
|
+
before_validation do
|
10
|
+
self.key ||= SecureRandom.uuid
|
11
|
+
self.value ||= self.contribution.try(:value)
|
12
|
+
self.state = 'pending' # mock initial state for here we do not include the stat machine
|
13
|
+
end
|
14
|
+
|
15
|
+
def value_should_be_equal_or_greater_than_pledge
|
16
|
+
errors.add(:value, I18n.t("activerecord.errors.models.payment.attributes.value.invalid")) if self.contribution && self.value < self.contribution.value
|
17
|
+
end
|
18
|
+
|
19
|
+
def notification_template_for_failed_project
|
20
|
+
if slip_payment?
|
21
|
+
:contribution_project_unsuccessful_slip
|
22
|
+
else
|
23
|
+
:contribution_project_unsuccessful_credit_card
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def refunded?
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
def paid?
|
32
|
+
true
|
33
|
+
end
|
34
|
+
|
35
|
+
def refused?
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
def refuse
|
40
|
+
end
|
41
|
+
|
42
|
+
def pay
|
43
|
+
end
|
44
|
+
|
45
|
+
def refund
|
46
|
+
end
|
47
|
+
|
48
|
+
def credits?
|
49
|
+
self.gateway == 'Credits'
|
50
|
+
end
|
51
|
+
|
52
|
+
def slip_payment?
|
53
|
+
self.payment_method == 'BoletoBancario'
|
54
|
+
end
|
55
|
+
end
|
@@ -7,10 +7,10 @@ describe CatarsePagarme::ApplicationHelper do
|
|
7
7
|
CatarsePagarme.configuration.stub(:max_installments).and_return(6)
|
8
8
|
end
|
9
9
|
|
10
|
-
let(:
|
10
|
+
let(:payment) { create(:payment, value: 100) }
|
11
11
|
|
12
12
|
context "#installments_for_select" do
|
13
|
-
subject { installments_for_select(
|
13
|
+
subject { installments_for_select(payment) }
|
14
14
|
it { expect(subject.size).to eq(6) }
|
15
15
|
it { expect(subject[0][0]).to eq('1x $100.00 ') }
|
16
16
|
it { expect(subject[0][1]).to eq(1) }
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe CatarsePagarme::CreditCardTransaction do
|
4
|
-
let(:
|
4
|
+
let(:payment) { create(:payment, value: 100) }
|
5
5
|
|
6
6
|
let(:pagarme_transaction) {
|
7
7
|
double({
|
@@ -24,61 +24,61 @@ describe CatarsePagarme::CreditCardTransaction do
|
|
24
24
|
card_expiration_month: '10',
|
25
25
|
card_expiration_year: '19',
|
26
26
|
card_cvv: '434',
|
27
|
-
amount:
|
27
|
+
amount: payment.pagarme_delegator.value_for_transaction,
|
28
28
|
postback_url: 'http://test.foo',
|
29
29
|
installments: 1
|
30
30
|
}
|
31
31
|
end
|
32
32
|
|
33
|
-
let(:card_transaction) { CatarsePagarme::CreditCardTransaction.new(valid_attributes,
|
33
|
+
let(:card_transaction) { CatarsePagarme::CreditCardTransaction.new(valid_attributes, payment) }
|
34
34
|
|
35
35
|
before do
|
36
36
|
PagarMe::Transaction.stub(:new).and_return(pagarme_transaction)
|
37
|
-
CatarsePagarme::
|
37
|
+
CatarsePagarme::PaymentDelegator.any_instance.stub(:change_status_by_transaction).and_return(true)
|
38
38
|
CatarsePagarme.configuration.stub(:credit_card_tax).and_return(0.01)
|
39
39
|
end
|
40
40
|
|
41
41
|
describe '#charge!' do
|
42
42
|
describe 'with valid attributes' do
|
43
43
|
before do
|
44
|
-
|
44
|
+
payment.should_receive(:update_attributes).at_least(1).and_call_original
|
45
45
|
PagarMe::Transaction.should_receive(:find_by_id).with(pagarme_transaction.id).and_return(pagarme_transaction)
|
46
|
-
CatarsePagarme::
|
46
|
+
CatarsePagarme::PaymentDelegator.any_instance.should_receive(:change_status_by_transaction).with('paid')
|
47
47
|
|
48
48
|
card_transaction.charge!
|
49
|
-
|
49
|
+
payment.reload
|
50
50
|
end
|
51
51
|
|
52
|
-
it "should update
|
53
|
-
expect(
|
52
|
+
it "should update payment payment_id" do
|
53
|
+
expect(payment.gateway_id).to eq('abcd')
|
54
54
|
end
|
55
55
|
|
56
|
-
it "should update
|
57
|
-
expect(
|
56
|
+
it "should update payment payment_service_fee" do
|
57
|
+
expect(payment.gateway_fee.to_f).to eq(4.08)
|
58
58
|
end
|
59
59
|
|
60
|
-
it "should update
|
61
|
-
expect(
|
60
|
+
it "should update payment payment_method" do
|
61
|
+
expect(payment.gateway).to eq('Pagarme')
|
62
62
|
end
|
63
63
|
|
64
|
-
it "should update
|
65
|
-
expect(
|
64
|
+
it "should update payment installments" do
|
65
|
+
expect(payment.installments).to eq(3)
|
66
66
|
end
|
67
67
|
|
68
|
-
it "should update
|
69
|
-
expect(
|
68
|
+
it "should update payment payment_choice" do
|
69
|
+
expect(payment.payment_method).to eq(CatarsePagarme::PaymentType::CREDIT_CARD)
|
70
70
|
end
|
71
71
|
|
72
|
-
it "should update
|
73
|
-
expect(
|
72
|
+
it "should update payment acquirer_name" do
|
73
|
+
expect(payment.gateway_data["acquirer_name"]).to eq('stone')
|
74
74
|
end
|
75
75
|
|
76
|
-
it "should update
|
77
|
-
expect(
|
76
|
+
it "should update payment acquirer_tid" do
|
77
|
+
expect(payment.gateway_data["acquirer_tid"]).to eq('123123')
|
78
78
|
end
|
79
79
|
|
80
|
-
it "should update
|
81
|
-
expect(
|
80
|
+
it "should update payment installment_value" do
|
81
|
+
expect(payment.installment_value).to_not be_nil
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|