catarse_pagarme 2.4.10 → 2.5.0

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: 167056c394c65f4907196b570a631367b03fe471
4
- data.tar.gz: 6dedfa0adf8a6b398e5967d320851adcc21eb637
3
+ metadata.gz: e52946e7b76e2534a9d25953d63cb5630f6ef1a5
4
+ data.tar.gz: 824db7ed69c85c3f83bee7c916e7ebee07184b98
5
5
  SHA512:
6
- metadata.gz: 0230b0a36302dace3afd5bdbeb2cc5e27a021260e096950b062353d5ec50479ae80eae3f74308b5e993e1ea00d8444dfa53df690d492a9f9d866ccc1965ccd58
7
- data.tar.gz: f59f6667a1237763d93fc3dfec731562eb355a89f666777e677a4f1a82a1ea4735ffdc96593169f69fca1631d6645d6f29290e5d28680939ebecc5736b0f7322
6
+ metadata.gz: c7db17756e8ff0b80f92ad14e31e2ad05f0fde8e4be4bb0bb6e58363703730ebd08a2dcb8cb2e760394e5568657db7c8196e27e3c4c4ac47b5f4d330bc8eb690
7
+ data.tar.gz: ce404a55c7b605940d757e36e6199edd28d07127c7593b90330447fc2624a8daef2634c10d7e1897f28af3535f2aad063ba6847331f818aa0fef0ff3b6a63dbd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- catarse_pagarme (2.4.10)
4
+ catarse_pagarme (2.5.0)
5
5
  pagarme (~> 1.9.8)
6
6
  rails (~> 4.0)
7
7
 
@@ -51,7 +51,7 @@ GEM
51
51
  treetop (~> 1.4.8)
52
52
  mime-types (1.25.1)
53
53
  minitest (5.4.0)
54
- multi_json (1.10.1)
54
+ multi_json (1.11.0)
55
55
  netrc (0.10.3)
56
56
  pagarme (1.9.9)
57
57
  multi_json
