Merge remote-tracking branch 'origin/master' into fix-registrant-contact-view

This commit is contained in:
Karl Erik Õunapuu 2020-05-04 14:29:42 +03:00
commit 05a8c5f031
133 changed files with 2328 additions and 1182 deletions

View file

@ -50,6 +50,7 @@ class Ability
can(:check, Epp::Domain)
can(:create, Epp::Domain)
can(:renew, Epp::Domain) { |d| d.registrar_id == @user.registrar_id }
can(:remove_hold, Epp::Domain) { |d| d.registrar_id == @user.registrar_id }
can(:update, Epp::Domain) { |d, pw| d.registrar_id == @user.registrar_id || d.transfer_code == pw }
can(:transfer, Epp::Domain)
can(:delete, Epp::Domain) { |d, pw| d.registrar_id == @user.registrar_id || d.transfer_code == pw }

View file

@ -4,7 +4,7 @@ class AdminUser < User
validates :identity_code, presence: true, if: -> { country_code == 'EE' }
validates :email, presence: true
validates :password, :password_confirmation, presence: true, if: :new_record?
validates :password_confirmation, presence: true, if: :encrypted_password_changed?
validates :password_confirmation, presence: true, if: :will_save_change_to_encrypted_password?
validate :validate_identity_code, if: -> { country_code == 'EE' }
ROLES = %w(user customer_service admin) # should not match to api_users roles

View file

@ -43,7 +43,7 @@ class ApiUser < User
after_initialize :set_defaults
def set_defaults
return unless new_record?
self.active = true unless active_changed?
self.active = true unless saved_change_to_active?
end
class << self

View file

@ -25,10 +25,16 @@ class BankStatement < ApplicationRecord
bank_transactions.build(bt_params)
end
prepare_dir
self.import_file_path = "#{ENV['bank_statement_import_dir']}/#{Time.zone.now.to_formatted_s(:number)}.txt"
File.open(import_file_path, 'w') { |f| f.write(th6_file.open.read) }
end
def prepare_dir
dirname = ENV['bank_statement_import_dir']
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
end
def parse_th6_row(row)
return parse_th6_header(row) if row[4, 3].strip == '000'
return if row[4, 3].strip == '999' # skip footer
@ -45,7 +51,7 @@ class BankStatement < ApplicationRecord
buyer_name: row[83, 35].strip,
document_no: row[118, 8].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
}
end
@ -80,7 +86,9 @@ class BankStatement < ApplicationRecord
status == FULLY_BINDED
end
def bind_invoices
bank_transactions.unbinded.each(&:autobind_invoice)
def bind_invoices(manual: false)
bank_transactions.unbinded.each do |transaction|
transaction.autobind_invoice(manual: manual)
end
end
end

View file

@ -13,6 +13,7 @@ class BankTransaction < ApplicationRecord
def binded_invoice
return unless binded?
account_activity.invoice
end
@ -30,31 +31,54 @@ class BankTransaction < ApplicationRecord
@registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer
end
# 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 unless registrar
return unless invoice
return unless invoice.payable?
create_activity(registrar, invoice)
channel = if manual
'admin_payment'
else
'system_payment'
end
create_internal_payment_record(channel: channel, invoice: invoice,
registrar: registrar)
end
def bind_invoice(invoice_no)
def create_internal_payment_record(channel: nil, invoice:, registrar:)
if channel.nil?
create_activity(invoice.buyer, invoice)
return
end
payment_order = PaymentOrder.new_with_type(type: channel, invoice: invoice)
payment_order.save!
if create_activity(registrar, invoice)
payment_order.paid!
else
payment_order.update(notes: 'Failed to create activity', status: 'failed')
end
end
def bind_invoice(invoice_no, manual: false)
if binded?
errors.add(:base, I18n.t('transaction_is_already_binded'))
return
end
invoice = Invoice.find_by(number: invoice_no)
@registrar = invoice.buyer
errors.add(:base, I18n.t('invoice_was_not_found')) unless invoice
validate_invoice_data(invoice)
return if errors.any?
unless invoice
errors.add(:base, I18n.t('invoice_was_not_found'))
return
end
create_internal_payment_record(channel: (manual ? 'admin_payment' : nil), invoice: invoice,
registrar: invoice.buyer)
end
def validate_invoice_data(invoice)
if invoice.paid?
errors.add(:base, I18n.t('invoice_is_already_binded'))
return
@ -65,23 +89,21 @@ class BankTransaction < ApplicationRecord
return
end
if invoice.total != sum
errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match'))
return
end
create_activity(invoice.buyer, invoice)
errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match')) if invoice.total != sum
end
def create_activity(registrar, invoice)
ActiveRecord::Base.transaction do
create_account_activity!(account: registrar.cash_account,
invoice: invoice,
sum: invoice.subtotal,
currency: currency,
description: description,
activity_type: AccountActivity::ADD_CREDIT)
activity = AccountActivity.new(
account: registrar.cash_account, bank_transaction: self,
invoice: invoice, sum: invoice.subtotal,
currency: currency, description: description,
activity_type: AccountActivity::ADD_CREDIT
)
if activity.save
reset_pending_registrar_balance_reload
true
else
false
end
end

View file

