stripe_model_callbacks 0.1.6 → 0.1.8
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/README.md +3 -3
- data/Rakefile +1 -1
- data/app/models/stripe_model_callbacks/application_record.rb +11 -7
- data/app/services/stripe_model_callbacks/account/external_account/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/base_service.rb +6 -6
- data/app/services/stripe_model_callbacks/charge/dispute_updated_service.rb +3 -3
- data/app/services/stripe_model_callbacks/charge/updated_service.rb +5 -12
- data/app/services/stripe_model_callbacks/configure_service.rb +62 -26
- data/app/services/stripe_model_callbacks/customer/bank_account/deleted_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/discount_updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/source_updated_service.rb +3 -3
- data/app/services/stripe_model_callbacks/customer/subscription/updated_service.rb +2 -2
- data/app/services/stripe_model_callbacks/customer/updated_service.rb +2 -2
- data/app/services/stripe_model_callbacks/event_mocker_service.rb +14 -8
- data/app/services/stripe_model_callbacks/invoice/updated_service.rb +4 -4
- data/app/services/stripe_model_callbacks/payment_intent/updated_service.rb +19 -0
- data/app/services/stripe_model_callbacks/payment_method/updated_service.rb +19 -0
- data/app/services/stripe_model_callbacks/payout/updated_service.rb +3 -3
- data/app/services/stripe_model_callbacks/plan/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/price/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/product/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/recipient/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/review/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/setup_intent/updated_service.rb +8 -0
- data/app/services/stripe_model_callbacks/sku/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/source/updated_service.rb +4 -4
- data/app/services/stripe_model_callbacks/subscription/state_checker_service.rb +1 -1
- data/app/services/stripe_model_callbacks/subscription_schedule/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/sync_everything.rb +17 -2
- data/app/services/stripe_model_callbacks/sync_from_stripe.rb +1 -1
- data/app/services/stripe_model_callbacks/tax_rate/updated_service.rb +3 -3
- data/app/services/stripe_model_callbacks/transfer/updated_service.rb +1 -1
- data/config/rails_best_practices.yml +1 -0
- data/db/migrate/20180206151132_change_activities_trackable_id_to_string.rb +1 -1
- data/db/migrate/20180208091647_change_stripe_subscription_items_quantity_to_integer.rb +1 -1
- data/db/migrate/20181219121712_add_new_primary_id_and_rename_old.rb +2 -2
- data/db/migrate/20230129142139_add_tax_rate_to_stripe_tax_rates.rb +5 -0
- data/db/migrate/20230129145312_add_country_to_stripe_tax_rates.rb +5 -0
- data/db/migrate/20230210081405_create_stripe_setup_intents.rb +28 -0
- data/db/migrate/20230331182902_add_amount_captured_to_stripe_charges.rb +6 -0
- data/db/migrate/20230420092307_create_stripe_payment_methods.rb +15 -0
- data/db/migrate/20230421072509_create_stripe_payment_intents.rb +44 -0
- data/db/migrate/20230422074329_add_payment_intent_to_stripe_charges.rb +6 -0
- data/db/migrate/20230422175529_add_payment_intent_to_stripe_refunds.rb +6 -0
- data/db/migrate/20230422180602_change_stripe_refunds_stripe_charge_id_to_nullable.rb +5 -0
- data/db/migrate/20230518072538_change_stripe_setup_intents_payment_method_to_string.rb +7 -0
- data/lib/stripe_model_callbacks/configuration.rb +1 -1
- data/lib/stripe_model_callbacks/event_mocker.rb +1 -1
- data/lib/stripe_model_callbacks/factories/stripe_charges.rb +10 -0
- data/lib/stripe_model_callbacks/factories/stripe_coupons.rb +9 -9
- data/lib/stripe_model_callbacks/factories/stripe_customers.rb +6 -4
- data/lib/stripe_model_callbacks/factories/stripe_payment_intents.rb +17 -0
- data/lib/stripe_model_callbacks/factories/stripe_payment_methods.rb +15 -0
- data/lib/stripe_model_callbacks/factories/stripe_plans.rb +9 -12
- data/lib/stripe_model_callbacks/factories/stripe_products.rb +6 -8
- data/lib/stripe_model_callbacks/factories/stripe_setup_intents.rb +7 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_items.rb +6 -7
- data/lib/stripe_model_callbacks/factories/stripe_subscriptions.rb +10 -12
- data/lib/stripe_model_callbacks/factories/stripe_tax_rates.rb +10 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.refunded.json +1 -22
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.amount_capturable_updated.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.canceled.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.created.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.partially_funded.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.payment_failed.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.processing.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.requires_action.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.succeeded.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.attached.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.automatically_updated.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.card_automatically_updated.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.detached.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.updated.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/refund/refund.created.json +28 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/setup_intent/setup_intent.created.json +47 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/setup_intent/setup_intent.updated.json +47 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/tax_rate/tax_rate.created.json +3 -1
- data/lib/stripe_model_callbacks/fixtures/stripe_events/tax_rate/tax_rate.updated.json +3 -1
- data/lib/stripe_model_callbacks/models/stripe_charge.rb +47 -3
- data/lib/stripe_model_callbacks/models/stripe_coupon.rb +11 -0
- data/lib/stripe_model_callbacks/models/stripe_customer.rb +12 -0
- data/lib/stripe_model_callbacks/models/stripe_invoice.rb +16 -1
- data/lib/stripe_model_callbacks/models/stripe_payment_intent.rb +126 -0
- data/lib/stripe_model_callbacks/models/stripe_payment_method.rb +32 -0
- data/lib/stripe_model_callbacks/models/stripe_plan.rb +13 -0
- data/lib/stripe_model_callbacks/models/stripe_product.rb +10 -0
- data/lib/stripe_model_callbacks/models/stripe_refund.rb +3 -1
- data/lib/stripe_model_callbacks/models/stripe_setup_intent.rb +21 -0
- data/lib/stripe_model_callbacks/models/stripe_source.rb +2 -1
- data/lib/stripe_model_callbacks/models/stripe_subscription.rb +23 -4
- data/lib/stripe_model_callbacks/models/stripe_subscription_item.rb +10 -1
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule.rb +1 -1
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule_phase.rb +1 -1
- data/lib/stripe_model_callbacks/models/stripe_tax_rate.rb +7 -1
- data/lib/stripe_model_callbacks/version.rb +1 -1
- data/lib/stripe_model_callbacks.rb +1 -1
- metadata +48 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8e42a27077a6efe3f89ef606f9cd40fc107ac4a84f96c75953c12fb42066d9a
|
4
|
+
data.tar.gz: 6904a23f07770ab9fcd00dfce19293e878294fd1d2651fd46f7b917868b74f43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c77cb2e07d248973a8f8b7a675fbecf49c65488d8870f8e63955c4e4f72b871d0336d293b4340d430c7d6a9e1a8617cd8c51ac36ced43116b96df2ad60f16493
|
7
|
+
data.tar.gz: 743908583a4e1f124a9462c2e78903768da48750aea8a6c0c7f752c0c0b6eb5b26d35c6163e3617be861661030fcf4f3adc7dd73b17d02741f11ef0a7ac1ad3f
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# StripeModelCallbacks
|
2
|
-
Stripe database models using ActiveRecord, Stripe event webhooks synchronization and
|
2
|
+
Stripe database models using ActiveRecord, Stripe event webhooks synchronization and ActiveRecordAuditable.
|
3
3
|
|
4
4
|
This is supposed to make it easier implementing a full blown Stripe implementation into your application,
|
5
5
|
so that you can code your app using ActiveRecord and all your favorite gems without having to mess around
|
@@ -20,11 +20,11 @@ And then execute:
|
|
20
20
|
bundle
|
21
21
|
```
|
22
22
|
|
23
|
-
You also need to install and setup the gems `
|
23
|
+
You also need to install and setup the gems `active_record_auditable`, `stripe` and `stripe_event`. Do this:
|
24
24
|
|
25
25
|
Install the migration for Public Activity, which will provide logging:
|
26
26
|
```bash
|
27
|
-
rails
|
27
|
+
rails active_record_auditable:install:migrations
|
28
28
|
```
|
29
29
|
|
30
30
|
You can install the migrations (or update an existing installation) with this command:
|
data/Rakefile
CHANGED
@@ -20,7 +20,7 @@ load "rails/tasks/statistics.rake"
|
|
20
20
|
|
21
21
|
require "bundler/gem_tasks"
|
22
22
|
|
23
|
-
if Rails.env.development? || Rails.env.test?
|
23
|
+
if Rails.env.development? || Rails.env.test? # rubocop:disable Rails/EnvLocal
|
24
24
|
require "best_practice_project"
|
25
25
|
BestPracticeProject.load_tasks
|
26
26
|
end
|
@@ -1,16 +1,18 @@
|
|
1
1
|
class StripeModelCallbacks::ApplicationRecord < ActiveRecord::Base
|
2
|
-
include PublicActivity::Model
|
3
|
-
tracked
|
4
|
-
|
5
2
|
self.abstract_class = true
|
6
3
|
|
7
|
-
|
4
|
+
attr_accessor :stripe_object
|
5
|
+
|
6
|
+
def self.inherited(child)
|
7
|
+
super
|
8
|
+
child.include ActiveRecordAuditable::Audited
|
9
|
+
end
|
8
10
|
|
9
11
|
def self.check_object_is_stripe_class(object, allowed = nil)
|
10
12
|
raise "'stripe_class' not defined on #{name}" unless respond_to?(:stripe_class)
|
11
13
|
|
12
14
|
# Ignore general objects
|
13
|
-
return if object.class.name == "Stripe::StripeObject" # rubocop:disable Style/ClassEqualityComparison
|
15
|
+
return if object.class.name == "Stripe::StripeObject" # rubocop:disable Style/ClassEqualityComparison
|
14
16
|
|
15
17
|
allowed ||= [stripe_class]
|
16
18
|
|
@@ -45,14 +47,14 @@ class StripeModelCallbacks::ApplicationRecord < ActiveRecord::Base
|
|
45
47
|
save!
|
46
48
|
end
|
47
49
|
|
48
|
-
def reload!(*args, &
|
50
|
+
def reload!(*args, &)
|
49
51
|
@to_stripe = nil
|
50
52
|
super
|
51
53
|
end
|
52
54
|
|
53
55
|
def update_on_stripe(attributes)
|
54
56
|
attributes.each do |key, value|
|
55
|
-
to_stripe.__send__("#{key}=", value)
|
57
|
+
to_stripe.__send__(:"#{key}=", value)
|
56
58
|
end
|
57
59
|
|
58
60
|
to_stripe.save
|
@@ -65,6 +67,8 @@ class StripeModelCallbacks::ApplicationRecord < ActiveRecord::Base
|
|
65
67
|
end
|
66
68
|
|
67
69
|
def destroy_on_stripe
|
70
|
+
raise "Can't delete #{self.class.name} on Stripe because it isn't supported" unless to_stripe.respond_to?(:delete)
|
71
|
+
|
68
72
|
to_stripe.delete
|
69
73
|
update!(deleted_at: Time.zone.now) if respond_to?(:deleted_at)
|
70
74
|
reload_from_stripe!
|
@@ -4,7 +4,7 @@ class StripeModelCallbacks::Account::ExternalAccount::UpdatedService < StripeMod
|
|
4
4
|
bank_account.assign_from_stripe(object)
|
5
5
|
|
6
6
|
if bank_account.save
|
7
|
-
bank_account.
|
7
|
+
bank_account.create_audit!(action: :deleted) if event.type == "account.external_account.deleted"
|
8
8
|
succeed!
|
9
9
|
else
|
10
10
|
fail! bank_account.errors.full_messages
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class StripeModelCallbacks::BaseService < ServicePattern::Service
|
2
|
-
def self.reported_execute!(
|
2
|
+
def self.reported_execute!(...)
|
3
3
|
with_exception_notifications do
|
4
|
-
response = execute(
|
4
|
+
response = execute(...)
|
5
5
|
raise response.errors.join(". ") unless response.success?
|
6
6
|
|
7
7
|
response
|
@@ -25,13 +25,13 @@ class StripeModelCallbacks::BaseService < ServicePattern::Service
|
|
25
25
|
raise e
|
26
26
|
end
|
27
27
|
|
28
|
-
def self.execute_with_advisory_lock!(
|
29
|
-
# The difference between the
|
28
|
+
def self.execute_with_advisory_lock!(*, **, &)
|
29
|
+
# The difference between the Stripe events is about a few milliseconds - with advisory_lock
|
30
30
|
# we will prevent from creating duplicated objects due to race condition.
|
31
31
|
# https://stripe.com/docs/webhooks/best-practices#event-ordering
|
32
32
|
with_exception_notifications do
|
33
|
-
StripeModelCallbacks::ApplicationRecord.with_advisory_lock(advisory_lock_name(
|
34
|
-
response = execute(
|
33
|
+
StripeModelCallbacks::ApplicationRecord.with_advisory_lock(advisory_lock_name(*, **)) do
|
34
|
+
response = execute(*, **, &)
|
35
35
|
raise response.errors.join(". ") unless response.success?
|
36
36
|
|
37
37
|
response
|
@@ -19,11 +19,11 @@ private
|
|
19
19
|
def create_activity
|
20
20
|
case event.type
|
21
21
|
when "charge.dispute.closed"
|
22
|
-
dispute.
|
22
|
+
dispute.create_audit!(action: :closed)
|
23
23
|
when "charge.dispute.funds_reinstated"
|
24
|
-
dispute.
|
24
|
+
dispute.create_audit!(action: :funds_reinstated)
|
25
25
|
when "charge.dispute.funds_withdrawn"
|
26
|
-
dispute.
|
26
|
+
dispute.create_audit!(action: :funds_withdrawn)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -3,7 +3,6 @@ class StripeModelCallbacks::Charge::UpdatedService < StripeModelCallbacks::BaseE
|
|
3
3
|
charge.assign_from_stripe(object)
|
4
4
|
|
5
5
|
if charge.save
|
6
|
-
create_refunds if event.type == "charge.refunded"
|
7
6
|
create_activity
|
8
7
|
succeed!
|
9
8
|
else
|
@@ -20,21 +19,15 @@ private
|
|
20
19
|
def create_activity
|
21
20
|
case event.type
|
22
21
|
when "charge.captured"
|
23
|
-
charge.
|
22
|
+
charge.create_audit!(action: :captured)
|
24
23
|
when "charge.failed"
|
25
|
-
charge.
|
24
|
+
charge.create_audit!(action: :failed)
|
26
25
|
when "charge.pending"
|
27
|
-
charge.
|
26
|
+
charge.create_audit!(action: :pending)
|
28
27
|
when "charge.refunded"
|
29
|
-
charge.
|
28
|
+
charge.create_audit!(action: :refunded)
|
30
29
|
when "charge.succeeded"
|
31
|
-
charge.
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def create_refunds
|
36
|
-
object.refunds.each do |stripe_refund|
|
37
|
-
StripeModelCallbacks::Refund::UpdatedService.reported_execute!(object: stripe_refund)
|
30
|
+
charge.create_audit!(action: :succeeded)
|
38
31
|
end
|
39
32
|
end
|
40
33
|
end
|
@@ -20,11 +20,15 @@ class StripeModelCallbacks::ConfigureService < StripeModelCallbacks::BaseEventSe
|
|
20
20
|
invoice_events
|
21
21
|
order_events
|
22
22
|
recipient_events
|
23
|
+
payment_intent_events
|
24
|
+
payment_method_events
|
23
25
|
payout_events
|
24
26
|
plan_events
|
25
27
|
price_events
|
26
28
|
product_events
|
29
|
+
refund_events
|
27
30
|
review_events
|
31
|
+
setup_intent_events
|
28
32
|
sku_events
|
29
33
|
source_events
|
30
34
|
subscription_events
|
@@ -40,35 +44,35 @@ private
|
|
40
44
|
def account_external_account_events
|
41
45
|
%w[created deleted updated].each do |external_account_event|
|
42
46
|
subscribe "account.external_account.#{external_account_event}" do |event|
|
43
|
-
StripeModelCallbacks::Account::ExternalAccount::UpdatedService.execute_with_advisory_lock!(event:
|
47
|
+
StripeModelCallbacks::Account::ExternalAccount::UpdatedService.execute_with_advisory_lock!(event:)
|
44
48
|
end
|
45
49
|
end
|
46
50
|
end
|
47
51
|
|
48
52
|
def all_events
|
49
53
|
events.all do |event|
|
50
|
-
StripeModelCallbacks::NotifierService.execute_with_advisory_lock!(event:
|
54
|
+
StripeModelCallbacks::NotifierService.execute_with_advisory_lock!(event:)
|
51
55
|
end
|
52
56
|
end
|
53
57
|
|
54
58
|
def charge_events
|
55
59
|
%w[captured failed pending refunded updated succeeded].each do |charge_event|
|
56
60
|
subscribe "charge.#{charge_event}" do |event|
|
57
|
-
StripeModelCallbacks::Charge::UpdatedService.execute_with_advisory_lock!(event:
|
61
|
+
StripeModelCallbacks::Charge::UpdatedService.execute_with_advisory_lock!(event:)
|
58
62
|
end
|
59
63
|
end
|
60
64
|
end
|
61
65
|
|
62
66
|
def charge_refund_events
|
63
67
|
subscribe "charge.refund.updated" do |event|
|
64
|
-
StripeModelCallbacks::Refund::UpdatedService.execute_with_advisory_lock!(event:
|
68
|
+
StripeModelCallbacks::Refund::UpdatedService.execute_with_advisory_lock!(event:)
|
65
69
|
end
|
66
70
|
end
|
67
71
|
|
68
72
|
def charge_dispute_events
|
69
73
|
%w[closed created funds_reinstated funds_withdrawn updated].each do |charge_event|
|
70
74
|
subscribe "charge.dispute.#{charge_event}" do |event|
|
71
|
-
StripeModelCallbacks::Charge::DisputeUpdatedService.execute_with_advisory_lock!(event:
|
75
|
+
StripeModelCallbacks::Charge::DisputeUpdatedService.execute_with_advisory_lock!(event:)
|
72
76
|
end
|
73
77
|
end
|
74
78
|
end
|
@@ -76,21 +80,21 @@ private
|
|
76
80
|
def coupon_events
|
77
81
|
%w[created deleted updated].each do |coupon_event|
|
78
82
|
subscribe "coupon.#{coupon_event}" do |event|
|
79
|
-
StripeModelCallbacks::Coupon::UpdatedService.execute_with_advisory_lock!(event:
|
83
|
+
StripeModelCallbacks::Coupon::UpdatedService.execute_with_advisory_lock!(event:)
|
80
84
|
end
|
81
85
|
end
|
82
86
|
end
|
83
87
|
|
84
88
|
def customer_bank_account_events
|
85
89
|
subscribe "customer.bank_account.deleted" do |event|
|
86
|
-
StripeModelCallbacks::Customer::BankAccount::DeletedService.execute_with_advisory_lock!(event:
|
90
|
+
StripeModelCallbacks::Customer::BankAccount::DeletedService.execute_with_advisory_lock!(event:)
|
87
91
|
end
|
88
92
|
end
|
89
93
|
|
90
94
|
def customer_discount_events
|
91
95
|
%w[created deleted updated].each do |customer_event|
|
92
96
|
subscribe "customer.discount.#{customer_event}" do |event|
|
93
|
-
StripeModelCallbacks::Customer::DiscountUpdatedService.execute_with_advisory_lock!(event:
|
97
|
+
StripeModelCallbacks::Customer::DiscountUpdatedService.execute_with_advisory_lock!(event:)
|
94
98
|
end
|
95
99
|
end
|
96
100
|
end
|
@@ -98,7 +102,7 @@ private
|
|
98
102
|
def customer_events
|
99
103
|
%w[created deleted updated].each do |customer_event|
|
100
104
|
subscribe "customer.#{customer_event}" do |event|
|
101
|
-
StripeModelCallbacks::Customer::UpdatedService.execute_with_advisory_lock!(event:
|
105
|
+
StripeModelCallbacks::Customer::UpdatedService.execute_with_advisory_lock!(event:)
|
102
106
|
end
|
103
107
|
end
|
104
108
|
end
|
@@ -106,7 +110,7 @@ private
|
|
106
110
|
def customer_source_events
|
107
111
|
%w[created deleted expiring updated].each do |customer_event|
|
108
112
|
subscribe "customer.source.#{customer_event}" do |event|
|
109
|
-
StripeModelCallbacks::Customer::SourceUpdatedService.execute_with_advisory_lock!(event:
|
113
|
+
StripeModelCallbacks::Customer::SourceUpdatedService.execute_with_advisory_lock!(event:)
|
110
114
|
end
|
111
115
|
end
|
112
116
|
end
|
@@ -118,7 +122,7 @@ private
|
|
118
122
|
# https://stripe.com/docs/billing/invoices/overview#invoice-status-transition-endpoints-and-webhooks
|
119
123
|
%w[created deleted marked_uncollectible payment_failed payment_succeeded sent updated voided].each do |invoice_event|
|
120
124
|
subscribe "invoice.#{invoice_event}" do |event|
|
121
|
-
StripeModelCallbacks::Invoice::UpdatedService.execute_with_advisory_lock!(event:
|
125
|
+
StripeModelCallbacks::Invoice::UpdatedService.execute_with_advisory_lock!(event:)
|
122
126
|
end
|
123
127
|
end
|
124
128
|
end
|
@@ -126,7 +130,7 @@ private
|
|
126
130
|
def invoice_item_events
|
127
131
|
%w[created deleted updated].each do |event_type|
|
128
132
|
subscribe "invoiceitem.#{event_type}" do |event|
|
129
|
-
StripeModelCallbacks::InvoiceItem::UpdatedService.execute_with_advisory_lock!(event:
|
133
|
+
StripeModelCallbacks::InvoiceItem::UpdatedService.execute_with_advisory_lock!(event:)
|
130
134
|
end
|
131
135
|
end
|
132
136
|
end
|
@@ -134,7 +138,7 @@ private
|
|
134
138
|
def order_events
|
135
139
|
%w[created updated].each do |order_event|
|
136
140
|
subscribe "order.#{order_event}" do |event|
|
137
|
-
StripeModelCallbacks::Order::UpdatedService.execute_with_advisory_lock!(event:
|
141
|
+
StripeModelCallbacks::Order::UpdatedService.execute_with_advisory_lock!(event:)
|
138
142
|
end
|
139
143
|
end
|
140
144
|
end
|
@@ -142,7 +146,7 @@ private
|
|
142
146
|
def payout_events
|
143
147
|
%w[canceled created failed paid updated].each do |payout_event|
|
144
148
|
subscribe "payout.#{payout_event}" do |event|
|
145
|
-
StripeModelCallbacks::Payout::UpdatedService.execute_with_advisory_lock!(event:
|
149
|
+
StripeModelCallbacks::Payout::UpdatedService.execute_with_advisory_lock!(event:)
|
146
150
|
end
|
147
151
|
end
|
148
152
|
end
|
@@ -150,7 +154,31 @@ private
|
|
150
154
|
def recipient_events
|
151
155
|
%w[created deleted updated].each do |recipient_event|
|
152
156
|
subscribe "recipient.#{recipient_event}" do |event|
|
153
|
-
StripeModelCallbacks::Recipient::UpdatedService.execute_with_advisory_lock!(event:
|
157
|
+
StripeModelCallbacks::Recipient::UpdatedService.execute_with_advisory_lock!(event:)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def refund_events
|
163
|
+
%w[created updated].each do |refund_event|
|
164
|
+
subscribe "refund.#{refund_event}" do |event|
|
165
|
+
StripeModelCallbacks::Refund::UpdatedService.execute_with_advisory_lock!(event:)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def payment_intent_events
|
171
|
+
%w[amount_capturable_updated canceled created partially_funded payment_failed processing requires_action succeeded].each do |plan_event|
|
172
|
+
subscribe "payment_intent.#{plan_event}" do |event|
|
173
|
+
StripeModelCallbacks::PaymentIntent::UpdatedService.execute_with_advisory_lock!(event:)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def payment_method_events
|
179
|
+
%w[attached automatically_updated card_automatically_updated detached updated].each do |plan_event|
|
180
|
+
subscribe "payment_method.#{plan_event}" do |event|
|
181
|
+
StripeModelCallbacks::PaymentMethod::UpdatedService.execute_with_advisory_lock!(event:)
|
154
182
|
end
|
155
183
|
end
|
156
184
|
end
|
@@ -158,7 +186,7 @@ private
|
|
158
186
|
def plan_events
|
159
187
|
%w[created deleted updated].each do |plan_event|
|
160
188
|
subscribe "plan.#{plan_event}" do |event|
|
161
|
-
StripeModelCallbacks::Plan::UpdatedService.execute_with_advisory_lock!(event:
|
189
|
+
StripeModelCallbacks::Plan::UpdatedService.execute_with_advisory_lock!(event:)
|
162
190
|
end
|
163
191
|
end
|
164
192
|
end
|
@@ -166,7 +194,7 @@ private
|
|
166
194
|
def sku_events
|
167
195
|
%w[created deleted updated].each do |sku_event|
|
168
196
|
subscribe "sku.#{sku_event}" do |event|
|
169
|
-
StripeModelCallbacks::Sku::UpdatedService.execute_with_advisory_lock!(event:
|
197
|
+
StripeModelCallbacks::Sku::UpdatedService.execute_with_advisory_lock!(event:)
|
170
198
|
end
|
171
199
|
end
|
172
200
|
end
|
@@ -174,7 +202,7 @@ private
|
|
174
202
|
def price_events
|
175
203
|
%w[created deleted updated].each do |price_event|
|
176
204
|
subscribe "price.#{price_event}" do |event|
|
177
|
-
StripeModelCallbacks::Price::UpdatedService.execute_with_advisory_lock!(event:
|
205
|
+
StripeModelCallbacks::Price::UpdatedService.execute_with_advisory_lock!(event:)
|
178
206
|
end
|
179
207
|
end
|
180
208
|
end
|
@@ -182,7 +210,7 @@ private
|
|
182
210
|
def product_events
|
183
211
|
%w[created deleted updated].each do |product_event|
|
184
212
|
subscribe "product.#{product_event}" do |event|
|
185
|
-
StripeModelCallbacks::Product::UpdatedService.execute_with_advisory_lock!(event:
|
213
|
+
StripeModelCallbacks::Product::UpdatedService.execute_with_advisory_lock!(event:)
|
186
214
|
end
|
187
215
|
end
|
188
216
|
end
|
@@ -190,7 +218,15 @@ private
|
|
190
218
|
def review_events
|
191
219
|
%w[opened closed].each do |review_event|
|
192
220
|
subscribe "review.#{review_event}" do |event|
|
193
|
-
StripeModelCallbacks::Review::UpdatedService.execute_with_advisory_lock!(event:
|
221
|
+
StripeModelCallbacks::Review::UpdatedService.execute_with_advisory_lock!(event:)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def setup_intent_events
|
227
|
+
%w[created updated].each do |product_event|
|
228
|
+
subscribe "setup_intent.#{product_event}" do |event|
|
229
|
+
StripeModelCallbacks::SetupIntent::UpdatedService.execute_with_advisory_lock!(event:)
|
194
230
|
end
|
195
231
|
end
|
196
232
|
end
|
@@ -198,14 +234,14 @@ private
|
|
198
234
|
def source_events
|
199
235
|
%w[canceled chargeable failed mandate_notification].each do |source_event|
|
200
236
|
subscribe "source.#{source_event}" do |event|
|
201
|
-
StripeModelCallbacks::Source::UpdatedService.execute_with_advisory_lock!(event:
|
237
|
+
StripeModelCallbacks::Source::UpdatedService.execute_with_advisory_lock!(event:)
|
202
238
|
end
|
203
239
|
end
|
204
240
|
end
|
205
241
|
|
206
242
|
def subscribe(event_name)
|
207
243
|
events.subscribe(event_name) do |event|
|
208
|
-
StripeModelCallbacks::Configuration.current.with_error_handling(args: {event:
|
244
|
+
StripeModelCallbacks::Configuration.current.with_error_handling(args: {event:}) do
|
209
245
|
yield event
|
210
246
|
end
|
211
247
|
end
|
@@ -214,7 +250,7 @@ private
|
|
214
250
|
def subscription_events
|
215
251
|
%w[created deleted trial_will_end updated].each do |subscription_event|
|
216
252
|
subscribe "customer.subscription.#{subscription_event}" do |event|
|
217
|
-
StripeModelCallbacks::Customer::Subscription::UpdatedService.execute_with_advisory_lock!(event:
|
253
|
+
StripeModelCallbacks::Customer::Subscription::UpdatedService.execute_with_advisory_lock!(event:)
|
218
254
|
end
|
219
255
|
end
|
220
256
|
end
|
@@ -222,7 +258,7 @@ private
|
|
222
258
|
def subscription_schedule_events
|
223
259
|
%w[canceled created updated].each do |subscription_event|
|
224
260
|
subscribe "subscription_schedule.#{subscription_event}" do |event|
|
225
|
-
StripeModelCallbacks::SubscriptionSchedule::UpdatedService.execute_with_advisory_lock!(event:
|
261
|
+
StripeModelCallbacks::SubscriptionSchedule::UpdatedService.execute_with_advisory_lock!(event:)
|
226
262
|
end
|
227
263
|
end
|
228
264
|
end
|
@@ -230,7 +266,7 @@ private
|
|
230
266
|
def tax_rate_events
|
231
267
|
%w[created updated].each do |transfer_event|
|
232
268
|
subscribe "tax_rate.#{transfer_event}" do |event|
|
233
|
-
StripeModelCallbacks::TaxRate::UpdatedService.execute_with_advisory_lock!(event:
|
269
|
+
StripeModelCallbacks::TaxRate::UpdatedService.execute_with_advisory_lock!(event:)
|
234
270
|
end
|
235
271
|
end
|
236
272
|
end
|
@@ -238,7 +274,7 @@ private
|
|
238
274
|
def transfer_events
|
239
275
|
%w[created reversed updated].each do |transfer_event|
|
240
276
|
subscribe "transfer.#{transfer_event}" do |event|
|
241
|
-
StripeModelCallbacks::Transfer::UpdatedService.execute_with_advisory_lock!(event:
|
277
|
+
StripeModelCallbacks::Transfer::UpdatedService.execute_with_advisory_lock!(event:)
|
242
278
|
end
|
243
279
|
end
|
244
280
|
end
|
@@ -4,7 +4,7 @@ class StripeModelCallbacks::Customer::BankAccount::DeletedService < StripeModelC
|
|
4
4
|
bank_account.assign_from_stripe(object)
|
5
5
|
|
6
6
|
if bank_account.save
|
7
|
-
bank_account.
|
7
|
+
bank_account.create_audit!(action: :customer_bank_account_deleted) if event.type == "customer.bank_account.deleted"
|
8
8
|
succeed!
|
9
9
|
else
|
10
10
|
fail! bank_account.errors.full_messages
|
@@ -14,7 +14,7 @@ class StripeModelCallbacks::Customer::DiscountUpdatedService < StripeModelCallba
|
|
14
14
|
private
|
15
15
|
|
16
16
|
def create_activity
|
17
|
-
discount.
|
17
|
+
discount.create_audit!(action: :deleted) if event&.type == "customer.discount.deleted"
|
18
18
|
end
|
19
19
|
|
20
20
|
def coupon_id_look_up_by
|
@@ -2,11 +2,11 @@ class StripeModelCallbacks::Customer::SourceUpdatedService < StripeModelCallback
|
|
2
2
|
def perform
|
3
3
|
source = stripe_class.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
source.assign_from_stripe(object)
|
5
|
-
source.deleted_at = Time.zone.now if event
|
5
|
+
source.deleted_at = Time.zone.now if event&.type == "customer.source.deleted"
|
6
6
|
|
7
7
|
if source.save
|
8
|
-
source.
|
9
|
-
source.
|
8
|
+
source.create_audit!(action: :deleted) if event&.type == "customer.source.deleted"
|
9
|
+
source.create_audit!(action: :expiring) if event&.type == "customer.source.expiring"
|
10
10
|
succeed!
|
11
11
|
else
|
12
12
|
fail! source.errors.full_messages
|
@@ -16,9 +16,9 @@ private
|
|
16
16
|
def create_activity
|
17
17
|
case event.type
|
18
18
|
when "customer.subscription.trial_will_end"
|
19
|
-
subscription.
|
19
|
+
subscription.create_audit!(action: :trial_will_end)
|
20
20
|
when "customer.subscription.deleted"
|
21
|
-
subscription.
|
21
|
+
subscription.create_audit!(action: :deleted)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -2,10 +2,10 @@ class StripeModelCallbacks::Customer::UpdatedService < StripeModelCallbacks::Bas
|
|
2
2
|
def perform
|
3
3
|
customer = StripeCustomer.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
customer.assign_from_stripe(object)
|
5
|
-
customer.deleted_at ||= Time.zone.now if event
|
5
|
+
customer.deleted_at ||= Time.zone.now if event&.type == "customer.deleted"
|
6
6
|
|
7
7
|
if customer.save
|
8
|
-
succeed!
|
8
|
+
succeed! customer
|
9
9
|
else
|
10
10
|
fail! customer.errors.full_messages
|
11
11
|
end
|
@@ -1,15 +1,28 @@
|
|
1
1
|
class StripeModelCallbacks::EventMockerService
|
2
2
|
attr_reader :args, :name, :scope
|
3
3
|
|
4
|
+
def self.execute!(args:, name:, scope:)
|
5
|
+
new(args:, name:, scope:).perform
|
6
|
+
end
|
7
|
+
|
4
8
|
def initialize(args:, name:, scope:)
|
5
9
|
@args = args
|
6
10
|
@name = name
|
7
11
|
@scope = scope
|
12
|
+
end
|
8
13
|
|
14
|
+
def perform
|
9
15
|
bypass_event_signature(payload)
|
10
16
|
post_event
|
11
17
|
end
|
12
18
|
|
19
|
+
def payload
|
20
|
+
file_content = File.read(fixture_path)
|
21
|
+
data = JSON.parse(file_content, symbolize_names: true)
|
22
|
+
data.deep_merge!(args) if args
|
23
|
+
data
|
24
|
+
end
|
25
|
+
|
13
26
|
private
|
14
27
|
|
15
28
|
def bypass_event_signature(payload)
|
@@ -22,14 +35,7 @@ private
|
|
22
35
|
end
|
23
36
|
|
24
37
|
def fixture_path
|
25
|
-
@fixture_path ||= "#{
|
26
|
-
end
|
27
|
-
|
28
|
-
def payload
|
29
|
-
file_content = File.read(fixture_path)
|
30
|
-
data = JSON.parse(file_content, symbolize_names: true)
|
31
|
-
data.deep_merge!(args) if args
|
32
|
-
data
|
38
|
+
@fixture_path ||= "#{__dir__}/../../../lib/stripe_model_callbacks/fixtures/stripe_events/#{first_part}/#{name}.json"
|
33
39
|
end
|
34
40
|
|
35
41
|
def post_event
|
@@ -14,10 +14,10 @@ class StripeModelCallbacks::Invoice::UpdatedService < StripeModelCallbacks::Base
|
|
14
14
|
private
|
15
15
|
|
16
16
|
def create_activity
|
17
|
-
invoice.
|
18
|
-
invoice.
|
19
|
-
invoice.
|
20
|
-
invoice.
|
17
|
+
invoice.create_audit!(action: :payment_failed) if event.type == "invoice.payment_failed"
|
18
|
+
invoice.create_audit!(action: :payment_succeeded) if event.type == "invoice.payment_succeeded"
|
19
|
+
invoice.create_audit!(action: :sent) if event.type == "invoice.sent"
|
20
|
+
invoice.create_audit!(action: :upcoming) if event.type == "invoice.upcoming"
|
21
21
|
end
|
22
22
|
|
23
23
|
def invoice
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class StripeModelCallbacks::PaymentIntent::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
+
def perform
|
3
|
+
payment_intent = StripePaymentIntent.find_or_initialize_by(stripe_id: object.id)
|
4
|
+
payment_intent.assign_from_stripe(object)
|
5
|
+
|
6
|
+
if payment_intent.save
|
7
|
+
create_activity!(payment_intent) if event
|
8
|
+
succeed!
|
9
|
+
else
|
10
|
+
fail! payment_intent.errors.full_messages
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_activity!(payment_intent)
|
15
|
+
match = event.type.match(/\Apayment_intent\.(.+)$/)
|
16
|
+
activity_type = match[1].to_sym
|
17
|
+
payment_intent.create_audit!(action: activity_type)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class StripeModelCallbacks::PaymentMethod::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
+
def perform
|
3
|
+
payment_method = StripePaymentMethod.find_or_initialize_by(stripe_id: object.id)
|
4
|
+
payment_method.assign_from_stripe(object)
|
5
|
+
|
6
|
+
if payment_method.save
|
7
|
+
create_activity!(payment_method) if event
|
8
|
+
succeed!
|
9
|
+
else
|
10
|
+
fail! payment_method.errors.full_messages
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_activity!(payment_method)
|
15
|
+
match = event.type.match(/\Apayment_method\.(.+)$/)
|
16
|
+
activity_type = match[1].to_sym
|
17
|
+
payment_method.create_audit!(action: activity_type)
|
18
|
+
end
|
19
|
+
end
|
@@ -15,11 +15,11 @@ private
|
|
15
15
|
def create_activity
|
16
16
|
case event.type
|
17
17
|
when "payout.canceled"
|
18
|
-
payout.
|
18
|
+
payout.create_audit!(action: :canceled)
|
19
19
|
when "payout.failed"
|
20
|
-
payout.
|
20
|
+
payout.create_audit!(action: :failed)
|
21
21
|
when "payout.paid"
|
22
|
-
payout.
|
22
|
+
payout.create_audit!(action: :paid)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -5,7 +5,7 @@ class StripeModelCallbacks::Plan::UpdatedService < StripeModelCallbacks::BaseEve
|
|
5
5
|
plan.deleted_at ||= Time.zone.now if event.type == "plan.deleted"
|
6
6
|
|
7
7
|
if plan.save
|
8
|
-
plan.
|
8
|
+
plan.create_audit!(action: :deleted) if event.type == "plan.deleted"
|
9
9
|
succeed!
|
10
10
|
else
|
11
11
|
fail! plan.errors.full_messages
|
@@ -5,7 +5,7 @@ class StripeModelCallbacks::Price::UpdatedService < StripeModelCallbacks::BaseEv
|
|
5
5
|
price.deleted_at ||= Time.zone.now if event.type == "price.deleted"
|
6
6
|
|
7
7
|
if price.save
|
8
|
-
price.
|
8
|
+
price.create_audit!(action: :deleted) if event.type == "price.deleted"
|
9
9
|
succeed!
|
10
10
|
else
|
11
11
|
fail! price.errors.full_messages
|
@@ -5,7 +5,7 @@ class StripeModelCallbacks::Product::UpdatedService < StripeModelCallbacks::Base
|
|
5
5
|
product.deleted_at ||= Time.zone.now if event.type == "product.deleted"
|
6
6
|
|
7
7
|
if product.save
|
8
|
-
product.
|
8
|
+
product.create_audit!(action: :deleted) if event.type == "product.deleted"
|
9
9
|
succeed!
|
10
10
|
else
|
11
11
|
fail! product.errors.full_messages
|