stripe_model_callbacks 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +153 -0
  4. data/Rakefile +26 -0
  5. data/app/assets/config/stripe_model_callbacks_manifest.js +2 -0
  6. data/app/assets/javascripts/stripe_model_callbacks/application.js +13 -0
  7. data/app/assets/stylesheets/stripe_model_callbacks/application.css +15 -0
  8. data/app/controllers/stripe_model_callbacks/application_controller.rb +3 -0
  9. data/app/jobs/stripe_model_callbacks/application_job.rb +2 -0
  10. data/app/mailers/stripe_model_callbacks/application_mailer.rb +4 -0
  11. data/app/models/stripe_model_callbacks/application_record.rb +60 -0
  12. data/app/services/stripe_model_callbacks/account/external_account/updated_service.rb +13 -0
  13. data/app/services/stripe_model_callbacks/attributes_assigner_service.rb +35 -0
  14. data/app/services/stripe_model_callbacks/base_event_service.rb +9 -0
  15. data/app/services/stripe_model_callbacks/base_service.rb +26 -0
  16. data/app/services/stripe_model_callbacks/charge/dispute_updated_service.rb +29 -0
  17. data/app/services/stripe_model_callbacks/charge/updated_service.rb +40 -0
  18. data/app/services/stripe_model_callbacks/configure_service.rb +217 -0
  19. data/app/services/stripe_model_callbacks/coupon/updated_service.rb +18 -0
  20. data/app/services/stripe_model_callbacks/customer/bank_account/deleted_service.rb +13 -0
  21. data/app/services/stripe_model_callbacks/customer/deleted_service.rb +13 -0
  22. data/app/services/stripe_model_callbacks/customer/discount_updated_service.rb +32 -0
  23. data/app/services/stripe_model_callbacks/customer/source_updated_service.rb +25 -0
  24. data/app/services/stripe_model_callbacks/customer/subscription/updated_service.rb +28 -0
  25. data/app/services/stripe_model_callbacks/customer/updated_service.rb +13 -0
  26. data/app/services/stripe_model_callbacks/event_mocker_service.rb +38 -0
  27. data/app/services/stripe_model_callbacks/invoice/updated_service.rb +26 -0
  28. data/app/services/stripe_model_callbacks/invoice_item/updated_service.rb +13 -0
  29. data/app/services/stripe_model_callbacks/notifier_service.rb +6 -0
  30. data/app/services/stripe_model_callbacks/order/updated_service.rb +28 -0
  31. data/app/services/stripe_model_callbacks/payout/updated_service.rb +29 -0
  32. data/app/services/stripe_model_callbacks/plan/updated_service.rb +14 -0
  33. data/app/services/stripe_model_callbacks/product/updated_service.rb +14 -0
  34. data/app/services/stripe_model_callbacks/recipient/updated_service.rb +14 -0
  35. data/app/services/stripe_model_callbacks/refund/updated_service.rb +12 -0
  36. data/app/services/stripe_model_callbacks/review/updated_service.rb +13 -0
  37. data/app/services/stripe_model_callbacks/sku/updated_service.rb +14 -0
  38. data/app/services/stripe_model_callbacks/source/updated_service.rb +31 -0
  39. data/app/services/stripe_model_callbacks/subscription/state_checker_service.rb +21 -0
  40. data/app/services/stripe_model_callbacks/transfer/updated_service.rb +18 -0
  41. data/app/views/layouts/stripe_model_callbacks/application.html.erb +14 -0
  42. data/config/rails_best_practices.yml +49 -0
  43. data/config/routes.rb +2 -0
  44. data/db/migrate/20180206115801_create_stripe_charges.rb +54 -0
  45. data/db/migrate/20180206115802_create_stripe_coupons.rb +22 -0
  46. data/db/migrate/20180206115803_create_stripe_customers.rb +21 -0
  47. data/db/migrate/20180206115804_create_stripe_discounts.rb +27 -0
  48. data/db/migrate/20180206115805_create_stripe_invoice_items.rb +26 -0
  49. data/db/migrate/20180206115806_create_stripe_invoices.rb +57 -0
  50. data/db/migrate/20180206115807_create_stripe_order_items.rb +15 -0
  51. data/db/migrate/20180206115808_create_stripe_orders.rb +46 -0
  52. data/db/migrate/20180206115809_create_stripe_payouts.rb +27 -0
  53. data/db/migrate/20180206115810_create_stripe_plans.rb +20 -0
  54. data/db/migrate/20180206115811_create_stripe_products.rb +25 -0
  55. data/db/migrate/20180206115812_create_stripe_recipients.rb +18 -0
  56. data/db/migrate/20180206115813_create_stripe_refunds.rb +21 -0
  57. data/db/migrate/20180206115814_create_stripe_skus.rb +22 -0
  58. data/db/migrate/20180206115815_create_stripe_sources.rb +70 -0
  59. data/db/migrate/20180206115816_create_stripe_subscriptions.rb +29 -0
  60. data/db/migrate/20180206115817_create_stripe_transfers.rb +25 -0
  61. data/db/migrate/20180206130408_create_disputes.rb +76 -0
  62. data/db/migrate/20180206151132_change_activities_trackable_id_to_string.rb +5 -0
  63. data/db/migrate/20180207121808_create_stripe_subscription_items.rb +14 -0
  64. data/db/migrate/20180207171559_remove_name_from_stripe_plans.rb +5 -0
  65. data/db/migrate/20180207172011_add_stripe_product_id_to_stripe_plans.rb +6 -0
  66. data/db/migrate/20180207185604_add_index_on_refund_charge.rb +5 -0
  67. data/db/migrate/20180208091647_change_stripe_subscription_items_quantity_to_integer.rb +5 -0
  68. data/db/migrate/20180208102513_add_attempt_count_to_stripe_invoices.rb +7 -0
  69. data/db/migrate/20180208121235_create_stripe_reviews.rb +13 -0
  70. data/db/migrate/20180208122654_create_stripe_bank_accounts.rb +20 -0
  71. data/db/migrate/20180208160046_add_nickname_to_stripe_plans.rb +5 -0
  72. data/db/migrate/20180211092437_rename_stripe_invoice_items_subscription_item.rb +6 -0
  73. data/db/migrate/20180216224335_rename_stripe_subscriptions_discount.rb +5 -0
  74. data/db/migrate/20180217200350_change_stripe_invoices_to_reference_discounts.rb +9 -0
  75. data/db/migrate/20180217204628_create_stripe_cards.rb +29 -0
  76. data/db/migrate/20180218092600_add_deleted_at_to_stripe_cards.rb +9 -0
  77. data/db/migrate/20181219121712_add_new_primary_id_and_rename_old.rb +37 -0
  78. data/db/migrate/20190629112050_rename_stripe_invoice_date_to_created.rb +5 -0
  79. data/lib/stripe_model_callbacks.rb +19 -0
  80. data/lib/stripe_model_callbacks/autoload_models.rb +8 -0
  81. data/lib/stripe_model_callbacks/configuration.rb +23 -0
  82. data/lib/stripe_model_callbacks/engine.rb +5 -0
  83. data/lib/stripe_model_callbacks/event_mocker.rb +5 -0
  84. data/lib/stripe_model_callbacks/factory_bot_definitions.rb +7 -0
  85. data/lib/stripe_model_callbacks/models/stripe_bank_account.rb +17 -0
  86. data/lib/stripe_model_callbacks/models/stripe_card.rb +25 -0
  87. data/lib/stripe_model_callbacks/models/stripe_charge.rb +47 -0
  88. data/lib/stripe_model_callbacks/models/stripe_coupon.rb +24 -0
  89. data/lib/stripe_model_callbacks/models/stripe_customer.rb +23 -0
  90. data/lib/stripe_model_callbacks/models/stripe_discount.rb +53 -0
  91. data/lib/stripe_model_callbacks/models/stripe_dispute.rb +86 -0
  92. data/lib/stripe_model_callbacks/models/stripe_invoice.rb +88 -0
  93. data/lib/stripe_model_callbacks/models/stripe_invoice_item.rb +33 -0
  94. data/lib/stripe_model_callbacks/models/stripe_order.rb +63 -0
  95. data/lib/stripe_model_callbacks/models/stripe_order_item.rb +20 -0
  96. data/lib/stripe_model_callbacks/models/stripe_payout.rb +26 -0
  97. data/lib/stripe_model_callbacks/models/stripe_plan.rb +32 -0
  98. data/lib/stripe_model_callbacks/models/stripe_product.rb +27 -0
  99. data/lib/stripe_model_callbacks/models/stripe_recipient.rb +16 -0
  100. data/lib/stripe_model_callbacks/models/stripe_refund.rb +28 -0
  101. data/lib/stripe_model_callbacks/models/stripe_review.rb +18 -0
  102. data/lib/stripe_model_callbacks/models/stripe_sku.rb +28 -0
  103. data/lib/stripe_model_callbacks/models/stripe_source.rb +79 -0
  104. data/lib/stripe_model_callbacks/models/stripe_subscription.rb +101 -0
  105. data/lib/stripe_model_callbacks/models/stripe_subscription_item.rb +36 -0
  106. data/lib/stripe_model_callbacks/models/stripe_transfer.rb +25 -0
  107. data/lib/stripe_model_callbacks/require_models.rb +7 -0
  108. data/lib/stripe_model_callbacks/version.rb +3 -0
  109. data/lib/tasks/stripe_model_callbacks_tasks.rake +4 -0
  110. metadata +249 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b9d3e6bf9290c17976c275cdabe05a67ba264a3ddb8f1f7af6b75dc4daea6b29
4
+ data.tar.gz: 0bd3eea3af9cf54996441dfd0e33969ce114107a42f01aee61b93185a16bee33
5
+ SHA512:
6
+ metadata.gz: e280ae9def15e1de122489e8a348949dc9e86af188650fef9d18bcf217be5bf52ef04df9c5e73b563e27a6208dc681f313ced34c7e0da4c5b680bfaa125c9c74
7
+ data.tar.gz: 1d99e5f48fd7c022d06406bb5b1b52b55ce21822b8d1190f702f0465f1f8317ec0aa202df46e2af59d3cd4b967f12ad32d9e4acec3c8595a040e951b6216ec48
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 kaspernj
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # StripeModelCallbacks
2
+ Stripe database models using ActiveRecord, Stripe event webhooks synchronization and PublicActivity.
3
+
4
+ This is supposed to make it easier implementing a full blown Stripe implementation into your application,
5
+ so that you can code your app using ActiveRecord and all your favorite gems without having to mess around
6
+ with webhooks, custom and complicated tests and more.
7
+
8
+ The purpose is that the complicated webhook stuff is done by this gem, and you can trust our tests to work,
9
+ so you dont have to write those thousand of lines yourself.
10
+
11
+ ## Installation
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'stripe_model_callbacks'
16
+ ```
17
+
18
+ And then execute:
19
+ ```bash
20
+ bundle
21
+ ```
22
+
23
+ You also need to install and setup the gems `public_activity`, `stripe` and `stripe_event`. Do this:
24
+
25
+ Install the migration for Public Activity, which will provide logging:
26
+ ```bash
27
+ rails g public_activity:migration
28
+ ```
29
+
30
+ Do something like this in `config/routes.rb`:
31
+ ```ruby
32
+ Rails.application.routes.draw do
33
+ mount StripeEvent::Engine => "/stripe-events"
34
+ end
35
+ ```
36
+
37
+ Do something like this in `config/initializers/stripe.rb`:
38
+ ```ruby
39
+ Stripe.api_key = ENV.fetch("STRIPE_PUBLIC_KEY")
40
+ StripeEvent.signing_secret = ENV.fetch("STRIPE_SIGNING_KEY")
41
+
42
+ Rails.configuration.stripe = {
43
+ publishable_key: ENV.fetch("STRIPE_PUBLIC_KEY"),
44
+ secret_key: ENV.fetch("STRIPE_SECRET_KEY")
45
+ }
46
+
47
+ StripeEvent.configure do |events|
48
+ StripeModelCallbacks::ConfigureService.execute!(events: events)
49
+ end
50
+ ```
51
+
52
+ Add the migrations for the Stripe models to your project like this:
53
+ ```bash
54
+ rake stripe_model_callbacks:install:migrations
55
+ ```
56
+
57
+ You should set up your Stripe account to post events to your website using a URL that looks something like this:
58
+ `https://www.yourdomain.com/stripe-events`.
59
+
60
+ Your application should now receive event webhooks from Stripe and then create, update, mark as deleted,
61
+ log and more automatically using the ActiveRecord models.
62
+
63
+ You can use a service like Ultrahook to set up a proxy for your local development machine to test against.
64
+
65
+ ## Usage
66
+
67
+ ### Queries with ActiveRecord
68
+
69
+ To see a list of transfers, you can do something like this, as you would with any given model:
70
+
71
+ ```ruby
72
+ StripeTransfer.where("stripe_transfers.created > ?", Time.zone.now.beginning_of_month)
73
+ ```
74
+
75
+ You can inspect the tables and see which tables and columns that are available. Most of it alligns with
76
+ the attributes mentioned in Stripe's own API.
77
+
78
+ ### Updating on Stripe
79
+
80
+ You can update the data on Stripe like this:
81
+
82
+ ```ruby
83
+ stripe_subscription = StripeSubscription.find(id)
84
+ stripe_subscription.update_on_stripe!(tax_percent: 10)
85
+ ```
86
+
87
+ Create a record from a Stripe object
88
+
89
+ ```ruby
90
+ object = Stripe::Subscription.retrieve(id)
91
+ stripe_subscription = StripeSubscription.create_from_stripe!(object)
92
+ ```
93
+
94
+ Sync data from Stripe:
95
+ ```ruby
96
+ stripe_subscription = StripeSubscription.find(id)
97
+ stripe_subscription.reload_from_stripe!
98
+ ```
99
+
100
+ Delete on Stripe:
101
+ ```ruby
102
+ stripe_subscription = StripeSubscription.find(id)
103
+ stripe_subscription.destroy_on_stripe!
104
+ ```
105
+
106
+ Convert model to a Stripe object:
107
+ ```ruby
108
+ stripe_subscription = StripeSubscription.find(id)
109
+ stripe_subscription.to_stripe.delete(at_period_end: true)
110
+
111
+ # We should probably reload so the model reflect that change instantly (else it should receive it through a sync event in a short while)
112
+ stripe_subscription.reload_from_stripe!
113
+ ```
114
+
115
+ ## Testing
116
+
117
+ ### Factories
118
+
119
+ If you need some FactoryBot factories, then you can do like this in `spec/rails_helper.rb`:
120
+ ```ruby
121
+ require "stripe_model_callbacks/factory_bot_definitions"
122
+ ```
123
+
124
+ You can take a look at the factories here:
125
+ https://github.com/kaspernj/stripe_model_callbacks/tree/master/spec/factories
126
+
127
+ ### Mock events
128
+
129
+ You can mock Stripe events by using the helper method `mock_stripe_event` by including this helper:
130
+
131
+ ```
132
+ RSpec.configure do |config|
133
+ config.include StripeModelCallbacks::EventMocker
134
+ end
135
+ ```
136
+
137
+ You can find all the events you can mock here: https://github.com/kaspernj/stripe_model_callbacks/tree/master/spec/fixtures/stripe_events
138
+
139
+ You can mock the events by their file name like this:
140
+ ```ruby
141
+ mock_stripe_event("charge.refunded")
142
+ ```
143
+
144
+ You can change the data of the event like this:
145
+ ```ruby
146
+ mock_stripe_event("invoice.created", data: {object: {discount: {"customer": "cus_CLI9d5IHGcdWBY"}}})
147
+ ```
148
+
149
+ ## Contributing
150
+ Contribution directions go here.
151
+
152
+ ## License
153
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require "bundler/setup"
3
+ rescue LoadError
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
+ end
6
+
7
+ require "rdoc/task"
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = "rdoc"
11
+ rdoc.title = "StripeModelCallbacks"
12
+ rdoc.options << "--line-numbers"
13
+ rdoc.rdoc_files.include("README.md")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
18
+ load "rails/tasks/engine.rake"
19
+ load "rails/tasks/statistics.rake"
20
+
21
+ require "bundler/gem_tasks"
22
+
23
+ if Rails.env.development? || Rails.env.test?
24
+ require "best_practice_project"
25
+ BestPracticeProject.load_tasks
26
+ end
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/stripe_model_callbacks .js
2
+ //= link_directory ../stylesheets/stripe_model_callbacks .css
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,3 @@
1
+ class StripeModelCallbacks::ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ end
@@ -0,0 +1,2 @@
1
+ class StripeModelCallbacks::ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class StripeModelCallbacks::ApplicationMailer < ActionMailer::Base
2
+ default from: "from@example.com"
3
+ layout "mailer"
4
+ end
@@ -0,0 +1,60 @@
1
+ class StripeModelCallbacks::ApplicationRecord < ActiveRecord::Base
2
+ include PublicActivity::Model
3
+ tracked
4
+
5
+ self.abstract_class = true
6
+
7
+ attr_writer :stripe_object
8
+
9
+ def self.create_from_stripe!(object)
10
+ model = new
11
+ model.stripe_object = object
12
+ model.assign_from_stripe(object)
13
+ model.save!
14
+ model
15
+ end
16
+
17
+ def self.create_on_stripe!(attributes)
18
+ object = stripe_class.create(attributes)
19
+ create_from_stripe!(object)
20
+ end
21
+
22
+ def to_stripe
23
+ @to_stripe ||= self.class.stripe_class.retrieve(stripe_id)
24
+ end
25
+
26
+ def reload_from_stripe!
27
+ assign_from_stripe(to_stripe)
28
+ save!
29
+ end
30
+
31
+ def reload!(*args, &blk)
32
+ @to_stripe = nil
33
+ super
34
+ end
35
+
36
+ def update_on_stripe(attributes)
37
+ attributes.each do |key, value|
38
+ to_stripe.__send__("#{key}=", value)
39
+ end
40
+
41
+ to_stripe.save
42
+ reload_from_stripe!
43
+ true
44
+ end
45
+
46
+ def update_on_stripe!(attributes)
47
+ raise ActiveRecord::RecordInvalid, self unless update_on_stripe(attributes)
48
+ end
49
+
50
+ def destroy_on_stripe
51
+ to_stripe.delete
52
+ update!(deleted_at: Time.zone.now) if respond_to?(:deleted_at)
53
+ reload_from_stripe!
54
+ true
55
+ end
56
+
57
+ def destroy_on_stripe!
58
+ raise ActiveRecord::RecordInvalid, self unless destroy_on_stripe
59
+ end
60
+ end
@@ -0,0 +1,13 @@
1
+ class StripeModelCallbacks::Account::ExternalAccount::UpdatedService < StripeModelCallbacks::BaseEventService
2
+ def execute
3
+ bank_account = StripeBankAccount.find_or_initialize_by(stripe_id: object.id)
4
+ bank_account.assign_from_stripe(object)
5
+
6
+ if bank_account.save
7
+ bank_account.create_activity :deleted if event.type == "account.external_account.deleted"
8
+ succeed!
9
+ else
10
+ fail! bank_account.errors.full_messages
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,35 @@
1
+ class StripeModelCallbacks::AttributesAssignerService < ServicePattern::Service
2
+ attr_reader :attributes, :model, :stripe_model
3
+
4
+ def initialize(attributes:, model:, stripe_model:)
5
+ @attributes = attributes
6
+ @model = model
7
+ @stripe_model = stripe_model
8
+ end
9
+
10
+ def execute
11
+ attributes.each do |attribute|
12
+ next unless stripe_model.respond_to?(attribute)
13
+
14
+ value = stripe_model.__send__(attribute)
15
+
16
+ if attribute == "metadata"
17
+ value = JSON.generate(value)
18
+ elsif attribute == "created" && value
19
+ value = Time.zone.at(value)
20
+ end
21
+
22
+ model.__send__(setter_method(attribute), value)
23
+ end
24
+
25
+ succeed!
26
+ end
27
+
28
+ def setter_method(attribute)
29
+ if attribute == "id"
30
+ "stripe_id="
31
+ else
32
+ "#{attribute}="
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ class StripeModelCallbacks::BaseEventService < StripeModelCallbacks::BaseService
2
+ attr_reader :event, :object
3
+
4
+ def initialize(event: nil, object: nil)
5
+ @event = event
6
+ @object = object
7
+ @object ||= event.data.object
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ class StripeModelCallbacks::BaseService < ServicePattern::Service
2
+ def self.reported_execute!(*args, &blk)
3
+ with_exception_notifications do
4
+ response = execute(*args, &blk)
5
+ raise response.errors.join(". ") unless response.success?
6
+
7
+ return response
8
+ end
9
+ end
10
+
11
+ def self.with_exception_notifications
12
+ yield
13
+ rescue => e # rubocop:disable Style/RescueStandardError
14
+ Rails.logger.error "ERROR: #{e.message}"
15
+
16
+ cleaned = Rails.backtrace_cleaner.clean(e.backtrace)
17
+ if cleaned.any?
18
+ Rails.logger.error cleaned
19
+ else
20
+ Rails.logger.error e.backtrace.join("\n")
21
+ end
22
+
23
+ ExceptionNotifier.notify_exception(e) if Object.const_defined?("ExceptionNotifier")
24
+ raise e
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ class StripeModelCallbacks::Charge::DisputeUpdatedService < StripeModelCallbacks::BaseEventService
2
+ def execute
3
+ dispute.assign_from_stripe(object)
4
+
5
+ if dispute.save
6
+ create_activity
7
+ succeed!
8
+ else
9
+ fail! dispute.errors.full_messages
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def dispute
16
+ @dispute ||= StripeDispute.find_or_initialize_by(stripe_id: object.id)
17
+ end
18
+
19
+ def create_activity
20
+ case event.type
21
+ when "charge.dispute.closed"
22
+ dispute.create_activity :closed
23
+ when "charge.dispute.funds_reinstated"
24
+ dispute.create_activity :funds_reinstated
25
+ when "charge.dispute.funds_withdrawn"
26
+ dispute.create_activity :funds_withdrawn
27
+ end
28
+ end
29
+ end