@ -3,7 +3,7 @@ module Concerns::Contact::Transferable
included do
validates :auth_info, presence: true
after_initialize :generate_auth_info, if: 'new_record? && auth_info.blank?'
after_initialize :generate_auth_info, if: -> { new_record? && auth_info.blank? }
end
def transfer(new_registrar)

View file

@ -0,0 +1,26 @@
module Concerns
module Invoice
module BookKeeping
extend ActiveSupport::Concern
def as_directo_json
invoice = ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(self))
invoice['customer_code'] = buyer.accounting_customer_code
invoice['issue_date'] = issue_date.strftime('%Y-%m-%d')
invoice['transaction_date'] = account_activity
.bank_transaction&.paid_at&.strftime('%Y-%m-%d')
invoice['language'] = buyer.language == 'en' ? 'ENG' : ''
invoice['invoice_lines'] = compose_directo_product
invoice
end
def compose_directo_product
[{ 'product_id': Setting.directo_receipt_product_name, 'description': order,
'quantity': 1, 'price': ActionController::Base.helpers.number_with_precision(
subtotal, precision: 2, separator: '.'
) }].as_json
end
end
end
end

View file

@ -0,0 +1,120 @@
module Concerns
module Registrar
module BookKeeping
extend ActiveSupport::Concern
DOMAIN_TO_PRODUCT = { 'ee': '01EE', 'com.ee': '02COM', 'pri.ee': '03PRI',
'fie.ee': '04FIE', 'med.ee': '05MED' }.freeze
def monthly_summary(month:)
activities = monthly_activites(month)
return unless activities.any?
invoice = {
'number': 1,
'customer_code': accounting_customer_code,
'language': language == 'en' ? 'ENG' : '', 'currency': activities.first.currency,
'date': month.end_of_month.strftime('%Y-%m-%d')
}.as_json
invoice['invoice_lines'] = prepare_invoice_lines(month: month, activities: activities)
invoice
end
def prepare_invoice_lines(month:, activities:)
lines = []
lines << { 'description': title_for_summary(month) }
activities.each do |activity|
fetch_invoice_lines(activity, lines)
end
lines << prepayment_for_all(lines)
lines.as_json
end
def title_for_summary(date)
I18n.with_locale(language == 'en' ? 'en' : 'et') do
I18n.t('registrar.monthly_summary_title', date: I18n.l(date, format: '%B %Y'))
end
end
def fetch_invoice_lines(activity, lines)
price = load_price(activity)
if price.duration.include? 'year'
price.duration.to_i.times do |duration|
lines << new_monthly_invoice_line(activity: activity, duration: duration + 1).as_json
end
else
lines << new_monthly_invoice_line(activity: activity).as_json
end
end
def monthly_activites(month)
AccountActivity.where(account_id: account_ids)
.where(created_at: month.beginning_of_month..month.end_of_month)
.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW])
end
def new_monthly_invoice_line(activity:, duration: nil)
price = load_price(activity)
line = {
'product_id': DOMAIN_TO_PRODUCT[price.zone_name.to_sym],
'quantity': 1,
'unit': language == 'en' ? 'pc' : 'tk',
}
finalize_invoice_line(line, price: price, duration: duration, activity: activity)
end
def finalize_invoice_line(line, price:, activity:, duration:)
yearly = price.duration.include?('year')
line['price'] = yearly ? (price.price.amount / price.duration.to_i) : price.price.amount
line['description'] = description_in_language(price: price, yearly: yearly)
if duration.present?
add_product_timeframe(line: line, activity: activity, duration: duration) if duration > 1
end
line
end
def add_product_timeframe(line:, activity:, duration:)
create_time = activity.created_at
line['start_date'] = (create_time + (duration - 1).year).end_of_month.strftime('%Y-%m-%d')
line['end_date'] = (create_time + (duration - 1).year + 1).end_of_month.strftime('%Y-%m-%d')
end
def description_in_language(price:, yearly:)
timeframe_string = yearly ? 'yearly' : 'monthly'
locale_string = "registrar.invoice_#{timeframe_string}_product_description"
I18n.with_locale(language == 'en' ? 'en' : 'et') do
I18n.t(locale_string, tld: ".#{price.zone_name}", length: price.duration.to_i)
end
end
def prepayment_for_all(lines)
total = 0
en = language == 'en'
lines.each { |l| total += l['quantity'].to_f * l['price'].to_f }
{
'product_id': Setting.directo_receipt_product_name,
'description': en ? 'Domains prepayment' : 'Domeenide ettemaks',
'quantity': -1,
'price': total,
'unit': en ? 'pc' : 'tk',
}
end
def load_price(account_activity)
@pricelists ||= {}
return @pricelists[account_activity.price_id] if @pricelists.key? account_activity.price_id
@pricelists[account_activity.price_id] = account_activity.price
end
end
end
end

View file

@ -0,0 +1,9 @@
module RemoveHold
extend ActiveSupport::Concern
def remove_hold(params)
xml = epp_xml.update(name: { value: params[:domain_name] },
rem: [status: { attrs: { s: 'clientHold' }, value: '' }])
current_user.request(xml)
end
end

View file

