catarse_pagarme 2.8.8 → 2.8.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ea6ec12b1dabcaa055aaa99e9253858e00f0300
4
- data.tar.gz: f2c5cf359952348edb0d01cc9fc03e122caf6830
3
+ metadata.gz: c2443b9ba729f2a1407ef3a274edba6082c76614
4
+ data.tar.gz: 833a7473cef82549a012993fbbac76a9e1ec666a
5
5
  SHA512:
6
- metadata.gz: 03a3a3dcb793054b7ddf0ccd9d5206902adc7f0dff1899fe3ab3fbd4b810eedb6589d7688749d55f7a433ef237bf32fdc1039e14de50f00f376e0b8498678532
7
- data.tar.gz: 9b04f03cf05df54269d993297043b6b31e2d82e7386909e3d4ef8f7a04b17ace92a114aeb34e0ea0d54f245ec3291de55aea47530e82cf531294fadbf880dec4
6
+ metadata.gz: eb7398738c1f43e71e9204a1cb581d44ee5824d6d78ea271512d96f93e1f1005aab5eb20efcc5b0e060880abde98375c5f016c93d9b2821f76f043aee90b5226
7
+ data.tar.gz: 734e501e94fc9a0917fa8f841aedcdfa043208b04f16e3e7f993c8b14d68cd18c7e9de45c8f573ae0ef3142b36a3f1240fceac1009c33a7853f51fb2dc159f17
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- catarse_pagarme (2.8.8)
4
+ catarse_pagarme (2.8.9)
5
5
  pagarme (= 1.10.0)
6
6
  rails (~> 4.0)
7
7
  sidekiq
@@ -68,8 +68,8 @@ GEM
68
68
  json (1.8.3)
69
69
  loofah (2.0.3)
70
70
  nokogiri (>= 1.5.9)
71
- mail (2.6.3)
72
- mime-types (>= 1.16, < 3)
71
+ mail (2.6.4)
72
+ mime-types (>= 1.16, < 4)
73
73
  method_source (0.8.2)
74
74
  mime-types (2.99.1)
75
75
  mini_portile (0.6.2)
