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
|
|
|
|
2020-11-16 16:37:40 +01:00
|
|
|
##
|
|
|
|
# @param subscription {Subscription}
|
2020-12-21 16:12:34 +01:00
|
|
|
# @param payment_details {Hash} as generated by Price.compute
|
2021-04-15 17:01:52 +02:00
|
|
|
# @param payment_id {String} from the payment gateway
|
2021-04-20 17:22:53 +02:00
|
|
|
# @param payment_type {String} the object type of payment_id
|
2020-11-16 16:37:40 +01:00
|
|
|
# @param schedule {Boolean}
|
2021-04-20 17:22:53 +02:00
|
|
|
# @param payment_method {String}
|
2020-11-16 16:37:40 +01:00
|
|
|
##
|
2021-04-20 17:22:53 +02:00
|
|
|
def pay_and_save(subscription, payment_details: nil, payment_id: nil, payment_type: nil, schedule: false, payment_method: nil)
|
2019-06-04 16:50:23 +02:00
|
|
|
return false if user_id.nil?
|
|
|
|
|
2020-12-21 16:12:34 +01:00
|
|
|
user = User.find(user_id)
|
2020-12-23 15:29:56 +01:00
|
|
|
subscription.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id
|
2020-12-21 16:12:34 +01:00
|
|
|
|
2020-12-23 15:29:56 +01:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
subscription.init_save
|
2020-12-30 10:16:39 +01:00
|
|
|
raise InvalidSubscriptionError unless subscription&.persisted?
|
|
|
|
|
2020-12-23 15:29:56 +01:00
|
|
|
payment = if schedule
|
|
|
|
generate_schedule(subscription: subscription,
|
|
|
|
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],
|
2021-04-20 17:22:53 +02:00
|
|
|
payment_id: payment_id,
|
|
|
|
payment_type: payment_type)
|
2020-12-23 15:29:56 +01:00
|
|
|
else
|
2021-04-15 17:01:52 +02:00
|
|
|
generate_invoice(subscription,
|
|
|
|
operator_profile_id,
|
|
|
|
payment_details,
|
|
|
|
payment_id: payment_id,
|
2021-04-20 17:22:53 +02:00
|
|
|
payment_type: payment_type,
|
2021-04-15 17:01:52 +02:00
|
|
|
payment_method: payment_method)
|
2020-12-23 15:29:56 +01:00
|
|
|
end
|
|
|
|
WalletService.debit_user_wallet(payment, user, subscription)
|
2020-12-29 18:55:00 +01:00
|
|
|
payment.save
|
2021-04-15 17:01:52 +02:00
|
|
|
payment.post_save(payment_id)
|
2020-12-23 15:29:56 +01:00
|
|
|
end
|
2020-12-21 16:12:34 +01:00
|
|
|
true
|
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-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
|
|
|
expiration_date: new_expiration_date
|
|
|
|
)
|
|
|
|
if new_sub.save
|
2021-02-10 13:32:13 +01:00
|
|
|
schedule = subscription.original_payment_schedule
|
2020-12-21 16:12:34 +01:00
|
|
|
details = Price.compute(true, new_sub.user, nil, [], plan_id: subscription.plan_id)
|
|
|
|
payment = if schedule
|
|
|
|
generate_schedule(subscription: new_sub,
|
|
|
|
total: details[:before_coupon],
|
|
|
|
operator_profile_id: operator_profile_id,
|
|
|
|
user: new_sub.user,
|
2020-12-23 16:25:24 +01:00
|
|
|
payment_method: schedule.payment_method,
|
2021-04-21 17:38:06 +02:00
|
|
|
payment_id: schedule.gateway_object('Stripe::SetupIntent').id)
|
2020-12-21 16:12:34 +01:00
|
|
|
else
|
2021-04-21 17:38:06 +02:00
|
|
|
generate_invoice(subscription,
|
|
|
|
operator_profile_id,
|
|
|
|
details,
|
|
|
|
payment_id: schedule.gateway_object('Stripe::SetupIntent').id,
|
|
|
|
payment_type: 'Stripe::SetupIntent',
|
|
|
|
payment_method: schedule.payment_method)
|
2020-12-21 16:12:34 +01:00
|
|
|
end
|
|
|
|
payment.save
|
2021-02-10 17:24:03 +01:00
|
|
|
payment.post_save(schedule&.stp_setup_intent_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
|
2020-12-21 16:12:34 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
##
|
|
|
|
# Generate the invoice for the given subscription
|
|
|
|
##
|
2020-12-30 10:16:39 +01:00
|
|
|
def generate_schedule(subscription: nil, total: nil, operator_profile_id: nil, user: nil, payment_method: nil, coupon: nil,
|
2021-04-20 17:22:53 +02:00
|
|
|
payment_id: nil, payment_type: nil)
|
2020-12-21 16:12:34 +01:00
|
|
|
operator = InvoicingProfile.find(operator_profile_id)&.user
|
|
|
|
|
|
|
|
PaymentScheduleService.new.create(
|
|
|
|
subscription,
|
|
|
|
total,
|
|
|
|
coupon: coupon,
|
|
|
|
operator: operator,
|
|
|
|
payment_method: payment_method,
|
2020-12-23 16:25:24 +01:00
|
|
|
user: user,
|
2021-04-20 17:22:53 +02:00
|
|
|
payment_id: payment_id,
|
|
|
|
payment_type: payment_type
|
2020-12-21 16:12:34 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Generate the invoice for the given subscription
|
|
|
|
##
|
2021-04-20 17:22:53 +02:00
|
|
|
def generate_invoice(subscription, operator_profile_id, payment_details, payment_id: nil, payment_type: nil, payment_method: nil)
|
2020-12-21 16:12:34 +01:00
|
|
|
InvoicesService.create(
|
|
|
|
payment_details,
|
|
|
|
operator_profile_id,
|
|
|
|
subscription: subscription,
|
2021-04-15 17:01:52 +02:00
|
|
|
payment_id: payment_id,
|
2021-04-20 17:22:53 +02:00
|
|
|
payment_type: payment_type,
|
2021-04-15 17:01:52 +02:00
|
|
|
payment_method: payment_method
|
2020-12-21 16:12:34 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-06-04 16:50:23 +02:00
|
|
|
end
|