Skip to content
Merged
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
2 changes: 1 addition & 1 deletion app/admin/payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
f.input :transaction_id, as: :hidden, :input_html => { value: DateTime.now.iso8601 + "_" + current_admin_user.email } # DateTime.now.iso8601 + current_admin_user.email
f.input :total_amount, label: "Total amount in $" # 10000 => 100.00
f.input :transaction_date, as: :datepicker # DateTime.now.iso8601
f.input :account_type, collection: ['scholarship', 'special', 'other']
f.input :account_type, collection: ['scholarship', 'special', 'other'], required: true
f.input :result_code, as: :hidden, :input_html => { value: "Manually Entered" } # 'Manually Entered'
f.input :result_message, as: :hidden, :input_html => { value: "This was manually entered by #{current_admin_user.email}" }
f.input :timestamp, as: :hidden, :input_html => { value: DateTime.now.strftime("%Q").to_i }
Expand Down
9 changes: 7 additions & 2 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Payment < ApplicationRecord
validates :transaction_id, presence: true, uniqueness: true
validates :total_amount, presence: true
validates :transaction_date, presence: true
validates :account_type, presence: true, if: :manual_entry?
belongs_to :user
validate :manual_payment_decimal
validate :valid_transaction_date
Expand All @@ -46,8 +47,12 @@ def self.ransackable_attributes(auth_object = nil)

scope :current_conference_payments, -> { where(arel_table[:conf_year].eq(ApplicationSetting.get_current_app_year)) }

def manual_entry?
transaction_type == "ManuallyEntered"
end
Comment thread
rsmoke marked this conversation as resolved.

def manual_payment_decimal
if self.transaction_type == "ManuallyEntered"
if manual_entry?
if self.total_amount !~ /^\s*[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$/
errors.add(:total_amount, "must be decimal")
elsif self.total_amount.to_f < 0
Expand All @@ -67,7 +72,7 @@ def valid_transaction_date
end

def check_manual_amount
if self.transaction_type == "ManuallyEntered"
if manual_entry?
self.total_amount = (self.total_amount.to_f * 100).to_s
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/controllers/payments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@
expect(payment.payer_identity).to eq(user.email)
expect(payment.conf_year).to eq(ApplicationSetting.get_current_app_year)
end

it 'creates a payment even when account_type is missing from gateway params' do
expect {
post :payment_receipt, params: valid_params.except(:transactionAcountType)
}.to change(Payment, :count).by(1)

payment = Payment.last
expect(payment.account_type).to be_nil
expect(payment.transaction_type).to eq('Credit')
expect(payment.transaction_id).to eq('test_transaction_123')
end
end

context 'when transaction_id already exists' do
Expand Down
22 changes: 22 additions & 0 deletions spec/models/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@
expect(payment).not_to be_valid
end

context 'when transaction_type is ManuallyEntered and account_type is present' do
it 'is valid' do
payment = build(:payment, :manual, account_type: 'scholarship')
expect(payment).to be_valid
end
end

context 'when transaction_type is ManuallyEntered and account_type is missing' do
it 'is not valid' do
payment = build(:payment, :manual, account_type: nil)
expect(payment).not_to be_valid
expect(payment.errors[:account_type]).to include("can't be blank")
end
end

Comment thread
rsmoke marked this conversation as resolved.
context 'when transaction_type is not ManuallyEntered' do
it 'is valid without an account_type' do
payment = build(:payment, transaction_type: 'Credit', account_type: nil)
expect(payment).to be_valid
end
end

context 'when transaction_type is ManuallyEntered' do
it 'validates that total_amount is a decimal' do
payment = build(:payment, transaction_type: 'ManuallyEntered', total_amount: 'not_a_number')
Expand Down
Loading