From 235ef9ba3ee0deb28d6f9accdebf0c1e111e0e31 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Fri, 28 May 2021 13:53:14 +0500 Subject: [PATCH 1/3] Add paid flag to email subject, check receipt date in tests --- app/mailers/invoice_mailer.rb | 5 ++-- app/models/registrar.rb | 3 ++- config/locales/en.yml | 3 ++- config/locales/et.yml | 2 ++ test/tasks/invoices/process_payments_test.rb | 24 +++++++++++++++++++- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/mailers/invoice_mailer.rb b/app/mailers/invoice_mailer.rb index 4f01b2c15..a9d544d63 100644 --- a/app/mailers/invoice_mailer.rb +++ b/app/mailers/invoice_mailer.rb @@ -1,9 +1,10 @@ class InvoiceMailer < ApplicationMailer - def invoice_email(invoice:, recipient:) + def invoice_email(invoice:, recipient:, paid: false) @invoice = invoice subject = default_i18n_subject(invoice_number: invoice.number) + subject << I18n.t('invoice.already_paid') if paid attachments["invoice-#{invoice.number}.pdf"] = invoice.as_pdf mail(to: recipient, subject: subject) end -end \ No newline at end of file +end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 264029c8a..284df83aa 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -101,7 +101,8 @@ class Registrar < ApplicationRecord ) unless payable - InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email).deliver_now + InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email, paid: !payable) + .deliver_later(wait: 1.minute) end SendEInvoiceJob.perform_later(invoice.id, payable) diff --git a/config/locales/en.yml b/config/locales/en.yml index ac31d19e9..a28d97f81 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -470,7 +470,8 @@ en: price: 'Price' total: 'Total' paid_at: 'Paid at' - invoice: 'Invoice' + invoice: + already_paid: " (already paid)" bank_statements: 'Bank statements' back_to_bank_statements: 'Back to bank statements' back_to_bank_statement: 'Back to bank statement' diff --git a/config/locales/et.yml b/config/locales/et.yml index 9cb8aaa4a..a2afd9848 100644 --- a/config/locales/et.yml +++ b/config/locales/et.yml @@ -6,3 +6,5 @@ et: # Don't forget the nil at the beginning; there's no such thing as a 0th month month_names: [~, Jaanuar, Veebruar, Märts, Aprill, Mai, Juuni, Juuli, August, September, Oktoober, November, Detsember] emails: "Meillaadressid" + invoice: + already_paid: " (juba makstud)" diff --git a/test/tasks/invoices/process_payments_test.rb b/test/tasks/invoices/process_payments_test.rb index 9cccee96e..a078dfec1 100644 --- a/test/tasks/invoices/process_payments_test.rb +++ b/test/tasks/invoices/process_payments_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class ProcessPaymentsTaskTest < ActiveSupport::TestCase +class ProcessPaymentsTaskTest < ActiveJob::TestCase setup do @payment_amount = payment_amount = 0.1 @payment_currency = payment_currency = 'EUR' @@ -162,6 +162,28 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase assert_equal 0.1, registrar.invoices.last.total end + def test_topup_creates_invoice_and_send_it_as_paid + registrar = registrars(:bestnames) + @invoice.payment_orders.destroy_all + @invoice.destroy + + perform_enqueued_jobs do + run_task + end + + invoice = Invoice.last + assert invoice.paid? + assert_not invoice.e_invoice_sent_at.blank? + + pdf_source = Invoice::PdfGenerator.new(invoice) + pdf_source.send(:invoice_html).include?('Receipt date') + + email= ActionMailer::Base.deliveries.last + assert email.subject.include?('already paid') + + assert_equal 0.1, registrar.invoices.last.total + end + def test_output assert_output "Transactions processed: 1\n" do run_task From fb810e5ccb620c985e983ca13d75733c2e5df03d Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Mon, 31 May 2021 13:25:52 +0500 Subject: [PATCH 2/3] Always send e-invoice if already paid --- app/models/invoice.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 73ed88567..3170687c9 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -105,7 +105,7 @@ class Invoice < ApplicationRecord end def do_not_send_e_invoice? - e_invoice_sent? || cancelled? || paid? + e_invoice_sent? || cancelled? end def e_invoice_sent? From 76a0b80f85d2399d20e648bb512400000617ea48 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Mon, 31 May 2021 14:10:51 +0500 Subject: [PATCH 3/3] Add test rake job for processing dummy payment --- app/jobs/send_e_invoice_job.rb | 2 +- app/models/concerns/invoice/cancellable.rb | 2 +- app/models/registrar.rb | 2 +- lib/tasks/invoices/process_payments.rake | 31 +++++++++++++++++----- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/jobs/send_e_invoice_job.rb b/app/jobs/send_e_invoice_job.rb index f8b1f8296..6b5fd4bba 100644 --- a/app/jobs/send_e_invoice_job.rb +++ b/app/jobs/send_e_invoice_job.rb @@ -21,7 +21,7 @@ class SendEInvoiceJob < ApplicationJob end def process(invoice:, payable:) - invoice.to_e_invoice(payable: payable).deliver + invoice.to_e_invoice(payable: payable).deliver unless Rails.env.development? invoice.update(e_invoice_sent_at: Time.zone.now) log_success(invoice) end diff --git a/app/models/concerns/invoice/cancellable.rb b/app/models/concerns/invoice/cancellable.rb index 0374c9d2a..8c9e142a8 100644 --- a/app/models/concerns/invoice/cancellable.rb +++ b/app/models/concerns/invoice/cancellable.rb @@ -15,7 +15,7 @@ module Invoice::Cancellable end def cancelled? - cancelled_at + cancelled_at.present? end def not_cancelled? diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 284df83aa..2f5cca0ad 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -105,7 +105,7 @@ class Registrar < ApplicationRecord .deliver_later(wait: 1.minute) end - SendEInvoiceJob.perform_later(invoice.id, payable) + SendEInvoiceJob.perform_now(invoice.id, payable) invoice end diff --git a/lib/tasks/invoices/process_payments.rake b/lib/tasks/invoices/process_payments.rake index 5e9972669..0d8f7c7d4 100644 --- a/lib/tasks/invoices/process_payments.rake +++ b/lib/tasks/invoices/process_payments.rake @@ -22,6 +22,30 @@ namespace :invoices do end end + process_transactions(incoming_transactions) + + puts "Transactions processed: #{incoming_transactions.size}" + end + + task test_payments: :environment do + registrar = Invoice.last.buyer + transactions = [OpenStruct.new(amount: 0.1, + currency: 'EUR', + date: Time.zone.today, + payment_reference_number: registrar.reference_no, + payment_description: "description #{registrar.reference_no}")] + process_transactions(transactions) + puts 'Last registrar invoice is' + pp registrar.invoices.last + puts "Last invoice paid at #{registrar.invoices.last.receipt_date}" + end + + def log(msg) + @log ||= Logger.new(STDOUT) + @log.info(msg) + end + + def process_transactions(incoming_transactions) if incoming_transactions.any? log 'Got incoming transactions' log incoming_transactions @@ -51,12 +75,5 @@ namespace :invoices do else log 'Got no incoming transactions parsed, aborting' end - - puts "Transactions processed: #{incoming_transactions.size}" - end - - def log(msg) - @log ||= Logger.new(STDOUT) - @log.info(msg) end end