mirror of
https://github.com/internetee/registry.git
synced 2025-06-14 00:24:44 +02:00
Create System / Admin payment log
This commit is contained in:
parent
6418924faf
commit
2645654aef
10 changed files with 122 additions and 41 deletions
|
@ -60,7 +60,7 @@ module Admin
|
||||||
end
|
end
|
||||||
|
|
||||||
def bind_invoices
|
def bind_invoices
|
||||||
@bank_statement.bind_invoices
|
@bank_statement.bind_invoices(manual: true)
|
||||||
|
|
||||||
flash[:notice] = t('invoices_were_fully_binded') if @bank_statement.fully_binded?
|
flash[:notice] = t('invoices_were_fully_binded') if @bank_statement.fully_binded?
|
||||||
flash[:warning] = t('invoices_were_partially_binded') if @bank_statement.partially_binded?
|
flash[:warning] = t('invoices_were_partially_binded') if @bank_statement.partially_binded?
|
||||||
|
|
|
@ -34,7 +34,7 @@ module Admin
|
||||||
end
|
end
|
||||||
|
|
||||||
def bind
|
def bind
|
||||||
if @bank_transaction.bind_invoice(params[:invoice_no])
|
if @bank_transaction.bind_invoice(params[:invoice_no], manual: true)
|
||||||
flash[:notice] = I18n.t('record_created')
|
flash[:notice] = I18n.t('record_created')
|
||||||
redirect_to [:admin, @bank_transaction]
|
redirect_to [:admin, @bank_transaction]
|
||||||
else
|
else
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BankStatement < ApplicationRecord
|
||||||
buyer_name: row[83, 35].strip,
|
buyer_name: row[83, 35].strip,
|
||||||
document_no: row[118, 8].strip,
|
document_no: row[118, 8].strip,
|
||||||
description: row[126, 140].strip,
|
description: row[126, 140].strip,
|
||||||
sum: BigDecimal.new(row[268, 12].strip) / BigDecimal.new('100.0'),
|
sum: BigDecimal(row[268, 12].strip) / BigDecimal('100.0'),
|
||||||
reference_no: row[280, 35].strip
|
reference_no: row[280, 35].strip
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -80,7 +80,9 @@ class BankStatement < ApplicationRecord
|
||||||
status == FULLY_BINDED
|
status == FULLY_BINDED
|
||||||
end
|
end
|
||||||
|
|
||||||
def bind_invoices
|
def bind_invoices(manual: false)
|
||||||
bank_transactions.unbinded.each(&:autobind_invoice)
|
bank_transactions.unbinded.each do |transaction|
|
||||||
|
transaction.autobind_invoice(manual: manual)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,7 @@ class BankTransaction < ApplicationRecord
|
||||||
|
|
||||||
def binded_invoice
|
def binded_invoice
|
||||||
return unless binded?
|
return unless binded?
|
||||||
|
|
||||||
account_activity.invoice
|
account_activity.invoice
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -31,28 +32,54 @@ class BankTransaction < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
# For successful binding, reference number, invoice id and sum must match with the invoice
|
# For successful binding, reference number, invoice id and sum must match with the invoice
|
||||||
def autobind_invoice
|
def autobind_invoice(manual: false)
|
||||||
return if binded?
|
return if binded?
|
||||||
return unless registrar
|
return unless registrar
|
||||||
return unless invoice
|
return unless invoice
|
||||||
return unless invoice.payable?
|
return unless invoice.payable?
|
||||||
|
|
||||||
create_activity(registrar, invoice)
|
channel = if manual
|
||||||
|
'admin_payment'
|
||||||
|
else
|
||||||
|
'system_payment'
|
||||||
|
end
|
||||||
|
record_system_payment(channel: channel, invoice: invoice, registrar: registrar)
|
||||||
end
|
end
|
||||||
|
|
||||||
def bind_invoice(invoice_no)
|
def record_system_payment(channel: nil, invoice:, registrar:)
|
||||||
|
if channel.nil?
|
||||||
|
create_activity(invoice.buyer, invoice)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
payment_order = PaymentOrder.create_with_type(type: channel, invoice: invoice)
|
||||||
|
payment_order.save!
|
||||||
|
|
||||||
|
if create_activity(registrar, invoice)
|
||||||
|
payment_order.paid!
|
||||||
|
else
|
||||||
|
payment_order.failed!
|
||||||
|
payment_order.notes = 'Failed to create activity'
|
||||||
|
payment_order.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def bind_invoice(invoice_no, manual: false)
|
||||||
if binded?
|
if binded?
|
||||||
errors.add(:base, I18n.t('transaction_is_already_binded'))
|
errors.add(:base, I18n.t('transaction_is_already_binded'))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
invoice = Invoice.find_by(number: invoice_no)
|
invoice = Invoice.find_by(number: invoice_no)
|
||||||
|
errors.add(:base, I18n.t('invoice_was_not_found')) unless invoice
|
||||||
|
validate_invoice_data(invoice)
|
||||||
|
return if errors.any?
|
||||||
|
|
||||||
unless invoice
|
record_system_payment(channel: (manual ? 'admin_payment' : nil), invoice: invoice,
|
||||||
errors.add(:base, I18n.t('invoice_was_not_found'))
|
registrar: invoice.buyer)
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validate_invoice_data(invoice)
|
||||||
if invoice.paid?
|
if invoice.paid?
|
||||||
errors.add(:base, I18n.t('invoice_is_already_binded'))
|
errors.add(:base, I18n.t('invoice_is_already_binded'))
|
||||||
return
|
return
|
||||||
|
@ -63,23 +90,19 @@ class BankTransaction < ApplicationRecord
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if invoice.total != sum
|
errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match')) if invoice.total != sum
|
||||||
errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match'))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
create_activity(invoice.buyer, invoice)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_activity(registrar, invoice)
|
def create_activity(registrar, invoice)
|
||||||
ActiveRecord::Base.transaction do
|
activity = AccountActivity.new(account: registrar.cash_account,
|
||||||
create_account_activity!(account: registrar.cash_account,
|
invoice: invoice, sum: invoice.subtotal,
|
||||||
invoice: invoice,
|
currency: currency, description: description,
|
||||||
sum: invoice.subtotal,
|
|
||||||
currency: currency,
|
|
||||||
description: description,
|
|
||||||
activity_type: AccountActivity::ADD_CREDIT)
|
activity_type: AccountActivity::ADD_CREDIT)
|
||||||
|
if activity.save
|
||||||
reset_pending_registrar_balance_reload
|
reset_pending_registrar_balance_reload
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,9 @@ class PaymentOrder < ApplicationRecord
|
||||||
|
|
||||||
PAYMENT_INTERMEDIARIES = ENV['payments_intermediaries'].to_s.strip.split(', ').freeze
|
PAYMENT_INTERMEDIARIES = ENV['payments_intermediaries'].to_s.strip.split(', ').freeze
|
||||||
PAYMENT_BANKLINK_BANKS = ENV['payments_banks'].to_s.strip.split(', ').freeze
|
PAYMENT_BANKLINK_BANKS = ENV['payments_banks'].to_s.strip.split(', ').freeze
|
||||||
PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS].flatten.freeze
|
INTERNAL_PAYMENT_METHODS = %w[admin_payment system_payment].freeze
|
||||||
|
PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS,
|
||||||
|
INTERNAL_PAYMENT_METHODS].flatten.freeze
|
||||||
|
|
||||||
belongs_to :invoice, optional: false
|
belongs_to :invoice, optional: false
|
||||||
|
|
||||||
|
|
9
app/models/payment_orders/admin_payment.rb
Normal file
9
app/models/payment_orders/admin_payment.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
module PaymentOrders
|
||||||
|
class AdminPayment < PaymentOrder
|
||||||
|
CONFIG_NAMESPACE = 'admin_payment'.freeze
|
||||||
|
|
||||||
|
def self.config_namespace_name
|
||||||
|
CONFIG_NAMESPACE
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
9
app/models/payment_orders/system_payment.rb
Normal file
9
app/models/payment_orders/system_payment.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
module PaymentOrders
|
||||||
|
class SystemPayment < PaymentOrder
|
||||||
|
CONFIG_NAMESPACE = 'system_payment'.freeze
|
||||||
|
|
||||||
|
def self.config_namespace_name
|
||||||
|
CONFIG_NAMESPACE
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -93,20 +93,6 @@ class BankLinkTest < ActiveSupport::TestCase
|
||||||
assert_equal(expected_response, @new_bank_link.form_fields)
|
assert_equal(expected_response, @new_bank_link.form_fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_correct_channel_is_assigned
|
|
||||||
swed_link = PaymentOrder.create_with_type(type: 'swed', invoice: @invoice)
|
|
||||||
assert_equal swed_link.channel, 'Swed'
|
|
||||||
assert_equal swed_link.class.config_namespace_name, 'swed'
|
|
||||||
|
|
||||||
seb_link = PaymentOrder.create_with_type(type: 'seb', invoice: @invoice)
|
|
||||||
assert_equal seb_link.channel, 'Seb'
|
|
||||||
assert_equal seb_link.class.config_namespace_name, 'seb'
|
|
||||||
|
|
||||||
lhv_link = PaymentOrder.create_with_type(type: 'lhv', invoice: @invoice)
|
|
||||||
assert_equal lhv_link.channel, 'Lhv'
|
|
||||||
assert_equal lhv_link.class.config_namespace_name, 'lhv'
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_valid_success_response_from_intermediary?
|
def test_valid_success_response_from_intermediary?
|
||||||
assert(@completed_bank_link.valid_response_from_intermediary?)
|
assert(@completed_bank_link.valid_response_from_intermediary?)
|
||||||
end
|
end
|
||||||
|
|
|
@ -43,6 +43,32 @@ class PaymentOrdersTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_correct_channel_is_assigned
|
||||||
|
everypay_channel = PaymentOrder.create_with_type(type: 'every_pay', invoice: @invoice)
|
||||||
|
assert_equal everypay_channel.channel, 'EveryPay'
|
||||||
|
assert_equal everypay_channel.class.config_namespace_name, 'every_pay'
|
||||||
|
|
||||||
|
swed_channel = PaymentOrder.create_with_type(type: 'swed', invoice: @invoice)
|
||||||
|
assert_equal swed_channel.channel, 'Swed'
|
||||||
|
assert_equal swed_channel.class.config_namespace_name, 'swed'
|
||||||
|
|
||||||
|
seb_channel = PaymentOrder.create_with_type(type: 'seb', invoice: @invoice)
|
||||||
|
assert_equal seb_channel.channel, 'Seb'
|
||||||
|
assert_equal seb_channel.class.config_namespace_name, 'seb'
|
||||||
|
|
||||||
|
lhv_channel = PaymentOrder.create_with_type(type: 'lhv', invoice: @invoice)
|
||||||
|
assert_equal lhv_channel.channel, 'Lhv'
|
||||||
|
assert_equal lhv_channel.class.config_namespace_name, 'lhv'
|
||||||
|
|
||||||
|
admin_channel = PaymentOrder.create_with_type(type: 'admin_payment', invoice: @invoice)
|
||||||
|
assert_equal admin_channel.channel, 'AdminPayment'
|
||||||
|
assert_equal admin_channel.class.config_namespace_name, 'admin_payment'
|
||||||
|
|
||||||
|
system_channel = PaymentOrder.create_with_type(type: 'system_payment', invoice: @invoice)
|
||||||
|
assert_equal system_channel.channel, 'SystemPayment'
|
||||||
|
assert_equal system_channel.class.config_namespace_name, 'system_payment'
|
||||||
|
end
|
||||||
|
|
||||||
def test_can_not_create_order_for_paid_invoice
|
def test_can_not_create_order_for_paid_invoice
|
||||||
invoice = invoices(:one)
|
invoice = invoices(:one)
|
||||||
payment_order = PaymentOrder.create_with_type(type: 'every_pay', invoice: invoice)
|
payment_order = PaymentOrder.create_with_type(type: 'every_pay', invoice: invoice)
|
||||||
|
|
|
@ -58,6 +58,30 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
|
||||||
assert @invoice.paid?
|
assert @invoice.paid?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_attaches_paid_payment_order_to_invoice
|
||||||
|
assert @invoice.unpaid?
|
||||||
|
|
||||||
|
capture_io { run_task }
|
||||||
|
@invoice.reload
|
||||||
|
|
||||||
|
payment_order = @invoice.payment_orders.last
|
||||||
|
assert_equal 'PaymentOrders::SystemPayment', payment_order.type
|
||||||
|
assert payment_order.paid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_attaches_failed_payment_order_to_invoice
|
||||||
|
assert @invoice.unpaid?
|
||||||
|
account = accounts(:cash)
|
||||||
|
account.update!(registrar: registrars(:goodnames))
|
||||||
|
|
||||||
|
capture_io { run_task }
|
||||||
|
@invoice.reload
|
||||||
|
|
||||||
|
payment_order = @invoice.payment_orders.last
|
||||||
|
assert_equal 'PaymentOrders::SystemPayment', payment_order.type
|
||||||
|
assert payment_order.failed?
|
||||||
|
end
|
||||||
|
|
||||||
def test_output
|
def test_output
|
||||||
assert_output "Transactions processed: 1\n" do
|
assert_output "Transactions processed: 1\n" do
|
||||||
run_task
|
run_task
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue