Debit account on domain create #2741

This commit is contained in:
Martin Lensment 2015-07-02 17:08:24 +03:00
parent e2374f9702
commit 98439b896c
7 changed files with 47 additions and 8 deletions

View file

@ -21,11 +21,18 @@ class Epp::DomainsController < EppController
def create
authorize! :create, Epp::Domain
@domain = Epp::Domain.new_from_epp(params[:parsed_frame], current_user)
@domain.valid?
if @domain.errors.any? || !@domain.save
handle_errors(@domain)
else
render_epp_response '/epp/domains/create'
handle_errors(@domain) and return if @domain.errors.any?
handle_errors and return unless balance_ok?('create')
ActiveRecord::Base.transaction do
if @domain.save
current_user.registrar.debit!(@domain_price, "#{I18n.t('create')} #{@domain.name}")
render_epp_response '/epp/domains/create'
else
handle_errors(@domain)
end
end
end
@ -185,4 +192,16 @@ class Epp::DomainsController < EppController
msg: "#{I18n.t(:client_side_status_editing_error)}: status [status]"
}
end
def balance_ok?(operation)
@domain_price = @domain.price(operation).amount
if current_user.registrar.balance < @domain_price
epp_errors << {
code: '2104',
msg: I18n.t('billing_failure_credit_balance_low')
}
return false
end
true
end
end

View file

@ -3,7 +3,9 @@ class BankTransaction < ActiveRecord::Base
belongs_to :bank_statement
has_one :account_activity
scope :unbinded, -> { where('id NOT IN (SELECT bank_transaction_id FROM account_activities)') }
scope :unbinded, -> {
where('id NOT IN (SELECT bank_transaction_id FROM account_activities where bank_transaction_id IS NOT NULL)')
}
def binded?
account_activity.present?

View file

@ -372,6 +372,12 @@ class Domain < ActiveRecord::Base
DomainMailer.pending_deleted(self).deliver_now
end
def price(operation)
zone = name.split('.').drop(1).join('.')
p = "#{self.period}year"
Pricelist.price_for(zone, operation, p)
end
### VALIDATIONS ###
def validate_nameserver_ips

View file

@ -23,8 +23,8 @@ class Pricelist < ActiveRecord::Base
end
class << self
def price_for(category, operation, duration)
lists = valid.where(category: category, operation_category: operation, duration: duration)
def price_for(zone, operation, period)
lists = valid.where(category: zone, operation_category: operation, duration: period)
return lists.first.price if lists.count == 1
lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first.price
end

View file

@ -12,6 +12,8 @@ class Registrar < ActiveRecord::Base
has_many :priv_contacts, -> { privs }, class_name: 'Contact'
has_many :white_ips, dependent: :destroy
delegate :balance, to: :cash_account
validates :name, :reg_no, :country_code, :email, :code, presence: true
validates :name, :reg_no, :reference_no, :code, uniqueness: true
validate :forbidden_codes
@ -121,6 +123,14 @@ class Registrar < ActiveRecord::Base
accounts.find_by(account_type: Account::CASH)
end
def debit!(sum, description)
cash_account.account_activities.create!(
sum: -sum,
currency: 'EUR',
description: description
)
end
def domain_transfers
at = DomainTransfer.arel_table
DomainTransfer.where(

View file

@ -20,7 +20,7 @@
- if x.invoice
%td= link_to(x.invoice, [:registrar, x.invoice])
- else
%td \-
%td -
- c = x.sum > 0.0 ? 'text-success' : 'text-danger'
- s = x.sum > 0.0 ? "+#{x.sum} #{x.currency}" : "#{x.sum} #{x.currency}"
%td{class: c}= s

View file

@ -854,3 +854,5 @@ en:
registry_zip: 'Postcode'
registry_country_code: 'Country'
blocked_domains: 'Blocked domains'
billing_failure_credit_balance_low: 'Billing failure - credit balance low'
create: 'Create'