Class: Reji::SubscriptionBuilder

Inherits:
Object
  • Object
show all
Includes:
InteractsWithPaymentBehavior, Prorates
Defined in:
lib/reji/subscription_builder.rb

Instance Method Summary collapse

Methods included from Prorates

#always_invoice, #no_prorate, #prorate, #proration_behavior, #set_proration_behavior

Methods included from InteractsWithPaymentBehavior

#allow_payment_failures, #error_if_payment_fails, #payment_behavior, #pending_if_payment_fails

Constructor Details

#initialize(owner, name, plans = []) ⇒ SubscriptionBuilder

Create a new subscription builder instance.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/reji/subscription_builder.rb', line 9

def initialize(owner, name, plans = [])
  @owner = owner
  @name = name
  @trial_expires = nil # The date and time the trial will expire.
  @skip_trial = false # Indicates that the trial should end immediately.
  @billing_cycle_anchor = nil # The date on which the billing cycle should be anchored.
  @coupon = nil # The coupon code being applied to the customer.
  @metadata = nil # The metadata to apply to the subscription.
  @items = {}

  plans = [plans] unless plans.instance_of? Array

  plans.each { |plan| self.plan(plan) }
end

Instance Method Details

#add(customer_options = {}, subscription_options = {}) ⇒ Object

Add a new Stripe subscription to the Stripe model.



94
95
96
# File 'lib/reji/subscription_builder.rb', line 94

def add(customer_options = {}, subscription_options = {})
  create(nil, customer_options, subscription_options)
end

#anchor_billing_cycle_on(date) ⇒ Object

Change the billing cycle anchor on a plan creation.



73
74
75
76
77
# File 'lib/reji/subscription_builder.rb', line 73

def anchor_billing_cycle_on(date)
  @billing_cycle_anchor = date

  self
end

#create(payment_method = nil, customer_options = {}, subscription_options = {}) ⇒ Object

Create a new Stripe subscription.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/reji/subscription_builder.rb', line 99

def create(payment_method = nil, customer_options = {}, subscription_options = {})
  customer = get_stripe_customer(payment_method, customer_options)

  payload = { customer: customer.id }.merge(build_payload).merge(subscription_options)

  stripe_subscription = Stripe::Subscription.create(
    payload,
    @owner.stripe_options
  )

  subscription = @owner.subscriptions.create({
    name: @name,
    stripe_id: stripe_subscription.id,
    stripe_status: stripe_subscription.status,
    stripe_plan: stripe_subscription.plan ? stripe_subscription.plan.id : nil,
    quantity: stripe_subscription.quantity,
    trial_ends_at: @skip_trial ? nil : @trial_expires,
    ends_at: nil,
  })

  stripe_subscription.items.each do |item|
    subscription.items.create({
      stripe_id: item.id,
      stripe_plan: item.plan.id,
      quantity: item.quantity,
    })
  end

  Payment.new(stripe_subscription.latest_invoice.payment_intent).validate if subscription.incomplete_payment?

  subscription
end

#plan(plan, quantity = 1) ⇒ Object

Set a plan on the subscription builder.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/reji/subscription_builder.rb', line 25

def plan(plan, quantity = 1)
  options = {
    plan: plan,
    quantity: quantity,
  }

  tax_rates = get_plan_tax_rates_for_payload(plan)

  options[:tax_rates] = tax_rates if tax_rates

  @items[plan] = options

  self
end

#quantity(quantity, plan = nil) ⇒ Object

Specify the quantity of a subscription item.



41
42
43
44
45
46
47
48
49
# File 'lib/reji/subscription_builder.rb', line 41

def quantity(quantity, plan = nil)
  if plan.nil?
    raise ArgumentError, 'Plan is required when creating multi-plan subscriptions.' if @items.length > 1

    plan = @items.values[0][:plan]
  end

  self.plan(plan, quantity)
end

#skip_trialObject

Force the trial to end immediately.



66
67
68
69
70
# File 'lib/reji/subscription_builder.rb', line 66

def skip_trial
  @skip_trial = true

  self
end

#trial_days(trial_days) ⇒ Object

Specify the number of days of the trial.



52
53
54
55
56
# File 'lib/reji/subscription_builder.rb', line 52

def trial_days(trial_days)
  @trial_expires = Time.current + trial_days.days

  self
end

#trial_until(trial_until) ⇒ Object

Specify the ending date of the trial.



59
60
61
62
63
# File 'lib/reji/subscription_builder.rb', line 59

def trial_until(trial_until)
  @trial_expires = trial_until

  self
end

#with_coupon(coupon) ⇒ Object

The coupon to apply to a new subscription.



80
81
82
83
84
# File 'lib/reji/subscription_builder.rb', line 80

def with_coupon(coupon)
  @coupon = coupon

  self
end

#with_metadata(metadata) ⇒ Object

The metadata to apply to a new subscription.



87
88
89
90
91
# File 'lib/reji/subscription_builder.rb', line 87

def ()
  @metadata = 

  self
end