stripe_model_callbacks 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -0,0 +1,76 @@
1
+ class CreateDisputes < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :stripe_disputes do |table|
4
+ table.string :stripe_id, index: true, null: false
5
+ table.datetime :created
6
+ amount_columns(table)
7
+ table.string :balance_transaction_id, index: true
8
+ table.string :stripe_charge_id, index: true
9
+ table.string :currency
10
+ table.text :evidence_access_activity_log
11
+ table.text :evidence_billing_address
12
+ evidence_cancellation_duplicate_columns(table)
13
+ evidence_customer_columns(table)
14
+ table.text :evidence_product_description
15
+ table.text :evidence_receipt
16
+ evidence_refund_service_columns(table)
17
+ evidence_shipping_columns(table)
18
+ table.text :evidence_uncategorized_file
19
+ table.text :evidence_uncategorized_text
20
+ evidence_details_columns(table)
21
+ table.boolean :is_charge_refundable
22
+ table.boolean :livemode
23
+ table.text :metadata
24
+ table.string :reason
25
+ table.string :status
26
+ table.timestamps
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def amount_columns(table)
33
+ table.integer :amount_cents
34
+ table.string :amount_currency
35
+ end
36
+
37
+ def evidence_cancellation_duplicate_columns(table)
38
+ table.text :evidence_cancellation_policy
39
+ table.text :evidence_cancellation_policy_disclosure
40
+ table.text :evidence_cancellation_rebuttal
41
+ table.text :evidence_duplicate_charge_documentation
42
+ table.text :evidence_duplicate_charge_explanation
43
+ table.string :evidence_duplicate_charge_id
44
+ end
45
+
46
+ def evidence_customer_columns(table)
47
+ table.text :evidence_customer_communication
48
+ table.string :evidence_customer_email_address
49
+ table.string :evidence_customer_name
50
+ table.string :evidence_customer_purchase_ip
51
+ table.text :evidence_customer_signature
52
+ end
53
+
54
+ def evidence_refund_service_columns(table)
55
+ table.text :evidence_refund_policy
56
+ table.text :evidence_refund_policy_disclosure
57
+ table.text :evidence_refund_refusal_explanation
58
+ table.text :evidence_service_date
59
+ table.text :evidence_service_documentation
60
+ end
61
+
62
+ def evidence_shipping_columns(table)
63
+ table.text :evidence_shipping_address
64
+ table.text :evidence_shipping_carrier
65
+ table.text :evidence_shipping_date
66
+ table.text :evidence_shipping_documentation
67
+ table.text :evidence_shipping_tracking_number
68
+ end
69
+
70
+ def evidence_details_columns(table)
71
+ table.datetime :evidence_details_due_by
72
+ table.boolean :evidence_details_has_evidence
73
+ table.boolean :evidence_details_past_due
74
+ table.integer :evidence_details_submission_count
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ class ChangeActivitiesTrackableIdToString < ActiveRecord::Migration[5.1]
2
+ def change
3
+ change_column :activities, :trackable_id, :string
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ class CreateStripeSubscriptionItems < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :stripe_subscription_items do |t|
4
+ t.string :stripe_id, index: true, null: false
5
+ t.datetime :created
6
+ t.string :stripe_subscription_id, index: true
7
+ t.string :stripe_plan_id, index: true
8
+ t.boolean :deleted, default: false, null: false
9
+ t.text :metadata
10
+ t.decimal :quantity
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ class RemoveNameFromStripePlans < ActiveRecord::Migration[5.1]
2
+ def change
3
+ remove_column :stripe_plans, :name, :string
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class AddStripeProductIdToStripePlans < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :stripe_plans, :stripe_product_id, :string
4
+ add_index :stripe_plans, :stripe_product_id
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class AddIndexOnRefundCharge < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_index :stripe_refunds, :stripe_charge_id
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class ChangeStripeSubscriptionItemsQuantityToInteger < ActiveRecord::Migration[5.1]
2
+ def change
3
+ change_column :stripe_subscription_items, :quantity, :integer
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ class AddAttemptCountToStripeInvoices < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :stripe_invoices, :attempt_count, :integer
4
+ add_column :stripe_invoices, :ending_balance, :integer
5
+ add_column :stripe_invoices, :starting_balance, :integer
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ class CreateStripeReviews < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :stripe_reviews do |t|
4
+ t.string :stripe_id, index: true, null: false
5
+ t.string :stripe_charge_id, index: true
6
+ t.datetime :created
7
+ t.boolean :livemode, default: false, null: false
8
+ t.boolean :open, default: true, null: false
9
+ t.string :reason, index: true
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ class CreateStripeBankAccounts < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :stripe_bank_accounts do |t|
4
+ t.string :stripe_id, index: true, null: false
5
+ t.string :stripe_account_id, index: true
6
+ t.string :account_holder_name
7
+ t.string :account_holder_type
8
+ t.string :bank_name
9
+ t.string :country
10
+ t.string :currency
11
+ t.boolean :default_for_currency
12
+ t.string :fingerprint
13
+ t.string :last4
14
+ t.text :metadata
15
+ t.string :routing_number
16
+ t.string :status, index: true
17
+ t.timestamps
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ class AddNicknameToStripePlans < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :stripe_plans, :nickname, :string
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class RenameStripeInvoiceItemsSubscriptionItem < ActiveRecord::Migration[5.1]
2
+ def change
3
+ rename_column :stripe_invoice_items, :subscription_item, :stripe_subscription_item_id
4
+ add_index :stripe_invoice_items, :stripe_subscription_item_id
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class RenameStripeSubscriptionsDiscount < ActiveRecord::Migration[5.1]
2
+ def change
3
+ rename_column :stripe_subscriptions, :discount, :stripe_discount_id
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class ChangeStripeInvoicesToReferenceDiscounts < ActiveRecord::Migration[5.1]
2
+ def change
3
+ remove_column :stripe_invoices, :discount_cents, :integer
4
+ remove_column :stripe_invoices, :discount_currency, :string
5
+
6
+ add_column :stripe_invoices, :stripe_discount_id, :string
7
+ add_index :stripe_invoices, :stripe_discount_id
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ class CreateStripeCards < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :stripe_cards do |t|
4
+ t.string :stripe_id, index: true, null: false
5
+ t.string :address_city
6
+ t.string :address_country
7
+ t.string :address_line1
8
+ t.string :address_line1_check
9
+ t.string :address_line2
10
+ t.string :address_state
11
+ t.string :address_zip
12
+ t.string :address_zip_check
13
+ t.string :brand
14
+ t.string :country
15
+ t.string :stripe_customer_id, index: true
16
+ t.string :cvc_check
17
+ t.string :dynamic_last4
18
+ t.integer :exp_month
19
+ t.integer :exp_year
20
+ t.string :fingerprint
21
+ t.string :funding
22
+ t.string :last4
23
+ t.text :metadata
24
+ t.string :name
25
+ t.string :tokenization_method
26
+ t.timestamps
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ class AddDeletedAtToStripeCards < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :stripe_cards, :deleted_at, :datetime
4
+ add_index :stripe_cards, :deleted_at
5
+
6
+ add_column :stripe_sources, :deleted_at, :datetime
7
+ add_index :stripe_sources, :deleted_at
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ class AddNewPrimaryIdAndRenameOld < ActiveRecord::Migration[5.2]
2
+ TABLES = %w[
3
+ bank_accounts cards charges coupons customers disputes invoice_items invoices
4
+ orders payouts plans products recipients refunds reviews skus sources
5
+ subscription_items subscriptions transfers
6
+ ].freeze
7
+
8
+ def up
9
+ TABLES.each do |table|
10
+ table_name = "stripe_#{table}"
11
+
12
+ next if column_exists?(table_name, :stripe_id)
13
+
14
+ add_column table_name, :stripe_id, :string
15
+ add_index table_name, :stripe_id
16
+ execute "UPDATE #{table_name} SET stripe_id = id"
17
+ change_column_null table_name, :stripe_id, false
18
+ remove_column table_name, :id
19
+
20
+ add_column table_name, :id, :primary_key
21
+ end
22
+ end
23
+
24
+ def down
25
+ TABLES.each do |table|
26
+ table_name = "stripe_#{table}"
27
+
28
+ next unless column_exists?(table_name, :stripe_id)
29
+
30
+ remove_column table_name, :id
31
+ add_column table_name, :id, :string, primary_key: true
32
+ execute "UPDATE #{table_name} SET id = stripe_id"
33
+ change_column_null table_name, :id, false
34
+ remove_column table_name, :stripe_id
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ class RenameStripeInvoiceDateToCreated < ActiveRecord::Migration[5.2]
2
+ def change
3
+ rename_column :stripe_invoices, :date, :created
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ require "auto_autoloader"
2
+ require "money-rails"
3
+ require "public_activity"
4
+ require "service_pattern"
5
+ require "stripe"
6
+ require "stripe_event"
7
+ require "stripe_model_callbacks/engine"
8
+ require "stripe_model_callbacks/autoload_models"
9
+
10
+ module StripeModelCallbacks
11
+ path = "#{File.dirname(__FILE__)}/stripe_model_callbacks"
12
+
13
+ autoload :Configuration, "#{path}/configuration"
14
+ autoload :EventMocker, "#{path}/event_mocker"
15
+
16
+ def self.configure
17
+ yield StripeModelCallbacks::Configuration.current
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ dir = "#{File.dirname(__FILE__)}/models"
2
+ Dir.foreach(dir) do |file|
3
+ match = file.match(/\A(.+)\.rb\Z/)
4
+ next unless match
5
+
6
+ constant_name = match[1].camelize
7
+ Object.autoload(constant_name, "#{dir}/#{file}")
8
+ end
@@ -0,0 +1,23 @@
1
+ class StripeModelCallbacks::Configuration
2
+ def self.current
3
+ @current ||= StripeModelCallbacks::Configuration.new
4
+ end
5
+
6
+ def initialize
7
+ @on_error_callbacks = []
8
+ end
9
+
10
+ def on_error(&blk)
11
+ @on_error_callbacks << blk
12
+ end
13
+
14
+ def with_error_handling(args: nil)
15
+ yield
16
+ rescue => e # rubocop:disable Style/RescueStandardError
17
+ @on_error_callbacks.each do |callback|
18
+ callback.call(args: args, error: e)
19
+ end
20
+
21
+ raise e
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module StripeModelCallbacks; end
2
+
3
+ class StripeModelCallbacks::Engine < ::Rails::Engine
4
+ isolate_namespace StripeModelCallbacks
5
+ end
@@ -0,0 +1,5 @@
1
+ module StripeModelCallbacks::EventMocker
2
+ def mock_stripe_event(name, args = nil)
3
+ StripeModelCallbacks::EventMockerService.new(name: name, scope: self, args: args)
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ factories_path = File.realpath("#{File.dirname(__FILE__)}/../../spec/factories")
2
+
3
+ Dir.foreach(factories_path) do |file|
4
+ next unless /\A(.+)\.rb\Z/.match?(file)
5
+
6
+ require "#{factories_path}/#{file}"
7
+ end
@@ -0,0 +1,17 @@
1
+ class StripeBankAccount < StripeModelCallbacks::ApplicationRecord
2
+ def self.stripe_class
3
+ Stripe::BankAccount
4
+ end
5
+
6
+ def assign_from_stripe(object)
7
+ assign_attributes(stripe_account_id: object.account)
8
+
9
+ StripeModelCallbacks::AttributesAssignerService.execute!(
10
+ model: self, stripe_model: object,
11
+ attributes: %w[
12
+ id account_holder_name account_holder_type bank_name country currency default_for_currency
13
+ fingerprint last4 metadata routing_number status
14
+ ]
15
+ )
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ class StripeCard < StripeModelCallbacks::ApplicationRecord
2
+ belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
3
+
4
+ def self.stripe_class
5
+ Stripe::Card
6
+ end
7
+
8
+ def assign_from_stripe(object)
9
+ self.stripe_customer_id = object.customer if object.respond_to?(:customer)
10
+
11
+ StripeModelCallbacks::AttributesAssignerService.execute!(
12
+ model: self, stripe_model: object,
13
+ attributes: %w[
14
+ id address_city address_country address_line1 address_line1_check
15
+ address_line2 address_state address_zip address_zip_check brand
16
+ country cvc_check dynamic_last4 exp_month exp_year fingerprint
17
+ funding last4 name tokenization_method
18
+ ]
19
+ )
20
+ end
21
+
22
+ def to_stripe
23
+ @to_stripe ||= Stripe::Customer.retrieve(stripe_customer_id).sources.retrieve(stripe_id)
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ class StripeCharge < StripeModelCallbacks::ApplicationRecord
2
+ belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
3
+ has_many :stripe_orders, primary_key: "stripe_id"
4
+ has_many :stripe_refunds, primary_key: "stripe_id"
5
+ has_many :stripe_reviews, primary_key: "stripe_id"
6
+
7
+ monetize :amount_cents
8
+ monetize :amount_refunded_cents, allow_nil: true
9
+ monetize :application_cents, allow_nil: true
10
+
11
+ def self.stripe_class
12
+ Stripe::Charge
13
+ end
14
+
15
+ def assign_from_stripe(object)
16
+ assign_attributes(
17
+ created: Time.zone.at(object.created),
18
+ stripe_customer_id: object.customer,
19
+ livemode: object.livemode,
20
+ stripe_invoice_id: object.invoice,
21
+ metadata: JSON.generate(object.metadata),
22
+ stripe_order_id: object.order,
23
+ stripe_source_id: object.source
24
+ )
25
+
26
+ assign_amounts_from_stripe(object)
27
+
28
+ StripeModelCallbacks::AttributesAssignerService.execute!(
29
+ model: self,
30
+ stripe_model: object,
31
+ attributes: %w[
32
+ captured currency description dispute outcome refunded fraud_details failure_message failure_code on_behalf_of paid
33
+ receipt_email receipt_number review shipping source_transfer statement_descriptor status transfer_group
34
+ ]
35
+ )
36
+ end
37
+
38
+ private
39
+
40
+ def assign_amounts_from_stripe(object)
41
+ assign_attributes(
42
+ amount: Money.new(object.amount, object.currency),
43
+ amount_refunded: object.amount_refunded ? Money.new(object.amount_refunded, object.currency) : nil,
44
+ application: object.application ? Money.new(object.application, object.currency) : nil
45
+ )
46
+ end
47
+ end