Class: Spree::StripeSubscription

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/stripe_subscription.rb

Constant Summary collapse

STATUS_OPTIONS =
{
  'incomplete' => 'Incomplete',
  'incomplete_expired' => 'Incomplete Expired',
  'trialing' => 'Trialing',
  'active' => 'Active',
  'past_due' => 'Past Due',
  'canceled' => 'Canceled',
  'unpaid' => 'Unpaid'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update_invoice(event) ⇒ Object



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
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/spree/stripe_subscription.rb', line 100

def self.create_or_update_invoice(event)
  event_data = event.data.object
  stripe_subscription_id = event_data.subscription

  webhook_subscription = Spree::StripeSubscription.find_by(stripe_subscription_id: stripe_subscription_id)

  unless webhook_subscription.present?
    # If invoice.paid webhook is sent earlier than subscription.updated then subscription will be created
    webhook_subscription = Spree::StripeSubscription.create_or_update_subscription(event)
  end

  if webhook_subscription.present?
    stripe_invoice_id = event_data.id
    customer = Spree::StripeCustomer.find_by(stripe_customer_id: event_data.customer)
    stripe_invoice = webhook_subscription.stripe_invoices.where(stripe_invoice_id: stripe_invoice_id).first_or_initialize

    period_start, period_end = if (line = event_data.lines.first)
                                 [line.period.start, line.period.end]
                               end

    stripe_invoice.update(
      customer: customer,
      user: customer.user,
      status: event_data.status,
      paid: event_data.paid,
      currency: event_data.currency,
      amount_paid: event_data.amount_paid || 0.0,
      subtotal: event_data.subtotal || 0.0,
      subtotal_excluding_tax: event_data.subtotal_excluding_tax || 0.0,
      tax: event_data.tax || 0.0,
      total: event_data.total || 0.0,
      total_excluding_tax: event_data.total_excluding_tax || 0.0,
      customer_name: event_data.customer_name,
      billing_reason: event_data.billing_reason,
      invoice_pdf: event_data.invoice_pdf,
      period_start: period_start ? Time.at(period_start).utc.to_datetime : nil,
      period_end: period_end ? Time.at(period_end).utc.to_datetime : nil,
      raw_data: event_data
    )
  end

  webhook_subscription
end

.create_or_update_subscription(event) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/spree/stripe_subscription.rb', line 35

def self.create_or_update_subscription(event)
  event_data = event.data.object

  subscription_event = %w[customer.subscription.updated customer.subscription.deleted].include?(event.type)
  invoice_event = (event.type == 'invoice.paid')
  schedule_event = (event.type == 'subscription_schedule.updated')

  stripe_subscription_id = if invoice_event || schedule_event
                             event_data.subscription
                           elsif subscription_event
                             event_data.id
                           end

  stripe_subscription = Stripe::Subscription.retrieve(stripe_subscription_id)

  customer = Spree::StripeCustomer.find_by!(stripe_customer_id: stripe_subscription.customer)
  plan = Spree::StripePlan.find_by!(stripe_plan_id: stripe_subscription.plan.id)

  webhook_subscription = Spree::StripeSubscription.where(
    stripe_subscription_id: stripe_subscription.id
  ).first_or_initialize

  webhook_subscription.update(
    customer: customer, user: customer.user, plan: plan, status: stripe_subscription.status,
    current_period_start: stripe_subscription.current_period_start ? Time.at(stripe_subscription.current_period_start).utc.to_datetime : nil,
    current_period_end: stripe_subscription.current_period_end ? Time.at(stripe_subscription.current_period_end).utc.to_datetime : nil,
    billing_cycle_anchor: stripe_subscription.billing_cycle_anchor ? Time.at(stripe_subscription.billing_cycle_anchor).utc.to_datetime : nil,
    cancel_at_period_end: stripe_subscription.cancel_at_period_end,
    cancel_at: stripe_subscription.cancel_at ? Time.at(stripe_subscription.cancel_at).utc.to_datetime : nil,
    canceled_at: stripe_subscription.canceled_at ? Time.at(stripe_subscription.canceled_at).utc.to_datetime : nil,
    ended_at: stripe_subscription.ended_at ? Time.at(stripe_subscription.ended_at).utc.to_datetime : nil,
    schedule: stripe_subscription.schedule
  )

  active_subscription = customer.user.stripe_subscriptions.active.last
  if active_subscription.present? && subscription_event
    # Checking if customer upgraded to other plan
    old_subscriptions = customer.user.stripe_subscriptions.active.where.not(id: [active_subscription.id, webhook_subscription.id].uniq)
    old_subscriptions.each do |old_subscription|
      # Unsubscribe from old subscription
      old_subscription.unsubscribe
    end
  end

  webhook_subscription
end

.update_subscription_schedule(event) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/spree/stripe_subscription.rb', line 82

def self.update_subscription_schedule(event)
  event_data = event.data.object
  stripe_subscription_id = event_data.subscription

  webhook_subscription = Spree::StripeSubscription.find_by(stripe_subscription_id: stripe_subscription_id)
  if webhook_subscription.present?
    if event_data.phases.count > 1
      last_phase = event_data.phases.last
      next_plan_id = last_phase.plans.first.plan
      next_plan = Spree::StripePlan.find_by(stripe_plan_id: next_plan_id)
      if next_plan.present?
        webhook_subscription.update(next_plan_id: next_plan.id)
      end
    end
  end
  webhook_subscription
end

Instance Method Details

#cancel_renewalObject



159
160
161
162
163
164
165
166
# File 'app/models/spree/stripe_subscription.rb', line 159

def cancel_renewal
  Stripe::Subscription.update(
    stripe_subscription_id,
    { cancel_at_period_end: true }
  )
rescue StandardError => e
  Rails.logger.error e
end

#register_webhook_event(event) ⇒ Object



176
177
178
179
180
181
182
183
# File 'app/models/spree/stripe_subscription.rb', line 176

def register_webhook_event(event)
  stripe_subscription_events.create(
    event_id: event.id,
    event_type: event.type,
    user: user,
    response: event.to_json
  )
end

#stripe_subscription_scheduleObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/spree/stripe_subscription.rb', line 144

def stripe_subscription_schedule
  stripe_schedule = nil
  if schedule.present?
    stripe_schedule = Stripe::SubscriptionSchedule.retrieve(schedule)
  else
    stripe_schedule = Stripe::SubscriptionSchedule.create({
                                                            from_subscription: stripe_subscription_id
                                                          })
  end
rescue StandardError => e
  Rails.logger.error e.message
ensure
  stripe_schedule
end

#unsubscribeObject



168
169
170
171
172
173
174
# File 'app/models/spree/stripe_subscription.rb', line 168

def unsubscribe
  Stripe::Subscription.delete(
    stripe_subscription_id
  )
rescue StandardError => e
  Rails.logger.error e
end