@ -1,10 +1,16 @@
# Papertrail concerns is mainly tested at country spec
module Versions
extend ActiveSupport::Concern
WITH_CHILDREN = %w[Domain Contact].freeze
included do
attr_accessor :version_loader
has_paper_trail class_name: "#{model_name}Version"
if WITH_CHILDREN.include?(model_name.name)
has_paper_trail class_name: "#{model_name}Version", meta: { children: :children_log }
else
has_paper_trail class_name: "#{model_name}Version"
end
# add creator and updator
before_create :add_creator
@ -45,17 +51,17 @@ module Versions
# callbacks
def touch_domain_version
domain.try(:touch_with_version)
domain.paper_trail.try(:touch_with_version)
end
def touch_domains_version
domains.each(&:touch_with_version)
domains.each { |domain| domain.paper_trail.touch_with_version }
end
end
module ClassMethods
def all_versions_for(ids, time)
ver_klass = paper_trail_version_class
ver_klass = paper_trail.version_class
from_history = ver_klass.where(item_id: ids.to_a).
order(:item_id).
preceding(time + 1, true).
@ -64,7 +70,8 @@ module Versions
valid_columns = ver.item_type.constantize&.column_names
o = new(ver.object&.slice(*valid_columns))
o.version_loader = ver
ver.object_changes.to_h.each { |k, v| o.public_send("#{k}=", v[-1]) }
changes = ver.object_changes.to_h&.slice(*valid_columns)
changes.each { |k, v| o.public_send("#{k}=", v[-1]) }
o
end
not_in_history = where(id: (ids.to_a - from_history.map(&:id)))

View file

@ -14,8 +14,6 @@ class Contact < ApplicationRecord
has_many :registrant_domains, class_name: 'Domain', foreign_key: 'registrant_id'
has_many :actions, dependent: :destroy
has_paper_trail class_name: "ContactVersion", meta: { children: :children_log }
attr_accessor :legal_document_id
alias_attribute :kind, :ident_type
alias_attribute :copy_from_id, :original_id # Old attribute name; for PaperTrail
@ -23,12 +21,14 @@ class Contact < ApplicationRecord
accepts_nested_attributes_for :legal_documents
validates :name, :email, presence: true
validates :street, :city, :zip, :country_code, presence: true, if: 'self.class.address_processing?'
validates :street, :city, :zip, :country_code, presence: true, if: lambda {
self.class.address_processing?
}
validates :phone, presence: true, e164: true, phone: true
validates :email, format: /@/
validates :email, email_format: { message: :invalid }, if: proc { |c| c.email_changed? }
validates :email, email_format: { message: :invalid }, if: proc { |c| c.will_save_change_to_email? }
validates :code,
uniqueness: { message: :epp_id_taken },
@ -37,7 +37,7 @@ class Contact < ApplicationRecord
validates_associated :identifier
validate :validate_html
validate :validate_country_code, if: 'self.class.address_processing?'
validate :validate_country_code, if: -> { self.class.address_processing? }
after_initialize do
self.status_notes = {} if status_notes.nil?

View file

@ -1,24 +0,0 @@
class Counter
def initialize value = 0
@value = value
end
attr_accessor :value
def method_missing *args, &blk
@value.send(*args, &blk)
end
def to_s
@value.to_s
end
def now
@value
end
# pre-increment ".+" when x not present
def next(x = 1)
@value += x
end
def prev(x = 1)
@value -= x
end
end

View file

@ -1,6 +1,7 @@
module Depp
class Domain
include ActiveModel::Conversion
include RemoveHold
extend ActiveModel::Naming
attr_accessor :name, :current_user, :epp_xml

View file

