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
@@ -0,0 +1,77 @@
1
+ {
2
+ "id": "evt_1GhwDXJ3a8kmO8fm05cNl8dF",
3
+ "object": "event",
4
+ "api_version": "2019-05-16",
5
+ "created": 1589282651,
6
+ "data": {
7
+ "object": {
8
+ "id": "sub_sched_1GhwDXJ3a8kmO8fm97mybWCy",
9
+ "object": "subscription_schedule",
10
+ "billing": "charge_automatically",
11
+ "billing_thresholds": null,
12
+ "canceled_at": null,
13
+ "collection_method": "charge_automatically",
14
+ "completed_at": null,
15
+ "created": 1589282651,
16
+ "current_phase": null,
17
+ "customer": "cus_HG9JJV3LEHUjhO",
18
+ "default_payment_method": null,
19
+ "default_settings": {
20
+ "billing_thresholds": null,
21
+ "collection_method": "charge_automatically",
22
+ "default_payment_method": null,
23
+ "default_source": null,
24
+ "invoice_settings": null
25
+ },
26
+ "default_source": null,
27
+ "end_behavior": "cancel",
28
+ "invoice_settings": null,
29
+ "livemode": false,
30
+ "metadata": {
31
+ },
32
+ "phases": [
33
+ {
34
+ "add_invoice_items": [
35
+ ],
36
+ "application_fee_percent": null,
37
+ "billing_thresholds": null,
38
+ "collection_method": "charge_automatically",
39
+ "coupon": null,
40
+ "default_payment_method": null,
41
+ "default_tax_rates": [
42
+ ],
43
+ "end_date": 1606780800,
44
+ "invoice_settings": null,
45
+ "plans": [
46
+ {
47
+ "billing_thresholds": null,
48
+ "plan": "plan_HG4dIu1k8KqRWi",
49
+ "price": "plan_HG4dIu1k8KqRWi",
50
+ "quantity": 2,
51
+ "tax_rates": [
52
+ ]
53
+ }
54
+ ],
55
+ "prorate": true,
56
+ "proration_behavior": "create_prorations",
57
+ "start_date": 1590969600,
58
+ "tax_percent": null,
59
+ "trial_end": null
60
+ }
61
+ ],
62
+ "released_at": null,
63
+ "released_subscription": null,
64
+ "renewal_behavior": "cancel",
65
+ "renewal_interval": null,
66
+ "status": "not_started",
67
+ "subscription": null
68
+ }
69
+ },
70
+ "livemode": false,
71
+ "pending_webhooks": 1,
72
+ "request": {
73
+ "id": "req_5afiaPgOrP3nYc",
74
+ "idempotency_key": null
75
+ },
76
+ "type": "subscription_schedule.created"
77
+ }
@@ -0,0 +1,87 @@
1
+ {
2
+ "id": "evt_1Ghw7aJ3a8kmO8fm2vPIdi50",
3
+ "object": "event",
4
+ "api_version": "2019-05-16",
5
+ "created": 1589282282,
6
+ "data": {
7
+ "object": {
8
+ "id": "sub_sched_1GhdpNJ3a8kmO8fmNG2vKBYY",
9
+ "object": "subscription_schedule",
10
+ "billing": "charge_automatically",
11
+ "billing_thresholds": null,
12
+ "canceled_at": null,
13
+ "collection_method": "charge_automatically",
14
+ "completed_at": null,
15
+ "created": 1589211961,
16
+ "current_phase": null,
17
+ "customer": "cus_HG9VgDZsSjwhZG",
18
+ "default_payment_method": null,
19
+ "default_settings": {
20
+ "billing_thresholds": null,
21
+ "collection_method": "charge_automatically",
22
+ "default_payment_method": null,
23
+ "default_source": null,
24
+ "invoice_settings": {
25
+ "days_until_due": null
26
+ }
27
+ },
28
+ "default_source": null,
29
+ "end_behavior": "release",
30
+ "invoice_settings": {
31
+ "days_until_due": null
32
+ },
33
+ "livemode": false,
34
+ "metadata": {
35
+ },
36
+ "phases": [
37
+ {
38
+ "add_invoice_items": [
39
+ ],
40
+ "application_fee_percent": null,
41
+ "billing_thresholds": null,
42
+ "collection_method": "charge_automatically",
43
+ "coupon": null,
44
+ "default_payment_method": null,
45
+ "default_tax_rates": [
46
+ ],
47
+ "end_date": 1623456000,
48
+ "invoice_settings": null,
49
+ "plans": [
50
+ {
51
+ "billing_thresholds": null,
52
+ "plan": "plan_HG4dIu1k8KqRWi",
53
+ "price": "plan_HG4dIu1k8KqRWi",
54
+ "quantity": 1,
55
+ "tax_rates": [
56
+ ]
57
+ }
58
+ ],
59
+ "prorate": true,
60
+ "proration_behavior": "create_prorations",
61
+ "start_date": 1620777600,
62
+ "tax_percent": null,
63
+ "trial_end": null
64
+ }
65
+ ],
66
+ "released_at": null,
67
+ "released_subscription": null,
68
+ "renewal_behavior": "release",
69
+ "renewal_interval": null,
70
+ "status": "not_started",
71
+ "subscription": null
72
+ },
73
+ "previous_attributes": {
74
+ "default_settings": {
75
+ "invoice_settings": null
76
+ },
77
+ "invoice_settings": null
78
+ }
79
+ },
80
+ "livemode": false,
81
+ "pending_webhooks": 2,
82
+ "request": {
83
+ "id": "req_sx6SHFvO3ZyCYh",
84
+ "idempotency_key": null
85
+ },
86
+ "type": "subscription_schedule.updated"
87
+ }
@@ -0,0 +1,26 @@
1
+ {
2
+ "created": 1326853478,
3
+ "livemode": false,
4
+ "id": "evt_00000000000000",
5
+ "type": "tax_rate.created",
6
+ "object": "event",
7
+ "request": null,
8
+ "pending_webhooks": 1,
9
+ "api_version": "2018-02-06",
10
+ "data": {
11
+ "object": {
12
+ "id": "txr_00000000000000",
13
+ "object": "tax_rate",
14
+ "active": true,
15
+ "created": 1608755171,
16
+ "description": "VAT Germany",
17
+ "display_name": "VAT",
18
+ "inclusive": false,
19
+ "jurisdiction": "DE",
20
+ "livemode": false,
21
+ "metadata": {
22
+ },
23
+ "percentage": 16
24
+ }
25
+ }
26
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "created": 1326853478,
3
+ "livemode": false,
4
+ "id": "evt_00000000000000",
5
+ "type": "tax_rate.updated",
6
+ "object": "event",
7
+ "request": null,
8
+ "pending_webhooks": 1,
9
+ "api_version": "2018-02-06",
10
+ "data": {
11
+ "object": {
12
+ "id": "txr_00000000000000",
13
+ "object": "tax_rate",
14
+ "active": true,
15
+ "created": 1608755201,
16
+ "description": "VAT Germany",
17
+ "display_name": "VAT",
18
+ "inclusive": false,
19
+ "jurisdiction": "DE",
20
+ "livemode": false,
21
+ "metadata": {
22
+ },
23
+ "percentage": 16
24
+ },
25
+ "previous_attributes": {
26
+ "display_name": "Old name"
27
+ }
28
+ }
29
+ }
@@ -4,6 +4,7 @@ class StripeBankAccount < StripeModelCallbacks::ApplicationRecord
4
4
  end
