1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/services/subscriptions/subscribe.rb

35 lines
993 B
Ruby
Raw Normal View History

2018-12-10 17:20:23 +01:00
module Subscriptions
class Subscribe
2019-03-18 11:11:09 +01:00
attr_accessor :user_id, :operator_id
2019-03-18 11:11:09 +01:00
def initialize(user_id, operator_id)
2018-12-10 17:20:23 +01:00
@user_id = user_id
2019-03-18 11:11:09 +01:00
@operator_id = operator_id
2018-12-10 17:20:23 +01:00
end
2018-12-10 17:20:23 +01:00
def pay_and_save(subscription, payment_method, coupon, invoice)
subscription.user_id = user_id
if payment_method == :local
2019-03-18 11:11:09 +01:00
subscription.save_with_local_payment(operator_id, invoice, coupon)
2018-12-10 17:20:23 +01:00
elsif payment_method == :stripe
2019-03-18 11:11:09 +01:00
subscription.save_with_payment(operator_id, invoice, coupon)
2018-12-10 17:20:23 +01:00
end
end
2018-12-10 17:20:23 +01:00
def extend_subscription(subscription, new_expiration_date, free_days)
return subscription.free_extend(new_expiration_date) if free_days
2018-12-10 17:20:23 +01:00
new_sub = Subscription.create(
plan_id: subscription.plan_id,
user_id: subscription.user_id,
expiration_date: new_expiration_date
)
if new_sub.save
2019-03-18 11:11:09 +01:00
new_sub.user.generate_subscription_invoice(operator_id)
2018-12-11 17:27:25 +01:00
return new_sub
2018-12-10 17:20:23 +01:00
end
false
end
end
end