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
|
|
|
|
attr_accessor :user_id, :operator_id
|
|
|
|
|
2019-06-04 16:50:23 +02:00
|
|
|
def initialize(operator_id, user_id = nil)
|
2019-04-25 15:09:37 +02:00
|
|
|
@user_id = user_id
|
|
|
|
@operator_id = operator_id
|
|
|
|
end
|
2018-12-06 18:26:01 +01:00
|
|
|
|
2019-04-25 15:09:37 +02:00
|
|
|
def pay_and_save(subscription, payment_method, coupon, invoice)
|
2019-06-04 16:50:23 +02:00
|
|
|
return false if user_id.nil?
|
|
|
|
|
2019-06-06 16:34:53 +02:00
|
|
|
subscription.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id
|
2019-04-25 15:09:37 +02:00
|
|
|
if payment_method == :local
|
|
|
|
subscription.save_with_local_payment(operator_id, invoice, coupon)
|
|
|
|
elsif payment_method == :stripe
|
|
|
|
subscription.save_with_payment(operator_id, invoice, coupon)
|
2018-12-06 18:26:01 +01:00
|
|
|
end
|
2019-04-25 15:09:37 +02:00
|
|
|
end
|
2018-12-10 13:24:00 +01:00
|
|
|
|
2019-04-25 15:09:37 +02:00
|
|
|
def extend_subscription(subscription, new_expiration_date, free_days)
|
2019-06-11 16:56:11 +02:00
|
|
|
return subscription.free_extend(new_expiration_date, @operator_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
|
|
|
expiration_date: new_expiration_date
|
|
|
|
)
|
|
|
|
if new_sub.save
|
|
|
|
new_sub.user.generate_subscription_invoice(operator_id)
|
|
|
|
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
|