mirror of
https://github.com/internetee/registry.git
synced 2025-08-13 21:19:31 +02:00
Add domain renewal with balance deduction
This commit is contained in:
parent
729f39f612
commit
7d2aad570e
6 changed files with 83 additions and 18 deletions
|
@ -16,11 +16,13 @@ class Registrar
|
||||||
|
|
||||||
if domain_ids_for_bulk_renew.present?
|
if domain_ids_for_bulk_renew.present?
|
||||||
domains = Epp::Domain.where(id: domain_ids_for_bulk_renew).to_a
|
domains = Epp::Domain.where(id: domain_ids_for_bulk_renew).to_a
|
||||||
task = Domains::BulkRenew::Start.run(domains: domains, period_element: @period)
|
task = Domains::BulkRenew::Start.run(domains: domains,
|
||||||
if task.valid?
|
period_element: @period,
|
||||||
flash[:notice] = t(:bulk_renew_completed)
|
registrar: current_registrar_user.registrar)
|
||||||
|
flash[:notice] = if task.valid?
|
||||||
|
t(:bulk_renew_completed)
|
||||||
else
|
else
|
||||||
flash[:notice] = task.errors.full_messages.join(' and ')
|
task.errors.full_messages.join(' and ')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
render file: 'registrar/bulk_change/new', locals: { active_tab: :bulk_renew }
|
render file: 'registrar/bulk_change/new', locals: { active_tab: :bulk_renew }
|
||||||
|
|
|
@ -3,9 +3,46 @@ module Domains
|
||||||
class SingleDomainRenew < ActiveInteraction::Base
|
class SingleDomainRenew < ActiveInteraction::Base
|
||||||
object :domain,
|
object :domain,
|
||||||
class: Epp::Domain
|
class: Epp::Domain
|
||||||
|
integer :period
|
||||||
|
string :unit
|
||||||
|
object :registrar
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
|
in_transaction_with_retries_do {
|
||||||
|
success = domain.renew(domain.valid_to, period, unit)
|
||||||
|
|
||||||
|
if success
|
||||||
|
check_balance
|
||||||
|
reduce_balance
|
||||||
|
else
|
||||||
|
errors.add(:domain, I18n.t('domain_renew_error_for_domain', domain: domain.name))
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_balance
|
||||||
|
compose(Domains::CheckBalance::SingleDomain,
|
||||||
|
domain: domain,
|
||||||
|
operation: 'renew',
|
||||||
|
period: period,
|
||||||
|
unit: unit)
|
||||||
|
end
|
||||||
|
|
||||||
|
def reduce_balance
|
||||||
|
domain_pricelist = domain.pricelist('renew', period, unit)
|
||||||
|
registrar.debit!(sum: domain_pricelist.price.amount,
|
||||||
|
description: "#{I18n.t('renew')} #{domain.name}",
|
||||||
|
activity_type: AccountActivity::RENEW,
|
||||||
|
price: domain_pricelist)
|
||||||
|
end
|
||||||
|
|
||||||
|
def in_transaction_with_retries_do
|
||||||
|
ActiveRecord::Base.transaction(isolation: :serializable) do
|
||||||
|
yield if block_given?
|
||||||
|
end
|
||||||
|
rescue ActiveRecord::StatementInvalid
|
||||||
|
sleep rand / 100
|
||||||
|
retry
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,17 +5,36 @@ module Domains
|
||||||
object class: Epp::Domain
|
object class: Epp::Domain
|
||||||
end
|
end
|
||||||
string :period_element
|
string :period_element
|
||||||
|
object :registrar
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
period = (period_element.to_i == 0) ? 1 : period_element.to_i
|
if mass_check_balance.valid?
|
||||||
unit = period_element[-1] || 'y'
|
domains.each do |domain|
|
||||||
task = Domains::CheckBalance::Mass.run(domains: domains,
|
Domains::BulkRenew::SingleDomainRenew.run(domain: domain,
|
||||||
|
period: period,
|
||||||
|
unit: unit,
|
||||||
|
registrar: registrar)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
errors.merge!(mass_check_balance.errors)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def period
|
||||||
|
period_element.to_i.zero? ? 1 : period_element.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
def unit
|
||||||
|
period_element[-1] || 'y'
|
||||||
|
end
|
||||||
|
|
||||||
|
def mass_check_balance
|
||||||
|
Domains::CheckBalance::Mass.run(domains: domains,
|
||||||
operation: 'renew',
|
operation: 'renew',
|
||||||
period: period,
|
period: period,
|
||||||
unit: unit)
|
unit: unit)
|
||||||
unless task.valid?
|
|
||||||
errors.merge!(task.errors)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,6 @@ module Domains
|
||||||
array :domains do
|
array :domains do
|
||||||
object class: Epp::Domain
|
object class: Epp::Domain
|
||||||
end
|
end
|
||||||
|
|
||||||
string :operation
|
string :operation
|
||||||
integer :period
|
integer :period
|
||||||
string :unit
|
string :unit
|
||||||
|
|
|
@ -9,18 +9,25 @@ module Domains
|
||||||
string :unit
|
string :unit
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
domain_pricelist = domain.pricelist(operation, period.try(:to_i), unit)
|
|
||||||
if domain_pricelist.try(:price) # checking if price list is not found
|
if domain_pricelist.try(:price) # checking if price list is not found
|
||||||
if current_user.registrar.balance < domain_pricelist.price.amount
|
if current_user.registrar.balance < domain_pricelist.price.amount
|
||||||
errors.add(:domain, I18n.t('billing_failure_credit_balance_low_for_domain', domain: domain.name))
|
errors.add(:domain, I18n.t('billing_failure_credit_balance_low_for_domain',
|
||||||
|
domain: domain.name))
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
errors.add(:domain, I18n.t(:active_price_missing_for_operation_with_domain, domain: domain.name))
|
errors.add(:domain, I18n.t(:active_price_missing_for_operation_with_domain,
|
||||||
|
domain: domain.name))
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def domain_pricelist
|
||||||
|
domain.pricelist(operation, period.try(:to_i), unit)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -589,6 +589,7 @@ en:
|
||||||
create_new_invoice: 'Create new invoice'
|
create_new_invoice: 'Create new invoice'
|
||||||
billing_failure_credit_balance_low: 'Billing failure - credit balance low'
|
billing_failure_credit_balance_low: 'Billing failure - credit balance low'
|
||||||
billing_failure_credit_balance_low_for_domain: 'Billing failure - credit balance low for %{domain}'
|
billing_failure_credit_balance_low_for_domain: 'Billing failure - credit balance low for %{domain}'
|
||||||
|
domain_renew_error_for_domain: 'Domain renew error for %{domain}'
|
||||||
create: 'Create'
|
create: 'Create'
|
||||||
activity_type: 'Activity type'
|
activity_type: 'Activity type'
|
||||||
receipt_date_from: 'Receipt date from'
|
receipt_date_from: 'Receipt date from'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue