stripe_model_callbacks 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -0
  3. data/app/models/stripe_model_callbacks/application_record.rb +17 -0
  4. data/app/services/stripe_model_callbacks/account/external_account/updated_service.rb +1 -1
  5. data/app/services/stripe_model_callbacks/attributes_assigner_service.rb +1 -1
  6. data/app/services/stripe_model_callbacks/base_service.rb +33 -1
  7. data/app/services/stripe_model_callbacks/charge/dispute_updated_service.rb +1 -1
  8. data/app/services/stripe_model_callbacks/charge/updated_service.rb +1 -1
  9. data/app/services/stripe_model_callbacks/configure_service.rb +52 -24
  10. data/app/services/stripe_model_callbacks/coupon/sync_all.rb +9 -0
  11. data/app/services/stripe_model_callbacks/coupon/updated_service.rb +1 -1
  12. data/app/services/stripe_model_callbacks/customer/bank_account/deleted_service.rb +1 -1
  13. data/app/services/stripe_model_callbacks/customer/deleted_service.rb +1 -1
  14. data/app/services/stripe_model_callbacks/customer/discount_updated_service.rb +1 -1
  15. data/app/services/stripe_model_callbacks/customer/source_updated_service.rb +1 -1
  16. data/app/services/stripe_model_callbacks/customer/subscription/updated_service.rb +1 -1
  17. data/app/services/stripe_model_callbacks/customer/updated_service.rb +1 -1
  18. data/app/services/stripe_model_callbacks/invoice/updated_service.rb +1 -1
  19. data/app/services/stripe_model_callbacks/invoice_item/updated_service.rb +1 -1
  20. data/app/services/stripe_model_callbacks/notifier_service.rb +1 -1
  21. data/app/services/stripe_model_callbacks/order/updated_service.rb +1 -1
  22. data/app/services/stripe_model_callbacks/payout/updated_service.rb +1 -1
  23. data/app/services/stripe_model_callbacks/plan/sync_all.rb +9 -0
  24. data/app/services/stripe_model_callbacks/plan/updated_service.rb +1 -1
  25. data/app/services/stripe_model_callbacks/price/updated_service.rb +14 -0
  26. data/app/services/stripe_model_callbacks/product/updated_service.rb +1 -1
  27. data/app/services/stripe_model_callbacks/recipient/updated_service.rb +1 -1
  28. data/app/services/stripe_model_callbacks/refund/updated_service.rb +1 -1
  29. data/app/services/stripe_model_callbacks/review/updated_service.rb +1 -1
  30. data/app/services/stripe_model_callbacks/sku/updated_service.rb +1 -1
  31. data/app/services/stripe_model_callbacks/source/updated_service.rb +1 -1
  32. data/app/services/stripe_model_callbacks/subscription/state_checker_service.rb +2 -2
  33. data/app/services/stripe_model_callbacks/subscription_schedule/updated_service.rb +46 -0
  34. data/app/services/stripe_model_callbacks/sync_everything.rb +8 -0
  35. data/app/services/stripe_model_callbacks/sync_from_stripe.rb +29 -0
  36. data/app/services/stripe_model_callbacks/tax_rate/updated_service.rb +27 -0
  37. data/app/services/stripe_model_callbacks/transfer/updated_service.rb +1 -1
  38. data/db/migrate/20200317160513_add_auto_advance_and_billing_reason_to_stripe_invoices.rb +6 -0
  39. data/db/migrate/20200317160602_add_status_and_status_transitions_to_stripe_invoices.rb +10 -0
  40. data/db/migrate/20200317180115_add_product_type_to_products.rb +5 -0
  41. data/db/migrate/20200317181224_rename_stripe_invoices_application_fee_to_application_fee_amount.rb +6 -0
  42. data/db/migrate/20200320105158_make_stripe_invoices_forgive_optional.rb +5 -0
  43. data/db/migrate/20200320114859_make_stripe_invoice_closed_optional.rb +5 -0
  44. data/db/migrate/20200323143634_add_amount_paid_and_remaining_and_invoice_url_and_pdf.rb +11 -0
  45. data/db/migrate/20200331074940_add_unit_label_to_stripe_products.rb +5 -0
  46. data/db/migrate/20200331075241_add_metered_info_to_stripe_plans.rb +9 -0
  47. data/db/migrate/20200513131105_create_stripe_subscription_schedules.rb +31 -0
  48. data/db/migrate/20200513131120_create_stripe_subscription_schedule_phases.rb +21 -0
  49. data/db/migrate/20200513131640_create_stripe_subscription_schedule_phase_plans.rb +13 -0
  50. data/db/migrate/20200514132247_add_stripe_id_uniq_to_stripe_invoices.rb +8 -0
  51. data/db/migrate/20200520152604_change_stripe_subscription_schedule_phase_id_to_bigint.rb +27 -0
  52. data/db/migrate/20200529144649_add_latest_invoice_id_to_stripe_subscription.rb +5 -0
  53. data/db/migrate/20201223173035_rename_stripe_subscriptions_start_to_start_date.rb +5 -0
  54. data/db/migrate/20201223202117_create_stripe_tax_rates.rb +15 -0
  55. data/db/migrate/20201224120534_create_stripe_subscription_default_tax_rates.rb +9 -0
  56. data/db/migrate/20201224122058_create_stripe_prices.rb +26 -0
  57. data/db/migrate/20201224123838_add_stripe_price_to_stripe_subscription_items.rb +6 -0
  58. data/lib/stripe_model_callbacks/configuration.rb +1 -1
  59. data/lib/stripe_model_callbacks/factories/stripe_coupons.rb +16 -0
  60. data/lib/stripe_model_callbacks/factories/stripe_plans.rb +3 -1
  61. data/lib/stripe_model_callbacks/factories/stripe_prices.rb +3 -0
  62. data/lib/stripe_model_callbacks/factories/stripe_products.rb +13 -0
  63. data/lib/stripe_model_callbacks/factories/stripe_subscription_default_tax_rates.rb +6 -0
  64. data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule.rb.rb +5 -0
  65. data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule_phase_plan.rb +5 -0
  66. data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule_phases.rb +5 -0
  67. data/lib/stripe_model_callbacks/factories/stripe_subscriptions.rb +1 -1
  68. data/lib/stripe_model_callbacks/factories/stripe_tax_rates.rb +5 -0
  69. data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.created.2020-12-24.json +112 -0
  70. data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.created.json +1 -0
  71. data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.deleted.json +2 -2
  72. data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.updated.json +1 -1
  73. data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.updated.json +1 -1
  74. data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.created.json +13 -3
  75. data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.payment_failed.json +14 -4
  76. data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.payment_succeeded.json +23 -4
  77. data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.sent.json +13 -3
  78. data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.upcoming.json +13 -3
  79. data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.updated.json +13 -3
  80. data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.voided.json +152 -0
  81. data/lib/stripe_model_callbacks/fixtures/stripe_events/plan/plan.created.json +7 -2
  82. data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.created.json +37 -0
  83. data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.deleted.json +37 -0
  84. data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.updated.json +40 -0
  85. data/lib/stripe_model_callbacks/fixtures/stripe_events/product/product.created.json +1 -0
  86. data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.canceled.json +81 -0
  87. data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.created.json +77 -0
  88. data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.updated.json +87 -0
  89. data/lib/stripe_model_callbacks/fixtures/stripe_events/tax_rate/tax_rate.created.json +26 -0
  90. data/lib/stripe_model_callbacks/fixtures/stripe_events/tax_rate/tax_rate.updated.json +29 -0
  91. data/lib/stripe_model_callbacks/models/stripe_bank_account.rb +1 -0
  92. data/lib/stripe_model_callbacks/models/stripe_card.rb +2 -1
  93. data/lib/stripe_model_callbacks/models/stripe_charge.rb +3 -0
  94. data/lib/stripe_model_callbacks/models/stripe_coupon.rb +1 -0
  95. data/lib/stripe_model_callbacks/models/stripe_customer.rb +4 -2
  96. data/lib/stripe_model_callbacks/models/stripe_discount.rb +10 -3
  97. data/lib/stripe_model_callbacks/models/stripe_dispute.rb +1 -0
  98. data/lib/stripe_model_callbacks/models/stripe_invoice.rb +62 -7
  99. data/lib/stripe_model_callbacks/models/stripe_invoice_item.rb +2 -0
  100. data/lib/stripe_model_callbacks/models/stripe_order.rb +1 -0
  101. data/lib/stripe_model_callbacks/models/stripe_order_item.rb +1 -0
  102. data/lib/stripe_model_callbacks/models/stripe_payout.rb +2 -1
  103. data/lib/stripe_model_callbacks/models/stripe_plan.rb +4 -1
  104. data/lib/stripe_model_callbacks/models/stripe_price.rb +27 -0
  105. data/lib/stripe_model_callbacks/models/stripe_product.rb +17 -7
  106. data/lib/stripe_model_callbacks/models/stripe_recipient.rb +1 -0
  107. data/lib/stripe_model_callbacks/models/stripe_refund.rb +1 -0
  108. data/lib/stripe_model_callbacks/models/stripe_review.rb +1 -0
  109. data/lib/stripe_model_callbacks/models/stripe_sku.rb +13 -6
  110. data/lib/stripe_model_callbacks/models/stripe_source.rb +4 -2
  111. data/lib/stripe_model_callbacks/models/stripe_subscription.rb +56 -6
  112. data/lib/stripe_model_callbacks/models/stripe_subscription_default_tax_rate.rb +4 -0
  113. data/lib/stripe_model_callbacks/models/stripe_subscription_item.rb +19 -1
  114. data/lib/stripe_model_callbacks/models/stripe_subscription_schedule.rb +94 -0
  115. data/lib/stripe_model_callbacks/models/stripe_subscription_schedule_phase.rb +67 -0
  116. data/lib/stripe_model_callbacks/models/stripe_subscription_schedule_phase_plan.rb +37 -0
  117. data/lib/stripe_model_callbacks/models/stripe_tax_rate.rb +22 -0
  118. data/lib/stripe_model_callbacks/models/stripe_transfer.rb +1 -0
  119. data/lib/stripe_model_callbacks/version.rb +1 -1
  120. data/lib/stripe_model_callbacks.rb +1 -0
  121. metadata +90 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 939298c1a7e304f9ad8d082d208cc395c3ffafdd5d68b202075f155f82921c02