5
5
 
6
6
  def assign_from_stripe(object)
7
+ check_object_is_stripe_class(object)
7
8
  assign_attributes(stripe_account_id: object.account)
8
9
 
9
10
  StripeModelCallbacks::AttributesAssignerService.execute!(
@@ -6,6 +6,7 @@ class StripeCard < StripeModelCallbacks::ApplicationRecord
6
6
  end
7
7
 
8
8
  def assign_from_stripe(object)
9
+ check_object_is_stripe_class(object)
9
10
  self.stripe_customer_id = object.customer if object.respond_to?(:customer)
10
11
 
11
12
  StripeModelCallbacks::AttributesAssignerService.execute!(
@@ -20,6 +21,6 @@ class StripeCard < StripeModelCallbacks::ApplicationRecord
20
21
  end
21
22
 
22
23
  def to_stripe
23
- @to_stripe ||= Stripe::Customer.retrieve(stripe_customer_id).sources.retrieve(stripe_id)
24
+ @to_stripe ||= Stripe::Customer.retrieve_source(stripe_customer_id, stripe_id)
24
25
  end
25
26
  end
@@ -1,5 +1,7 @@
1
1
  class StripeCharge < StripeModelCallbacks::ApplicationRecord
2
2
  belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
3
+ belongs_to :stripe_invoice, optional: true, primary_key: "stripe_id"
4
+ belongs_to :stripe_source, optional: true, primary_key: "stripe_id"
3
5
  has_many :stripe_orders, primary_key: "stripe_id"
4
6
  has_many :stripe_refunds, primary_key: "stripe_id"
5
7
  has_many :stripe_reviews, primary_key: "stripe_id"
@@ -13,6 +15,7 @@ class StripeCharge < StripeModelCallbacks::ApplicationRecord
13
15
  end
14
16
 
15
17
  def assign_from_stripe(object)
18
+ check_object_is_stripe_class(object)
16
19
  assign_attributes(
17
20
  created: Time.zone.at(object.created),
18
21
  stripe_customer_id: object.customer,
@@ -8,6 +8,7 @@ class StripeCoupon < StripeModelCallbacks::ApplicationRecord
8
8
  end
9
9
 
10
10
  def assign_from_stripe(object)
11
+ check_object_is_stripe_class(object)
11
12
  assign_attributes(
12
13
  amount_off: object.amount_off ? Money.new(object.amount_off, object.currency) : nil,
13
14
  stripe_valid: object.valid
@@ -6,17 +6,19 @@ class StripeCustomer < StripeModelCallbacks::ApplicationRecord
6
6
  has_many :stripe_invoice_items, primary_key: "stripe_id"
7
7
  has_many :stripe_orders, primary_key: "stripe_id"
8
8
  has_many :stripe_subscriptions, primary_key: "stripe_id"
9
+ has_many :stripe_subscription_schedules, primary_key: "stripe_id"
9
10
 
10
11
  def self.stripe_class
11
12
  Stripe::Customer
12
13
  end
13
14
 
14
15
  def assign_from_stripe(object)
16
+ check_object_is_stripe_class(object)
15
17
  StripeModelCallbacks::AttributesAssignerService.execute!(
16
18
  model: self, stripe_model: object,
17
19
  attributes: %w[
18
- account_balance currency created delinquent description discount email id
19
- livemode metadata
20
+ account_balance currency created default_source delinquent description discount email
21
+ id livemode metadata
20
22
  ]
21
23
  )
22
24
  end
@@ -14,12 +14,19 @@ class StripeDiscount < StripeModelCallbacks::ApplicationRecord
14
14
  end
15
15
 
16
16
  def assign_from_stripe(object)
17
+ check_object_is_stripe_class(object)
18
+ if object.respond_to?(:customer)
19
+ customer_id = object.customer
20
+ else
21
+ customer_id = Stripe::Subscription.retrieve(object.subscription).customer
22
+ end
23
+
17
24
  assign_attributes(
18
25
  created: object.respond_to?(:created) ? Time.zone.at(object.created) : nil,
19
26
  start: Time.zone.at(object.start),
20
27
  end: object.end ? Time.zone.at(object.end) : nil,
21
28
  stripe_coupon_id: object.coupon.id,
22
- stripe_customer_id: object.customer,
29
+ stripe_customer_id: customer_id,
23
30
  stripe_subscription_id: object.subscription
24
31
  )
25
32
 
@@ -32,11 +39,11 @@ private
32
39
  def assign_coupon_attributes(object)
33
40
  assign_attributes(
34
41
  coupon_amount_off_cents: object.coupon.amount_off ? Money.new(object.coupon.amount_off, object.coupon.currency) : nil,
35
- coupon_created: Time.zone.at(object.coupon.created),
42
+ coupon_created: object.coupon.respond_to?(:created) ? Time.zone.at(object.coupon.created) : nil,
36
43
  coupon_currency: object.coupon.currency,
37
44
  coupon_duration: object.coupon.duration,
38
45
  coupon_duration_in_months: object.coupon.duration_in_months,
39
- coupon_livemode: object.coupon.livemode
46
+ coupon_livemode: object.coupon.respond_to?(:livemode) ? object.coupon.livemode : nil
40
47
  )
41
48
  end
42
49
 
@@ -6,6 +6,7 @@ class StripeDispute < StripeModelCallbacks::ApplicationRecord
6
6
  end
7
7
 
8
8
  def assign_from_stripe(object)
9
+ check_object_is_stripe_class(object)
9
10
  assign_attributes(
10
11
  amount: Money.new(object.amount, object.currency),
11
12
  balance_transaction_id: object.balance_transaction,
@@ -1,12 +1,17 @@
1
1
  class StripeInvoice < StripeModelCallbacks::ApplicationRecord
2
+ belongs_to :stripe_charge, optional: true, primary_key: "stripe_id"
2
3
  belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
3
4
  belongs_to :stripe_discount, optional: true
4
5
  belongs_to :stripe_subscription, optional: true, primary_key: "stripe_id"
5
6
 
6
7
  has_many :stripe_invoice_items, autosave: true, primary_key: "stripe_id"
7
8
 
9
+ validates :stripe_id, uniqueness: true
10
+
8
11
  monetize :amount_due_cents, allow_nil: true
9
- monetize :application_fee_cents, allow_nil: true
12
+ monetize :amount_paid_cents, allow_nil: true
13
+ monetize :amount_remaining_cents, allow_nil: true
14
+ monetize :application_fee_amount_cents, allow_nil: true
10
15
  monetize :subtotal_cents, allow_nil: true
11
16
  monetize :tax_cents, allow_nil: true
12
17
  monetize :total_cents, allow_nil: true
@@ -16,6 +21,7 @@ class StripeInvoice < StripeModelCallbacks::ApplicationRecord
16
21
  end
17
22
 
18
23
  def assign_from_stripe(object)
24
+ check_object_is_stripe_class(object)
19
25
  assign_attributes(
20
26
  stripe_charge_id: object.charge,
21
27
  stripe_customer_id: object.customer,
@@ -25,15 +31,24 @@ class StripeInvoice < StripeModelCallbacks::ApplicationRecord
25
31
  period_end: Time.zone.at(object.period_end)
26
32
  )
27
33
 
34
+ assign_closed(object)
28
35
  assign_created(object)
36
+
29
37
  assign_amounts(object)
38
+ assign_discount_item(object)
39
+ assign_tax(object)
40
+
41
+ assign_forgiven(object)
42
+ assign_status_transitions(object)
30
43
 
31
44
  StripeModelCallbacks::AttributesAssignerService.execute!(
32
45
  model: self, stripe_model: object,
33
46
  attributes: %w[
34
- attempted attempt_count billing closed currency description forgiven id livemode
35
- ending_balance forgiven next_payment_attempt number paid receipt_number
36
- starting_balance statement_descriptor tax_percent
47
+ attempted attempt_count auto_advance billing billing_reason
48
+ collection_method currency description ending_balance hosted_invoice_url
49
+ id invoice_pdf livemode next_payment_attempt number
50
+ paid receipt_number starting_balance statement_descriptor
51
+ status tax_percent
37
52
  ]
38
53
  )
39
54
 
@@ -45,14 +60,23 @@ private
45
60
  def assign_amounts(object)
46
61
  assign_attributes(
47
62
  amount_due: Money.new(object.amount_due, object.currency),
48
- application_fee: object.application_fee ? Money.new(object.application_fee, object.currency) : nil,
49
- stripe_discount_id: stripe_discount_id_from_object(object),
63
+ amount_paid: Money.new(object.amount_paid, object.currency),
64
+ amount_remaining: Money.new(object.amount_remaining, object.currency),
65
+ application_fee_amount: object.application_fee_amount ? Money.new(object.application_fee_amount, object.currency) : nil,
50
66
  subtotal: Money.new(object.subtotal, object.currency),
51
- tax: object.tax ? Money.new(object.tax, object.currency) : nil,
52
67
  total: object.total ? Money.new(object.total, object.currency) : nil
53
68
  )
54
69
  end
55
70
 
71
+ def assign_closed(object)
72
+ # The date-field was changed to auto_advance on 2018-11-08
73
+ if object.respond_to?(:closed)
74
+ self.closed = object.closed
75
+ else
76
+ self.closed = object.auto_advance == false
77
+ end
78
+ end
79
+
56
80
  def assign_created(object)
57
81
  # The date-field was renamed to created on 2019-03-14
58
82
  if object.respond_to?(:date)
@@ -62,6 +86,19 @@ private
62
86
  end
63
87
  end
64
88
 
89
+ def assign_discount_item(object)
90
+ self.stripe_discount_id = stripe_discount_id_from_object(object)
91
+ end
92
+
93
+ def assign_forgiven(object)
94
+ # The date-field was changed 2018-11-08
95
+ if object.respond_to?(:forgiven)
96
+ self.forgiven = object.forgiven
97
+ else
98
+ self.forgiven = object.status == "uncollectible"
99
+ end
100
+ end
101
+
65
102
  def assign_invoice_items(object)
66
103
  object.lines.each do |item|
67
104
  # Has to be found this way to actually update the values
@@ -77,6 +114,24 @@ private
77
114
  end
78
115
  end
79
116
 
117
+ def assign_status_transitions(object)
118
+ return unless object.respond_to?(:status_transitions)
119
+
120
+ transition_dates = {}
121
+ %i[finalized_at marked_uncollectible_at paid_at voided_at].each do |date_attribute|
122
+ date_value = object.status_transitions.__send__(date_attribute)
123
+ transition_dates[date_attribute] = Time.zone.at(date_value) if date_value.present?
124
+ end
125
+
126
+ assign_attributes(transition_dates) if transition_dates.any?
127
+ end
128
+
129
+ def assign_tax(object)
130
+ return unless object.tax
131
+
132
+ self.tax = Money.new(object.tax, object.currency)
133
+ end
134
+
80
135
  def stripe_discount_id_from_object(object)
81
136
  return nil unless object.discount
82
137
 
@@ -1,6 +1,7 @@
1
1
  class StripeInvoiceItem < StripeModelCallbacks::ApplicationRecord
2
2
  belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
3
3
  belongs_to :stripe_invoice, optional: true, primary_key: "stripe_id"
4
+ belongs_to :stripe_subscription, optional: true, primary_key: "stripe_id"
4
5
  belongs_to :stripe_subscription_item, optional: true, primary_key: "stripe_id"
5
6
  belongs_to :stripe_plan, optional: true, primary_key: "stripe_id"
6
7
 
@@ -11,6 +12,7 @@ class StripeInvoiceItem < StripeModelCallbacks::ApplicationRecord
11
12
  end
12
13
 
13
14
  def assign_from_stripe(object)
15
+ check_object_is_stripe_class(object, [Stripe::InvoiceItem, Stripe::InvoiceLineItem])
14
16
  assign_attributes(
15
17
  amount: Money.new(object.amount, object.currency),
16
18
  stripe_customer_id: object.try(:customer),
@@ -12,6 +12,7 @@ class StripeOrder < StripeModelCallbacks::ApplicationRecord
12
12
  end
13
13
 
14
14
  def assign_from_stripe(object)
15
+ check_object_is_stripe_class(object)
15
16
  assign_attributes(
16
17
  stripe_charge_id: object.charge,
17
18
  created: Time.zone.at(object.created),
@@ -8,6 +8,7 @@ class StripeOrderItem < StripeModelCallbacks::ApplicationRecord
8
8
  end
9
9
 
10
10
  def assign_from_stripe(object)
11
+ check_object_is_stripe_class(object)
11
12
  assign_attributes(
12
13
  amount: Money.new(object.amount, object.currency),
13
14
  currency: object.currency,
@@ -2,10 +2,11 @@ class StripePayout < StripeModelCallbacks::ApplicationRecord
2
2
  monetize :amount_cents, allow_nil: true
3
3
 
4
4
  def self.stripe_class
5
- Stripe::Subscription
5
+ Stripe::Payout
6
6
  end
7
7
 
8
8
  def assign_from_stripe(object)
9
+ check_object_is_stripe_class(object)
9
10
  assign_attributes(
10
11
  amount: Money.new(object.amount, object.currency),
11
12
  arrival_date: Time.zone.at(object.arrival_date),
@@ -14,14 +14,17 @@ class StripePlan < StripeModelCallbacks::ApplicationRecord
14
14
  end
15
15
 
16
16
  def assign_from_stripe(object)
17
+ check_object_is_stripe_class(object)
18
+ self.active = object.active == true if object.respond_to?(:active)
17
19
  assign_attributes(amount: Money.new(object.amount, object.currency))
18
20
  self.stripe_product_id = object.product if object.respond_to?(:product)
19
21
 
20
22
  StripeModelCallbacks::AttributesAssignerService.execute!(
21
23
  model: self, stripe_model: object,
22
24
  attributes: %w[
25
+ aggregate_usage amount_decimal billing_scheme
23
26
  created currency id interval interval_count livemode metadata nickname statement_descriptor
24
- trial_period_days
27
+ trial_period_days usage_type
25
28
  ]
26
29
  )
27
30
  end
@@ -0,0 +1,27 @@
1
+ class StripePrice < StripeModelCallbacks::ApplicationRecord
2
+ def self.stripe_class
3
+ Stripe::Price
4
+ end
5
+
6
+ def assign_from_stripe(object)
7
+ check_object_is_stripe_class(object)
8
+
9
+ assign_attributes(
10
+ stripe_id: object.id,
11
+ stripe_product_id: object.product,
12
+ recurring_aggregate_usage: object.recurring&.aggregate_usage,
13
+ recurring_interval: object.recurring&.interval,
14
+ recurring_interval_count: object.recurring&.interval_count,
15
+ recurring_usage_type: object.recurring&.usage_type,
16
+ transform_quantity_divide_by: object.transform_quantity&.divide_by,
17
+ transform_quantity_round: object.transform_quantity&.round
18
+ )
19
+
20
+ StripeModelCallbacks::AttributesAssignerService.execute!(
21
+ model: self, stripe_model: object,
22
+ attributes: %w[
23
+ active billing_scheme created currency lookup_key metadata nickname tiers_mode unit_amount
24
+ ]
25
+ )
26
+ end
27
+ end
@@ -1,26 +1,36 @@
1
1
  class StripeProduct < StripeModelCallbacks::ApplicationRecord
2
2
  has_many :stripe_plans, primary_key: "stripe_id"
3
+ has_many :stripe_subscriptions, through: :stripe_plans
3
4
 
4
5
  def self.stripe_class
5
6
  Stripe::Subscription
6
7
  end
7
8
 
8
9
  def assign_from_stripe(object)
10
+ check_object_is_stripe_class(object, [Stripe::Product, Stripe::Subscription])
9
11
  assign_attributes(
10
12
  active: object.active == true,
11
- updated: Time.zone.at(object.updated),
13
+ product_type: object.type,
12
14
  stripe_attributes: JSON.generate(object.attributes),
13
- package_dimensions_height: object.package_dimensions&.height,
14
- package_dimensions_length: object.package_dimensions&.length,
15
- package_dimensions_weight: object.package_dimensions&.weight,
16
- package_dimensions_width: object.package_dimensions&.width,
17
- shippable: object.shippable == true
15
+ updated: Time.zone.at(object.updated)
18
16
  )
19
17
 
18
+ if object.respond_to?(:package_dimensions)
19
+ assign_attributes(
20
+ package_dimensions_height: object.package_dimensions&.height,
21
+ package_dimensions_length: object.package_dimensions&.length,
22
+ package_dimensions_weight: object.package_dimensions&.weight,
23
+ package_dimensions_width: object.package_dimensions&.width
24
+ )
25
+ end
26
+
27
+ assign_attributes(shippable: object.shippable == true) if object.respond_to?(:shippable)
28
+
20
29
  StripeModelCallbacks::AttributesAssignerService.execute!(
21
30
  model: self, stripe_model: object,
22
31
  attributes: %w[
23
- caption created description id livemode name metadata statement_descriptor
32
+ caption created description id livemode
33
+ name metadata statement_descriptor unit_label
24
34
  ]
25
35
  )
26
36
  end
@@ -4,6 +4,7 @@ class StripeRecipient < StripeModelCallbacks::ApplicationRecord
4
4
  end
5
5
 
6
6
  def assign_from_stripe(object)
7
+ check_object_is_stripe_class(object)
7
8
  assign_attributes(
8
9
  stripe_type: object.type
9
10
  )
@@ -8,6 +8,7 @@ class StripeRefund < StripeModelCallbacks::ApplicationRecord
8
8
  end
9
9
 
10
10
  def assign_from_stripe(object)
11
+ check_object_is_stripe_class(object)
11
12
  assign_attributes(
12
13
  amount: Money.new(object.amount, object.currency),
13
14
  stripe_charge_id: object.charge,
@@ -6,6 +6,7 @@ class StripeReview < StripeModelCallbacks::ApplicationRecord
6
6
  end
7
7
 
8
8
  def assign_from_stripe(object)
9
+ check_object_is_stripe_class(object)
9
10
  assign_attributes(
10
11
  stripe_charge_id: object.charge
11
12
  )
@@ -2,10 +2,22 @@ class StripeSku < StripeModelCallbacks::ApplicationRecord
2
2
  monetize :price_cents
3
3
 
4
4
  def self.stripe_class
5
- Stripe::Sku
5
+ Stripe::SKU
6
6
  end
7
7
 
8
8
  def assign_from_stripe(object)
9
+ check_object_is_stripe_class(object)
10
+ assign_base_attributes(object)
11
+
12
+ StripeModelCallbacks::AttributesAssignerService.execute!(
13
+ model: self, stripe_model: object,
14
+ attributes: %w[currency]
15
+ )
16
+ end
17
+
18
+ private
19
+
20
+ def assign_base_attributes(object)
9
21
  assign_attributes(
10
22
  active: object.active == true,
11
23
  created: Time.zone.at(object.created),
@@ -19,10 +31,5 @@ class StripeSku < StripeModelCallbacks::ApplicationRecord
19
31
  price: Money.new(object.price, object.currency),
20
32
  stripe_product_id: object.product
21
33
  )
22
-
23
- StripeModelCallbacks::AttributesAssignerService.execute!(
24
- model: self, stripe_model: object,
25
- attributes: %w[currency]
26
- )
27
34
  end
28
35
  end