catarse_pagarme 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +120 -0
- data/MIT-LICENSE +20 -0
- data/README.md +35 -0
- data/Rakefile +34 -0
- data/app/assets/images/catarse_pagarme/.keep +0 -0
- data/app/assets/javascripts/catarse_pagarme.js +5 -0
- data/app/assets/javascripts/catarse_pagarme/application.js +12 -0
- data/app/assets/javascripts/catarse_pagarme/credit_card.js +142 -0
- data/app/assets/javascripts/catarse_pagarme/payment_choice.js +22 -0
- data/app/assets/javascripts/catarse_pagarme/payment_slip.js +63 -0
- data/app/assets/stylesheets/catarse_pagarme/application.css +15 -0
- data/app/controllers/catarse_pagarme/application_controller.rb +51 -0
- data/app/controllers/catarse_pagarme/credit_card_base_controller.rb +48 -0
- data/app/controllers/catarse_pagarme/credit_cards_controller.rb +8 -0
- data/app/controllers/catarse_pagarme/notifications_controller.rb +20 -0
- data/app/controllers/catarse_pagarme/pagarme_controller.rb +10 -0
- data/app/controllers/catarse_pagarme/slip_controller.rb +43 -0
- data/app/controllers/catarse_pagarme/subscriptions_controller.rb +27 -0
- data/app/helpers/catarse_pagarme/application_helper.rb +23 -0
- data/app/models/catarse_pagarme/contribution_concern.rb +9 -0
- data/app/models/catarse_pagarme/contribution_delegator.rb +94 -0
- data/app/models/catarse_pagarme/credit_card_concern.rb +9 -0
- data/app/models/catarse_pagarme/credit_card_delegator.rb +26 -0
- data/app/models/catarse_pagarme/credit_card_transaction.rb +11 -0
- data/app/models/catarse_pagarme/payment_type.rb +6 -0
- data/app/models/catarse_pagarme/save_credit_card_transaction.rb +33 -0
- data/app/models/catarse_pagarme/slip_transaction.rb +37 -0
- data/app/models/catarse_pagarme/subscription_transaction.rb +21 -0
- data/app/models/catarse_pagarme/transaction_base.rb +36 -0
- data/app/views/catarse_pagarme/pagarme/review.html.slim +112 -0
- data/app/views/layouts/catarse_pagarme/application.html.erb +14 -0
- data/bin/rails +12 -0
- data/catarse_pagarme.gemspec +26 -0
- data/config/initializers/register.rb +5 -0
- data/config/routes.rb +15 -0
- data/lib/catarse_pagarme.rb +19 -0
- data/lib/catarse_pagarme/configuration.rb +16 -0
- data/lib/catarse_pagarme/engine.rb +10 -0
- data/lib/catarse_pagarme/payment_engine.rb +37 -0
- data/lib/catarse_pagarme/version.rb +3 -0
- data/lib/tasks/catarse_pagarme_tasks.rake +4 -0
- data/spec/controllers/catarse_pagarme/credit_cards_controller_spec.rb +62 -0
- data/spec/controllers/catarse_pagarme/notifications_controller_spec.rb +41 -0
- data/spec/controllers/catarse_pagarme/pagarme_controller_spec.rb +5 -0
- data/spec/controllers/catarse_pagarme/slip_controller_spec.rb +68 -0
- data/spec/controllers/catarse_pagarme/subscriptions_controller_spec.rb +95 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/bank_account.rb +5 -0
- data/spec/dummy/app/models/category.rb +2 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/contribution.rb +29 -0
- data/spec/dummy/app/models/credit_card.rb +3 -0
- data/spec/dummy/app/models/payment_engines.rb +2 -0
- data/spec/dummy/app/models/payment_notification.rb +3 -0
- data/spec/dummy/app/models/project.rb +5 -0
- data/spec/dummy/app/models/user.rb +13 -0
- data/spec/dummy/app/models/user_total.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +55 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +82 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/assets.rb +8 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/pagarme.rb +6 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/helpers/catarse_pagarme/application_helper_spec.rb +23 -0
- data/spec/models/catarse_pagarme/contribution_delegator_spec.rb +146 -0
- data/spec/models/catarse_pagarme/credit_card_transaction_spec.rb +70 -0
- data/spec/models/catarse_pagarme/slip_transaction_spec.rb +102 -0
- data/spec/spec_helper.rb +36 -0
- data/spec/support/factories.rb +70 -0
- metadata +280 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "pagarme"
|
2
|
+
|
3
|
+
module CatarsePagarme
|
4
|
+
class ApplicationController < ActionController::Base
|
5
|
+
|
6
|
+
before_filter :authenticate_user!
|
7
|
+
before_action :configure_pagarme
|
8
|
+
skip_before_filter :force_http
|
9
|
+
layout :false
|
10
|
+
|
11
|
+
protected
|
12
|
+
def configure_pagarme
|
13
|
+
PagarMe.api_key = CatarsePagarme.configuration.api_key
|
14
|
+
end
|
15
|
+
|
16
|
+
def authenticate_user!
|
17
|
+
unless defined?(current_user) && current_user
|
18
|
+
raise Exception.new('invalid user')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def permited_attrs(attributes)
|
23
|
+
attrs = ActionController::Parameters.new(attributes)
|
24
|
+
attrs.permit([
|
25
|
+
slip_payment: [:payment_method, :amount, :postback_url,
|
26
|
+
customer: [:name, :email]
|
27
|
+
],
|
28
|
+
user: [
|
29
|
+
bank_account_attributes: [
|
30
|
+
:name, :account, :account_digit, :agency,
|
31
|
+
:agency_digit, :owner_name, :owner_document
|
32
|
+
]
|
33
|
+
]
|
34
|
+
])
|
35
|
+
end
|
36
|
+
|
37
|
+
def contribution
|
38
|
+
conditions = {id: params[:id] }
|
39
|
+
|
40
|
+
unless params[:controller] == 'catarse_pagarme/notifications'
|
41
|
+
conditions.merge!({user_id: current_user.id})
|
42
|
+
end
|
43
|
+
|
44
|
+
@contribution ||= PaymentEngines.find_payment(conditions)
|
45
|
+
end
|
46
|
+
|
47
|
+
def delegator
|
48
|
+
contribution.pagarme_delegator
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module CatarsePagarme
|
2
|
+
class CreditCardBaseController < 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 < CatarsePagarme.configuration.minimum_value_for_installment
|
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_url(contribution),
|
40
|
+
installments: get_installment,
|
41
|
+
customer: {
|
42
|
+
email: contribution.user.email,
|
43
|
+
name: contribution.user.name
|
44
|
+
}
|
45
|
+
}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module CatarsePagarme
|
2
|
+
class NotificationsController < ApplicationController
|
3
|
+
skip_before_filter :authenticate_user!
|
4
|
+
|
5
|
+
def create
|
6
|
+
if PagarMe::validate_fingerprint(contribution.try(:payment_id), params[:fingerprint])
|
7
|
+
if contribution
|
8
|
+
contribution.payment_notifications.create(extra_data: params.to_json)
|
9
|
+
delegator.change_status_by_transaction(params[:current_status])
|
10
|
+
|
11
|
+
return render nothing: true, status: 200
|
12
|
+
end
|
13
|
+
|
14
|
+
return render nothing: true, status: 404
|
15
|
+
end
|
16
|
+
|
17
|
+
render nothing: true, status: 404
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module CatarsePagarme
|
2
|
+
class SlipController < CatarsePagarme::ApplicationController
|
3
|
+
|
4
|
+
def create
|
5
|
+
transaction = SlipTransaction.new(permitted_attributes, contribution).charge!
|
6
|
+
|
7
|
+
render json: { boleto_url: transaction.boleto_url, payment_status: transaction.status }
|
8
|
+
rescue PagarMe::PagarMeError => e
|
9
|
+
render json: { boleto_url: nil, payment_status: 'failed', message: e.message }
|
10
|
+
end
|
11
|
+
|
12
|
+
def update
|
13
|
+
transaction = SlipTransaction.new(permitted_attributes, contribution).charge!
|
14
|
+
render text: transaction.boleto_url
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def slip_attributes
|
20
|
+
{
|
21
|
+
payment_method: 'boleto',
|
22
|
+
amount: delegator.value_for_transaction,
|
23
|
+
postback_url: ipn_pagarme_url(contribution),
|
24
|
+
customer: {
|
25
|
+
email: contribution.user.email,
|
26
|
+
name: contribution.user.name
|
27
|
+
}
|
28
|
+
}.update({ user: params[:user] })
|
29
|
+
end
|
30
|
+
|
31
|
+
def permitted_attributes
|
32
|
+
attrs = ActionController::Parameters.new(slip_attributes)
|
33
|
+
attrs.permit(:payment_method, :amount, :postback_url, customer: [:name, :email],
|
34
|
+
user: [
|
35
|
+
bank_account_attributes: [
|
36
|
+
:name, :account, :account_digit, :agency,
|
37
|
+
:agency_digit, :owner_name, :owner_document
|
38
|
+
]
|
39
|
+
])
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,27 @@
|
|
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
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module CatarsePagarme
|
2
|
+
module ApplicationHelper
|
3
|
+
|
4
|
+
def installments_for_select(contribution)
|
5
|
+
installments = contribution.pagarme_delegator.get_installments['installments']
|
6
|
+
|
7
|
+
collection = installments.map do |installment|
|
8
|
+
installment_number = installment[0].to_i
|
9
|
+
if installment_number <= CatarsePagarme.configuration.max_installments
|
10
|
+
amount = installment[1]['installment_amount'] / 100.0
|
11
|
+
[format_instalment_text(installment_number, amount), installment_number]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
collection.compact
|
16
|
+
end
|
17
|
+
|
18
|
+
def format_instalment_text(number, amount)
|
19
|
+
[number, number_to_currency(amount, precision: 2)].join('x ')
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module CatarsePagarme
|
2
|
+
class ContributionDelegator
|
3
|
+
attr_accessor :contribution, :transaction
|
4
|
+
|
5
|
+
def initialize(contribution)
|
6
|
+
configure_pagarme
|
7
|
+
self.contribution = contribution
|
8
|
+
end
|
9
|
+
|
10
|
+
def change_status_by_transaction(transactin_status)
|
11
|
+
case transactin_status
|
12
|
+
when 'paid', 'authorized' then
|
13
|
+
self.contribution.confirm unless self.contribution.confirmed?
|
14
|
+
when 'refunded' then
|
15
|
+
self.contribution.refund unless self.contribution.refunded?
|
16
|
+
when 'refused' then
|
17
|
+
self.contribution.cancel unless self.contribution.canceled?
|
18
|
+
when 'waiting_payment', 'processing' then
|
19
|
+
self.contribution.waiting unless self.contribution.waiting_confirmation?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def refund
|
24
|
+
if contribution.is_credit_card?
|
25
|
+
transaction.refund
|
26
|
+
else
|
27
|
+
transaction.refund(bank_account_attributes)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def value_for_transaction
|
32
|
+
(self.contribution.value * 100).to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
def value_with_installment_tax(installment)
|
36
|
+
current_installment = get_installment(installment)
|
37
|
+
|
38
|
+
if current_installment.present?
|
39
|
+
current_installment['amount']
|
40
|
+
else
|
41
|
+
value_for_transaction
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def transaction
|
46
|
+
@transaction ||= ::PagarMe::Transaction.find_by_id(self.contribution.payment_id)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_installment(installment_number)
|
50
|
+
installment = get_installments['installments'].select do |installment|
|
51
|
+
!installment[installment_number.to_s].nil?
|
52
|
+
end
|
53
|
+
|
54
|
+
installment[installment_number.to_s]
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_installments
|
58
|
+
@installments ||= PagarMe::Transaction.calculate_installments({
|
59
|
+
amount: self.value_for_transaction,
|
60
|
+
interest_rate: CatarsePagarme.configuration.interest_rate
|
61
|
+
})
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_fee(payment_method)
|
65
|
+
if payment_method == PaymentType::SLIP
|
66
|
+
CatarsePagarme.configuration.slip_tax
|
67
|
+
else
|
68
|
+
(self.contribution.value * CatarsePagarme.configuration.credit_card_tax.to_f) + CatarsePagarme.configuration.credit_card_cents_fee
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
protected
|
73
|
+
|
74
|
+
def bank_account_attributes
|
75
|
+
bank = contribution.user.bank_account
|
76
|
+
|
77
|
+
{
|
78
|
+
bank_account: {
|
79
|
+
bank_code: (bank.bank_code || bank.name),
|
80
|
+
agencia: bank.agency,
|
81
|
+
agencia_dv: bank.agency_digit,
|
82
|
+
conta: bank.account,
|
83
|
+
conta_dv: bank.account_digit,
|
84
|
+
legal_name: bank.owner_name,
|
85
|
+
document_number: bank.owner_document
|
86
|
+
}
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
def configure_pagarme
|
91
|
+
::PagarMe.api_key = CatarsePagarme.configuration.api_key
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,26 @@
|
|
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
|
@@ -0,0 +1,33 @@
|
|
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
|