@ -1,198 +1,3 @@
class Directo < ApplicationRecord
DOMAIN_TO_PRODUCT = {"ee" => "01EE", "com.ee" => "02COM", "pri.ee" => "03PRI", "fie.ee"=>"04FIE", "med.ee" => "05MED"}.freeze
belongs_to :item, polymorphic: true
def self.send_receipts
new_trans = Invoice.where(in_directo: false).non_cancelled
total = new_trans.count
counter = 0
Rails.logger.info("[DIRECTO] Will try to send #{total} invoices")
new_trans.find_in_batches(batch_size: 10).each do |group|
mappers = {} # need them as no direct connection between invoice
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
xml.invoices {
group.each do |invoice|
if invoice.account_activity.nil? || invoice.account_activity.bank_transaction.nil? ||
invoice.account_activity.bank_transaction.sum.nil? || invoice.account_activity.bank_transaction.sum != invoice.total
Rails.logger.info("[DIRECTO] Invoice #{invoice.number} has been skipped")
next
end
counter += 1
num = invoice.number
paid_at = invoice.account_activity.bank_transaction&.paid_at&.strftime("%Y-%m-%d")
mappers[num] = invoice
xml.invoice(
"SalesAgent" => Setting.directo_sales_agent,
"Number" => num,
"InvoiceDate" => invoice.issue_date.strftime("%Y-%m-%d"),
'TransactionDate' => paid_at,
"PaymentTerm" => Setting.directo_receipt_payment_term,
"Currency" => invoice.currency,
"CustomerCode"=> invoice.buyer.accounting_customer_code
){
xml.line(
"ProductID" => Setting.directo_receipt_product_name,
"Quantity" => 1,
"UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.subtotal, precision: 2, separator: "."),
"ProductName" => invoice.order
)
}
end
}
end
data = builder.to_xml.gsub("\n",'')
Rails.logger.info("[Directo] XML request: #{data}")
response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false)
Rails.logger.info("[Directo] Directo responded with code: #{response.code}, body: #{response.body}")
dump_result_to_db(mappers, response.to_s)
end
STDOUT << "#{Time.zone.now.utc} - Directo receipts sending finished. #{counter} of #{total} are sent\n"
end
def self.dump_result_to_db mappers, xml
Nokogiri::XML(xml).css("Result").each do |res|
obj = mappers[res.attributes["docid"].value.to_i]
obj.directo_records.create!(response: res.as_json.to_h, invoice_number: obj.number)
obj.update_columns(in_directo: true)
Rails.logger.info("[DIRECTO] Invoice #{res.attributes["docid"].value} was pushed and return is #{res.as_json.to_h.inspect}")
end
end
def self.send_monthly_invoices(debug: false)
I18n.locale = :et
month = Time.now - 1.month
invoices_until = month.end_of_month
date_format = "%Y-%m-%d"
invoice_counter= Counter.new
min_directo = Setting.directo_monthly_number_min.presence.try(:to_i)
max_directo = Setting.directo_monthly_number_max.presence.try(:to_i)
last_directo = [Setting.directo_monthly_number_last.presence.try(:to_i), min_directo].compact.max || 0
if max_directo && max_directo <= last_directo
raise "Directo counter is out of period (max allowed number is smaller than last counter number)"
end
directo_next = last_directo
Registrar.where.not(test_registrar: true).find_each do |registrar|
unless registrar.cash_account
Rails.logger.info("[DIRECTO] Monthly invoice for registrar #{registrar.id} has been skipped as it doesn't has cash_account")
next
end
counter = Counter.new(1)
items = {}
registrar_activities = AccountActivity.where(account_id: registrar.account_ids).where("created_at BETWEEN ? AND ?",month.beginning_of_month, month.end_of_month)
# adding domains items
registrar_activities.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW]).each do |activity|
price = load_price(activity)
if price.duration.include?('year')
price.duration.to_i.times do |i|
year = i+1
hash = {
"ProductID" => DOMAIN_TO_PRODUCT[price.zone_name],
"Unit" => "tk",
"ProductName" => ".#{price.zone_name} registreerimine: #{price.duration.to_i} aasta#{price.duration.to_i > 1 ? 't' : ''}",
"UnitPriceWoVAT" => price.price.amount / price.duration.to_i
}
hash["StartDate"] = (activity.created_at + (year-1).year).end_of_month.strftime(date_format) if year > 1
hash["EndDate"] = (activity.created_at + (year-1).year + 1).end_of_month.strftime(date_format) if year > 1
if items.has_key?(hash)
items[hash]["Quantity"] += 1
else
items[hash] = { "RN" => counter.next, "RR" => counter.now - i, "Quantity" => 1 }
end
end
else
1.times do |i|
quantity = price.account_activities
.where(account_id: registrar.account_ids)
.where(created_at: month.beginning_of_month..month.end_of_month)
.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW])
.count
hash = {
"ProductID" => DOMAIN_TO_PRODUCT[price.zone_name],
"Unit" => "tk",
"ProductName" => ".#{price.zone_name} registreerimine: #{price.duration.to_i} kuud",
"UnitPriceWoVAT" => price.price.amount,
}
if items.has_key?(hash)
#items[hash]["Quantity"] += 1
else
items[hash] = { "RN" => counter.next, "RR" => counter.now - i, "Quantity" => quantity }
end
end
end
end
#adding prepaiments
if items.any?
total = 0
items.each{ |key, val| total += val["Quantity"] * key["UnitPriceWoVAT"] }
hash = {"ProductID" => Setting.directo_receipt_product_name, "Unit" => "tk", "ProductName" => "Domeenide ettemaks", "UnitPriceWoVAT"=>total}
items[hash] = {"RN"=>counter.next, "RR" => counter.now, "Quantity"=> -1}
end
# generating XML
if items.any?
directo_next += 1
invoice_counter.next
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
xml.invoices{
xml.invoice("Number" =>directo_next,
"InvoiceDate" =>invoices_until.strftime(date_format),
"PaymentTerm" =>Setting.directo_receipt_payment_term,
"CustomerCode"=>registrar.accounting_customer_code,
"Language" =>"",
"Currency" =>registrar_activities.first.currency,
"SalesAgent" =>Setting.directo_sales_agent){
xml.line("RN" => 1, "RR"=>1, "ProductName"=> "Domeenide registreerimine - #{I18n.l(invoices_until, format: "%B %Y").titleize}")
items.each do |line, val|
xml.line(val.merge(line))
end
}
}
end
data = builder.to_xml.gsub("\n",'')
Rails.logger.info("[Directo] XML request: #{data}")
if debug
STDOUT << "#{Time.zone.now.utc} - Directo xml had to be sent #{data}\n"
else
response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false)
Rails.logger.info("[Directo] Directo responded with code: #{response.code}, body: #{response.body}")
response = response.to_s
Setting.directo_monthly_number_last = directo_next
Nokogiri::XML(response).css("Result").each do |res|
Directo.create!(request: data, response: res.as_json.to_h, invoice_number: directo_next)
Rails.logger.info("[DIRECTO] Invoice #{res.attributes["docid"].value} was pushed and return is #{res.as_json.to_h.inspect}")
end
end
else
Rails.logger.info("[DIRECTO] Registrar #{registrar.id} has nothing to be sent to Directo")
end
end
STDOUT << "#{Time.zone.now.utc} - Directo invoices sending finished. #{invoice_counter.now} are sent\n"
end
def self.load_price(account_activity)
@pricelists ||= {}
return @pricelists[account_activity.price_id] if @pricelists.has_key?(account_activity.price_id)
@pricelists[account_activity.price_id] = account_activity.price
end
end

