2019-04-25 15:09:37 +02:00
|
|
|
# frozen_string_literal: true
|
2018-12-06 18:26:01 +01:00
|
|
|
|
2019-04-25 15:09:37 +02:00
|
|
|
# Provides helper methods for Subscription actions
|
|
|
|
class Subscriptions::Subscribe
|
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(operator_profile_id, user_id = nil)
|
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-06 18:26:01 +01:00
|
|
|
|
2019-04-25 15:09:37 +02:00
|
|
|
def extend_subscription(subscription, new_expiration_date, free_days)
|
2019-06-12 12:22:38 +02:00
|
|
|
return subscription.free_extend(new_expiration_date, @operator_profile_id) if free_days
|
2018-12-10 13:24:00 +01:00
|
|
|
|
2019-04-25 15:09:37 +02:00
|
|
|
new_sub = Subscription.create(
|
|
|
|
plan_id: subscription.plan_id,
|
2019-06-04 16:50:23 +02:00
|
|
|
statistic_profile_id: subscription.statistic_profile_id,
|
2019-04-25 15:09:37 +02:00
|
|
|
)
|
2021-05-31 11:52:53 +02:00
|
|
|
new_sub.expiration_date = new_expiration_date
|
2019-04-25 15:09:37 +02:00
|
|
|
if new_sub.save
|
2021-02-10 13:32:13 +01:00
|
|
|
schedule = subscription.original_payment_schedule
|
2021-04-23 12:52:06 +02:00
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
operator = InvoicingProfile.find(@operator_profile_id).user
|
|
|
|
cs = CartService.new(operator)
|
|
|
|
cart = cs.from_hash(customer_id: subscription.user.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: subscription.plan_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2021-04-23 12:52:06 +02:00
|
|
|
payment_schedule: !schedule.nil?)
|
|
|
|
details = cart.total
|
|
|
|
|
2020-12-21 16:12:34 +01:00
|
|
|
payment = if schedule
|
2021-05-21 18:25:18 +02:00
|
|
|
operator = InvoicingProfile.find(operator_profile_id)&.user
|
|
|
|
|
|
|
|
PaymentScheduleService.new.create(
|
2021-05-28 17:34:20 +02:00
|
|
|
[new_sub],
|
2021-05-21 18:25:18 +02:00
|
|
|
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
|
|
|
|
)
|
2020-12-21 16:12:34 +01:00
|
|
|
else
|
2021-05-21 18:25:18 +02:00
|
|
|
InvoicesService.create(
|
|
|
|
details,
|
|
|
|
operator_profile_id,
|
2021-05-28 17:34:20 +02:00
|
|
|
[new_sub],
|
|
|
|
new_sub.user
|
2021-05-21 18:25:18 +02:00
|
|
|
)
|
2020-12-21 16:12:34 +01:00
|
|
|
end
|
|
|
|
payment.save
|
2021-04-23 17:54:59 +02:00
|
|
|
payment.post_save(schedule&.gateway_payment_mean&.id)
|
2019-09-12 11:37:03 +02:00
|
|
|
UsersCredits::Manager.new(user: new_sub.user).reset_credits
|
2019-04-25 15:09:37 +02:00
|
|
|
return new_sub
|
2018-12-10 13:24:00 +01:00
|
|
|
end
|
2019-04-25 15:09:37 +02:00
|
|
|
false
|
2018-12-10 13:24:00 +01:00
|
|
|
end
|
2019-06-04 16:50:23 +02:00
|
|
|
end
|