houdini/lib/construct/construct_billing_subscription.rb
2019-11-05 13:38:36 -06:00

21 lines
656 B
Ruby

# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
require 'stripe'
require 'active_support/core_ext'
module ConstructBillingSubscription
def self.with_stripe(np, billing_plan)
raise ArgumentError, 'Billing plan not found' if billing_plan.nil?
customer = Stripe::Customer.retrieve np.active_card.stripe_customer_id
stripe_subscription = customer.subscriptions.create(
plan: billing_plan.stripe_plan_id
)
{
billing_plan_id: billing_plan.id,
stripe_subscription_id: stripe_subscription.id,
status: stripe_subscription.status
}
end
end