View file

@ -9,10 +9,16 @@ class Dnskey < ApplicationRecord
validate :validate_protocol
validate :validate_flags
before_save -> { generate_digest if public_key_changed? && !ds_digest_changed? }
before_save lambda {
generate_digest if will_save_change_to_public_key? && !will_save_change_to_ds_digest?
}
before_save lambda {
if (public_key_changed? || flags_changed? || alg_changed? || protocol_changed?) && !ds_key_tag_changed?
if (will_save_change_to_public_key? ||
will_save_change_to_flags? ||
will_save_change_to_alg? ||
will_save_change_to_protocol?) &&
!will_save_change_to_ds_key_tag?
generate_ds_key_tag
end
}

View file

@ -10,8 +10,6 @@ class Domain < ApplicationRecord
include Concerns::Domain::RegistryLockable
include Concerns::Domain::Releasable
has_paper_trail class_name: "DomainVersion", meta: { children: :children_log }
attr_accessor :roles
attr_accessor :legal_document_id
@ -73,12 +71,13 @@ class Domain < ApplicationRecord
before_update :manage_statuses
def manage_statuses
return unless registrant_id_changed? # rollback has not yet happened
return unless will_save_change_to_registrant_id? # rollback has not yet happened
pending_update! if registrant_verification_asked?
true
end
after_commit :update_whois_record, unless: 'domain_name.at_auction?'
after_commit :update_whois_record, unless: -> { domain_name.at_auction? }
after_create :update_reserved_domains
def update_reserved_domains
@ -486,9 +485,9 @@ class Domain < ApplicationRecord
self.delete_date = nil
when DomainStatus::SERVER_MANUAL_INZONE # removal causes server hold to set
self.outzone_at = Time.zone.now if force_delete_scheduled?
when DomainStatus::DomainStatus::EXPIRED # removal causes server hold to set
when DomainStatus::EXPIRED # removal causes server hold to set
self.outzone_at = self.expire_time + 15.day
when DomainStatus::DomainStatus::SERVER_HOLD # removal causes server hold to set
when DomainStatus::SERVER_HOLD # removal causes server hold to set
self.outzone_at = nil
end
end
@ -547,7 +546,7 @@ class Domain < ApplicationRecord
activate if nameservers.reject(&:marked_for_destruction?).size >= Setting.ns_min_count
end
cancel_force_delete if force_delete_scheduled? && registrant_id_changed?
cancel_force_delete if force_delete_scheduled? && will_save_change_to_registrant_id?
if statuses.empty? && valid?
statuses << DomainStatus::OK

View file

@ -1,7 +1,5 @@
class DomainStatus < ApplicationRecord
include Versions # version/domain_status_version.rb
include EppErrors
belongs_to :domain
# Requests to delete the object MUST be rejected.

View file

@ -182,7 +182,7 @@ class Epp::Contact < Contact
self.attributes = at
email_changed = email_changed?
email_changed = will_save_change_to_email?
old_email = email_was
updated = save

View file

@ -2,11 +2,13 @@ class Invoice < ApplicationRecord
include Versions
include Concerns::Invoice::Cancellable
include Concerns::Invoice::Payable
include Concerns::Invoice::BookKeeping
belongs_to :buyer, class_name: 'Registrar'
has_one :account_activity
has_many :items, class_name: 'InvoiceItem', dependent: :destroy
has_many :directo_records, as: :item, class_name: 'Directo'
has_many :payment_orders
accepts_nested_attributes_for :items
@ -70,7 +72,7 @@ class Invoice < ApplicationRecord
Country.new(buyer_country_code)
end
# order is used for directo/banklink description
# order is used for directo/banklink description
def order
"Order nr. #{number}"
end
@ -102,6 +104,14 @@ class Invoice < ApplicationRecord
generator.generate
end
def do_not_send_e_invoice?
e_invoice_sent? || cancelled? || paid?
end
def e_invoice_sent?
e_invoice_sent_at.present?
end
private
def apply_default_buyer_vat_no
@ -111,4 +121,4 @@ class Invoice < ApplicationRecord
def calculate_total
self.total = subtotal + vat_amount
end
end
end

View file

@ -48,7 +48,7 @@ class Nameserver < ApplicationRecord
[:ipv6, :invalid, { value: { obj: 'hostAddr', val: ipv6 } }]
],
'2003' => [
[:ipv4, :blank]
%i[base ip_required],
]
}
end
@ -83,6 +83,7 @@ class Nameserver < ApplicationRecord
def glue_record_required?
return unless hostname? && domain
DomainName(hostname).domain == domain.name
end

102
app/models/payment_order.rb Normal file
View file

