Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ GEM
activesupport (>= 5.0)
i18n (1.8.11)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
jbuilder (2.11.3)
activesupport (>= 5.0.0)
listen (3.7.0)
rb-fsevent (~> 0.10, >= 0.10.3)
Expand Down Expand Up @@ -193,7 +193,7 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.3)
rubocop (1.22.3)
rubocop (1.23.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
Expand Down Expand Up @@ -228,11 +228,11 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
spring (3.0.0)
spring (3.1.0)
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.3.0)
sprockets-rails (3.4.1)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
Expand All @@ -247,7 +247,7 @@ GEM
unicode-display_width (2.1.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.1.0)
web-console (4.2.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/api/v1/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Api::V1::ApiController < ActionController::API
rescue_from ActiveRecord::ActiveRecordError, with: :render_generic_error
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
ActiveRecord::Base.include_root_in_json = true
before_action :authenticate_company!


private

def authenticate_company!
@company = Company.find_by(token: request.headers['companyToken'])
render_not_authorized if @company.nil?
end

def render_not_authorized
render status: 401, json: { message: 'Há algo errado com sua autenticação.' }
end

def render_not_found(e)
render status: 404, json: { message: 'Objeto não encontrado' }
end

def render_generic_error(e)
render status: 500, json: { message: 'Erro geral' }
end
end
40 changes: 40 additions & 0 deletions app/controllers/api/v1/customer_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Api::V1::CustomerController < Api::V1::ApiController

def index
@customers = Customer.all.where(company: @company)

render status:200, json: @customers.as_json( except: %i[id company_id
created_at updated_at],
include: { company: { only: :legal_name } } )
end

def show
@customer = Customer.find_by(token: params[:id])
raise ActiveRecord::RecordNotFound if @customer.nil?

return render_not_authorized if @customer.company != @company

render status: :ok, json: @customer.as_json(except: %i[id company_id
created_at updated_at],
include: { company: { only: :legal_name } })
end

def create
@customer = @company.customers.create(customer_params)

if @customer.save
render status: :created, json: @customer.as_json(except: %i[id company_id created_at updated_at],
include: { company: { only: :legal_name } })
else
render status: :unprocessable_entity, json: { message: 'Requisição inválida', errors: @customer.errors,
request: @customer.as_json(except: %i[id token company_id
created_at updated_at]) }
end
end


private
def customer_params
params.require(:customer).permit(:name, :cpf)
end
end
77 changes: 77 additions & 0 deletions app/controllers/api/v1/customer_payment_method_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module Api
module V1
class CustomerPaymentMethodController < Api::V1::ApiController
def index
@customer_payment_method = CustomerPaymentMethod.where(company: @company)

render status: :ok, json: success_json
end

def show
@customer_payment_method = CustomerPaymentMethod.find_by(token: params[:id], company: @company)
raise ActiveRecord::RecordNotFound if @customer_payment_method.nil?

render status: :ok, json: success_json
end

def create
@payment_method = find_payment_method
@customer = Customer.find_by(token: customer_payment_method_params[:customer_token], company: @company)

@customer_payment_method = CustomerPaymentMethod.new(
payment_method: @payment_method, customer: @customer, company: @company
)
@customer_payment_method.add_credit_card(credit_card_params) if @payment_method&.credit_card?

return render status: :created, json: success_json if @customer_payment_method.save

render status: :unprocessable_entity, json: error_json
end

private

def customer_payment_method_params
params.require(:customer_payment_method).permit(:customer_token, :payment_method_token)
end

def credit_card_params
params.require(:customer_payment_method).permit(
:credit_card_name, :credit_card_number,
:credit_card_expiration_date, :credit_card_security_code
)
end

def find_payment_method
payment_setting = @company.find_enabled_payment_setting_by_token(
customer_payment_method_params[:payment_method_token]
)
payment_setting&.payment_method
end

def success_json
@customer_payment_method.as_json(
only: %i[token],
include: {
payment_method: { only: %i[name type_of] },
customer: { only: %i[token] },
company: { only: %i[legal_name] }
}
)
end

def error_json
{
message: 'Requisição inválida', errors: @customer_payment_method.errors,
# TODO: passar entrada do cartão de crédito de volta?
request: @customer_payment_method.as_json(
only: %i[],
include: {
payment_method: { only: %i[name type_of] },
customer: { only: %i[token] }, company: { only: %i[legal_name] }
}
)
}
end
end
end
end
3 changes: 3 additions & 0 deletions app/models/boleto_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class BoletoSetting < ApplicationRecord
belongs_to :company
belongs_to :payment_method, -> { where(type_of: :boleto) }, inverse_of: :boleto_settings

enum status: { enabled: 5, disabled: 10 }

validates :agency_number, :account_number, :bank_code, presence: true
validates :bank_code, bank_code: true
after_create :generate_token_attribute
end
12 changes: 12 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ def payment_settings
pix_settings + credit_card_settings + boleto_settings
end

def find_enabled_payment_setting_by_token(token)
payment_settings.find do |ps|
ps.enabled? && ps.payment_method.enabled? && ps.token == token
end
end

# TODO: remover se não for utilizado
def list_payment_methods
pix_settings.map(&:payment_method) + credit_card_settings.map(&:payment_method) +
boleto_settings.map(&:payment_method)
end

def blank_all_info!
self.cnpj = ''
self.legal_name = ''
Expand Down
2 changes: 2 additions & 0 deletions app/models/credit_card_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ class CreditCardSetting < ApplicationRecord
belongs_to :company
belongs_to :payment_method, -> { where(type_of: :credit_card) }, inverse_of: :credit_card_settings

enum status: { enabled: 5, disabled: 10 }

validates :company_code, presence: true
end
8 changes: 8 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Customer < ApplicationRecord
belongs_to :company

after_create :generate_token_attribute

validates :name, presence: true
validates :cpf, presence: true
end
35 changes: 35 additions & 0 deletions app/models/customer_payment_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class CustomerPaymentMethod < ApplicationRecord
belongs_to :payment_method
belongs_to :company
belongs_to :customer

after_create :generate_token_attribute

validates :credit_card_name, :credit_card_number, :credit_card_expiration_date,
:credit_card_security_code, presence: true, if: -> { payment_method&.credit_card? }

validate :expiration_date_cannot_be_in_the_past, if: -> { payment_method&.credit_card? }

enum name: { pix: 5, boleto: 10, cartao_de_credito: 15 }

# TODO: testar esse metodo?
def add_credit_card(params)
return unless payment_method&.credit_card?

self.credit_card_name = params[:credit_card_name]
self.credit_card_number = params[:credit_card_number]
self.credit_card_expiration_date = params[:credit_card_expiration_date]
self.credit_card_security_code = params[:credit_card_security_code]
end

private

def expiration_date_cannot_be_in_the_past
return unless credit_card_expiration_date.present? && credit_card_expiration_date < Time.zone.today

errors.add(:credit_card_name, 'inválido(a)')
errors.add(:credit_card_number, 'inválido(a)')
errors.add(:credit_card_expiration_date, 'inválido(a)')
errors.add(:credit_card_security_code, 'inválido(a)')
end
end
2 changes: 2 additions & 0 deletions app/models/pix_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class PixSetting < ApplicationRecord
belongs_to :company
belongs_to :payment_method, -> { where(type_of: :pix) }, inverse_of: :pix_settings

enum status: { enabled: 5, disabled: 10 }

validates :pix_key, :bank_code, presence: true
validates :bank_code, bank_code: true
end
10 changes: 10 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@
resources :boleto_settings, only: %i[new create]

resources :credit_card_settings, only: %i[new create]

namespace :api do
namespace :v1 do
resources :customer, only: %i[index show create]
resources :pix_settings, only: %i[index show]
resources :boleto_settings, only: %i[index show]
resources :credit_card_settings, only: %i[index show]
resources :customer_payment_method, only: %i[index show create]
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20211124234405_add_token_to_pix_setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTokenToPixSetting < ActiveRecord::Migration[6.1]
def change
add_column :pix_settings, :token, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20211124234501_add_token_to_boleto_setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTokenToBoletoSetting < ActiveRecord::Migration[6.1]
def change
add_column :boleto_settings, :token, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20211124234511_add_token_to_credit_card_setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTokenToCreditCardSetting < ActiveRecord::Migration[6.1]
def change
add_column :credit_card_settings, :token, :string
end
end
12 changes: 12 additions & 0 deletions db/migrate/20211125160101_create_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateCustomers < ActiveRecord::Migration[6.1]
def change
create_table :customers do |t|
t.string :name
t.string :cpf
t.string :token
t.references :company, null: false, foreign_key: true

t.timestamps
end
end
end
16 changes: 16 additions & 0 deletions db/migrate/20211125201854_create_customer_payment_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateCustomerPaymentMethods < ActiveRecord::Migration[6.1]
def change
create_table :customer_payment_methods do |t|
t.references :payment_method, null: false, foreign_key: true
t.string :credit_card_name
t.string :credit_card_number
t.date :credit_card_expiration_date
t.string :credit_card_security_code
t.references :company, null: false, foreign_key: true
t.references :customer, null: false, foreign_key: true
t.string :token

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20211126165220_add_status_to_pix_setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToPixSetting < ActiveRecord::Migration[6.1]
def change
add_column :pix_settings, :status, :integer, default: 5
end
end
5 changes: 5 additions & 0 deletions db/migrate/20211127004210_add_status_to_boleto_setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToBoletoSetting < ActiveRecord::Migration[6.1]
def change
add_column :boleto_settings, :status, :integer, default: 5
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToCreditCardSetting < ActiveRecord::Migration[6.1]
def change
add_column :credit_card_settings, :status, :integer, default: 5
end
end
Loading