@@ -118,38 +118,64 @@ App.views.Pagarme.addChild('PaymentCard', _.extend({
118
118
  return creditCard;
119
119
  },
120
120
 
121
- requestPayment: function(data){
122
- var that = this;
123
-
124
- $.ajax({
125
- type: 'POST',
126
- url: '/payment/pagarme/'+that.parent.contributionId+'/pay_credit_card',
127
- data: data,
128
- success: function(response){
129
- that.parent.loader.hide();
130
-
131
- if(response.payment_status == 'failed'){
132
- that.message.find('.message-text').html(response.message);
133
- that.message.slideDown('slow');
134
-
135
- $("#credit_card_submit").show();
136
- } else {
137
- var thank_you = $('#project_review').data('thank-you-path');
138
-
139
- if(thank_you){
140
- location.href = thank_you;
141
- } else {
142
- location.href = '/';
143
- }
144
- }
145
- }
121
+ reUpdateContributionData: function(callback) {
122
+ var contribution_data = {
123
+ anonymous: $('#review_form #contribution_anonymous').is(':checked'),
124
+ country_id: $('#review_form #contribution_country_id').val(),
125
+ payer_name: $('#review_form #contribution_payer_name').val(),
126
+ payer_email: $('#review_form #contribution_payer_email').val(),
127
+ payer_document: $('#review_form #contribution_payer_document').val(),
128
+ address_street: $('#review_form #contribution_address_street').val(),
129
+ address_number: $('#review_form #contribution_address_number').val(),
130
+ address_complement: $('#review_form #contribution_address_complement').val(),
131
+ address_neighbourhood: $('#review_form #contribution_address_neighbourhood').val(),
132
+ address_zip_code: $('#review_form #contribution_address_zip_code').val(),
133
+ address_city: $('#review_form #contribution_address_city').val(),
134
+ address_state: $('#review_form #contribution_address_state').val(),
135
+ address_phone_number: $('#review_form #contribution_address_phone_number').val()
136
+ };
137
+ $.post($('#review_form').data('update-info-path'), {
138
+ _method: 'put',
139
+ contribution: contribution_data
140
+ }).always(function(data) {
141
+ callback();
146
142
  });
143
+ },
147
144
 
145
+ requestPayment: function(data){
146
+ var that = this,
147
+ doCreditCardAjax = function() {
148
+ $.ajax({
149
+ type: 'POST',
150
+ url: '/payment/pagarme/'+that.parent.contributionId+'/pay_credit_card',
151
+ data: data,
152
+ success: function(response){
153
+ that.parent.loader.hide();
154
+
155
+ if(response.payment_status == 'failed'){
156
+ that.message.find('.message-text').html(response.message);
157
+ that.message.slideDown('slow');
158
+
159
+ $("#credit_card_submit").show();
160
+ } else {
161
+ var thank_you = $('#project_review').data('thank-you-path');
162
+
163
+ if(thank_you){
164
+ location.href = thank_you;
165
+ } else {
166
+ location.href = '/';
167
+ }
168
+ }
169
+ }
170
+ });
171
+ };
172
+
173
+ this.reUpdateContributionData(doCreditCardAjax);
148
174
  },
149
175
 
150
176
  onKeyupPaymentCardNumber: function(e){
151
177
  var number = $(e.currentTarget).val();
152
- this.$('#payment_card_flag').html(this.getCardFlag(number))
178
+ this.$('#payment_card_flag').html(this.getCardFlag(number));
153
179
  },
154
180
 
155
181
  getCardFlag: function(number) {
@@ -7,6 +7,8 @@ module CatarsePagarme
7
7
 
8
8
  render json: { payment_status: transaction.status }
9
9
  rescue PagarMe::PagarMeError => e
10
+ payment.destroy if payment.persisted? && !payment.gateway_id.present?
11
+
10
12
  render json: { payment_status: 'failed', message: e.message }
11
13
  end
12
14
 
@@ -3,10 +3,11 @@ module CatarsePagarme
3
3
 
4
4
  def installments_for_select(payment)
5
5
  installments = payment.pagarme_delegator.get_installments['installments']
6
+ project = payment.project
6
7
 
7
8
  collection = installments.map do |installment|
8
9
  installment_number = installment[0].to_i
9
- if installment_number <= CatarsePagarme.configuration.max_installments.to_i
10
+ if installment_number <= (project.try(:total_installments) || CatarsePagarme.configuration.max_installments.to_i)
10
11
  amount = installment[1]['installment_amount'] / 100.0
11
12
 
12
13
  optional_text = nil
@@ -0,0 +1,9 @@
1
+ module CatarsePagarme::BalanceTransferConcern
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ def pagarme_delegator
6
+ CatarsePagarme::BalanceTransferDelegator.new(self)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,55 @@
1
+ module CatarsePagarme
2
+ class BalanceTransferDelegator
3
+ attr_accessor :balance_transfer, :transfer
4
+
5
+ def initialize(balance_transfer)
6
+ configure_pagarme
7
+ self.balance_transfer = balance_transfer
8
+ end
9
+
10
+ def transfer_funds
11
+ ActiveRecord::Base.transaction do
12
+ raise "unable to create transfer, need to be authorized" if !balance_transfer.authorized?
13
+
14
+ bank_account = PagarMe::BankAccount.new(bank_account_attributes.delete(:bank_account))
15
+ bank_account.create
16
+ raise "unable to create an bank account" unless bank_account.id.present?
17
+
18
+ transfer = PagarMe::Transfer.new({
19
+ bank_account_id: bank_account.id,
20
+ amount: value_for_transaction
21
+ })
22
+ transfer.create
23
+ raise "unable to create a transfer" unless transfer.id.present?
24
+
25
+ balance_transfer.update_attribute(:transfer_id, transfer.id)
26
+ balance_transfer.transition_to(:processing, transfer_data: transfer.to_hash)
27
+ balance_transfer
28
+ end
29
+ end
30
+
31
+ def bank_account_attributes
32
+ account = balance_transfer.project.account
33
+
34
+ {
35
+ bank_account: {
36
+ bank_code: (account.bank.code || account.bank.name),
37
+ agencia: account.agency,
38
+ agencia_dv: account.agency_digit,
39
+ conta: account.account,
40
+ conta_dv: account.account_digit,
41
+ legal_name: account.owner_name,
42
+ document_number: account.owner_document
43
+ }
44
+ }
45
+ end
46
+
47
+ def configure_pagarme
48
+ ::PagarMe.api_key = CatarsePagarme.configuration.api_key
49
+ end
50
+
51
+ def value_for_transaction
52
+ (self.balance_transfer.amount * 100).to_i
53
+ end
54
+ end
55
+ end
@@ -12,10 +12,10 @@ module CatarsePagarme
12
12
  })
13
13
  payment.save!
14
14
 
15
- VerifyPagarmeWorker.perform_in(5.minutes, payment.key)
16
-
17
15
  self.transaction.charge
18
16
 
17
+ VerifyPagarmeWorker.perform_in(5.minutes, payment.key)
18
+
19
19
  change_payment_state
20
20
 
21
21
  if self.transaction.status == 'refused'
@@ -4,6 +4,7 @@ module CatarsePagarme
4
4
 
5
5
  config.to_prepare do
6
6
  ::Payment.send(:include, CatarsePagarme::PaymentConcern)
7
+ ::BalanceTransfer.send(:include, CatarsePagarme::BalanceTransferConcern)
7
8
  end
8
9
  end
9
10
  end
@@ -1,3 +1,3 @@
1
1
  module CatarsePagarme
2
- VERSION = "2.8.8"
2
+ VERSION = "2.8.9"
3
3
  end
@@ -8,7 +8,12 @@ describe CatarsePagarme::CreditCardsController, type: :controller do
8
8
 
9
9
  let(:project) { create(:project, goal: 10_000, state: 'online') }
10
10
  let(:contribution) { create(:contribution, value: 10, project: project) }
11
- let(:payment) { contribution.payments.first }
11
+ let(:payment) do
12
+ contribution.reload
13
+ p = contribution.payments.first
14
+ #p.update_attributes({ state: 'pending', gateway_id: nil })
15
+ p
16
+ end
12
17
 
13
18
  describe 'pay with credit card' do
14
19
  context 'without an user' do
@@ -22,9 +27,11 @@ describe CatarsePagarme::CreditCardsController, type: :controller do
22
27
  end
23
28
 
24
29
  context 'with an user' do
25
- let(:user) { payment.user }
30
+ let(:user) { contribution.user }
31
+
26
32
  context "with valid card data" do
27
33
  before do
34
+ contribution.payments.destroy_all
28
35
  allow(CatarsePagarme::CreditCardTransaction).to receive(:new).and_call_original
29
36
  post :create, {
30
37
  locale: :pt, id: contribution.id,
@@ -38,10 +45,16 @@ describe CatarsePagarme::CreditCardsController, type: :controller do
38
45
  it 'and payment_status is not failed' do
39
46
  expect(ActiveSupport::JSON.decode(response.body)['payment_status']).not_to eq('failed')
40
47
  end
48
+
49
+ it 'should persist payment record with references' do
50
+ expect(payment.persisted?).to eq(true)
51
+ expect(payment.gateway_id).not_to be_nil
52
+ end
41
53
  end
42
54
 
43
55
  context 'with invalid card data' do
44
56
  before do
57
+ contribution.payments.destroy_all
45
58
  post :create, {
46
59
  locale: :pt, id: contribution.id, card_hash: "abcd" }
47
60
  end
@@ -49,22 +62,27 @@ describe CatarsePagarme::CreditCardsController, type: :controller do
49
62
  it 'payment_status should be failed' do
50
63
  expect(ActiveSupport::JSON.decode(response.body)['payment_status']).to eq('failed')
51
64
  end
65
+
66
+ it 'destroy payment record when not created insied gateway' do
67
+ contribution.reload
68
+ expect(contribution.payments.size).to eq(0)
69
+ #expect(contribution.payments.first.persisted?).to eq(false)
70
+ end
52
71
  end
53
72
 
54
73
  context "when charges fails" do
55
74
  before do
75
+ contribution.payments.destroy_all
56
76
  allow_any_instance_of(PagarMe::Transaction).to receive(:charge).and_raise(PagarMe::PagarMeError)
57
77
  post :create, {
58
78
  locale: :pt, id: contribution.id,
59
79
  card_hash: sample_card_hash }
60
80
  end
61
81
 
62
- it "should have created a payment" do
63
- expect(contribution.payments.size).to eq(2)
64
- end
65
-
66
- it "should update the payment key on the local database" do
67
- expect(contribution.payments.last.key).to_not be_nil
82
+ it 'destroy payment record when not created insied gateway' do
83
+ contribution.reload
84
+ expect(contribution.payments.size).to eq(0)
85
+ #expect(contribution.payments.first.persisted?).to eq(false)
68
86
  end
69
87
  end
70
88
  end
@@ -0,0 +1,4 @@
1
+ class BalanceTransfer < ActiveRecord::Base
2
+ belongs_to :project
3
+ belongs_to :user
4
+ end
@@ -10,7 +10,7 @@ class Payment < ActiveRecord::Base
10
10
  before_validation do
11
11
  generate_key
12
12
  self.value ||= self.contribution.try(:value)
13
- self.state = 'pending' # mock initial state for here we do not include the stat machine
13
+ self.state ||= 'pending' # mock initial state for here we do not include the stat machine
14
14
  end
15
15
 
16
16
  def slip_expiration_date
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe CatarsePagarme::BalanceTransferDelegator do
4
+ let(:project) { create(:project, state: 'successful') }
5
+ let(:project_acc) { create(:project_account, project: project) }
6
+ let(:balance_transfer) { create(:balance_transfer, amount: 10, user: project.user, project: project)}
7
+ let(:delegator) { balance_transfer.pagarme_delegator }
8
+
9
+ before do
10
+ allow(CatarsePagarme).to receive(:configuration).and_return(double('fake config', {
11
+ slip_tax: 2.00,
12
+ credit_card_tax: 0.01,
13
+ pagarme_tax: 0.0063,
14
+ cielo_tax: 0.038,
15
+ stone_tax: 0.0307,
16
+ stone_installment_tax: 0.0307,
17
+ credit_card_cents_fee: 0.39,
18
+ api_key: '',
19
+ interest_rate: 0
20
+ }))
21
+ end
22
+
23
+ describe "instance of CatarsePagarme::BalanceTransferDelegator" do
24
+ it { expect(delegator).to be_a CatarsePagarme::BalanceTransferDelegator}
25
+ end
26
+
27
+ describe "#value_for_transaction" do
28
+ subject { delegator.value_for_transaction }
29
+
30
+ it "should convert balance value to pagarme value format" do
31
+ expect(subject).to eq(1000)
32
+ end
33
+ end
34
+
35
+ describe "#transfer_funds" do
36
+ let(:transfer_mock) { double(create: true, id: "123", foo: false, to_hash: {id: '123'}, to_json: {id: '123'}.to_json) }
37
+ let(:bank_acc_mock) { double(create: true, id: "1234")}
38
+ before do
39
+ allow(PagarMe::BankAccount).to receive(:new).and_return(bank_acc_mock)
40
+ allow(PagarMe::Transfer).to receive(:new).and_return(transfer_mock)
41
+ end
42
+
43
+ context "when transfer is not authorized?" do
44
+ before do
45
+ allow(balance_transfer).to receive(:authorized?).and_return(false)
46
+ end
47
+
48
+ it do
49
+ expect { delegator.transfer_funds }.to raise_error('unable to create transfer, need to be authorized')
50
+ end
51
+ end
52
+
53
+ context "when transfer is authorized?" do
54
+ before do
55
+ allow(balance_transfer).to receive(:authorized?).and_return(true)
56
+ allow(balance_transfer).to receive(:transition_to).and_return(true)
57
+ expect(balance_transfer).to receive(:transition_to).with(:processing, transfer_data: transfer_mock.to_hash)
58
+ end
59
+
60
+ it do
61
+ transfer = delegator.transfer_funds
62
+
63
+ expect(transfer.transfer_id).to eq(transfer_mock.id)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -143,7 +143,7 @@ describe CatarsePagarme::PaymentDelegator do
143
143
  let(:transfer_mock) { double(create: true, id: "123", to_json: {id: '123'}.to_json) }
144
144
  before do
145
145
  allow(PagarMe::Transfer).to receive(:new).and_return(transfer_mock)
146
- create(:bank_account, user: payment.user)
146
+ create(:bank_account, user: payment.user, bank: create(:bank, code: '237'))
147
147
  end
148
148
 
149
149
  it do
@@ -40,6 +40,19 @@ FactoryGirl.define do
40
40
  f.code { generate(:serial) }
41
41
  end
42
42
 
43
+ factory :balance_transfer do |f|
44
+ f.association :user
45
+ f.association :project
46
+ f.amount 100
47
+ end
48
+
49
+ factory :balance_transaction do |f|
50
+ f.association :user
51
+ f.association :project
52
+ f.amount 100
53
+ f.event_name 'foo'
54
+ end
55
+
43
56
  factory :bank_account do |f|
44
57
  f.association :bank
45
58
  f.account '25334'
@@ -85,7 +98,7 @@ FactoryGirl.define do
85
98
  end
86
99
  after :build do |project|
87
100
  project.account = build(:project_account, project: nil)
88
- project.rewards.build(deliver_at: Time.now, minimum_value: 10, description: 'test')
101
+ project.rewards.build(deliver_at: 1.year.from_now, minimum_value: 10, description: 'test')
89
102
  end
90
103
  end
91
104
 
@@ -118,6 +131,12 @@ FactoryGirl.define do
118
131
  factory :project_account do |f|
119
132
  f.association :project
120
133
  f.association :bank
134
+ f.account '25334'
135
+ f.account_digit '2'
136
+ f.agency '1432'
137
+ f.agency_digit '2'
138
+ f.owner_name 'Lorem amenori'
139
+ f.owner_document '11111111111'
121
140
  f.email "foo@bar.com"
122
141
  f.address_zip_code "foo"
123
142
  f.address_neighbourhood "foo"
@@ -126,12 +145,6 @@ FactoryGirl.define do
126
145
  f.address_number "foo"
127
146
  f.address_street "foo"
128
147
  f.phone_number "1234"
129
- f.agency "fooo"
130
- f.agency_digit "foo"
131
- f.owner_document "foo"
132
- f.owner_name "foo"
133
- f.account "1"
134
- f.account_digit "1000"
135
148
  f.account_type "foo"
136
149
  end
137
150
 
@@ -155,13 +168,13 @@ FactoryGirl.define do
155
168
 
156
169
  factory :payment do |f|
157
170
  f.association :contribution
158
- f.gateway 'pagarme'
171
+ f.gateway 'Pagarme'
159
172
  f.value 10.00
160
173
  f.state 'paid'
161
174
  f.installment_value nil
162
175
  f.payment_method "CartaoDeCredito"
163
176
  after :build do |payment|
164
- payment.gateway = 'pagarme'
177
+ payment.gateway = 'Pagarme'
165
178
  end
166
179
  end
167
180
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: catarse_pagarme
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.8
4
+ version: 2.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antônio Roberto Silva
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-30 00:00:00.000000000 Z
12
+ date: 2016-04-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -150,6 +150,8 @@ files:
150
150
  - app/controllers/catarse_pagarme/pagarme_controller.rb
151
151
  - app/controllers/catarse_pagarme/slip_controller.rb
152
152
  - app/helpers/catarse_pagarme/application_helper.rb
153
+ - app/models/catarse_pagarme/balance_transfer_concern.rb
154
+ - app/models/catarse_pagarme/balance_transfer_delegator.rb
153
155
  - app/models/catarse_pagarme/bank_account_concern.rb
154
156
  - app/models/catarse_pagarme/credit_card_transaction.rb
155
157
  - app/models/catarse_pagarme/fee_calculator_concern.rb
@@ -186,6 +188,7 @@ files:
186
188
  - spec/dummy/app/helpers/application_helper.rb
187
189
  - spec/dummy/app/mailers/.keep
188
190
  - spec/dummy/app/models/.keep
191
+ - spec/dummy/app/models/balance_transfer.rb
189
192
  - spec/dummy/app/models/bank.rb
190
193
  - spec/dummy/app/models/bank_account.rb
191
194
  - spec/dummy/app/models/category.rb
@@ -237,6 +240,7 @@ files:
237
240
  - spec/dummy/public/favicon.ico
238
241
  - spec/dummy/spec/support/testimg.png
239
242
  - spec/helpers/catarse_pagarme/application_helper_spec.rb
243
+ - spec/models/catarse_pagarme/balance_transfer_delegator_spec.rb
240
244
  - spec/models/catarse_pagarme/bank_account_concern_spec.rb
241
245
  - spec/models/catarse_pagarme/credit_card_transaction_spec.rb
242
246
  - spec/models/catarse_pagarme/payment_delegator_spec.rb
@@ -282,6 +286,7 @@ test_files:
282
286
  - spec/dummy/app/helpers/application_helper.rb
283
287
  - spec/dummy/app/mailers/.keep
284
288
  - spec/dummy/app/models/.keep
289
+ - spec/dummy/app/models/balance_transfer.rb
285
290
  - spec/dummy/app/models/bank.rb
286
291
  - spec/dummy/app/models/bank_account.rb
287
292
  - spec/dummy/app/models/category.rb
@@ -333,6 +338,7 @@ test_files:
333
338
  - spec/dummy/public/favicon.ico
334
339
  - spec/dummy/spec/support/testimg.png
335
340
  - spec/helpers/catarse_pagarme/application_helper_spec.rb
341
+ - spec/models/catarse_pagarme/balance_transfer_delegator_spec.rb
336
342
  - spec/models/catarse_pagarme/bank_account_concern_spec.rb
337
343
  - spec/models/catarse_pagarme/credit_card_transaction_spec.rb
338
344
  - spec/models/catarse_pagarme/payment_delegator_spec.rb