@ -0,0 +1,102 @@
class PaymentOrder < ApplicationRecord
include Versions
include ActionView::Helpers::NumberHelper
PAYMENT_INTERMEDIARIES = ENV['payments_intermediaries'].to_s.strip.split(', ').freeze
PAYMENT_BANKLINK_BANKS = ENV['payments_banks'].to_s.strip.split(', ').freeze
INTERNAL_PAYMENT_METHODS = %w[admin_payment system_payment].freeze
PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS,
INTERNAL_PAYMENT_METHODS].flatten.freeze
CUSTOMER_PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS].flatten.freeze
belongs_to :invoice, optional: false
validate :invoice_cannot_be_already_paid, on: :create
validate :supported_payment_method
enum status: { issued: 'issued', paid: 'paid', cancelled: 'cancelled',
failed: 'failed' }
attr_accessor :return_url, :response_url
def self.supported_methods
supported = []
PAYMENT_METHODS.each do |method|
class_name = ('PaymentOrders::' + method.camelize).constantize
raise(NoMethodError, class_name) unless class_name < PaymentOrder
supported << class_name
end
supported
end
def self.new_with_type(type:, invoice:)
channel = ('PaymentOrders::' + type.camelize).constantize
PaymentOrder.new(type: channel, invoice: invoice)
end
# Name of configuration namespace
def self.config_namespace_name; end
def supported_payment_method
return if PaymentOrder.supported_method?(type)
errors.add(:type, 'is not supported')
end
def invoice_cannot_be_already_paid
return unless invoice&.paid?
errors.add(:invoice, 'is already paid')
end
def self.supported_method?(name, shortname: false)
some_class = if shortname
('PaymentOrders::' + name.camelize).constantize
else
name.constantize
end
supported_methods.include? some_class
rescue NameError
false
end
def base_transaction(sum:, paid_at:, buyer_name:)
BankTransaction.new(
description: invoice.order,
reference_no: invoice.reference_no,
currency: invoice.currency,
iban: invoice.seller_iban,
sum: sum,
paid_at: paid_at,
buyer_name: buyer_name
)
end
def complete_transaction
return NoMethodError unless payment_received?
paid!
transaction = composed_transaction
transaction.save! && transaction.bind_invoice(invoice.number)
return unless transaction.errors.any?
worded_errors = 'Failed to bind. '
transaction.errors.full_messages.each do |err|
worded_errors << "#{err}, "
end
update!(notes: worded_errors)
end
def channel
type.gsub('PaymentOrders::', '')
end
def form_url
ENV["payments_#{self.class.config_namespace_name}_url"]
end
end

View file

@ -1,15 +0,0 @@
module PaymentOrders
PAYMENT_INTERMEDIARIES = ENV['payments_intermediaries'].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
def self.create_with_type(type, invoice, opts = {})
raise ArgumentError unless PAYMENT_METHODS.include?(type)
if PAYMENT_BANKLINK_BANKS.include?(type)
BankLink.new(type, invoice, opts)
elsif type == 'every_pay'
EveryPay.new(type, invoice, opts)
end
end
end

View 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

View file

