1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/services/subscriptions/subscribe.rb
Sylvain c7a59c8cb7 WIP: refactoring to singularize the booking process
We need to achieve only one process for all booking, not one for subscription, one for reservations, etc.
Moreover we must store one object per invoice_item/payment_schedule_object and stop using Invoice.invoiced or PaymentSchedule.scheduled
2021-05-21 18:25:18 +02:00

63 lines
2.1 KiB
Ruby

# frozen_string_literal: true
# Provides helper methods for Subscription actions
class Subscriptions::Subscribe
attr_accessor :user_id, :operator_profile_id
def initialize(operator_profile_id, user_id = nil)
@user_id = user_id
@operator_profile_id = operator_profile_id
end
def extend_subscription(subscription, new_expiration_date, free_days)
return subscription.free_extend(new_expiration_date, @operator_profile_id) if free_days
new_sub = Subscription.create(
plan_id: subscription.plan_id,
statistic_profile_id: subscription.statistic_profile_id,
expiration_date: new_expiration_date
)
if new_sub.save
schedule = subscription.original_payment_schedule
operator = InvoicingProfile.find(@operator_profile_id).user
cs = CartService.new(operator)
cart = cs.from_hash(customer_id: subscription.user.id,
items: [
{
subscription: {
plan_id: subscription.plan_id
}
}
],
payment_schedule: !schedule.nil?)
details = cart.total
payment = if schedule
operator = InvoicingProfile.find(operator_profile_id)&.user
PaymentScheduleService.new.create(
new_sub,
details[:before_coupon],
operator: operator,
payment_method: schedule.payment_method,
user: new_sub.user,
payment_id: schedule.gateway_payment_mean&.id,
payment_type: schedule.gateway_payment_mean&.class
)
else
InvoicesService.create(
details,
operator_profile_id,
subscription: new_sub
)
end
payment.save
payment.post_save(schedule&.gateway_payment_mean&.id)
UsersCredits::Manager.new(user: new_sub.user).reset_credits
return new_sub
end
false
end
end