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 'stripe'
|
|
|
|
require 'active_support/core_ext'
|
|
|
|
|
|
|
|
module ConstructBillingSubscription
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.with_stripe(np, billing_plan)
|
|
|
|
raise ArgumentError, 'Billing plan not found' if billing_plan.nil?
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
trial_end = QueryBillingSubscriptions.currently_in_trial?(np.id) ? (np.created_at + 15.days).to_i : nil
|
|
|
|
customer = Stripe::Customer.retrieve np.active_card.stripe_customer_id
|
|
|
|
stripe_subscription = customer.subscriptions.create(
|
|
|
|
plan: billing_plan.stripe_plan_id,
|
|
|
|
trial_end: trial_end
|
|
|
|
)
|
|
|
|
{
|
|
|
|
billing_plan_id: billing_plan.id,
|
|
|
|
stripe_subscription_id: stripe_subscription.id,
|
|
|
|
status: stripe_subscription.status
|
|
|
|
}
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|