mirror of
https://github.com/internetee/registry.git
synced 2025-08-01 23:42:04 +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,12 +16,14 @@ class Registrar
|
|||
|
||||
if domain_ids_for_bulk_renew.present?
|
||||
domains = Epp::Domain.where(id: domain_ids_for_bulk_renew).to_a
|
||||
task = Domains::BulkRenew::Start.run(domains: domains, period_element: @period)
|
||||
if task.valid?
|
||||
flash[:notice] = t(:bulk_renew_completed)
|
||||
else
|
||||
flash[:notice] = task.errors.full_messages.join(' and ')
|
||||
end
|
||||
task = Domains::BulkRenew::Start.run(domains: domains,
|
||||
period_element: @period,
|
||||
registrar: current_registrar_user.registrar)
|
||||
flash[:notice] = if task.valid?
|
||||
t(:bulk_renew_completed)
|
||||
else
|
||||
task.errors.full_messages.join(' and ')
|
||||
end
|
||||
end
|
||||
render file: 'registrar/bulk_change/new', locals: { active_tab: :bulk_renew }
|
||||
end
|
||||
|
|
|
@ -3,9 +3,46 @@ module Domains
|
|||
class SingleDomainRenew < ActiveInteraction::Base
|
||||
object :domain,
|
||||
class: Epp::Domain
|
||||
integer :period
|
||||
string :unit
|
||||
object :registrar
|
||||
|
||||
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
|
||||
|
|
|
@ -5,18 +5,37 @@ module Domains
|
|||
object class: Epp::Domain
|
||||
end
|
||||
string :period_element
|
||||
object :registrar
|
||||
|
||||
def execute
|
||||
period = (period_element.to_i == 0) ? 1 : period_element.to_i
|
||||
unit = period_element[-1] || 'y'
|
||||
task = Domains::CheckBalance::Mass.run(domains: domains,
|
||||
operation: 'renew',
|
||||
period: period,
|
||||
unit: unit)
|
||||
unless task.valid?
|
||||
errors.merge!(task.errors)
|
||||
if mass_check_balance.valid?
|
||||
domains.each do |domain|
|
||||
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',
|
||||
period: period,
|
||||
unit: unit)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,6 @@ module Domains
|
|||
array :domains do
|
||||
object class: Epp::Domain
|
||||
end
|
||||
|
||||
string :operation
|
||||
integer :period
|
||||
string :unit
|
||||
|
|
|
@ -9,18 +9,25 @@ module Domains
|
|||
string :unit
|
||||
|
||||
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 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
|
||||
end
|
||||
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
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def domain_pricelist
|
||||
domain.pricelist(operation, period.try(:to_i), unit)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -589,6 +589,7 @@ en:
|
|||
create_new_invoice: 'Create new invoice'
|
||||
billing_failure_credit_balance_low: 'Billing failure - credit balance low'
|
||||
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'
|
||||
activity_type: 'Activity type'
|
||||
receipt_date_from: 'Receipt date from'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue