Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.
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
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class ApplicationController < ActionController::API
rescue_from ActionController::ParameterMissing, with: :render_bad_request
rescue_from ActiveRecord::RecordNotFound, with: :render_bad_request
rescue_from ArgumentError, with: :render_bad_request

private
def render_bad_request
render status: :bad_request
end
end
31 changes: 30 additions & 1 deletion app/controllers/transaction_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
class TransactionController < ApplicationController
before_action :validate_transaction_params, only: [:create]
before_action :set_credit_card, only: [:create]

def create
raise NotImplementedError
if @credit_card.status != 'active' || @credit_card.limit_cents < params[:amount]
return render status: :payment_required
end

ZippiMediator.submit_transaction(
amount_cents: params[:amount],
occurred_at: params[:datetime],
)

updated_limit = @credit_card.limit_cents - params[:amount]
@credit_card.update(limit_cents: updated_limit)

render status: :ok
end

private
def validate_transaction_params
params.require(%i[id amount datetime])

params[:amount] = (Float(params[:amount]) * 100).round
params[:datetime] = DateTime.
strptime(params[:datetime], "%d/%m/%Y %R").
change(:offset => "-0300")
end

def set_credit_card
@credit_card = CreditCard.find(params[:id])
end
end
2 changes: 1 addition & 1 deletion spec/requests/transaction_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
end

it 'for sending an unregistered id' do
post '/transaction', params: { amount: 1.99, datetime: '123', id: 1 }
post '/transaction', params: { amount: 1.99, datetime: '02/02/2019 10:01', id: 1 }
expect(response).to have_http_status(:bad_request)
end
end
Expand Down