4
- data.tar.gz: 5b144fd536551f1f56e3a3fe332eb95813e4b8124dc3acaf4d6a0d3dd7faff64
3
+ metadata.gz: 11a762bf963e197d38e74291fc7efe1c3d13d718a9905db6fb7ea502d7b84e9e
4
+ data.tar.gz: c0ffe2a2e5425471556f2987ce77877bda02452d2af2d0bed2fe266e340af849
5
5
  SHA512:
6
- metadata.gz: 904991f19ef50dc23ed77367d28c6ec16aa9f7f595c0c1546bc2f0a2620651d30c76c14055b859ccac9439e759073c368c9f47157b8f674db40d143ec4fd33d6
7
- data.tar.gz: b1265ecbaeac487fe38d51d3653c5c023e97a0314e6cd3f65b5b7745dadc9fc56c896bfdf3bdf897ed69fdb4d8e8ff592b5876005fc6c7a61a5aa9d0852c7af8
6
+ metadata.gz: c689a4d577b5d9a97ed070a25d4716a56553449ab1ea6c4859cd3d188c57c634d150b8c0beb70500093cce05660ed53f8073e440d70cb9886f8371c2b17161d5
7
+ data.tar.gz: f8b8705e2568a73469eab5c27c06d2d1c8ba271495dcfe89e3fff508af1537b6064c4e4756c3e40661e837d8f15538cc57a0cb8c5f7d50ac7f34a625a57d307f
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
@@ -149,5 +154,14 @@ mock_stripe_event("invoice.created", data: {object: {discount: {"customer": "cus
149
154
  ## Contributing
150
155
  Contribution directions go here.
151
156
 
157
+ # capybara-webkit installation issue
158
+ Instruction how to solve the issue: https://github.com/thoughtbot/capybara-webkit/wiki/Installing-Qt-and-compiling-capybara-webkit#macOS-catalina-1015
159
+
160
+ # Database schema changes
161
+ 1. Add migration to `/db/migrate`: ```rails g migration AddStripeIdUniqToStripeInvoices```
162
+ 2. Go to directory: `spec/dummy`
163
+ 3. Run ```rails stripe_model_callbacks:install:migrations``` to copy missing migrations
164
+ 4. Run `rails db:migrate` to apply schema changes
165
+
152
166
  ## License
153
167
  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,5 +1,5 @@
1
1
  class StripeModelCallbacks::Account::ExternalAccount::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  bank_account = StripeBankAccount.find_or_initialize_by(stripe_id: object.id)
4
4
  bank_account.assign_from_stripe(object)
5
5
 
@@ -7,7 +7,7 @@ class StripeModelCallbacks::AttributesAssignerService < ServicePattern::Service
7
7
  @stripe_model = stripe_model
8
8
  end
9
9
 
10
- def execute
10
+ def perform
11
11
  attributes.each do |attribute|
12
12
  next unless stripe_model.respond_to?(attribute)
13
13
 
@@ -4,7 +4,7 @@ class StripeModelCallbacks::BaseService < ServicePattern::Service
4
4
  response = execute(*args, &blk)
5
5
  raise response.errors.join(". ") unless response.success?
6
6
 
7
- return response
7
+ response
8
8
  end
9
9
  end
10
10
 
@@ -23,4 +23,36 @@ class StripeModelCallbacks::BaseService < ServicePattern::Service
23
23
  ExceptionNotifier.notify_exception(e) if Object.const_defined?("ExceptionNotifier")
24
24
  raise e
25
25
  end
26
+
27
+ def self.execute_with_advisory_lock!(*args, &blk)
28
+ # The difference between the stripe events is about a few milliseconds - with advisory_lock
29
+ # we will prevent from creating duplicated objects due to race condition.
30
+ # https://stripe.com/docs/webhooks/best-practices#event-ordering
31
+ with_exception_notifications do
32
+ StripeModelCallbacks::ApplicationRecord.with_advisory_lock(advisory_lock_name(*args)) do
33
+ response = execute(*args, &blk)
34
+ raise response.errors.join(". ") unless response.success?
35
+
36
+ response
37
+ end
38
+ end
39
+ end
40
+
41
+ def self.advisory_lock_name(*args)
42
+ stripe_event_data = args.first[:event].data.object
43
+
44
+ ["stripe", stripe_event_data.object, "id", advisory_lock_id(stripe_event_data)].join("-")
45
+ end
46
+
47
+ def self.advisory_lock_id(stripe_event_data)
48
+ return stripe_event_data.id if stripe_event_data.respond_to?(:id)
49
+ return stripe_event_data.coupon.id if stripe_event_data.object == "discount"
50
+ return unless stripe_event_data.respond_to?(:customer)
51
+
52
+ if stripe_event_data.customer.is_a?(String)
53
+ stripe_event_data.customer
54
+ else
55
+ stripe_event_data.customer.id
56
+ end
57
+ end
26
58
  end
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Charge::DisputeUpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  dispute.assign_from_stripe(object)
4
4
 
5
5
  if dispute.save
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Charge::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  charge.assign_from_stripe(object)
4
4
 
5
5
  if charge.save
@@ -5,7 +5,7 @@ class StripeModelCallbacks::ConfigureService < StripeModelCallbacks::BaseEventSe
5
5
  @events = events
6
6
  end
7
7
 
8
- def execute
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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
- %w[created payment_failed payment_succeeded sent updated].each do |invoice_event|
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
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.reported_execute!(event: event)
241
+ StripeModelCallbacks::Transfer::UpdatedService.execute_with_advisory_lock!(event: event)
214
242
  end
215
243
  end
216
244
  end
@@ -0,0 +1,9 @@
1
+ class StripeModelCallbacks::Coupon::SyncAll < StripeModelCallbacks::BaseService
2
+ def perform
3
+ Stripe::Coupon.list.each do |coupon|
4
+ StripeModelCallbacks::SyncFromStripe.execute!(stripe_object: coupon)
5
+ end
6
+
7
+ succeed!
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Coupon::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  coupon.assign_from_stripe(object)
4
4
  coupon.deleted_at ||= Time.zone.now if event.type == "coupon.deleted"
5
5
 
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Customer::BankAccount::DeletedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  bank_account = StripeBankAccount.find_or_initialize_by(stripe_id: object.id)
4
4
  bank_account.assign_from_stripe(object)
5
5
 
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Customer::DeletedService < StripeModelCallbacks::BaseEventService
2
- def execute
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
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Customer::DiscountUpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  discount.assign_from_stripe(object)
4
4
  discount.deleted_at ||= Time.zone.now if event&.type == "customer.discount.deleted"
5
5
 
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Customer::SourceUpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
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 execute
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 execute
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"
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Invoice::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  invoice.assign_from_stripe(object)
4
4
 
5
5
  if invoice.save
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::InvoiceItem::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
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::NotifierService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  Rails.logger.info "New Stripe event: #{event.type}"
4
4
  succeed!
5
5
  end
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Order::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  order.assign_from_stripe(object)
4
4
 
5
5
  if order.save
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Payout::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  payout.assign_from_stripe(object)
4
4
 
5
5
  if payout.save
@@ -0,0 +1,9 @@
1
+ class StripeModelCallbacks::Plan::SyncAll < StripeModelCallbacks::BaseService
2
+ def perform
3
+ Stripe::Plan.list.each do |plan|
4
+ StripeModelCallbacks::SyncFromStripe.execute!(stripe_object: plan)
5
+ end
6
+
7
+ succeed!
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Plan::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
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 execute
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 execute
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::Refund::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  refund = StripeRefund.find_or_initialize_by(stripe_id: object.id)
4
4
  refund.assign_from_stripe(object)
5
5
 
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Review::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  review = StripeReview.find_or_initialize_by(stripe_id: object.id)
4
4
  review.assign_from_stripe(object)
5
5
 
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Sku::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
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"
@@ -1,5 +1,5 @@
1
1
  class StripeModelCallbacks::Source::UpdatedService < StripeModelCallbacks::BaseEventService
2
- def execute
2
+ def perform
3
3
  source.assign_from_stripe(object)
4
4
 
5
5
  if source.save
@@ -6,13 +6,13 @@ class StripeModelCallbacks::Subscription::StateCheckerService < StripeModelCallb
6
6
  @state = state.to_s
7
7
  end
8
8
 
9
- def execute
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 !allowed.include?(state)
15
+ elsif allowed.exclude?(state)
16
16
  return fail! ["Not allowed: #{state}"]
17
17
  end
18
18
 
@@ -0,0 +1,46 @@
1
+ class StripeModelCallbacks::SubscriptionSchedule::UpdatedService < StripeModelCallbacks::BaseEventService
2
+ TRACKED_ACTIVITIES = {
3
+ "subscription_schedule.canceled": :canceled
4
+ }.with_indifferent_access.freeze
5
+ private_constant :TRACKED_ACTIVITIES
6
+
7
+ def perform
8
+ subscription_schedule.assign_from_stripe(object)
9
+ set_cancellation_date if canceled?
10
+
11
+ return success_actions if subscription_schedule.save
12
+
13
+ fail!(subscription_schedule.errors.full_messages)
14
+ end
15
+
16
+ private
17
+
18
+ def canceled?
19
+ @canceled ||= event.type == "subscription_schedule.canceled"
20
+ end
21
+
22
+ def create_activity
23
+ return unless tracked_activities[event.type]
24
+
25
+ subscription_schedule.create_activity(tracked_activities[event.type])
26
+ end
27
+
28
+ def set_cancellation_date
29
+ return unless canceled?
30
+
31
+ subscription_schedule.canceled_at ||= Time.zone.now
32
+ end
33
+
34
+ def subscription_schedule
35
+ @subscription_schedule ||= StripeSubscriptionSchedule.find_or_initialize_by(stripe_id: object.id)
36
+ end
37
+
38
+ def success_actions
39
+ create_activity
40
+ succeed!
41
+ end
42
+
43
+ def tracked_activities
44
+ TRACKED_ACTIVITIES
45
+ end
46
+ end
@@ -0,0 +1,8 @@
1
+ class StripeModelCallbacks::SyncEverything < StripeModelCallbacks::BaseService
2
+ def perform
3
+ StripeModelCallbacks::Coupon::SyncAll.execute!
4
+ StripeModelCallbacks::Plan::SyncAll.execute!
5
+
6
+ succeed!
7
+ end
8
+ end