spree_stripe_subscriptions 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9085cdba71a500882f7360cb06f86ef9d9ee5617c52440e6c02c57e8a04218d6
4
- data.tar.gz: d4891c7b4184367479b884b3cb740273b62d5fbcb1a18de4e3eb5ebeb81953db
3
+ metadata.gz: f79c6ac45672093c0d5e05829a45d29aee68f0d3ce738cfd15b0a61b7aaaac8f
4
+ data.tar.gz: 9c9d0c093cfe191b6fb935a8da17cd4ab62e54e622f9b912fd6b644da89324c7
5
5
  SHA512:
6
- metadata.gz: 693ad9f3a7739fdd12d52410ec29ecc0036106cea56e4ee2edef986ff53e64ec5ddfc40184c03f9945eabe15c112f0aa5e2ddd252a2f351f2fe2861eaecfbecd
7
- data.tar.gz: 5f4d4c7ba7b99676bd735834178a6aaf4f28f7793d13eaa465d2dd9fe3b7daa15b8fa5ae158fdd3659f1c1917e9fe4345baaef8948f74c7d9e02033b4969c91a
6
+ metadata.gz: e23cef4662313aa0a8ed116c7aa2669707dbbb0041b527d8650697951f90a868177c9afc9874190f88057623882256ae4f16081bbaa788f5cc493e4561d2587a
7
+ data.tar.gz: a629bea13f0f29b026db94c9abcc1142ca951d0e3c4a06a09096c0b9d1fe0b9cf78728dab2b0dbb5f598b056e974e772bc7604462fe464e699865693766e0b99
@@ -34,29 +34,42 @@ module Spree
34
34
 
35
35
  def self.create_or_update_subscription(event)
36
36
  event_data = event.data.object
37
- customer = Spree::StripeCustomer.find_by!(stripe_customer_id: event_data.customer)
38
- plan = Spree::StripePlan.find_by!(stripe_plan_id: event_data.plan.id)
39
37
 
40
- stripe_subscription_id = event_data.id
38
+ subscription_event = %w[customer.subscription.updated customer.subscription.deleted].include?(event.type)
39
+ invoice_event = (event.type == 'invoice.paid')
40
+ schedule_event = (event.type == 'subscription_schedule.updated')
41
41
 
42
- webhook_subscription = Spree::StripeSubscription.where(stripe_subscription_id: stripe_subscription_id).first_or_initialize
42
+ stripe_subscription_id = if invoice_event || schedule_event
43
+ event_data.subscription
44
+ elsif subscription_event
45
+ event_data.id
46
+ end
47
+
48
+ stripe_subscription = Stripe::Subscription.retrieve(stripe_subscription_id)
49
+
50
+ customer = Spree::StripeCustomer.find_by!(stripe_customer_id: stripe_subscription.customer)
51
+ plan = Spree::StripePlan.find_by!(stripe_plan_id: stripe_subscription.plan.id)
52
+
53
+ webhook_subscription = Spree::StripeSubscription.where(
54
+ stripe_subscription_id: stripe_subscription.id
55
+ ).first_or_initialize
43
56
 
44
57
  webhook_subscription.update(
45
- customer: customer, user: customer.user, plan: plan, status: event_data.status,
46
- current_period_start: event_data.current_period_start ? Time.at(event_data.current_period_start).utc.to_datetime : nil,
47
- current_period_end: event_data.current_period_end ? Time.at(event_data.current_period_end).utc.to_datetime : nil,
48
- billing_cycle_anchor: event_data.billing_cycle_anchor ? Time.at(event_data.billing_cycle_anchor).utc.to_datetime : nil,
49
- cancel_at_period_end: event_data.cancel_at_period_end,
50
- cancel_at: event_data.cancel_at ? Time.at(event_data.cancel_at).utc.to_datetime : nil,
51
- canceled_at: event_data.canceled_at ? Time.at(event_data.canceled_at).utc.to_datetime : nil,
52
- ended_at: event_data.ended_at ? Time.at(event_data.ended_at).utc.to_datetime : nil,
53
- schedule: event_data.schedule
58
+ customer: customer, user: customer.user, plan: plan, status: stripe_subscription.status,
59
+ current_period_start: stripe_subscription.current_period_start ? Time.at(stripe_subscription.current_period_start).utc.to_datetime : nil,
60
+ current_period_end: stripe_subscription.current_period_end ? Time.at(stripe_subscription.current_period_end).utc.to_datetime : nil,
61
+ billing_cycle_anchor: stripe_subscription.billing_cycle_anchor ? Time.at(stripe_subscription.billing_cycle_anchor).utc.to_datetime : nil,
62
+ cancel_at_period_end: stripe_subscription.cancel_at_period_end,
63
+ cancel_at: stripe_subscription.cancel_at ? Time.at(stripe_subscription.cancel_at).utc.to_datetime : nil,
64
+ canceled_at: stripe_subscription.canceled_at ? Time.at(stripe_subscription.canceled_at).utc.to_datetime : nil,
65
+ ended_at: stripe_subscription.ended_at ? Time.at(stripe_subscription.ended_at).utc.to_datetime : nil,
66
+ schedule: stripe_subscription.schedule
54
67
  )
55
68
 
56
- active_subscription = user.stripe_subscriptions.active.last
57
- if active_subscription.present?
69
+ active_subscription = customer.user.stripe_subscriptions.active.last
70
+ if active_subscription.present? && subscription_event
58
71
  # Checking if customer upgraded to other plan
59
- old_subscriptions = user.stripe_subscriptions.active.where.not(id: [active_subscription.id, webhook_subscription.id].uniq)
72
+ old_subscriptions = customer.user.stripe_subscriptions.active.where.not(id: [active_subscription.id, webhook_subscription.id].uniq)
60
73
  old_subscriptions.each do |old_subscription|
61
74
  # Unsubscribe from old subscription
62
75
  old_subscription.unsubscribe
@@ -90,6 +103,11 @@ module Spree
90
103
 
91
104
  webhook_subscription = Spree::StripeSubscription.find_by(stripe_subscription_id: stripe_subscription_id)
92
105
 
106
+ unless webhook_subscription.present?
107
+ # If invoice.paid webhook is sent earlier than subscription.updated then subscription will be created
108
+ webhook_subscription = Spree::StripeSubscription.create_or_update_subscription(event)
109
+ end
110
+
93
111
  if webhook_subscription.present?
94
112
  stripe_invoice_id = event_data.id
95
113
  customer = Spree::StripeCustomer.find_by(stripe_customer_id: event_data.customer)
@@ -1,5 +1,5 @@
1
1
  module SpreeStripeSubscriptions
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
 
4
4
  module_function
5
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_stripe_subscriptions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Satyakam Pandya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-01 00:00:00.000000000 Z
11
+ date: 2023-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree