2019-04-25 15:09:37 +02:00
|
|
|
# frozen_string_literal: true
|
2018-12-11 15:07:21 +01:00
|
|
|
|
2019-04-25 15:09:37 +02:00
|
|
|
# Provides helper methods for Reservation actions
|
|
|
|
class Reservations::Reserve
|
2019-06-12 12:22:38 +02:00
|
|
|
attr_accessor :user_id, :operator_profile_id
|
2019-04-25 15:09:37 +02:00
|
|
|
|
2019-06-12 12:22:38 +02:00
|
|
|
def initialize(user_id, operator_profile_id)
|
2019-04-25 15:09:37 +02:00
|
|
|
@user_id = user_id
|
2019-06-12 12:22:38 +02:00
|
|
|
@operator_profile_id = operator_profile_id
|
2019-04-25 15:09:37 +02:00
|
|
|
end
|
2018-12-11 15:07:21 +01:00
|
|
|
|
2020-12-16 18:33:43 +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)
|
2020-12-16 18:33:43 +01:00
|
|
|
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
|
2020-12-16 18:33:43 +01:00
|
|
|
|
2020-12-23 15:29:56 +01:00
|
|
|
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,
|
2020-12-30 10:16:39 +01:00
|
|
|
coupon: payment_details[:coupon],
|
2020-12-23 16:25:24 +01:00
|
|
|
setup_intent_id: intent_id)
|
2020-12-23 15:29:56 +01:00
|
|
|
else
|
2020-12-23 16:25:24 +01:00
|
|
|
generate_invoice(reservation, operator_profile_id, payment_details, intent_id)
|
2020-12-23 15:29:56 +01:00
|
|
|
end
|
2020-12-29 18:55:00 +01:00
|
|
|
WalletService.debit_user_wallet(payment, user, reservation)
|
2020-12-29 11:27:59 +01:00
|
|
|
reservation.save
|
2020-12-23 15:29:56 +01:00
|
|
|
reservation.post_save
|
2020-12-30 10:16:39 +01:00
|
|
|
payment.save
|
|
|
|
payment.post_save(intent_id)
|
2020-12-23 15:29:56 +01:00
|
|
|
end
|
2020-12-21 12:02:51 +01:00
|
|
|
true
|
2020-12-16 18:33:43 +01:00
|
|
|
end
|
|
|
|
|
2020-12-21 16:12:34 +01:00
|
|
|
private
|
|
|
|
|
2020-12-16 18:33:43 +01:00
|
|
|
##
|
|
|
|
# Generate the invoice for the given reservation+subscription
|
|
|
|
##
|
2020-12-30 10:16:39 +01:00
|
|
|
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)
|
2020-12-16 18:33:43 +01:00
|
|
|
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
|
2020-12-16 18:33:43 +01:00
|
|
|
)
|
|
|
|
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
|