Renew billing #2741

This commit is contained in:
Martin Lensment 2015-07-03 17:28:54 +03:00
parent fa77056f81
commit 2f0666dec3
3 changed files with 37 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -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