2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'qx'
|
|
|
|
require 'delayed_job_helper'
|
|
|
|
require 'active_support/core_ext'
|
|
|
|
|
|
|
|
module InsertBillingSubscriptions
|
|
|
|
def self.trial(np_id, stripe_plan_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
nonprofit = Nonprofit.includes(:billing_subscription).find(np_id)
|
|
|
|
billing_plan = BillingPlan.where('stripe_plan_id = ?', stripe_plan_id).last
|
|
|
|
sub = nonprofit.create_billing_subscription(billing_plan: billing_plan, status: 'trialing')
|
|
|
|
n = 10
|
|
|
|
DelayedJobHelper.enqueue_job(self, :check_trial, [sub['id']], run_at: n.days.from_now)
|
|
|
|
{ json: sub }
|
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
|
|
{ json: { error: e }, status: :unprocessable_entity }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.check_trial(bs_id)
|
|
|
|
sub = Qx.fetch(:billing_subscriptions, bs_id).last
|
|
|
|
if sub['status'] == 'trialing'
|
|
|
|
Qx.update(:billing_subscriptions)
|
|
|
|
.set(status: 'inactive')
|
|
|
|
.timestamps
|
2019-07-30 21:29:24 +00:00
|
|
|
.where('id = $id', id: bs_id)
|
2018-03-25 17:30:42 +00:00
|
|
|
.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|