stripe_model_callbacks 0.1.7 → 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 -5
- data/app/services/stripe_model_callbacks/configure_service.rb +54 -27
- 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/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 -0
- 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/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/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 +6 -0
- data/lib/stripe_model_callbacks/version.rb +1 -1
- data/lib/stripe_model_callbacks.rb +1 -1
- metadata +45 -13
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
|
@@ -19,15 +19,15 @@ private
|
|
19
19
|
def create_activity
|
20
20
|
case event.type
|
21
21
|
when "charge.captured"
|
22
|
-
charge.
|
22
|
+
charge.create_audit!(action: :captured)
|
23
23
|
when "charge.failed"
|
24
|
-
charge.
|
24
|
+
charge.create_audit!(action: :failed)
|
25
25
|
when "charge.pending"
|
26
|
-
charge.
|
26
|
+
charge.create_audit!(action: :pending)
|
27
27
|
when "charge.refunded"
|
28
|
-
charge.
|
28
|
+
charge.create_audit!(action: :refunded)
|
29
29
|
when "charge.succeeded"
|
30
|
-
charge.
|
30
|
+
charge.create_audit!(action: :succeeded)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
@@ -20,12 +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
|
27
29
|
refund_events
|
28
30
|
review_events
|
31
|
+
setup_intent_events
|
29
32
|
sku_events
|
30
33
|
source_events
|
31
34
|
subscription_events
|
@@ -41,35 +44,35 @@ private
|
|
41
44
|
def account_external_account_events
|
42
45
|
%w[created deleted updated].each do |external_account_event|
|
43
46
|
subscribe "account.external_account.#{external_account_event}" do |event|
|
44
|
-
StripeModelCallbacks::Account::ExternalAccount::UpdatedService.execute_with_advisory_lock!(event:
|
47
|
+
StripeModelCallbacks::Account::ExternalAccount::UpdatedService.execute_with_advisory_lock!(event:)
|
45
48
|
end
|
46
49
|
end
|
47
50
|
end
|
48
51
|
|
49
52
|
def all_events
|
50
53
|
events.all do |event|
|
51
|
-
StripeModelCallbacks::NotifierService.execute_with_advisory_lock!(event:
|
54
|
+
StripeModelCallbacks::NotifierService.execute_with_advisory_lock!(event:)
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
55
58
|
def charge_events
|
56
59
|
%w[captured failed pending refunded updated succeeded].each do |charge_event|
|
57
60
|
subscribe "charge.#{charge_event}" do |event|
|
58
|
-
StripeModelCallbacks::Charge::UpdatedService.execute_with_advisory_lock!(event:
|
61
|
+
StripeModelCallbacks::Charge::UpdatedService.execute_with_advisory_lock!(event:)
|
59
62
|
end
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
63
66
|
def charge_refund_events
|
64
67
|
subscribe "charge.refund.updated" do |event|
|
65
|
-
StripeModelCallbacks::Refund::UpdatedService.execute_with_advisory_lock!(event:
|
68
|
+
StripeModelCallbacks::Refund::UpdatedService.execute_with_advisory_lock!(event:)
|
66
69
|
end
|
67
70
|
end
|
68
71
|
|
69
72
|
def charge_dispute_events
|
70
73
|
%w[closed created funds_reinstated funds_withdrawn updated].each do |charge_event|
|
71
74
|
subscribe "charge.dispute.#{charge_event}" do |event|
|
72
|
-
StripeModelCallbacks::Charge::DisputeUpdatedService.execute_with_advisory_lock!(event:
|
75
|
+
StripeModelCallbacks::Charge::DisputeUpdatedService.execute_with_advisory_lock!(event:)
|
73
76
|
end
|
74
77
|
end
|
75
78
|
end
|
@@ -77,21 +80,21 @@ private
|
|
77
80
|
def coupon_events
|
78
81
|
%w[created deleted updated].each do |coupon_event|
|
79
82
|
subscribe "coupon.#{coupon_event}" do |event|
|
80
|
-
StripeModelCallbacks::Coupon::UpdatedService.execute_with_advisory_lock!(event:
|
83
|
+
StripeModelCallbacks::Coupon::UpdatedService.execute_with_advisory_lock!(event:)
|
81
84
|
end
|
82
85
|
end
|
83
86
|
end
|
84
87
|
|
85
88
|
def customer_bank_account_events
|
86
89
|
subscribe "customer.bank_account.deleted" do |event|
|
87
|
-
StripeModelCallbacks::Customer::BankAccount::DeletedService.execute_with_advisory_lock!(event:
|
90
|
+
StripeModelCallbacks::Customer::BankAccount::DeletedService.execute_with_advisory_lock!(event:)
|
88
91
|
end
|
89
92
|
end
|
90
93
|
|
91
94
|
def customer_discount_events
|
92
95
|
%w[created deleted updated].each do |customer_event|
|
93
96
|
subscribe "customer.discount.#{customer_event}" do |event|
|
94
|
-
StripeModelCallbacks::Customer::DiscountUpdatedService.execute_with_advisory_lock!(event:
|
97
|
+
StripeModelCallbacks::Customer::DiscountUpdatedService.execute_with_advisory_lock!(event:)
|
95
98
|
end
|
96
99
|
end
|
97
100
|
end
|
@@ -99,7 +102,7 @@ private
|
|
99
102
|
def customer_events
|
100
103
|
%w[created deleted updated].each do |customer_event|
|
101
104
|
subscribe "customer.#{customer_event}" do |event|
|
102
|
-
StripeModelCallbacks::Customer::UpdatedService.execute_with_advisory_lock!(event:
|
105
|
+
StripeModelCallbacks::Customer::UpdatedService.execute_with_advisory_lock!(event:)
|
103
106
|
end
|
104
107
|
end
|
105
108
|
end
|
@@ -107,7 +110,7 @@ private
|
|
107
110
|
def customer_source_events
|
108
111
|
%w[created deleted expiring updated].each do |customer_event|
|
109
112
|
subscribe "customer.source.#{customer_event}" do |event|
|
110
|
-
StripeModelCallbacks::Customer::SourceUpdatedService.execute_with_advisory_lock!(event:
|
113
|
+
StripeModelCallbacks::Customer::SourceUpdatedService.execute_with_advisory_lock!(event:)
|
111
114
|
end
|
112
115
|
end
|
113
116
|
end
|
@@ -119,7 +122,7 @@ private
|
|
119
122
|
# https://stripe.com/docs/billing/invoices/overview#invoice-status-transition-endpoints-and-webhooks
|
120
123
|
%w[created deleted marked_uncollectible payment_failed payment_succeeded sent updated voided].each do |invoice_event|
|
121
124
|
subscribe "invoice.#{invoice_event}" do |event|
|
122
|
-
StripeModelCallbacks::Invoice::UpdatedService.execute_with_advisory_lock!(event:
|
125
|
+
StripeModelCallbacks::Invoice::UpdatedService.execute_with_advisory_lock!(event:)
|
123
126
|
end
|
124
127
|
end
|
125
128
|
end
|
@@ -127,7 +130,7 @@ private
|
|
127
130
|
def invoice_item_events
|
128
131
|
%w[created deleted updated].each do |event_type|
|
129
132
|
subscribe "invoiceitem.#{event_type}" do |event|
|
130
|
-
StripeModelCallbacks::InvoiceItem::UpdatedService.execute_with_advisory_lock!(event:
|
133
|
+
StripeModelCallbacks::InvoiceItem::UpdatedService.execute_with_advisory_lock!(event:)
|
131
134
|
end
|
132
135
|
end
|
133
136
|
end
|
@@ -135,7 +138,7 @@ private
|
|
135
138
|
def order_events
|
136
139
|
%w[created updated].each do |order_event|
|
137
140
|
subscribe "order.#{order_event}" do |event|
|
138
|
-
StripeModelCallbacks::Order::UpdatedService.execute_with_advisory_lock!(event:
|
141
|
+
StripeModelCallbacks::Order::UpdatedService.execute_with_advisory_lock!(event:)
|
139
142
|
end
|
140
143
|
end
|
141
144
|
end
|
@@ -143,7 +146,7 @@ private
|
|
143
146
|
def payout_events
|
144
147
|
%w[canceled created failed paid updated].each do |payout_event|
|
145
148
|
subscribe "payout.#{payout_event}" do |event|
|
146
|
-
StripeModelCallbacks::Payout::UpdatedService.execute_with_advisory_lock!(event:
|
149
|
+
StripeModelCallbacks::Payout::UpdatedService.execute_with_advisory_lock!(event:)
|
147
150
|
end
|
148
151
|
end
|
149
152
|
end
|
@@ -151,7 +154,7 @@ private
|
|
151
154
|
def recipient_events
|
152
155
|
%w[created deleted updated].each do |recipient_event|
|
153
156
|
subscribe "recipient.#{recipient_event}" do |event|
|
154
|
-
StripeModelCallbacks::Recipient::UpdatedService.execute_with_advisory_lock!(event:
|
157
|
+
StripeModelCallbacks::Recipient::UpdatedService.execute_with_advisory_lock!(event:)
|
155
158
|
end
|
156
159
|
end
|
157
160
|
end
|
@@ -159,7 +162,23 @@ private
|
|
159
162
|
def refund_events
|
160
163
|
%w[created updated].each do |refund_event|
|
161
164
|
subscribe "refund.#{refund_event}" do |event|
|
162
|
-
StripeModelCallbacks::Refund::UpdatedService.execute_with_advisory_lock!(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:)
|
163
182
|
end
|
164
183
|
end
|
165
184
|
end
|
@@ -167,7 +186,7 @@ private
|
|
167
186
|
def plan_events
|
168
187
|
%w[created deleted updated].each do |plan_event|
|
169
188
|
subscribe "plan.#{plan_event}" do |event|
|
170
|
-
StripeModelCallbacks::Plan::UpdatedService.execute_with_advisory_lock!(event:
|
189
|
+
StripeModelCallbacks::Plan::UpdatedService.execute_with_advisory_lock!(event:)
|
171
190
|
end
|
172
191
|
end
|
173
192
|
end
|
@@ -175,7 +194,7 @@ private
|
|
175
194
|
def sku_events
|
176
195
|
%w[created deleted updated].each do |sku_event|
|
177
196
|
subscribe "sku.#{sku_event}" do |event|
|
178
|
-
StripeModelCallbacks::Sku::UpdatedService.execute_with_advisory_lock!(event:
|
197
|
+
StripeModelCallbacks::Sku::UpdatedService.execute_with_advisory_lock!(event:)
|
179
198
|
end
|
180
199
|
end
|
181
200
|
end
|
@@ -183,7 +202,7 @@ private
|
|
183
202
|
def price_events
|
184
203
|
%w[created deleted updated].each do |price_event|
|
185
204
|
subscribe "price.#{price_event}" do |event|
|
186
|
-
StripeModelCallbacks::Price::UpdatedService.execute_with_advisory_lock!(event:
|
205
|
+
StripeModelCallbacks::Price::UpdatedService.execute_with_advisory_lock!(event:)
|
187
206
|
end
|
188
207
|
end
|
189
208
|
end
|
@@ -191,7 +210,7 @@ private
|
|
191
210
|
def product_events
|
192
211
|
%w[created deleted updated].each do |product_event|
|
193
212
|
subscribe "product.#{product_event}" do |event|
|
194
|
-
StripeModelCallbacks::Product::UpdatedService.execute_with_advisory_lock!(event:
|
213
|
+
StripeModelCallbacks::Product::UpdatedService.execute_with_advisory_lock!(event:)
|
195
214
|
end
|
196
215
|
end
|
197
216
|
end
|
@@ -199,7 +218,15 @@ private
|
|
199
218
|
def review_events
|
200
219
|
%w[opened closed].each do |review_event|
|
201
220
|
subscribe "review.#{review_event}" do |event|
|
202
|
-
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:)
|
203
230
|
end
|
204
231
|
end
|
205
232
|
end
|
@@ -207,14 +234,14 @@ private
|
|
207
234
|
def source_events
|
208
235
|
%w[canceled chargeable failed mandate_notification].each do |source_event|
|
209
236
|
subscribe "source.#{source_event}" do |event|
|
210
|
-
StripeModelCallbacks::Source::UpdatedService.execute_with_advisory_lock!(event:
|
237
|
+
StripeModelCallbacks::Source::UpdatedService.execute_with_advisory_lock!(event:)
|
211
238
|
end
|
212
239
|
end
|
213
240
|
end
|
214
241
|
|
215
242
|
def subscribe(event_name)
|
216
243
|
events.subscribe(event_name) do |event|
|
217
|
-
StripeModelCallbacks::Configuration.current.with_error_handling(args: {event:
|
244
|
+
StripeModelCallbacks::Configuration.current.with_error_handling(args: {event:}) do
|
218
245
|
yield event
|
219
246
|
end
|
220
247
|
end
|
@@ -223,7 +250,7 @@ private
|
|
223
250
|
def subscription_events
|
224
251
|
%w[created deleted trial_will_end updated].each do |subscription_event|
|
225
252
|
subscribe "customer.subscription.#{subscription_event}" do |event|
|
226
|
-
StripeModelCallbacks::Customer::Subscription::UpdatedService.execute_with_advisory_lock!(event:
|
253
|
+
StripeModelCallbacks::Customer::Subscription::UpdatedService.execute_with_advisory_lock!(event:)
|
227
254
|
end
|
228
255
|
end
|
229
256
|
end
|
@@ -231,7 +258,7 @@ private
|
|
231
258
|
def subscription_schedule_events
|
232
259
|
%w[canceled created updated].each do |subscription_event|
|
233
260
|
subscribe "subscription_schedule.#{subscription_event}" do |event|
|
234
|
-
StripeModelCallbacks::SubscriptionSchedule::UpdatedService.execute_with_advisory_lock!(event:
|
261
|
+
StripeModelCallbacks::SubscriptionSchedule::UpdatedService.execute_with_advisory_lock!(event:)
|
235
262
|
end
|
236
263
|
end
|
237
264
|
end
|
@@ -239,7 +266,7 @@ private
|
|
239
266
|
def tax_rate_events
|
240
267
|
%w[created updated].each do |transfer_event|
|
241
268
|
subscribe "tax_rate.#{transfer_event}" do |event|
|
242
|
-
StripeModelCallbacks::TaxRate::UpdatedService.execute_with_advisory_lock!(event:
|
269
|
+
StripeModelCallbacks::TaxRate::UpdatedService.execute_with_advisory_lock!(event:)
|
243
270
|
end
|
244
271
|
end
|
245
272
|
end
|
@@ -247,7 +274,7 @@ private
|
|
247
274
|
def transfer_events
|
248
275
|
%w[created reversed updated].each do |transfer_event|
|
249
276
|
subscribe "transfer.#{transfer_event}" do |event|
|
250
|
-
StripeModelCallbacks::Transfer::UpdatedService.execute_with_advisory_lock!(event:
|
277
|
+
StripeModelCallbacks::Transfer::UpdatedService.execute_with_advisory_lock!(event:)
|
251
278
|
end
|
252
279
|
end
|
253
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
|
@@ -5,7 +5,7 @@ class StripeModelCallbacks::Recipient::UpdatedService < StripeModelCallbacks::Ba
|
|
5
5
|
recipient.deleted_at ||= Time.zone.now if event.type == "recipient.deleted"
|
6
6
|
|
7
7
|
if recipient.save
|
8
|
-
recipient.
|
8
|
+
recipient.create_audit!(action: :deleted) if event.type == "recipient.deleted"
|
9
9
|
succeed!
|
10
10
|
else
|
11
11
|
fail! recipient.errors.full_messages
|