stripe_model_callbacks 0.1.1 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -1
- data/app/models/stripe_model_callbacks/application_record.rb +17 -0
- data/app/services/stripe_model_callbacks/account/external_account/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/attributes_assigner_service.rb +1 -1
- data/app/services/stripe_model_callbacks/base_service.rb +37 -4
- data/app/services/stripe_model_callbacks/charge/dispute_updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/charge/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/configure_service.rb +52 -24
- data/app/services/stripe_model_callbacks/coupon/sync_all.rb +9 -0
- data/app/services/stripe_model_callbacks/coupon/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/bank_account/deleted_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/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 +1 -1
- data/app/services/stripe_model_callbacks/customer/subscription/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/event_mocker_service.rb +1 -1
- data/app/services/stripe_model_callbacks/invoice/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/invoice_item/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/notifier_service.rb +1 -1
- data/app/services/stripe_model_callbacks/order/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/payout/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/plan/sync_all.rb +9 -0
- data/app/services/stripe_model_callbacks/plan/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/price/updated_service.rb +14 -0
- 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/refund/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/review/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/sku/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/source/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/subscription/state_checker_service.rb +2 -2
- data/app/services/stripe_model_callbacks/subscription_schedule/updated_service.rb +46 -0
- data/app/services/stripe_model_callbacks/sync_everything.rb +8 -0
- data/app/services/stripe_model_callbacks/sync_from_stripe.rb +29 -0
- data/app/services/stripe_model_callbacks/tax_rate/updated_service.rb +27 -0
- data/app/services/stripe_model_callbacks/transfer/updated_service.rb +1 -1
- data/config/routes.rb +1 -1
- 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/20200317160513_add_auto_advance_and_billing_reason_to_stripe_invoices.rb +6 -0
- data/db/migrate/20200317160602_add_status_and_status_transitions_to_stripe_invoices.rb +10 -0
- data/db/migrate/20200317180115_add_product_type_to_products.rb +5 -0
- data/db/migrate/20200317181224_rename_stripe_invoices_application_fee_to_application_fee_amount.rb +6 -0
- data/db/migrate/20200320105158_make_stripe_invoices_forgive_optional.rb +5 -0
- data/db/migrate/20200320114859_make_stripe_invoice_closed_optional.rb +5 -0
- data/db/migrate/20200323143634_add_amount_paid_and_remaining_and_invoice_url_and_pdf.rb +11 -0
- data/db/migrate/20200331074940_add_unit_label_to_stripe_products.rb +5 -0
- data/db/migrate/20200331075241_add_metered_info_to_stripe_plans.rb +9 -0
- data/db/migrate/20200513131105_create_stripe_subscription_schedules.rb +31 -0
- data/db/migrate/20200513131120_create_stripe_subscription_schedule_phases.rb +21 -0
- data/db/migrate/20200513131640_create_stripe_subscription_schedule_phase_plans.rb +13 -0
- data/db/migrate/20200514132247_add_stripe_id_uniq_to_stripe_invoices.rb +8 -0
- data/db/migrate/20200520152604_change_stripe_subscription_schedule_phase_id_to_bigint.rb +27 -0
- data/db/migrate/20200529144649_add_latest_invoice_id_to_stripe_subscription.rb +5 -0
- data/db/migrate/20201223173035_rename_stripe_subscriptions_start_to_start_date.rb +5 -0
- data/db/migrate/20201223202117_create_stripe_tax_rates.rb +15 -0
- data/db/migrate/20201224120534_create_stripe_subscription_default_tax_rates.rb +9 -0
- data/db/migrate/20201224122058_create_stripe_prices.rb +26 -0
- data/db/migrate/20201224123838_add_stripe_price_to_stripe_subscription_items.rb +6 -0
- data/db/migrate/20211121155313_change_invoice_billing_to_nullable.rb +5 -0
- data/db/migrate/20211121155732_rename_invoices_billing_to_deprecated_billing.rb +5 -0
- data/lib/stripe_model_callbacks/configuration.rb +1 -1
- data/lib/stripe_model_callbacks/factories/stripe_coupons.rb +16 -0
- data/lib/stripe_model_callbacks/factories/stripe_invoices.rb +1 -1
- data/lib/stripe_model_callbacks/factories/stripe_plans.rb +3 -1
- data/lib/stripe_model_callbacks/factories/stripe_prices.rb +3 -0
- data/lib/stripe_model_callbacks/factories/stripe_products.rb +13 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_default_tax_rates.rb +6 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule.rb.rb +5 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule_phase_plan.rb +5 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule_phases.rb +5 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscriptions.rb +1 -1
- data/lib/stripe_model_callbacks/factories/stripe_tax_rates.rb +5 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/account/account.external_account.created.json +29 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/account/account.external_account.deleted.json +29 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/account/account.external_account.updated.json +31 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.dispute.closed.json +65 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.dispute.created.json +65 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.dispute.funds_reinstated.json +65 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.dispute.funds_withdrawn.json +65 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.dispute.updated.json +96 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.refund.updated.json +28 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.refunded.json +98 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/coupon/coupon.created.json +29 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/coupon/coupon.deleted.json +29 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/coupon/coupon.updated.json +31 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.bank_account.deleted.json +29 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.created.json +36 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.deleted.json +47 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.discount.created.json +36 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.discount.deleted.json +36 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.discount.updated.json +55 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.source.created.card.json +80 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.source.created.json +53 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.source.deleted.json +53 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.source.expiring.json +53 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.source.updated.json +56 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.created.2020-12-24.json +112 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.created.json +84 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.deleted.json +83 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.trial_will_end.json +88 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.updated.json +100 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.updated.json +50 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.created.json +135 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.payment_failed.json +100 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.payment_succeeded.json +207 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.sent.json +100 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.upcoming.json +100 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.updated.json +105 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.voided.json +152 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoiceitem/invoiceitem.created.json +34 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/order/order.created.json +72 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/order/order.updated.json +74 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payout/payout.canceled.json +35 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payout/payout.created.json +35 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payout/payout.failed.json +35 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payout/payout.paid.json +35 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payout/payout.updated.json +37 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/plan/plan.created.json +32 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/plan/plan.deleted.json +27 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/plan/plan.updated.json +30 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.created.json +37 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.deleted.json +37 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.updated.json +40 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/product/product.created.json +49 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/product/product.deleted.json +48 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/product/product.updated.json +50 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/recipient/recipient.created.json +37 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/recipient/recipient.deleted.json +37 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/recipient/recipient.updated.json +39 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/review/review.closed.json +21 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/review/review.opened.json +21 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/sku/sku.created.json +36 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/sku/sku.deleted.json +36 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/sku/sku.updated.json +38 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/source/source.canceled.json +53 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/source/source.chargeable.json +53 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/source/source.failed.json +53 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/source/source.mandate_notification.json +53 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/source/source.transaction_created.json +21 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.canceled.json +81 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.created.json +77 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.updated.json +87 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/tax_rate/tax_rate.created.json +26 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/tax_rate/tax_rate.updated.json +29 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/transfer/transfer.created.json +41 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/transfer/transfer.reversed.json +40 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/transfer/transfer.updated.json +44 -0
- data/lib/stripe_model_callbacks/models/stripe_bank_account.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_card.rb +2 -1
- data/lib/stripe_model_callbacks/models/stripe_charge.rb +3 -0
- data/lib/stripe_model_callbacks/models/stripe_coupon.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_customer.rb +4 -2
- data/lib/stripe_model_callbacks/models/stripe_discount.rb +10 -3
- data/lib/stripe_model_callbacks/models/stripe_dispute.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_invoice.rb +62 -7
- data/lib/stripe_model_callbacks/models/stripe_invoice_item.rb +2 -0
- data/lib/stripe_model_callbacks/models/stripe_order.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_order_item.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_payout.rb +2 -1
- data/lib/stripe_model_callbacks/models/stripe_plan.rb +4 -1
- data/lib/stripe_model_callbacks/models/stripe_price.rb +27 -0
- data/lib/stripe_model_callbacks/models/stripe_product.rb +17 -7
- data/lib/stripe_model_callbacks/models/stripe_recipient.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_refund.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_review.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_sku.rb +13 -6
- data/lib/stripe_model_callbacks/models/stripe_source.rb +4 -2
- data/lib/stripe_model_callbacks/models/stripe_subscription.rb +56 -6
- data/lib/stripe_model_callbacks/models/stripe_subscription_default_tax_rate.rb +4 -0
- data/lib/stripe_model_callbacks/models/stripe_subscription_item.rb +19 -1
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule.rb +94 -0
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule_phase.rb +67 -0
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule_phase_plan.rb +37 -0
- data/lib/stripe_model_callbacks/models/stripe_tax_rate.rb +22 -0
- data/lib/stripe_model_callbacks/models/stripe_transfer.rb +1 -0
- data/lib/stripe_model_callbacks/version.rb +1 -1
- data/lib/stripe_model_callbacks.rb +1 -0
- metadata +176 -24
- data/app/assets/config/stripe_model_callbacks_manifest.js +0 -2
- data/app/assets/javascripts/stripe_model_callbacks/application.js +0 -13
- data/app/assets/stylesheets/stripe_model_callbacks/application.css +0 -15
- data/app/controllers/stripe_model_callbacks/application_controller.rb +0 -3
- data/app/jobs/stripe_model_callbacks/application_job.rb +0 -2
- data/app/mailers/stripe_model_callbacks/application_mailer.rb +0 -4
- data/app/views/layouts/stripe_model_callbacks/application.html.erb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42089c7d894f5caccf7893fe4cb02f580e8a4cfbb6a72ba465894c5ebfb090f4
|
4
|
+
data.tar.gz: 9be3e19bc9f6028312f9449704a79f232b86053961f61770ae36a679e93b183e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80831241dc6bebde63eb620b78c4c10cf2701e66867da8339f77ffcad40d8306126c7e84084980679929f7f765e3ba00bc3ddc78a9ba26fda82ba4a9528ff7d0
|
7
|
+
data.tar.gz: dc731f5bed637ed10b1beac0205b1199c5cf3a787d5139f30d38b0ff3d70aa86e0fe8557fe9bffeec55eef147e58b96ccffed3b27a8ab9f23043a7b9076768fe
|
data/README.md
CHANGED
@@ -27,6 +27,11 @@ Install the migration for Public Activity, which will provide logging:
|
|
27
27
|
rails g public_activity:migration
|
28
28
|
```
|
29
29
|
|
30
|
+
You can install the migrations (or update an existing installation) with this command:
|
31
|
+
```bash
|
32
|
+
rails stripe_model_callbacks:install:migrations
|
33
|
+
```
|
34
|
+
|
30
35
|
Do something like this in `config/routes.rb`:
|
31
36
|
```ruby
|
32
37
|
Rails.application.routes.draw do
|
@@ -62,6 +67,16 @@ log and more automatically using the ActiveRecord models.
|
|
62
67
|
|
63
68
|
You can use a service like Ultrahook to set up a proxy for your local development machine to test against.
|
64
69
|
|
70
|
+
### Report errors
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
StripeModelCallbacks.configure do |config|
|
74
|
+
config.on_error do |args:, error:|
|
75
|
+
PeakFlowUtils::Notifier.notify(error: error)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
65
80
|
## Usage
|
66
81
|
|
67
82
|
### Queries with ActiveRecord
|
@@ -128,7 +143,7 @@ https://github.com/kaspernj/stripe_model_callbacks/tree/master/spec/factories
|
|
128
143
|
|
129
144
|
You can mock Stripe events by using the helper method `mock_stripe_event` by including this helper:
|
130
145
|
|
131
|
-
```
|
146
|
+
```ruby
|
132
147
|
RSpec.configure do |config|
|
133
148
|
config.include StripeModelCallbacks::EventMocker
|
134
149
|
end
|
@@ -149,5 +164,11 @@ mock_stripe_event("invoice.created", data: {object: {discount: {"customer": "cus
|
|
149
164
|
## Contributing
|
150
165
|
Contribution directions go here.
|
151
166
|
|
167
|
+
# Database schema changes
|
168
|
+
1. Add migration to `/db/migrate`: ```rails g migration AddStripeIdUniqToStripeInvoices```
|
169
|
+
2. Go to directory: `spec/dummy`
|
170
|
+
3. Run ```rails stripe_model_callbacks:install:migrations``` to copy missing migrations
|
171
|
+
4. Run `rails db:migrate` to apply schema changes
|
172
|
+
|
152
173
|
## License
|
153
174
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -6,7 +6,24 @@ class StripeModelCallbacks::ApplicationRecord < ActiveRecord::Base
|
|
6
6
|
|
7
7
|
attr_writer :stripe_object
|
8
8
|
|
9
|
+
def self.check_object_is_stripe_class(object, allowed = nil)
|
10
|
+
raise "'stripe_class' not defined on #{name}" unless respond_to?(:stripe_class)
|
11
|
+
|
12
|
+
# Ignore general objects
|
13
|
+
return if object.class.name == "Stripe::StripeObject" # rubocop:disable Style/ClassEqualityComparison:
|
14
|
+
|
15
|
+
allowed ||= [stripe_class]
|
16
|
+
|
17
|
+
raise "Expected #{object.class.name} to be a #{allowed.map(&:name).join(", ")}" unless allowed.any? { |stripe_class| object.is_a?(stripe_class) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def check_object_is_stripe_class(object, allowed = nil)
|
21
|
+
self.class.check_object_is_stripe_class(object, allowed)
|
22
|
+
end
|
23
|
+
|
9
24
|
def self.create_from_stripe!(object)
|
25
|
+
check_object_is_stripe_class(object)
|
26
|
+
|
10
27
|
model = new
|
11
28
|
model.stripe_object = object
|
12
29
|
model.assign_from_stripe(object)
|
@@ -1,10 +1,10 @@
|
|
1
1
|
class StripeModelCallbacks::BaseService < ServicePattern::Service
|
2
|
-
def self.reported_execute!(*args, &blk)
|
2
|
+
def self.reported_execute!(*args, **opts, &blk)
|
3
3
|
with_exception_notifications do
|
4
|
-
response = execute(*args, &blk)
|
4
|
+
response = execute(*args, **opts, &blk)
|
5
5
|
raise response.errors.join(". ") unless response.success?
|
6
6
|
|
7
|
-
|
7
|
+
response
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -20,7 +20,40 @@ class StripeModelCallbacks::BaseService < ServicePattern::Service
|
|
20
20
|
Rails.logger.error e.backtrace.join("\n")
|
21
21
|
end
|
22
22
|
|
23
|
-
ExceptionNotifier.notify_exception(e) if Object.const_defined?(
|
23
|
+
ExceptionNotifier.notify_exception(e) if Object.const_defined?(:ExceptionNotifier)
|
24
|
+
PeakFlowUtils::Notifier.notify(error: e) if Object.const_defined?(:PeakFlowUtils)
|
24
25
|
raise e
|
25
26
|
end
|
27
|
+
|
28
|
+
def self.execute_with_advisory_lock!(*args, **opts, &blk)
|
29
|
+
# The difference between the stripe events is about a few milliseconds - with advisory_lock
|
30
|
+
# we will prevent from creating duplicated objects due to race condition.
|
31
|
+
# https://stripe.com/docs/webhooks/best-practices#event-ordering
|
32
|
+
with_exception_notifications do
|
33
|
+
StripeModelCallbacks::ApplicationRecord.with_advisory_lock(advisory_lock_name(*args, **opts)) do
|
34
|
+
response = execute(*args, **opts, &blk)
|
35
|
+
raise response.errors.join(". ") unless response.success?
|
36
|
+
|
37
|
+
response
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.advisory_lock_name(event:)
|
43
|
+
stripe_event_data = event.data.object
|
44
|
+
|
45
|
+
["stripe", stripe_event_data.object, "id", advisory_lock_id(stripe_event_data)].join("-")
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.advisory_lock_id(stripe_event_data)
|
49
|
+
return stripe_event_data.id if stripe_event_data.respond_to?(:id)
|
50
|
+
return stripe_event_data.coupon.id if stripe_event_data.object == "discount"
|
51
|
+
return unless stripe_event_data.respond_to?(:customer)
|
52
|
+
|
53
|
+
if stripe_event_data.customer.is_a?(String)
|
54
|
+
stripe_event_data.customer
|
55
|
+
else
|
56
|
+
stripe_event_data.customer.id
|
57
|
+
end
|
58
|
+
end
|
26
59
|
end
|
@@ -5,7 +5,7 @@ class StripeModelCallbacks::ConfigureService < StripeModelCallbacks::BaseEventSe
|
|
5
5
|
@events = events
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
8
|
+
def perform # rubocop:disable Metrics/AbcSize
|
9
9
|
all_events
|
10
10
|
charge_refund_events
|
11
11
|
customer_bank_account_events
|
@@ -22,11 +22,14 @@ class StripeModelCallbacks::ConfigureService < StripeModelCallbacks::BaseEventSe
|
|
22
22
|
recipient_events
|
23
23
|
payout_events
|
24
24
|
plan_events
|
25
|
+
price_events
|
25
26
|
product_events
|
26
27
|
review_events
|
27
28
|
sku_events
|
28
29
|
source_events
|
29
30
|
subscription_events
|
31
|
+
subscription_schedule_events
|
32
|
+
tax_rate_events
|
30
33
|
transfer_events
|
31
34
|
|
32
35
|
succeed!
|
@@ -37,35 +40,35 @@ private
|
|
37
40
|
def account_external_account_events
|
38
41
|
%w[created deleted updated].each do |external_account_event|
|
39
42
|
subscribe "account.external_account.#{external_account_event}" do |event|
|
40
|
-
StripeModelCallbacks::Account::ExternalAccount::UpdatedService.
|
43
|
+
StripeModelCallbacks::Account::ExternalAccount::UpdatedService.execute_with_advisory_lock!(event: event)
|
41
44
|
end
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
45
48
|
def all_events
|
46
49
|
events.all do |event|
|
47
|
-
StripeModelCallbacks::NotifierService.
|
50
|
+
StripeModelCallbacks::NotifierService.execute_with_advisory_lock!(event: event)
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|
51
54
|
def charge_events
|
52
55
|
%w[captured failed pending refunded updated succeeded].each do |charge_event|
|
53
56
|
subscribe "charge.#{charge_event}" do |event|
|
54
|
-
StripeModelCallbacks::Charge::UpdatedService.
|
57
|
+
StripeModelCallbacks::Charge::UpdatedService.execute_with_advisory_lock!(event: event)
|
55
58
|
end
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
59
62
|
def charge_refund_events
|
60
63
|
subscribe "charge.refund.updated" do |event|
|
61
|
-
StripeModelCallbacks::Refund::UpdatedService.
|
64
|
+
StripeModelCallbacks::Refund::UpdatedService.execute_with_advisory_lock!(event: event)
|
62
65
|
end
|
63
66
|
end
|
64
67
|
|
65
68
|
def charge_dispute_events
|
66
69
|
%w[closed created funds_reinstated funds_withdrawn updated].each do |charge_event|
|
67
70
|
subscribe "charge.dispute.#{charge_event}" do |event|
|
68
|
-
StripeModelCallbacks::Charge::DisputeUpdatedService.
|
71
|
+
StripeModelCallbacks::Charge::DisputeUpdatedService.execute_with_advisory_lock!(event: event)
|
69
72
|
end
|
70
73
|
end
|
71
74
|
end
|
@@ -73,21 +76,21 @@ private
|
|
73
76
|
def coupon_events
|
74
77
|
%w[created deleted updated].each do |coupon_event|
|
75
78
|
subscribe "coupon.#{coupon_event}" do |event|
|
76
|
-
StripeModelCallbacks::Coupon::UpdatedService.
|
79
|
+
StripeModelCallbacks::Coupon::UpdatedService.execute_with_advisory_lock!(event: event)
|
77
80
|
end
|
78
81
|
end
|
79
82
|
end
|
80
83
|
|
81
84
|
def customer_bank_account_events
|
82
85
|
subscribe "customer.bank_account.deleted" do |event|
|
83
|
-
StripeModelCallbacks::Customer::BankAccount::DeletedService.
|
86
|
+
StripeModelCallbacks::Customer::BankAccount::DeletedService.execute_with_advisory_lock!(event: event)
|
84
87
|
end
|
85
88
|
end
|
86
89
|
|
87
90
|
def customer_discount_events
|
88
91
|
%w[created deleted updated].each do |customer_event|
|
89
92
|
subscribe "customer.discount.#{customer_event}" do |event|
|
90
|
-
StripeModelCallbacks::Customer::DiscountUpdatedService.
|
93
|
+
StripeModelCallbacks::Customer::DiscountUpdatedService.execute_with_advisory_lock!(event: event)
|
91
94
|
end
|
92
95
|
end
|
93
96
|
end
|
@@ -95,7 +98,7 @@ private
|
|
95
98
|
def customer_events
|
96
99
|
%w[created deleted updated].each do |customer_event|
|
97
100
|
subscribe "customer.#{customer_event}" do |event|
|
98
|
-
StripeModelCallbacks::Customer::UpdatedService.
|
101
|
+
StripeModelCallbacks::Customer::UpdatedService.execute_with_advisory_lock!(event: event)
|
99
102
|
end
|
100
103
|
end
|
101
104
|
end
|
@@ -103,7 +106,7 @@ private
|
|
103
106
|
def customer_source_events
|
104
107
|
%w[created deleted expiring updated].each do |customer_event|
|
105
108
|
subscribe "customer.source.#{customer_event}" do |event|
|
106
|
-
StripeModelCallbacks::Customer::SourceUpdatedService.
|
109
|
+
StripeModelCallbacks::Customer::SourceUpdatedService.execute_with_advisory_lock!(event: event)
|
107
110
|
end
|
108
111
|
end
|
109
112
|
end
|
@@ -112,9 +115,10 @@ private
|
|
112
115
|
# Upcoming event doesnt send an invoice ID. Dunno what to do about it... Disabling for now
|
113
116
|
# upcoming
|
114
117
|
|
115
|
-
|
118
|
+
# https://stripe.com/docs/billing/invoices/overview#invoice-status-transition-endpoints-and-webhooks
|
119
|
+
%w[created deleted marked_uncollectible payment_failed payment_succeeded sent updated voided].each do |invoice_event|
|
116
120
|
subscribe "invoice.#{invoice_event}" do |event|
|
117
|
-
StripeModelCallbacks::Invoice::UpdatedService.
|
121
|
+
StripeModelCallbacks::Invoice::UpdatedService.execute_with_advisory_lock!(event: event)
|
118
122
|
end
|
119
123
|
end
|
120
124
|
end
|
@@ -122,7 +126,7 @@ private
|
|
122
126
|
def invoice_item_events
|
123
127
|
%w[created deleted updated].each do |event_type|
|
124
128
|
subscribe "invoiceitem.#{event_type}" do |event|
|
125
|
-
StripeModelCallbacks::InvoiceItem::UpdatedService.
|
129
|
+
StripeModelCallbacks::InvoiceItem::UpdatedService.execute_with_advisory_lock!(event: event)
|
126
130
|
end
|
127
131
|
end
|
128
132
|
end
|
@@ -130,7 +134,7 @@ private
|
|
130
134
|
def order_events
|
131
135
|
%w[created updated].each do |order_event|
|
132
136
|
subscribe "order.#{order_event}" do |event|
|
133
|
-
StripeModelCallbacks::Order::UpdatedService.
|
137
|
+
StripeModelCallbacks::Order::UpdatedService.execute_with_advisory_lock!(event: event)
|
134
138
|
end
|
135
139
|
end
|
136
140
|
end
|
@@ -138,7 +142,7 @@ private
|
|
138
142
|
def payout_events
|
139
143
|
%w[canceled created failed paid updated].each do |payout_event|
|
140
144
|
subscribe "payout.#{payout_event}" do |event|
|
141
|
-
StripeModelCallbacks::Payout::UpdatedService.
|
145
|
+
StripeModelCallbacks::Payout::UpdatedService.execute_with_advisory_lock!(event: event)
|
142
146
|
end
|
143
147
|
end
|
144
148
|
end
|
@@ -146,7 +150,7 @@ private
|
|
146
150
|
def recipient_events
|
147
151
|
%w[created deleted updated].each do |recipient_event|
|
148
152
|
subscribe "recipient.#{recipient_event}" do |event|
|
149
|
-
StripeModelCallbacks::Recipient::UpdatedService.
|
153
|
+
StripeModelCallbacks::Recipient::UpdatedService.execute_with_advisory_lock!(event: event)
|
150
154
|
end
|
151
155
|
end
|
152
156
|
end
|
@@ -154,7 +158,7 @@ private
|
|
154
158
|
def plan_events
|
155
159
|
%w[created deleted updated].each do |plan_event|
|
156
160
|
subscribe "plan.#{plan_event}" do |event|
|
157
|
-
StripeModelCallbacks::Plan::UpdatedService.
|
161
|
+
StripeModelCallbacks::Plan::UpdatedService.execute_with_advisory_lock!(event: event)
|
158
162
|
end
|
159
163
|
end
|
160
164
|
end
|
@@ -162,7 +166,15 @@ private
|
|
162
166
|
def sku_events
|
163
167
|
%w[created deleted updated].each do |sku_event|
|
164
168
|
subscribe "sku.#{sku_event}" do |event|
|
165
|
-
StripeModelCallbacks::Sku::UpdatedService.
|
169
|
+
StripeModelCallbacks::Sku::UpdatedService.execute_with_advisory_lock!(event: event)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def price_events
|
175
|
+
%w[created deleted updated].each do |price_event|
|
176
|
+
subscribe "price.#{price_event}" do |event|
|
177
|
+
StripeModelCallbacks::Price::UpdatedService.execute_with_advisory_lock!(event: event)
|
166
178
|
end
|
167
179
|
end
|
168
180
|
end
|
@@ -170,7 +182,7 @@ private
|
|
170
182
|
def product_events
|
171
183
|
%w[created deleted updated].each do |product_event|
|
172
184
|
subscribe "product.#{product_event}" do |event|
|
173
|
-
StripeModelCallbacks::Product::UpdatedService.
|
185
|
+
StripeModelCallbacks::Product::UpdatedService.execute_with_advisory_lock!(event: event)
|
174
186
|
end
|
175
187
|
end
|
176
188
|
end
|
@@ -178,7 +190,7 @@ private
|
|
178
190
|
def review_events
|
179
191
|
%w[opened closed].each do |review_event|
|
180
192
|
subscribe "review.#{review_event}" do |event|
|
181
|
-
StripeModelCallbacks::Review::UpdatedService.
|
193
|
+
StripeModelCallbacks::Review::UpdatedService.execute_with_advisory_lock!(event: event)
|
182
194
|
end
|
183
195
|
end
|
184
196
|
end
|
@@ -186,7 +198,7 @@ private
|
|
186
198
|
def source_events
|
187
199
|
%w[canceled chargeable failed mandate_notification].each do |source_event|
|
188
200
|
subscribe "source.#{source_event}" do |event|
|
189
|
-
StripeModelCallbacks::Source::UpdatedService.
|
201
|
+
StripeModelCallbacks::Source::UpdatedService.execute_with_advisory_lock!(event: event)
|
190
202
|
end
|
191
203
|
end
|
192
204
|
end
|
@@ -202,7 +214,23 @@ private
|
|
202
214
|
def subscription_events
|
203
215
|
%w[created deleted trial_will_end updated].each do |subscription_event|
|
204
216
|
subscribe "customer.subscription.#{subscription_event}" do |event|
|
205
|
-
StripeModelCallbacks::Customer::Subscription::UpdatedService.
|
217
|
+
StripeModelCallbacks::Customer::Subscription::UpdatedService.execute_with_advisory_lock!(event: event)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def subscription_schedule_events
|
223
|
+
%w[canceled created updated].each do |subscription_event|
|
224
|
+
subscribe "subscription_schedule.#{subscription_event}" do |event|
|
225
|
+
StripeModelCallbacks::SubscriptionSchedule::UpdatedService.execute_with_advisory_lock!(event: event)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def tax_rate_events
|
231
|
+
%w[created updated].each do |transfer_event|
|
232
|
+
subscribe "tax_rate.#{transfer_event}" do |event|
|
233
|
+
StripeModelCallbacks::TaxRate::UpdatedService.execute_with_advisory_lock!(event: event)
|
206
234
|
end
|
207
235
|
end
|
208
236
|
end
|
@@ -210,7 +238,7 @@ private
|
|
210
238
|
def transfer_events
|
211
239
|
%w[created reversed updated].each do |transfer_event|
|
212
240
|
subscribe "transfer.#{transfer_event}" do |event|
|
213
|
-
StripeModelCallbacks::Transfer::UpdatedService.
|
241
|
+
StripeModelCallbacks::Transfer::UpdatedService.execute_with_advisory_lock!(event: event)
|
214
242
|
end
|
215
243
|
end
|
216
244
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class StripeModelCallbacks::Customer::SourceUpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
-
def
|
2
|
+
def perform
|
3
3
|
source = stripe_class.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
source.assign_from_stripe(object)
|
5
5
|
source.deleted_at = Time.zone.now if event.type == "customer.source.deleted"
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class StripeModelCallbacks::Customer::Subscription::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
-
def
|
2
|
+
def perform
|
3
3
|
subscription.assign_from_stripe(object)
|
4
4
|
subscription.deleted_at ||= Time.zone.now if event.type == "customer.subscription.deleted"
|
5
5
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class StripeModelCallbacks::Customer::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
-
def
|
2
|
+
def perform
|
3
3
|
customer = StripeCustomer.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
customer.assign_from_stripe(object)
|
5
5
|
customer.deleted_at ||= Time.zone.now if event.type == "customer.deleted"
|
@@ -22,7 +22,7 @@ private
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def fixture_path
|
25
|
-
@fixture_path ||= "#{File.dirname(__FILE__)}/../../../
|
25
|
+
@fixture_path ||= "#{File.dirname(__FILE__)}/../../../lib/stripe_model_callbacks/fixtures/stripe_events/#{first_part}/#{name}.json"
|
26
26
|
end
|
27
27
|
|
28
28
|
def payload
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class StripeModelCallbacks::InvoiceItem::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
-
def
|
2
|
+
def perform
|
3
3
|
invoice_item = ::StripeInvoiceItem.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
invoice_item.assign_from_stripe(object)
|
5
5
|
invoice_item.deleted_at = Time.zone.now if event.type == "invoiceitem.deleted"
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class StripeModelCallbacks::Plan::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
-
def
|
2
|
+
def perform
|
3
3
|
plan = StripePlan.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
plan.assign_from_stripe(object)
|
5
5
|
plan.deleted_at ||= Time.zone.now if event.type == "plan.deleted"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class StripeModelCallbacks::Price::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
+
def perform
|
3
|
+
price = StripePrice.find_or_initialize_by(stripe_id: object.id)
|
4
|
+
price.assign_from_stripe(object)
|
5
|
+
price.deleted_at ||= Time.zone.now if event.type == "price.deleted"
|
6
|
+
|
7
|
+
if price.save
|
8
|
+
price.create_activity :deleted if event.type == "price.deleted"
|
9
|
+
succeed!
|
10
|
+
else
|
11
|
+
fail! price.errors.full_messages
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class StripeModelCallbacks::Product::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
-
def
|
2
|
+
def perform
|
3
3
|
product = StripeProduct.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
product.assign_from_stripe(object)
|
5
5
|
product.deleted_at ||= Time.zone.now if event.type == "product.deleted"
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class StripeModelCallbacks::Recipient::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
-
def
|
2
|
+
def perform
|
3
3
|
recipient = StripeRecipient.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
recipient.assign_from_stripe(object)
|
5
5
|
recipient.deleted_at ||= Time.zone.now if event.type == "recipient.deleted"
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class StripeModelCallbacks::Sku::UpdatedService < StripeModelCallbacks::BaseEventService
|
2
|
-
def
|
2
|
+
def perform
|
3
3
|
sku = StripeSku.find_or_initialize_by(stripe_id: object.id)
|
4
4
|
sku.assign_from_stripe(object)
|
5
5
|
sku.deleted_at ||= Time.zone.now if event.type == "sku.deleted"
|
@@ -6,13 +6,13 @@ class StripeModelCallbacks::Subscription::StateCheckerService < StripeModelCallb
|
|
6
6
|
@state = state.to_s
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
9
|
+
def perform
|
10
10
|
if state.is_a?(Array)
|
11
11
|
state.each do |state_i|
|
12
12
|
response = Subscription::StateCheckerService.execute!(allowed: allowed, state: state_i)
|
13
13
|
return response unless response.success?
|
14
14
|
end
|
15
|
-
elsif
|
15
|
+
elsif allowed.exclude?(state)
|
16
16
|
return fail! ["Not allowed: #{state}"]
|
17
17
|
end
|
18
18
|
|