1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-05 16:24:21 +01:00
fab-manager/app/services/reservations/reserve.rb

76 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-12-11 15:07:21 +01:00
# Provides helper methods for Reservation actions
class Reservations::Reserve
attr_accessor :user_id, :operator_profile_id
def initialize(user_id, operator_profile_id)
@user_id = user_id
@operator_profile_id = operator_profile_id
end
2018-12-11 15:07:21 +01:00
##
# Confirm the payment of the given reservation, generate the associated documents and save teh record into
# the database.
##
2020-12-23 16:25:24 +01:00
def pay_and_save(reservation, payment_details: nil, intent_id: nil, schedule: false, payment_method: nil)
user = User.find(user_id)
2019-06-06 16:34:53 +02:00
reservation.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id
ActiveRecord::Base.transaction do
reservation.pre_check
payment = if schedule
generate_schedule(reservation: reservation,
total: payment_details[:before_coupon],
operator_profile_id: operator_profile_id,
user: user,
payment_method: payment_method,
coupon: payment_details[:coupon],
2020-12-23 16:25:24 +01:00
setup_intent_id: intent_id)
else
2020-12-23 16:25:24 +01:00
generate_invoice(reservation, operator_profile_id, payment_details, intent_id)
end
WalletService.debit_user_wallet(payment, user, reservation)
2020-12-29 11:27:59 +01:00
reservation.save
reservation.post_save
payment.save
payment.post_save(intent_id)
end
2020-12-21 12:02:51 +01:00
true
end
private
##
# Generate the invoice for the given reservation+subscription
##
def generate_schedule(reservation: nil, total: nil, operator_profile_id: nil, user: nil, payment_method: nil, coupon: nil,
2020-12-23 16:25:24 +01:00
setup_intent_id: nil)
operator = InvoicingProfile.find(operator_profile_id)&.user
PaymentScheduleService.new.create(
nil,
total,
coupon: coupon,
operator: operator,
payment_method: payment_method,
user: user,
2020-12-23 16:25:24 +01:00
reservation: reservation,
setup_intent_id: setup_intent_id
)
end
##
# Generate the invoice for the given reservation
##
def generate_invoice(reservation, operator_profile_id, payment_details, payment_intent_id = nil)
InvoicesService.create(
payment_details,
operator_profile_id,
reservation: reservation,
payment_intent_id: payment_intent_id
)
end
2019-03-18 11:11:09 +01:00
end