2016-03-23 18:39:41 +01:00
|
|
|
class Subscription < ActiveRecord::Base
|
|
|
|
include NotifyWith::NotificationAttachedObject
|
|
|
|
|
|
|
|
belongs_to :plan
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
has_many :invoices, as: :invoiced, dependent: :destroy
|
|
|
|
has_many :offer_days, dependent: :destroy
|
|
|
|
|
|
|
|
validates_presence_of :plan_id
|
2016-04-07 12:28:25 +02:00
|
|
|
validates_with SubscriptionGroupValidator
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
attr_accessor :card_token
|
|
|
|
|
|
|
|
# creation
|
2018-12-10 17:20:23 +01:00
|
|
|
after_save :notify_member_subscribed_plan
|
|
|
|
after_save :notify_admin_subscribed_plan
|
2016-03-23 18:39:41 +01:00
|
|
|
after_save :notify_partner_subscribed_plan, if: :of_partner_plan?
|
|
|
|
|
|
|
|
# Stripe subscription payment
|
2016-11-29 11:38:33 +01:00
|
|
|
# @params [invoice] if true then subscription pay itself, dont pay with reservation
|
|
|
|
# if false then subscription pay with reservation
|
2019-03-18 11:11:09 +01:00
|
|
|
def save_with_payment(operator_id, invoice = true, coupon_code = nil)
|
2018-12-06 18:26:01 +01:00
|
|
|
return unless valid?
|
2016-07-08 17:24:51 +02:00
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
begin
|
|
|
|
customer = Stripe::Customer.retrieve(user.stp_customer_id)
|
|
|
|
invoice_items = []
|
|
|
|
|
|
|
|
unless coupon_code.nil?
|
|
|
|
@coupon = Coupon.find_by(code: coupon_code)
|
|
|
|
raise InvalidCouponError if @coupon.nil? || @coupon.status(user.id) != 'active'
|
|
|
|
|
|
|
|
total = plan.amount
|
|
|
|
|
|
|
|
discount = 0
|
|
|
|
if @coupon.type == 'percent_off'
|
2018-12-10 13:24:00 +01:00
|
|
|
discount = (total * @coupon.percent_off / 100).to_i
|
2018-12-06 18:26:01 +01:00
|
|
|
elsif @coupon.type == 'amount_off'
|
|
|
|
discount = @coupon.amount_off
|
|
|
|
else
|
|
|
|
raise InvalidCouponError
|
2016-09-21 16:19:04 +02:00
|
|
|
end
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
invoice_items << Stripe::InvoiceItem.create(
|
|
|
|
customer: user.stp_customer_id,
|
|
|
|
amount: -discount,
|
|
|
|
currency: Rails.application.secrets.stripe_currency,
|
|
|
|
description: "coupon #{@coupon.code} - subscription"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# only add a wallet invoice item if pay subscription
|
|
|
|
# dont add if pay subscription + reservation
|
|
|
|
if invoice
|
|
|
|
@wallet_amount_debit = get_wallet_amount_debit
|
|
|
|
if @wallet_amount_debit != 0
|
|
|
|
invoice_items << Stripe::InvoiceItem.create(
|
|
|
|
customer: user.stp_customer_id,
|
|
|
|
amount: -@wallet_amount_debit,
|
|
|
|
currency: Rails.application.secrets.stripe_currency,
|
|
|
|
description: "wallet -#{@wallet_amount_debit / 100.0}"
|
|
|
|
)
|
2016-07-19 11:37:01 +02:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
new_subscription = customer.subscriptions.create(plan: plan.stp_plan_id, source: card_token)
|
|
|
|
self.stp_subscription_id = new_subscription.id
|
2016-03-23 18:39:41 +01:00
|
|
|
self.canceled_at = nil
|
2018-12-10 17:20:23 +01:00
|
|
|
self.expiration_date = Time.at(new_subscription.current_period_end)
|
2018-12-06 18:26:01 +01:00
|
|
|
save!
|
|
|
|
|
2018-12-10 17:20:23 +01:00
|
|
|
UsersCredits::Manager.new(user: user).reset_credits
|
2018-12-06 18:26:01 +01:00
|
|
|
|
|
|
|
# generate invoice
|
|
|
|
stp_invoice = Stripe::Invoice.all(customer: user.stp_customer_id, limit: 1).data.first
|
|
|
|
if invoice
|
2019-03-18 11:11:09 +01:00
|
|
|
db_invoice = generate_invoice(operator_id, stp_invoice.id, coupon_code)
|
2018-12-06 18:26:01 +01:00
|
|
|
# debit wallet
|
|
|
|
wallet_transaction = debit_user_wallet
|
|
|
|
if wallet_transaction
|
2018-12-10 13:24:00 +01:00
|
|
|
db_invoice.wallet_amount = @wallet_amount_debit
|
|
|
|
db_invoice.wallet_transaction_id = wallet_transaction.id
|
2016-07-20 15:07:43 +02:00
|
|
|
end
|
2018-12-10 13:24:00 +01:00
|
|
|
db_invoice.save
|
2016-07-19 11:37:01 +02:00
|
|
|
end
|
2018-12-06 18:26:01 +01:00
|
|
|
# cancel subscription after create
|
|
|
|
cancel
|
|
|
|
return true
|
|
|
|
rescue Stripe::CardError => card_error
|
|
|
|
clear_wallet_and_goupon_invoice_items(invoice_items)
|
|
|
|
logger.error card_error
|
|
|
|
errors[:card] << card_error.message
|
|
|
|
return false
|
|
|
|
rescue Stripe::InvalidRequestError => e
|
|
|
|
clear_wallet_and_goupon_invoice_items(invoice_items)
|
|
|
|
# Invalid parameters were supplied to Stripe's API
|
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
|
|
|
rescue Stripe::AuthenticationError => e
|
|
|
|
clear_wallet_and_goupon_invoice_items(invoice_items)
|
|
|
|
# Authentication with Stripe's API failed
|
|
|
|
# (maybe you changed API keys recently)
|
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
|
|
|
rescue Stripe::APIConnectionError => e
|
|
|
|
clear_wallet_and_goupon_invoice_items(invoice_items)
|
|
|
|
# Network communication with Stripe failed
|
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
|
|
|
rescue Stripe::StripeError => e
|
|
|
|
clear_wallet_and_goupon_invoice_items(invoice_items)
|
|
|
|
# Display a very generic error to the user, and maybe send
|
|
|
|
# yourself an email
|
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
2018-12-10 13:24:00 +01:00
|
|
|
rescue StandardError => e
|
2018-12-06 18:26:01 +01:00
|
|
|
clear_wallet_and_goupon_invoice_items(invoice_items)
|
|
|
|
# Something else happened, completely unrelated to Stripe
|
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
2016-03-23 18:39:41 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
# @params [invoice] if true then only the subscription is payed, without reservation
|
|
|
|
# if false then the subscription is payed with reservation
|
2019-03-18 11:11:09 +01:00
|
|
|
def save_with_local_payment(operator_id, invoice = true, coupon_code = nil)
|
2018-12-06 18:26:01 +01:00
|
|
|
return false unless valid?
|
|
|
|
|
2018-12-11 15:07:21 +01:00
|
|
|
set_expiration_date
|
2018-12-06 18:26:01 +01:00
|
|
|
return false unless save
|
|
|
|
|
2018-12-10 17:20:23 +01:00
|
|
|
UsersCredits::Manager.new(user: user).reset_credits
|
2018-12-06 18:26:01 +01:00
|
|
|
if invoice
|
|
|
|
@wallet_amount_debit = get_wallet_amount_debit
|
|
|
|
|
|
|
|
# debit wallet
|
|
|
|
wallet_transaction = debit_user_wallet
|
|
|
|
|
2019-03-18 11:11:09 +01:00
|
|
|
invoc = generate_invoice(operator_id, nil, coupon_code)
|
2019-01-10 16:50:54 +01:00
|
|
|
if wallet_transaction
|
|
|
|
invoc.wallet_amount = @wallet_amount_debit
|
|
|
|
invoc.wallet_transaction_id = wallet_transaction.id
|
2018-12-06 18:26:01 +01:00
|
|
|
end
|
2019-01-10 16:50:54 +01:00
|
|
|
invoc.save
|
2018-12-06 18:26:01 +01:00
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-03-18 11:11:09 +01:00
|
|
|
def generate_invoice(operator_id, stp_invoice_id = nil, coupon_code = nil)
|
2016-08-11 18:17:28 +02:00
|
|
|
coupon_id = nil
|
|
|
|
total = plan.amount
|
|
|
|
|
|
|
|
unless coupon_code.nil?
|
2016-11-24 17:57:48 +01:00
|
|
|
@coupon = Coupon.find_by(code: coupon_code)
|
|
|
|
|
|
|
|
unless @coupon.nil?
|
2016-11-29 14:57:43 +01:00
|
|
|
total = CouponService.new.apply(plan.amount, @coupon, user.id)
|
2016-11-24 17:57:48 +01:00
|
|
|
coupon_id = @coupon.id
|
2016-09-22 18:03:33 +02:00
|
|
|
end
|
2016-08-11 18:17:28 +02:00
|
|
|
end
|
|
|
|
|
2019-03-18 11:11:09 +01:00
|
|
|
invoice = Invoice.new(invoiced_id: id, invoiced_type: 'Subscription', user: user, total: total, stp_invoice_id: stp_invoice_id, coupon_id: coupon_id, operator_id: operator_id)
|
2016-03-23 18:39:41 +01:00
|
|
|
invoice.invoice_items.push InvoiceItem.new(amount: plan.amount, stp_invoice_item_id: stp_subscription_id, description: plan.name, subscription_id: self.id)
|
|
|
|
invoice
|
|
|
|
end
|
|
|
|
|
2019-03-18 11:11:09 +01:00
|
|
|
def generate_and_save_invoice(operator_id, stp_invoice_id = nil)
|
|
|
|
generate_invoice(operator_id, stp_invoice_id).save
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def cancel
|
2018-12-06 18:26:01 +01:00
|
|
|
return unless stp_subscription_id.present?
|
|
|
|
|
|
|
|
stp_subscription = stripe_subscription
|
|
|
|
stp_subscription.delete(at_period_end: true)
|
|
|
|
update_columns(canceled_at: Time.now)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def stripe_subscription
|
|
|
|
user.stripe_customer.subscriptions.retrieve(stp_subscription_id) if stp_subscription_id.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def expire(time)
|
2018-12-06 18:26:01 +01:00
|
|
|
if !expired?
|
2018-12-10 17:20:23 +01:00
|
|
|
update_columns(expiration_date: time, canceled_at: time)
|
2016-03-23 18:39:41 +01:00
|
|
|
notify_admin_subscription_canceled
|
|
|
|
notify_member_subscription_canceled
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
def expired?
|
2016-03-23 18:39:41 +01:00
|
|
|
expired_at <= Time.now
|
|
|
|
end
|
|
|
|
|
2018-12-10 13:24:00 +01:00
|
|
|
def expired_at
|
|
|
|
last_offered = offer_days.order(:end_at).last
|
|
|
|
return last_offered.end_at if last_offered
|
|
|
|
|
|
|
|
expiration_date
|
|
|
|
end
|
|
|
|
|
|
|
|
def free_extend(expiration)
|
|
|
|
return false if expiration <= expired_at
|
|
|
|
|
|
|
|
od = offer_days.create(start_at: expired_at, end_at: expiration)
|
|
|
|
invoice = Invoice.new(invoiced_id: od.id, invoiced_type: 'OfferDay', user: user, total: 0)
|
|
|
|
invoice.invoice_items.push InvoiceItem.new(amount: 0, description: plan.name, subscription_id: id)
|
|
|
|
invoice.save
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
if save
|
2018-12-10 13:24:00 +01:00
|
|
|
notify_subscription_extended(true)
|
2016-03-23 18:39:41 +01:00
|
|
|
return true
|
|
|
|
end
|
2018-12-06 18:26:01 +01:00
|
|
|
false
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2018-12-06 18:26:01 +01:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def notify_member_subscribed_plan
|
|
|
|
NotificationCenter.call type: 'notify_member_subscribed_plan',
|
|
|
|
receiver: user,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_admin_subscribed_plan
|
|
|
|
NotificationCenter.call type: 'notify_admin_subscribed_plan',
|
|
|
|
receiver: User.admins,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_admin_subscription_canceled
|
|
|
|
NotificationCenter.call type: 'notify_admin_subscription_canceled',
|
|
|
|
receiver: User.admins,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_member_subscription_canceled
|
|
|
|
NotificationCenter.call type: 'notify_member_subscription_canceled',
|
|
|
|
receiver: user,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_partner_subscribed_plan
|
|
|
|
NotificationCenter.call type: 'notify_partner_subscribed_plan',
|
|
|
|
receiver: plan.partners,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_subscription_extended(free_days)
|
|
|
|
meta_data = {}
|
2019-02-26 10:45:12 +01:00
|
|
|
meta_data[:free_days] = true if free_days
|
|
|
|
NotificationCenter.call type: :notify_member_subscription_extended,
|
|
|
|
receiver: user,
|
|
|
|
attached_object: self,
|
|
|
|
meta_data: meta_data
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-02-26 10:45:12 +01:00
|
|
|
NotificationCenter.call type: :notify_admin_subscription_extended,
|
|
|
|
receiver: User.admins,
|
|
|
|
attached_object: self,
|
|
|
|
meta_data: meta_data
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2018-12-11 15:07:21 +01:00
|
|
|
def set_expiration_date
|
|
|
|
start_at = Time.now
|
|
|
|
self.expiration_date = start_at + plan.duration
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def of_partner_plan?
|
|
|
|
plan.is_a?(PartnerPlan)
|
|
|
|
end
|
|
|
|
|
2016-07-08 17:24:51 +02:00
|
|
|
def get_wallet_amount_debit
|
|
|
|
total = plan.amount
|
2018-12-06 18:26:01 +01:00
|
|
|
total = CouponService.new.apply(total, @coupon, user.id) if @coupon
|
2016-07-08 17:24:51 +02:00
|
|
|
wallet_amount = (user.wallet.amount * 100).to_i
|
2018-12-06 18:26:01 +01:00
|
|
|
wallet_amount >= total ? total : wallet_amount
|
2016-07-08 17:24:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def debit_user_wallet
|
2018-12-06 18:26:01 +01:00
|
|
|
return unless @wallet_amount_debit.present? || @wallet_amount_debit.zero?
|
|
|
|
|
|
|
|
amount = @wallet_amount_debit / 100.0
|
|
|
|
WalletService.new(user: user, wallet: user.wallet).debit(amount, self)
|
2016-07-08 17:24:51 +02:00
|
|
|
end
|
2016-09-20 17:42:30 +02:00
|
|
|
|
|
|
|
def clear_wallet_and_goupon_invoice_items(invoice_items)
|
|
|
|
begin
|
|
|
|
invoice_items.each(&:delete)
|
|
|
|
rescue Stripe::InvalidRequestError => e
|
|
|
|
logger.error e
|
|
|
|
rescue Stripe::AuthenticationError => e
|
|
|
|
logger.error e
|
|
|
|
rescue Stripe::APIConnectionError => e
|
|
|
|
logger.error e
|
|
|
|
rescue Stripe::StripeError => e
|
|
|
|
logger.error e
|
|
|
|
rescue => e
|
|
|
|
logger.error e
|
|
|
|
end
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|