Class: Pay::Stripe::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/stripe/subscription.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_subscription) ⇒ Subscription

Returns a new instance of Subscription.



52
53
54
# File 'lib/pay/stripe/subscription.rb', line 52

def initialize(pay_subscription)
  @pay_subscription = pay_subscription
end

Instance Attribute Details

#pay_subscriptionObject (readonly)

Returns the value of attribute pay_subscription.



4
5
6
# File 'lib/pay/stripe/subscription.rb', line 4

def pay_subscription
  @pay_subscription
end

Class Method Details

.sync(subscription_id, object: nil, name: Pay.default_product_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pay/stripe/subscription.rb', line 23

def self.sync(subscription_id, object: nil, name: Pay.default_product_name)
  # Skip loading the latest subscription details from the API if we already have it
  object ||= ::Stripe::Subscription.retrieve(id: subscription_id, expand: ["pending_setup_intent", "latest_invoice.payment_intent"])

  owner = Pay.find_billable(processor: :stripe, processor_id: object.customer)
  return unless owner

  attributes = {
    application_fee_percent: object.application_fee_percent,
    processor_plan: object.plan.id,
    quantity: object.quantity,
    name: name,
    status: object.status,
    stripe_account: owner.,
    trial_ends_at: (object.trial_end ? Time.at(object.trial_end) : nil)
  }

  # Subscriptions cancelling in the future
  attributes[:ends_at] = Time.at(object.current_period_end) if object.cancel_at_period_end

  # Fully cancelled subscription
  attributes[:ends_at] = Time.at(object.ended_at) if object.ended_at

  # Update or create the subscription
  pay_subscription = owner.subscriptions.find_or_initialize_by(processor: :stripe, processor_id: object.id)
  pay_subscription.update(attributes)
  pay_subscription
end

Instance Method Details

#cancelObject



60
61
62
63
64
65
# File 'lib/pay/stripe/subscription.rb', line 60

def cancel
  stripe_sub = ::Stripe::Subscription.update(processor_id, {cancel_at_period_end: true}, {stripe_account: })
  pay_subscription.update(ends_at: (on_trial? ? trial_ends_at : Time.at(stripe_sub.current_period_end)))
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

#cancel_now!Object



67
68
69
70
71
72
# File 'lib/pay/stripe/subscription.rb', line 67

def cancel_now!
  ::Stripe::Subscription.delete(processor_id, {stripe_account: })
  pay_subscription.update(ends_at: Time.current, status: :canceled)
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

#change_quantity(quantity) ⇒ Object



74
75
76
77
78
# File 'lib/pay/stripe/subscription.rb', line 74

def change_quantity(quantity)
  ::Stripe::Subscription.update(processor_id, quantity: quantity)
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

#on_grace_period?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/pay/stripe/subscription.rb', line 80

def on_grace_period?
  canceled? && Time.zone.now < ends_at
end

#pauseObject

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/pay/stripe/subscription.rb', line 88

def pause
  raise NotImplementedError, "Stripe does not support pausing subscriptions"
end

#paused?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/pay/stripe/subscription.rb', line 84

def paused?
  false
end

#resumeObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pay/stripe/subscription.rb', line 92

def resume
  unless on_grace_period?
    raise StandardError, "You can only resume subscriptions within their grace period."
  end

  ::Stripe::Subscription.update(
    processor_id,
    {
      plan: processor_plan,
      trial_end: (on_trial? ? trial_ends_at.to_i : "now"),
      cancel_at_period_end: false
    },
    {stripe_account: }
  )
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

#subscription(**options) ⇒ Object



56
57
58
# File 'lib/pay/stripe/subscription.rb', line 56

def subscription(**options)
  ::Stripe::Subscription.retrieve(options.merge(id: processor_id))
end

#swap(plan) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pay/stripe/subscription.rb', line 110

def swap(plan)
  ::Stripe::Subscription.update(
    processor_id,
    {
      cancel_at_period_end: false,
      plan: plan,
      proration_behavior: (prorate ? "create_prorations" : "none"),
      trial_end: (on_trial? ? trial_ends_at.to_i : "now"),
      quantity: quantity
    },
    {stripe_account: }
  )
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end