-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathorder.rb
More file actions
29 lines (25 loc) · 843 Bytes
/
order.rb
File metadata and controls
29 lines (25 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Order < ApplicationRecord
belongs_to :reservation
belongs_to :user
belongs_to :item
has_one :payment, dependent: :destroy
validates :email, presence: true
validates :name, presence: true
def execute_payment_and_update_reservation_and_item(token:)
response = PaymentApiClient.execute(token:, amount: item.price)
ActiveRecord::Base.transaction do
build_payment(payment_id: response[:payment_id], amount: response[:amount])
if reservation.status == "completed"
raise ActiveRecord::RecordInvalid, "Reservation is already completed"
end
reservation.update!(status: :completed)
item.update!(stock: item.stock - 1)
save!
end
true
rescue Timeout::Error, ArgumentError
false
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
false
end
end