@ -1,44 +1,44 @@
module PaymentOrders
class BankLink < Base
BANK_LINK_VERSION = '008'
class BankLink < PaymentOrder
BANK_LINK_VERSION = '008'.freeze
NEW_TRANSACTION_SERVICE_NUMBER = '1012'
SUCCESSFUL_PAYMENT_SERVICE_NUMBER = '1111'
CANCELLED_PAYMENT_SERVICE_NUMBER = '1911'
NEW_TRANSACTION_SERVICE_NUMBER = '1012'.freeze
SUCCESSFUL_PAYMENT_SERVICE_NUMBER = '1111'.freeze
CANCELLED_PAYMENT_SERVICE_NUMBER = '1911'.freeze
NEW_MESSAGE_KEYS = %w(VK_SERVICE VK_VERSION VK_SND_ID VK_STAMP VK_AMOUNT
NEW_MESSAGE_KEYS = %w[VK_SERVICE VK_VERSION VK_SND_ID VK_STAMP VK_AMOUNT
VK_CURR VK_REF VK_MSG VK_RETURN VK_CANCEL
VK_DATETIME).freeze
SUCCESS_MESSAGE_KEYS = %w(VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP
VK_DATETIME].freeze
SUCCESS_MESSAGE_KEYS = %w[VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP
VK_T_NO VK_AMOUNT VK_CURR VK_REC_ACC VK_REC_NAME
VK_SND_ACC VK_SND_NAME VK_REF VK_MSG
VK_T_DATETIME).freeze
CANCEL_MESSAGE_KEYS = %w(VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP
VK_REF VK_MSG).freeze
VK_T_DATETIME].freeze
CANCEL_MESSAGE_KEYS = %w[VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP
VK_REF VK_MSG].freeze
def form_fields
hash = {}
hash["VK_SERVICE"] = NEW_TRANSACTION_SERVICE_NUMBER
hash["VK_VERSION"] = BANK_LINK_VERSION
hash["VK_SND_ID"] = seller_account
hash["VK_STAMP"] = invoice.number
hash["VK_AMOUNT"] = number_with_precision(invoice.total, precision: 2, separator: ".")
hash["VK_CURR"] = invoice.currency
hash["VK_REF"] = ""
hash["VK_MSG"] = invoice.order
hash["VK_RETURN"] = return_url
hash["VK_CANCEL"] = return_url
hash["VK_DATETIME"] = Time.zone.now.strftime("%Y-%m-%dT%H:%M:%S%z")
hash["VK_MAC"] = calc_mac(hash)
hash["VK_ENCODING"] = "UTF-8"
hash["VK_LANG"] = "ENG"
hash['VK_SERVICE'] = NEW_TRANSACTION_SERVICE_NUMBER
hash['VK_VERSION'] = BANK_LINK_VERSION
hash['VK_SND_ID'] = seller_account
hash['VK_STAMP'] = invoice.number
hash['VK_AMOUNT'] = number_with_precision(invoice.total, precision: 2, separator: ".")
hash['VK_CURR'] = invoice.currency
hash['VK_REF'] = ''
hash['VK_MSG'] = invoice.order
hash['VK_RETURN'] = return_url
hash['VK_CANCEL'] = return_url
hash['VK_DATETIME'] = Time.zone.now.strftime('%Y-%m-%dT%H:%M:%S%z')
hash['VK_MAC'] = calc_mac(hash)
hash['VK_ENCODING'] = 'UTF-8'
hash['VK_LANG'] = 'ENG'
hash
end
def valid_response_from_intermediary?
return false unless response
case response["VK_SERVICE"]
case response['VK_SERVICE']
when SUCCESSFUL_PAYMENT_SERVICE_NUMBER
valid_successful_transaction?
when CANCELLED_PAYMENT_SERVICE_NUMBER
@ -48,29 +48,31 @@ module PaymentOrders
end
end
def complete_transaction
return unless valid_successful_transaction?
def payment_received?
valid_response_from_intermediary? && settled_payment?
end
transaction = compose_or_find_transaction
def create_failure_report
notes = "User failed to make payment. Bank responded with code #{response['VK_SERVICE']}"
status = 'cancelled'
update!(notes: notes, status: status)
end
transaction.sum = response['VK_AMOUNT']
transaction.bank_reference = response['VK_T_NO']
transaction.buyer_bank_code = response["VK_SND_ID"]
transaction.buyer_iban = response["VK_SND_ACC"]
transaction.buyer_name = response["VK_SND_NAME"]
transaction.paid_at = Time.parse(response["VK_T_DATETIME"])
def composed_transaction
paid_at = Time.parse(response['VK_T_DATETIME'])
transaction = base_transaction(sum: response['VK_AMOUNT'],
paid_at: paid_at,
buyer_name: response['VK_SND_NAME'])
transaction.save!
transaction.bind_invoice(invoice.number)
if transaction.errors.empty?
Rails.logger.info("Invoice ##{invoice.number} was marked as paid")
else
Rails.logger.error("Failed to bind invoice ##{invoice.number}")
end
transaction.bank_reference = response['VK_T_NO']
transaction.buyer_bank_code = response['VK_SND_ID']
transaction.buyer_iban = response['VK_SND_ACC']
transaction
end
def settled_payment?
response["VK_SERVICE"] == SUCCESSFUL_PAYMENT_SERVICE_NUMBER
response['VK_SERVICE'] == SUCCESSFUL_PAYMENT_SERVICE_NUMBER
end
private
@ -89,17 +91,15 @@ module PaymentOrders
def valid_amount?
source = number_with_precision(
BigDecimal.new(response["VK_AMOUNT"]), precision: 2, separator: "."
)
target = number_with_precision(
invoice.total, precision: 2, separator: "."
BigDecimal(response['VK_AMOUNT']), precision: 2, separator: '.'
)
target = number_with_precision(invoice.total, precision: 2, separator: '.')
source == target
end
def valid_currency?
invoice.currency == response["VK_CURR"]
invoice.currency == response['VK_CURR']
end
def sign(data)
@ -117,7 +117,7 @@ module PaymentOrders
def valid_mac?(hash, keys)
data = keys.map { |element| prepend_size(hash[element]) }.join
verify_mac(data, hash["VK_MAC"])
verify_mac(data, hash['VK_MAC'])
end
def verify_mac(data, mac)
@ -126,22 +126,22 @@ module PaymentOrders
end
def prepend_size(value)
value = (value || "").to_s.strip
string = ""
value = (value || '').to_s.strip
string = ''
string << format("%03i", value.size)
string << value
end
def seller_account
ENV["payments_#{type}_seller_account"]
ENV["payments_#{self.class.config_namespace_name}_seller_account"]
end
def seller_certificate
ENV["payments_#{type}_seller_private"]
ENV["payments_#{self.class.config_namespace_name}_seller_private"]
end
def bank_certificate
ENV["payments_#{type}_bank_certificate"]
ENV["payments_#{self.class.config_namespace_name}_bank_certificate"]
end
end
end

View file

@ -1,59 +0,0 @@
module PaymentOrders
class Base
include ActionView::Helpers::NumberHelper
attr_reader :type,
:invoice,
:return_url,
:response_url,
:response
def initialize(type, invoice, opts = {})
@type = type
@invoice = invoice
@return_url = opts[:return_url]
@response_url = opts[:response_url]
@response = opts[:response]
end
def create_transaction
transaction = BankTransaction.where(description: invoice.order).first_or_initialize(
reference_no: invoice.reference_no,
currency: invoice.currency,
iban: invoice.seller_iban
)
transaction.save!
end
def compose_or_find_transaction
transaction = BankTransaction.find_by(base_transaction_params)
# Transaction already autobinded (possibly) invalid invoice
if transaction.binded?
Rails.logger.info("Transaction #{transaction.id} is already binded")
Rails.logger.info('Creating new BankTransaction record.')
transaction = new_base_transaction
end
transaction
end
def new_base_transaction
BankTransaction.new(base_transaction_params)
end
def base_transaction_params
{
description: invoice.order,
currency: invoice.currency,
iban: invoice.seller_iban,
}
end
def form_url
ENV["payments_#{type}_url"]
end
end
end

