diff --git a/app/controllers/epp/domains_controller.rb b/app/controllers/epp/domains_controller.rb index 5ca4f0a0e..c2e92ea49 100644 --- a/app/controllers/epp/domains_controller.rb +++ b/app/controllers/epp/domains_controller.rb @@ -87,13 +87,27 @@ class Epp::DomainsController < EppController def renew authorize! :renew, @domain - handle_errors(@domain) and return unless @domain.renew( - params[:parsed_frame].css('curExpDate').text, - params[:parsed_frame].css('period').text, - params[:parsed_frame].css('period').first['unit'] - ) + period = params[:parsed_frame].css('period').text + period_unit = params[:parsed_frame].css('period').first['unit'] - render_epp_response '/epp/domains/renew' + ActiveRecord::Base.transaction do + success = @domain.renew( + params[:parsed_frame].css('curExpDate').text, + period, period_unit + ) + + if success + unless balance_ok?('renew', period, period_unit) + handle_errors + fail ActiveRecord::Rollback + end + + current_user.registrar.debit!(@domain_price, "#{I18n.t('renew')} #{@domain.name}") + render_epp_response '/epp/domains/renew' + else + handle_errors(@domain) + end + end end def transfer @@ -197,13 +211,14 @@ class Epp::DomainsController < EppController } end - def balance_ok?(operation) - @domain_price = @domain.price(operation).amount + def balance_ok?(operation, period = nil, unit = nil) + @domain_price = @domain.price(operation, period.try(:to_i), unit).amount if current_user.registrar.balance < @domain_price epp_errors << { code: '2104', msg: I18n.t('billing_failure_credit_balance_low') } + return false end true diff --git a/app/models/domain.rb b/app/models/domain.rb index 1800c98ec..a39f7b8da 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -372,14 +372,22 @@ class Domain < ActiveRecord::Base DomainMailer.pending_deleted(self).deliver_now end - def price(operation) + def price(operation, period_i = nil, unit = nil) + period_i ||= period + unit ||= period_unit + zone = name.split('.').drop(1).join('.') - p = period / 365 if period_unit == 'd' - p = period / 12 if period_unit == 'm' - p = period if period_unit == 'y' + p = period_i / 365 if unit == 'd' + p = period_i / 12 if unit == 'm' + p = period_i if unit == 'y' + + if p > 1 + p = "#{p}years" + else + p = "#{p}year" + end - p = "#{p}year" Pricelist.price_for(zone, operation, p) end diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index c71e4117e..a3460db90 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -20,6 +20,7 @@ describe 'EPP Domain', epp: true do Fabricate(:reserved_domain) Fabricate(:blocked_domain) Fabricate(:pricelist, valid_to: nil) + Fabricate(:pricelist, operation_category: 'renew', price: 15, valid_to: nil) @uniq_no = proc { @i ||= 0; @i += 1 } end