data/README.md CHANGED
@@ -24,6 +24,7 @@ create an (config/initializers/pagarme.rb) and configure with:
24
24
  ```
25
25
  CatarsePagarme.configure do |config|
26
26
  config.api_key = "API_KEY"
27
+ config.ecr_key = "ENCRYPTION KEY"
27
28
  config.slip_tax = "Slip payment tax"
28
29
  config.credit_card_tax = "Credit card transaction tax don't need to define the 0.39"
29
30
  config.interest_rate = "Interest rate for installments"
@@ -35,28 +35,6 @@ App.views.Pagarme.addChild('PaymentCard', _.extend({
35
35
  this.$('#payment_card_source').payment('formatCardCVC');
36
36
  },
37
37
 
38
- getUrl: function(){
39
- var that = this;
40
- var url = '';
41
-
42
- if(that.$('input#payment_save_card').prop('checked') || that.hasSelectedSomeCard()) {
43
- url = '/payment/pagarme/'+that.parent.contributionId+'/pay_with_subscription';
44
- } else {
45
- url = '/payment/pagarme/'+that.parent.contributionId+'/pay_credit_card';
46
- }
47
-
48
- return url;
49
- },
50
-
51
- getAjaxType: function() {
52
- var type = 'POST';
53
-
54
- if(this.hasSelectedSomeCard()) {
55
- type = 'PUT'
56
- }
57
- return type;
58
- },
59
-
60
38
  selectedCard: function() {
61
39
  return this.$('input:radio[data-stored][name=payment_subscription_card]:checked');
62
40
  },
@@ -65,26 +43,6 @@ App.views.Pagarme.addChild('PaymentCard', _.extend({
65
43
  return this.selectedCard().length > 0;
66
44
  },
67
45
 
68
- getPaymentData: function() {
69
- var data = {};
70
-
71
- if(this.hasSelectedSomeCard()) {
72
- data = {
73
- subscription_id: this.selectedCard().val(),
74
- payment_card_installments: this.getInstallments() }
75
- } else {
76
- data = {
77
- payment_card_number: this.$('input#payment_card_number').val(),
78
- payment_card_name: this.$('input#payment_card_name').val(),
79
- payment_card_date: this.$('input#payment_card_date').val(),
80
- payment_card_source: this.$('input#payment_card_source').val(),
81
- payment_card_installments: this.getInstallments()
82
- }
83
- }
84
-
85
- return data;
86
- },
87
-
88
46
  getInstallments: function() {
89
47
  if(this.hasSelectedSomeCard()) {
90
48
  return this.$('.my-credit-cards .selected select#payment_card_installments').val();
@@ -104,18 +62,77 @@ App.views.Pagarme.addChild('PaymentCard', _.extend({
104
62
  $(e.currentTarget).hide();
105
63
  that.parent.loader.show();
106
64
 
65
+ if( that.hasSelectedSomeCard() ) {
66
+ that.requestPayment({
67
+ card_id: this.selectedCard().val(),
68
+ payment_card_installments: that.getInstallments()
69
+ });
70
+ } else {
71
+ PagarMe.encryption_key = this.$('.pagarme-e-key').data('key');
72
+
73
+ var creditCard = this.newCreditCard();
74
+ var fieldErrors = creditCard.fieldErrors();
75
+
76
+ if(_.keys(fieldErrors).length > 0) {
77
+ this.displayErrors(fieldErrors);
78
+ } else {
79
+ this.generateCardHash(creditCard);
80
+ }
81
+ }
82
+
83
+ return false;
84
+ },
85
+
86
+ generateCardHash: function(creditCard){
87
+ var that = this;
88
+ creditCard.generateHash(function(cardHash) {
89
+ that.requestPayment({
90
+ card_hash: cardHash,
91
+ payment_card_installments: that.getInstallments(),
92
+ save_card: that.$('input#payment_save_card').is(':checked')
93
+ });
94
+ });
95
+ },
96
+
97
+ displayErrors: function(errors){
98
+ var msg = [];
99
+ this.parent.loader.hide();
100
+
101
+ $.each(errors, function(i, value){
102
+ msg.push(value)
103
+ });
104
+
105
+ this.message.find('.message-text').html(msg.join("<br/>"));
106
+ this.message.slideDown('slow');
107
+
108
+ $("#credit_card_submit").show();
109
+ },
110
+
111
+ newCreditCard: function(){
112
+ var creditCard = new PagarMe.creditCard();
113
+ creditCard.cardHolderName = this.$('input#payment_card_name').val();
114
+ creditCard.cardExpirationMonth = $.trim(this.$('input#payment_card_date').val().split('/')[0]);
115
+ creditCard.cardExpirationYear = $.trim(this.$('input#payment_card_date').val().split('/')[1]);
116
+ creditCard.cardNumber = this.$('input#payment_card_number').val();
117
+ creditCard.cardCVV = this.$('input#payment_card_source').val();
118
+ return creditCard;
119
+ },
120
+
121
+ requestPayment: function(data){
122
+ var that = this;
123
+
107
124
  $.ajax({
108
- type: that.getAjaxType(),
109
- url: that.getUrl(),
110
- data: that.getPaymentData(),
125
+ type: 'POST',
126
+ url: '/payment/pagarme/'+that.parent.contributionId+'/pay_credit_card',
127
+ data: data,
111
128
  success: function(response){
112
129
  that.parent.loader.hide();
113
130
 
114
131
  if(response.payment_status == 'failed'){
115
132
  that.message.find('.message-text').html(response.message);
116
- that.message.slideDown('slow')
133
+ that.message.slideDown('slow');
117
134
 
118
- $(e.currentTarget).show();
135
+ $("#credit_card_submit").show();
119
136
  } else {
120
137
  var thank_you = $('#project_review').data('thank-you-path');
121
138
 
@@ -127,6 +144,7 @@ App.views.Pagarme.addChild('PaymentCard', _.extend({
127
144
  }
128
145
  }
129
146
  });
147
+
130
148
  },
131
149
 
132
150
  onKeyupPaymentCardNumber: function(e){
@@ -5,7 +5,6 @@ module CatarsePagarme
5
5
 
6
6
  before_filter :authenticate_user!
7
7
  before_filter :configure_pagarme
8
- skip_before_filter :force_http
9
8
  layout :false
10
9
 
11
10
  protected
@@ -1,8 +1,55 @@
1
1
  module CatarsePagarme
2
- class CreditCardsController < CreditCardBaseController
2
+ class CreditCardsController < CatarsePagarme::ApplicationController
3
3
 
4
4
  def create
5
- charge_with_class CreditCardTransaction
5
+ transaction = CreditCardTransaction.new(credit_card_attributes, contribution).charge!
6
+
7
+ render json: { payment_status: transaction.status }
8
+ rescue Exception => e
9
+ render json: { payment_status: 'failed', message: e.message }
10
+ end
11
+
12
+ protected
13
+
14
+ def credit_card_attributes
15
+ hash = {
16
+ payment_method: 'credit_card',
17
+ amount: delegator.value_with_installment_tax(get_installment),
18
+ postback_url: ipn_pagarme_index_url(host: CatarsePagarme.configuration.host,
19
+ subdomain: CatarsePagarme.configuration.subdomain,
20
+ protocol: CatarsePagarme.configuration.protocol),
21
+ installments: get_installment,
22
+ customer: {
23
+ email: contribution.user.email,
24
+ name: contribution.user.name
25
+ },
26
+ metadata: {
27
+ key: contribution.key
28
+ }
29
+ }
30
+
31
+ if params[:card_hash].present?
32
+ hash[:card_hash] = params[:card_hash]
33
+ else
34
+ hash[:card_id] = params[:card_id]
35
+ end
36
+
37
+ if params[:save_card] === "true"
38
+ hash[:save_card] = true
39
+ end
40
+
41
+ hash
6
42
  end
43
+
44
+ def get_installment
45
+ if contribution.value.to_f < CatarsePagarme.configuration.minimum_value_for_installment.to_f
46
+ 1
47
+ elsif params[:payment_card_installments].to_i > 0
48
+ params[:payment_card_installments].to_i
49
+ else
50
+ 1
51
+ end
52
+ end
53
+
7
54
  end
8
55
  end
@@ -1,7 +1,6 @@
1
1
  module CatarsePagarme
2
2
  class NotificationsController < CatarsePagarme::ApplicationController
3
3
  skip_before_filter :authenticate_user!
4
- skip_before_filter :force_http
5
4
 
6
5
  def create
7
6
  if contribution
@@ -1,15 +1,31 @@
1
1
  module CatarsePagarme
2
2
  class CreditCardTransaction < TransactionBase
3
+
3
4
  def charge!
5
+ save_card = self.attributes.delete(:save_card)
6
+
4
7
  self.transaction = PagarMe::Transaction.new(self.attributes)
5
8
  self.transaction.charge
6
9
 
10
+ change_contribution_state
11
+
7
12
  if self.transaction.status == 'refused'
8
13
  raise ::PagarMe::PagarMeError.new(I18n.t('projects.contributions.edit.transaction_error'))
9
14
  end
10
15
 
11
- change_contribution_state
16
+ save_user_credit_card if save_card
12
17
  self.transaction
13
18
  end
19
+
20
+ def save_user_credit_card
21
+ card = self.transaction.card
22
+
23
+ credit_card = self.user.credit_cards.find_or_initialize_by(card_key: card.id)
24
+ credit_card.last_digits = card.last_digits
25
+ credit_card.card_brand = card.brand
26
+
27
+ credit_card.save!
28
+ end
29
+
14
30
  end
15
31
  end
@@ -1,7 +1,7 @@
1
1
  module CatarsePagarme
2
2
  class TransactionBase
3
3
  attr_accessor :attributes, :contribution,
4
- :transaction, :user, :subscription
4
+ :transaction, :user
5
5
 
6
6
  def initialize(attributes, contribution)
7
7
  self.attributes = attributes
@@ -1,6 +1,6 @@
1
- = javascript_include_tag 'catarse_pagarme'
2
- #catarse_pagarme_form
1
+ = javascript_include_tag 'catarse_pagarme', 'https://pagar.me/assets/pagarme-v2.min.js'
3
2
 
3
+ #catarse_pagarme_form
4
4
  .next_step_after_valid_document
5
5
  .w-row.list_payment.w-form
6
6
  .w-col.w-col-7.w-col-small-7.back-payment-moip-options-column
@@ -16,13 +16,14 @@
16
16
 
17
17
  #payment_type_credit_card_section.payment_section
18
18
  = form_tag 'javascript:void(0);', :class => 'pagarme' do
19
+ .pagarme-e-key data-key=CatarsePagarme.configuration.ecr_key
19
20
  div[class="my-credit-cards w-form back-payment-form-creditcard records-choice #{(current_user.credit_cards.present? ? '' : 'w-hidden')}"]
20
21
  - current_user.credit_cards.each_with_index do |credit_card, i|
21
- = label_tag "payment_subscription_card_#{credit_card.subscription_id}" do
22
+ = label_tag "payment_subscription_card_#{credit_card.card_key}" do
22
23
  .w-row.creditcard-records
23
24
  .w-col.w-col-1
24
25
  .w-radio.w-clearfix.back-payment-credit-card-radio-field
25
- = radio_button_tag :payment_subscription_card, credit_card.subscription_id, i == 0, {"data-stored" => true}
26
+ = radio_button_tag :payment_subscription_card, credit_card.card_key, i == 0, {"data-stored" => true}
26
27
  .w-col.w-col-2
27
28
  .fontsize-small.fontweight-semibold.text-success
28
29
  = credit_card.card_brand.upcase
data/config/routes.rb CHANGED
@@ -6,8 +6,6 @@ CatarsePagarme::Engine.routes.draw do
6
6
  put :second_slip, to: 'slip#update'
7
7
  post :pay_credit_card, to: 'credit_cards#create'
8
8
  post :pay_slip, to: 'slip#create'
9
- post :pay_with_subscription, to: 'subscriptions#create'
10
- put :pay_with_subscription, to: 'subscriptions#update'
11
9
  end
12
10
 
13
11
  collection do
@@ -3,10 +3,11 @@ module CatarsePagarme
3
3
  attr_accessor :api_key, :slip_tax, :credit_card_tax, :interest_rate, :host, :subdomain, :protocol,
4
4
  :max_installments, :minimum_value_for_installment, :credit_card_cents_fee, :pagarme_tax, :stone_tax,
5
5
  :cielo_tax, :cielo_installment_diners_tax, :cielo_installment_not_diners_tax,
6
- :cielo_installment_amex_tax, :cielo_installment_not_amex_tax
6
+ :cielo_installment_amex_tax, :cielo_installment_not_amex_tax, :ecr_key
7
7
 
8
8
  def initialize
9
9
  self.api_key = ''
10
+ self.ecr_key = ''
10
11
  self.slip_tax = 0
11
12
  self.credit_card_tax = 0
12
13
  self.interest_rate = 0
@@ -4,7 +4,6 @@ module CatarsePagarme
4
4
 
5
5
  config.to_prepare do
6
6
  ::Contribution.send(:include, CatarsePagarme::ContributionConcern)
7
- ::CreditCard.send(:include, CatarsePagarme::CreditCardConcern)
8
7
  end
9
8
  end
10
9
  end
@@ -1,3 +1,3 @@
1
1
  module CatarsePagarme
2
- VERSION = "2.4.10"
2
+ VERSION = "2.5.0"
3
3
  end
@@ -8,7 +8,6 @@ describe CatarsePagarme::CreditCardsController 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(:credit_card) { create(:credit_card, subscription_id: '1542')}
12
11
 
13
12
  describe 'pay with credit card' do
14
13
  context 'without an user' do
@@ -27,13 +26,7 @@ describe CatarsePagarme::CreditCardsController do
27
26
  before do
28
27
  post :create, {
29
28
  locale: :pt, project_id: project.id, contribution_id: contribution.id, use_route: 'catarse_pagarme',
30
- payment_card_date: '10/17', payment_card_number: '4012888888881881', payment_card_name: 'Foo bar',
31
- payment_card_source: '574', payment_card_installments: '1' }
32
-
33
- end
34
-
35
- it 'payment_status should be filled' do
36
- expect(ActiveSupport::JSON.decode(response.body)['payment_status']).not_to be_nil
29
+ card_hash: sample_card_hash }
37
30
  end
38
31
 
39
32
  it 'and payment_status is not failed' do
@@ -44,18 +37,12 @@ describe CatarsePagarme::CreditCardsController do
44
37
  context 'with invalid card data' do
45
38
  before do
46
39
  post :create, {
47
- locale: :pt, project_id: project.id, contribution_id: contribution.id, use_route: 'catarse_pagarme',
48
- payment_card_date: '', payment_card_name: '',
49
- payment_card_source: '143', payment_card_installments: '1' }
40
+ locale: :pt, project_id: project.id, contribution_id: contribution.id, use_route: 'catarse_pagarme', card_hash: "abcd" }
50
41
  end
51
42
 
52
43
  it 'payment_status should be failed' do
53
44
  expect(ActiveSupport::JSON.decode(response.body)['payment_status']).to eq('failed')
54
45
  end
55
-
56
- it 'message should be filled' do
57
- expect(ActiveSupport::JSON.decode(response.body)['message']).not_to be_nil
58
- end
59
46
  end
60
47
  end
61
48
  end
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,7 @@ ENV["RAILS_ENV"] ||= 'test'
4
4
  require File.expand_path("../dummy/config/environment", __FILE__)
5
5
 
6
6
  require 'pagarme'
7
+ require 'open-uri'
7
8
  require 'rspec/rails'
8
9
  require 'factory_girl'
9
10
  # Requires supporting ruby files with custom matchers and macros, etc,
@@ -34,3 +35,12 @@ RSpec.configure do |config|
34
35
  PaymentEngines.stub(:configuration).and_return({})
35
36
  end
36
37
  end
38
+
39
+ def sample_card_hash
40
+ r = open("https://api.pagar.me/1/transactions/card_hash_key/?encryption_key=ek_test_zwAwnRvqD1c9GUERQ7oAP4FPyN9o2v").read
41
+ json = ActiveSupport::JSON.decode(r)
42
+ public_key = OpenSSL::PKey::RSA.new(json["public_key"])
43
+ encode_string = Base64.encode64(public_key.public_encrypt("card_number=4901720080344448&card_holder_name=Usuario de Teste&card_expiration_date=1220&card_cvv=314"))
44
+
45
+ "#{json['id']}_#{encode_string}"
46
+ end
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.4.10
4
+ version: 2.5.0
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: 2015-03-02 00:00:00.000000000 Z
12
+ date: 2015-03-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -103,24 +103,18 @@ files:
103
103
  - app/assets/javascripts/catarse_pagarme/payment_slip.js
104
104
  - app/assets/stylesheets/catarse_pagarme/application.css
105
105
  - app/controllers/catarse_pagarme/application_controller.rb
106
- - app/controllers/catarse_pagarme/credit_card_base_controller.rb
107
106
  - app/controllers/catarse_pagarme/credit_cards_controller.rb
108
107
  - app/controllers/catarse_pagarme/notifications_controller.rb
109
108
  - app/controllers/catarse_pagarme/pagarme_controller.rb
110
109
  - app/controllers/catarse_pagarme/slip_controller.rb
111
- - app/controllers/catarse_pagarme/subscriptions_controller.rb
112
110
  - app/helpers/catarse_pagarme/application_helper.rb
113
111
  - app/models/catarse_pagarme/bank_account_concern.rb
114
112
  - app/models/catarse_pagarme/contribution_concern.rb
115
113
  - app/models/catarse_pagarme/contribution_delegator.rb
116
- - app/models/catarse_pagarme/credit_card_concern.rb
117
- - app/models/catarse_pagarme/credit_card_delegator.rb
118
114
  - app/models/catarse_pagarme/credit_card_transaction.rb
119
115
  - app/models/catarse_pagarme/fee_calculator_concern.rb
120
116
  - app/models/catarse_pagarme/payment_type.rb
121
- - app/models/catarse_pagarme/save_credit_card_transaction.rb
122
117
  - app/models/catarse_pagarme/slip_transaction.rb
123
- - app/models/catarse_pagarme/subscription_transaction.rb
124
118
  - app/models/catarse_pagarme/transaction_base.rb
125
119
  - app/views/catarse_pagarme/pagarme/_terms.html.slim
126
120
  - app/views/catarse_pagarme/pagarme/review.html.slim
@@ -139,7 +133,6 @@ files:
139
133
  - spec/controllers/catarse_pagarme/notifications_controller_spec.rb
140
134
  - spec/controllers/catarse_pagarme/pagarme_controller_spec.rb
141
135
  - spec/controllers/catarse_pagarme/slip_controller_spec.rb
142
- - spec/controllers/catarse_pagarme/subscriptions_controller_spec.rb
143
136
  - spec/dummy/README.rdoc
144
137
  - spec/dummy/Rakefile
145
138
  - spec/dummy/app/assets/images/.keep
@@ -226,7 +219,6 @@ test_files:
226
219
  - spec/controllers/catarse_pagarme/notifications_controller_spec.rb
227
220
  - spec/controllers/catarse_pagarme/pagarme_controller_spec.rb
228
221
  - spec/controllers/catarse_pagarme/slip_controller_spec.rb
229
- - spec/controllers/catarse_pagarme/subscriptions_controller_spec.rb
230
222
  - spec/dummy/README.rdoc
231
223
  - spec/dummy/Rakefile
232
224
  - spec/dummy/app/assets/images/.keep
@@ -1,53 +0,0 @@
1
- module CatarsePagarme
2
- class CreditCardBaseController < CatarsePagarme::ApplicationController
3
-
4
- protected
5
-
6
- def charge_with_class(kclass)
7
- transaction = kclass.new(credit_card_attributes, contribution).charge!
8
-
9
- render json: { payment_status: transaction.status }
10
- rescue Exception => e
11
- render json: { payment_status: 'failed', message: e.message }
12
- end
13
-
14
- def splited_month_and_year
15
- params[:payment_card_date].split('/')
16
- rescue
17
- [0, 0]
18
- end
19
-
20
- def get_installment
21
- if contribution.value.to_f < CatarsePagarme.configuration.minimum_value_for_installment.to_f
22
- 1
23
- elsif params[:payment_card_installments].to_i > 0
24
- params[:payment_card_installments].to_i
25
- else
26
- 1
27
- end
28
- end
29
-
30
- def credit_card_attributes
31
- {
32
- payment_method: 'credit_card',
33
- card_number: params[:payment_card_number],
34
- card_holder_name: params[:payment_card_name],
35
- card_expiration_month: splited_month_and_year[0],
36
- card_expiration_year: splited_month_and_year[1],
37
- card_cvv: params[:payment_card_source],
38
- amount: delegator.value_with_installment_tax(get_installment),
39
- postback_url: ipn_pagarme_index_url(host: CatarsePagarme.configuration.host,
40
- subdomain: CatarsePagarme.configuration.subdomain,
41
- protocol: CatarsePagarme.configuration.protocol),
42
- installments: get_installment,
43
- customer: {
44
- email: contribution.user.email,
45
- name: contribution.user.name
46
- },
47
- metadata: {
48
- key: contribution.key
49
- }
50
- }
51
- end
52
- end
53
- end
@@ -1,27 +0,0 @@
1
- module CatarsePagarme
2
- class SubscriptionsController < CreditCardBaseController
3
- def create
4
- charge_with_class SaveCreditCardTransaction
5
- end
6
-
7
- def update
8
- charge_with_class SubscriptionTransaction
9
- end
10
-
11
- protected
12
-
13
- def credit_card_attributes
14
- attributes = super
15
-
16
- if has_subscription?
17
- attributes.update({subscription_id: params[:subscription_id]})
18
- end
19
-
20
- attributes
21
- end
22
-
23
- def has_subscription?
24
- params[:subscription_id].present?
25
- end
26
- end
27
- end
@@ -1,9 +0,0 @@
1
- module CatarsePagarme::CreditCardConcern
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- def pagarme_delegator
6
- CatarsePagarme::CreditCardDelegator.new(self)
7
- end
8
- end
9
- end
@@ -1,26 +0,0 @@
1
- module CatarsePagarme
2
- class CreditCardDelegator
3
- attr_accessor :credit_card, :subscription
4
-
5
- def initialize(credit_card)
6
- configure_pagarme
7
- self.credit_card = credit_card
8
- end
9
-
10
- def cancel_subscription
11
- get_subscription
12
- self.subscription.cancel
13
- end
14
-
15
- def get_subscription
16
- self.subscription ||= ::PagarMe::Subscription.find_by_id(self.credit_card.subscription_id)
17
- end
18
-
19
-
20
- protected
21
-
22
- def configure_pagarme
23
- ::PagarMe.api_key = CatarsePagarme.configuration.api_key
24
- end
25
- end
26
- end
@@ -1,33 +0,0 @@
1
- module CatarsePagarme
2
- class SaveCreditCardTransaction < TransactionBase
3
- def charge!
4
- installments = self.attributes.delete(:installments)
5
- amount = self.attributes.delete(:amount)
6
-
7
- self.subscription = PagarMe::Subscription.new(self.attributes)
8
- self.subscription.create
9
-
10
- self.subscription.charge(amount, installments)
11
- self.transaction = subscription.current_transaction
12
-
13
- save_user_credit_card
14
- change_contribution_state
15
-
16
-
17
- self.transaction
18
- end
19
-
20
- protected
21
-
22
- def save_user_credit_card
23
- credit_cards = self.user.credit_cards
24
- unless credit_cards.where(subscription_id: self.subscription.id.to_s).present?
25
- credit_cards.create!({
26
- last_digits: self.subscription.card_last_digits,
27
- card_brand: self.subscription.card_brand,
28
- subscription_id: self.subscription.id
29
- })
30
- end
31
- end
32
- end
33
- end
@@ -1,21 +0,0 @@
1
- module CatarsePagarme
2
- class SubscriptionTransaction < TransactionBase
3
- def charge!
4
- validate_subscription_id
5
-
6
- self.subscription = PagarMe::Subscription.find_by_id(self.attributes[:subscription_id])
7
- self.subscription.charge(self.attributes[:amount], self.attributes[:installments])
8
- self.transaction = subscription.current_transaction
9
-
10
- change_contribution_state
11
-
12
- self.transaction
13
- end
14
-
15
- def validate_subscription_id
16
- unless self.user.credit_cards.where(subscription_id: self.attributes[:subscription_id]).present?
17
- raise PagarMe::PagarMeError.new('invalid subscription')
18
- end
19
- end
20
- end
21
- end
@@ -1,95 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CatarsePagarme::SubscriptionsController do
4
- before do
5
- PaymentEngines.stub(:find_payment).and_return(contribution)
6
- controller.stub(:current_user).and_return(user)
7
- end
8
-
9
- let(:project) { create(:project, goal: 10_000, state: 'online') }
10
- let(:contribution) { create(:contribution, value: 10, project: project) }
11
- let(:credit_card) { create(:credit_card, subscription_id: '1542')}
12
-
13
- describe "pay with a saved credit card" do
14
- context 'without an user' do
15
- let(:user) { nil }
16
-
17
- it 'should raise a error' do
18
- expect {
19
- post :create, { locale: :pt, project_id: project.id, contribution_id: contribution.id, use_route: 'catarse_pagarme' }
20
- }.to raise_error('invalid user')
21
- end
22
- end
23
-
24
- context 'with an user' do
25
- let(:user) { contribution.user }
26
- context 'when subscription_id not associated with contribution user' do
27
- before do
28
- put :update, { locale: :pt, subscription_id: '1542', project_id: project.id, contribution_id: contribution.id, use_route: 'catarse_pagarme' }
29
- end
30
-
31
- it 'payment_status should be failed' do
32
- expect(ActiveSupport::JSON.decode(response.body)['payment_status']).to eq('failed')
33
- end
34
-
35
- it 'message should be filled' do
36
- expect(ActiveSupport::JSON.decode(response.body)['message']).to eq('invalid subscription')
37
- end
38
- end
39
- end
40
- end
41
-
42
- describe 'pay and save credit card' do
43
- context 'without an user' do
44
- let(:user) { nil }
45
-
46
- it 'should raise a error' do
47
- expect {
48
- post :create, { locale: :pt, project_id: project.id, contribution_id: contribution.id, use_route: 'catarse_pagarme' }
49
- }.to raise_error('invalid user')
50
- end
51
- end
52
-
53
- context "with an user" do
54
- let(:user) { contribution.user }
55
-
56
- context 'with invalid card data' do
57
- before do
58
- post :create, {
59
- locale: :pt, project_id: project.id, contribution_id: contribution.id, use_route: 'catarse_pagarme',
60
- payment_card_date: '', payment_card_name: '',
61
- payment_card_source: '143', payment_card_installments: '1' }
62
- end
63
-
64
- it 'payment_status should be failed' do
65
- expect(ActiveSupport::JSON.decode(response.body)['payment_status']).to eq('failed')
66
- end
67
-
68
- it 'message should be filled' do
69
- expect(ActiveSupport::JSON.decode(response.body)['message']).not_to be_nil
70
- end
71
- end
72
-
73
- context "with valid card data" do
74
- before do
75
- post :create, {
76
- locale: :pt, project_id: project.id, contribution_id: contribution.id, use_route: 'catarse_pagarme',
77
- payment_card_date: '10/17', payment_card_number: '4901720080344448', payment_card_name: 'Foo bar',
78
- payment_card_source: '143', payment_card_installments: '1' }
79
- end
80
-
81
- it "should save card data into user" do
82
- expect(user.credit_cards.count).to eq(1)
83
- end
84
-
85
- it 'payment_status should be filled and not failed' do
86
- expect(ActiveSupport::JSON.decode(response.body)['payment_status']).not_to be_nil
87
- end
88
-
89
- it 'and payment_status is not failed' do
90
- expect(ActiveSupport::JSON.decode(response.body)['payment_status']).not_to eq('failed')
91
- end
92
- end
93
- end
94
- end
95
- end