View file

@ -1,9 +1,15 @@
module PaymentOrders
class EveryPay < Base
USER = ENV['payments_every_pay_api_user'].freeze
KEY = ENV['payments_every_pay_api_key'].freeze
ACCOUNT_ID = ENV['payments_every_pay_seller_account'].freeze
SUCCESSFUL_PAYMENT = %w(settled authorized).freeze
class EveryPay < PaymentOrder
USER = ENV['payments_every_pay_api_user']
KEY = ENV['payments_every_pay_api_key']
ACCOUNT_ID = ENV['payments_every_pay_seller_account']
SUCCESSFUL_PAYMENT = %w[settled authorized].freeze
CONFIG_NAMESPACE = 'every_pay'.freeze
def self.config_namespace_name
CONFIG_NAMESPACE
end
def form_fields
base_json = base_params
@ -25,25 +31,23 @@ module PaymentOrders
end
def settled_payment?
SUCCESSFUL_PAYMENT.include?(response[:payment_state])
SUCCESSFUL_PAYMENT.include?(response['payment_state'])
end
def complete_transaction
return unless valid_response_from_intermediary? && settled_payment?
def payment_received?
valid_response_from_intermediary? && settled_payment?
end
transaction = compose_or_find_transaction
def composed_transaction
base_transaction(sum: response['amount'],
paid_at: Date.strptime(response['timestamp'], '%s'),
buyer_name: response['cc_holder_name'])
end
transaction.sum = response[:amount]
transaction.paid_at = Date.strptime(response[:timestamp], '%s')
transaction.buyer_name = response[:cc_holder_name]
transaction.save!
transaction.bind_invoice(invoice.number)
if transaction.errors.empty?
Rails.logger.info("Invoice ##{invoice.number} marked as paid")
else
Rails.logger.error("Failed to bind invoice ##{invoice.number}")
end
def create_failure_report
notes = "User failed to make valid payment. Payment state: #{response['payment_state']}"
status = 'cancelled'
update!(notes: notes, status: status)
end
private
@ -63,24 +67,27 @@ module PaymentOrders
end
def valid_hmac?
hmac_fields = response[:hmac_fields].split(',')
hmac_fields = response['hmac_fields'].split(',')
hmac_hash = {}
hmac_fields.map do |field|
symbol = field.to_sym
hmac_hash[symbol] = response[symbol]
hmac_hash[field] = response[field]
end
hmac_string = hmac_hash.map { |key, _v| "#{key}=#{hmac_hash[key]}" }.join('&')
expected_hmac = OpenSSL::HMAC.hexdigest('sha1', KEY, hmac_string)
expected_hmac == response[:hmac]
expected_hmac == response['hmac']
rescue NoMethodError
false
end
def valid_amount?
invoice.total == BigDecimal.new(response[:amount])
return false unless response.key? 'amount'
invoice.total == BigDecimal(response['amount'])
end
def valid_account?
response[:account_id] == ACCOUNT_ID
response['account_id'] == ACCOUNT_ID
end
end
end

View file

@ -0,0 +1,7 @@
module PaymentOrders
class Lhv < BankLink
def self.config_namespace_name
'lhv'
end
end
end

View file

@ -0,0 +1,7 @@
module PaymentOrders
class Seb < BankLink
def self.config_namespace_name
'seb'
end
end
end

View file

@ -0,0 +1,7 @@
module PaymentOrders
class Swed < BankLink
def self.config_namespace_name
'swed'
end
end
end

View 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

View file

@ -1,5 +1,6 @@
class Registrar < ApplicationRecord
include Versions # version/registrar_version.rb
include Concerns::Registrar::BookKeeping
has_many :domains, dependent: :restrict_with_error
has_many :contacts, dependent: :restrict_with_error
@ -21,9 +22,9 @@ class Registrar < ApplicationRecord
validates :reference_no, format: Billing::ReferenceNo::REGEXP
validate :forbid_special_code
validates :vat_rate, presence: true, if: 'vat_liable_in_foreign_country? && vat_no.blank?'
validates :vat_rate, presence: true, if: -> { vat_liable_in_foreign_country? && vat_no.blank? }
validates :vat_rate, absence: true, if: :vat_liable_locally?
validates :vat_rate, absence: true, if: 'vat_liable_in_foreign_country? && vat_no?'
validates :vat_rate, absence: true, if: -> { vat_liable_in_foreign_country? && vat_no? }
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 },
allow_nil: true
@ -33,7 +34,7 @@ class Registrar < ApplicationRecord
after_initialize :set_defaults
validates :email, email_format: { message: :invalid },
allow_blank: true, if: proc { |c| c.email_changed? }
allow_blank: true, if: proc { |c| c.will_save_change_to_email? }
validates :billing_email, email_format: { message: :invalid }, allow_blank: true
alias_attribute :contact_email, :email
@ -99,9 +100,7 @@ class Registrar < ApplicationRecord
}
]
)
e_invoice = invoice.to_e_invoice
e_invoice.deliver
SendEInvoiceJob.enqueue(invoice.id)
invoice
end

View file

@ -0,0 +1,4 @@
class PaymentOrderVersion < PaperTrail::Version
self.table_name = :log_payment_orders
self.sequence_name = :log_payment_orders_id_seq
end