spree_stripe_subscriptions 0.0.1 → 0.0.4
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 +4 -4
- data/app/controllers/spree/stripe_subscriptions_controller.rb +1 -0
- data/app/models/spree/stripe_subscription.rb +34 -16
- data/app/views/spree/admin/stripe_configurations/_form.html.erb +7 -0
- data/config/locales/en.yml +1 -0
- data/db/migrate/20230711073051_add_promotion_field_to_configurations.rb +5 -0
- data/lib/spree_stripe_subscriptions/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9993c64782cf530fa6c6188abe90a01237b30bceaf0966263869e5763066cf3d
|
4
|
+
data.tar.gz: 5ea78d7490da356490610d931046581ca43ac70891a0bbbd2fc3e5ba277b8600
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84626b69ea36f39ba111fa3c03ae4a88fa3c5061fc2cc027bb9507d81546184b45560491d1a10cfddd71021713ba30dce78516c95bc5547bc8dbd514d2e26035
|
7
|
+
data.tar.gz: 70aacf21c781cb71ecc138be06754d29dcfc8b957b439a6732efafb9af3046a97b2c8c8bc9cd3764fc9e7c76e905a91bad4d7608b504f2b744cd85f0df023766
|
@@ -20,6 +20,7 @@ module Spree
|
|
20
20
|
'quantity': 1
|
21
21
|
}
|
22
22
|
],
|
23
|
+
allow_promotion_codes: @configuration.allow_promotion_codes,
|
23
24
|
automatic_tax: { enabled: @configuration.automatic_tax },
|
24
25
|
tax_id_collection: { enabled: @configuration.tax_id_collection },
|
25
26
|
billing_address_collection: @configuration.billing_address_collection,
|
@@ -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
|
-
|
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
|
-
|
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:
|
46
|
-
current_period_start:
|
47
|
-
current_period_end:
|
48
|
-
billing_cycle_anchor:
|
49
|
-
cancel_at_period_end:
|
50
|
-
cancel_at:
|
51
|
-
canceled_at:
|
52
|
-
ended_at:
|
53
|
-
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)
|
@@ -19,6 +19,13 @@
|
|
19
19
|
<%= f.select :billing_address_collection, Spree::StripeConfiguration::ADDRESS_COLLECTION_OPTIONS.collect { |k, v| [v, k] }, {}, { class: 'select2' } %>
|
20
20
|
<% end %>
|
21
21
|
|
22
|
+
<%= f.field_container :allow_promotion_codes, class: ['checkbox'] do %>
|
23
|
+
<%= f.label :allow_promotion_codes do %>
|
24
|
+
<%= f.check_box :allow_promotion_codes %>
|
25
|
+
<%= I18n.t('spree_stripe_subscriptions.models.stripe_configuration.allow_promotion_codes') %>
|
26
|
+
<% end %>
|
27
|
+
<% end %>
|
28
|
+
|
22
29
|
<%= f.field_container :automatic_tax, class: ['checkbox'] do %>
|
23
30
|
<%= f.label :automatic_tax do %>
|
24
31
|
<%= f.check_box :automatic_tax %>
|
data/config/locales/en.yml
CHANGED
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.
|
4
|
+
version: 0.0.4
|
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-
|
11
|
+
date: 2023-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: spree
|
@@ -174,6 +174,7 @@ files:
|
|
174
174
|
- db/migrate/20221209094013_add_stripe_invoices.rb
|
175
175
|
- db/migrate/20221209131817_add_user_to_invoice.rb
|
176
176
|
- db/migrate/20221221080141_add_extra_plan_fields.rb
|
177
|
+
- db/migrate/20230711073051_add_promotion_field_to_configurations.rb
|
177
178
|
- lib/generators/spree_stripe_subscriptions/install/install_generator.rb
|
178
179
|
- lib/spree_stripe_subscriptions.rb
|
179
180
|
- lib/spree_stripe_subscriptions/engine.rb
|