Add payment after order create for users
This commit is contained in:
parent
12b3a68972
commit
ee5ebd7ba4
3 changed files with 13 additions and 0 deletions
|
@ -22,6 +22,7 @@ class OrdersController < ApplicationController
|
|||
@products = Product.all
|
||||
@order_products = @order.order_products
|
||||
if @order.save
|
||||
@user.pay(@order.price)
|
||||
redirect_to root_path
|
||||
else
|
||||
render 'new'
|
||||
|
|
|
@ -30,4 +30,12 @@ class Order < ActiveRecord::Base
|
|||
|
||||
accepts_nested_attributes_for :order_products, reject_if: proc { |op| op[:count].to_i <= 0 }
|
||||
|
||||
def price
|
||||
price = 0
|
||||
products.each do |p|
|
||||
price += p.price * p.count(self)
|
||||
end
|
||||
price
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -23,5 +23,9 @@ class User < ActiveRecord::Base
|
|||
"#{name} #{last_name}"
|
||||
end
|
||||
|
||||
def pay(amount)
|
||||
self.increment!(:balance, - amount)
|
||||
end
|
||||
|
||||
has_secure_password
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue