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.
- checksums.yaml +4 -4
- data/README.md +14 -0
- data/app/models/stripe_model_callbacks/application_record.rb +17 -0
- data/app/services/stripe_model_callbacks/account/external_account/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/attributes_assigner_service.rb +1 -1
- data/app/services/stripe_model_callbacks/base_service.rb +33 -1
- data/app/services/stripe_model_callbacks/charge/dispute_updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/charge/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/configure_service.rb +52 -24
- data/app/services/stripe_model_callbacks/coupon/sync_all.rb +9 -0
- data/app/services/stripe_model_callbacks/coupon/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/bank_account/deleted_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/deleted_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/discount_updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/source_updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/subscription/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/customer/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/invoice/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/invoice_item/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/notifier_service.rb +1 -1
- data/app/services/stripe_model_callbacks/order/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/payout/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/plan/sync_all.rb +9 -0
- data/app/services/stripe_model_callbacks/plan/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/price/updated_service.rb +14 -0
- data/app/services/stripe_model_callbacks/product/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/recipient/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/refund/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/review/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/sku/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/source/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/subscription/state_checker_service.rb +2 -2
- data/app/services/stripe_model_callbacks/subscription_schedule/updated_service.rb +46 -0
- data/app/services/stripe_model_callbacks/sync_everything.rb +8 -0
- data/app/services/stripe_model_callbacks/sync_from_stripe.rb +29 -0
- data/app/services/stripe_model_callbacks/tax_rate/updated_service.rb +27 -0
- data/app/services/stripe_model_callbacks/transfer/updated_service.rb +1 -1
- data/db/migrate/20200317160513_add_auto_advance_and_billing_reason_to_stripe_invoices.rb +6 -0
- data/db/migrate/20200317160602_add_status_and_status_transitions_to_stripe_invoices.rb +10 -0
- data/db/migrate/20200317180115_add_product_type_to_products.rb +5 -0
- data/db/migrate/20200317181224_rename_stripe_invoices_application_fee_to_application_fee_amount.rb +6 -0
- data/db/migrate/20200320105158_make_stripe_invoices_forgive_optional.rb +5 -0
- data/db/migrate/20200320114859_make_stripe_invoice_closed_optional.rb +5 -0
- data/db/migrate/20200323143634_add_amount_paid_and_remaining_and_invoice_url_and_pdf.rb +11 -0
- data/db/migrate/20200331074940_add_unit_label_to_stripe_products.rb +5 -0
- data/db/migrate/20200331075241_add_metered_info_to_stripe_plans.rb +9 -0
- data/db/migrate/20200513131105_create_stripe_subscription_schedules.rb +31 -0
- data/db/migrate/20200513131120_create_stripe_subscription_schedule_phases.rb +21 -0
- data/db/migrate/20200513131640_create_stripe_subscription_schedule_phase_plans.rb +13 -0
- data/db/migrate/20200514132247_add_stripe_id_uniq_to_stripe_invoices.rb +8 -0
- data/db/migrate/20200520152604_change_stripe_subscription_schedule_phase_id_to_bigint.rb +27 -0
- data/db/migrate/20200529144649_add_latest_invoice_id_to_stripe_subscription.rb +5 -0
- data/db/migrate/20201223173035_rename_stripe_subscriptions_start_to_start_date.rb +5 -0
- data/db/migrate/20201223202117_create_stripe_tax_rates.rb +15 -0
- data/db/migrate/20201224120534_create_stripe_subscription_default_tax_rates.rb +9 -0
- data/db/migrate/20201224122058_create_stripe_prices.rb +26 -0
- data/db/migrate/20201224123838_add_stripe_price_to_stripe_subscription_items.rb +6 -0
- data/lib/stripe_model_callbacks/configuration.rb +1 -1
- data/lib/stripe_model_callbacks/factories/stripe_coupons.rb +16 -0
- data/lib/stripe_model_callbacks/factories/stripe_plans.rb +3 -1
- data/lib/stripe_model_callbacks/factories/stripe_prices.rb +3 -0
- data/lib/stripe_model_callbacks/factories/stripe_products.rb +13 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_default_tax_rates.rb +6 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule.rb.rb +5 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule_phase_plan.rb +5 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_schedule_phases.rb +5 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscriptions.rb +1 -1
- data/lib/stripe_model_callbacks/factories/stripe_tax_rates.rb +5 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.created.2020-12-24.json +112 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.created.json +1 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.deleted.json +2 -2
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.subscription.updated.json +1 -1
- data/lib/stripe_model_callbacks/fixtures/stripe_events/customer/customer.updated.json +1 -1
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.created.json +13 -3
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.payment_failed.json +14 -4
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.payment_succeeded.json +23 -4
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.sent.json +13 -3
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.upcoming.json +13 -3
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.updated.json +13 -3
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.voided.json +152 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/plan/plan.created.json +7 -2
- data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.created.json +37 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.deleted.json +37 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/price/price.updated.json +40 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/product/product.created.json +1 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.canceled.json +81 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.created.json +77 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/subscription_schedule/subscription_schedule.updated.json +87 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/tax_rate/tax_rate.created.json +26 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/tax_rate/tax_rate.updated.json +29 -0
- data/lib/stripe_model_callbacks/models/stripe_bank_account.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_card.rb +2 -1
- data/lib/stripe_model_callbacks/models/stripe_charge.rb +3 -0
- data/lib/stripe_model_callbacks/models/stripe_coupon.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_customer.rb +4 -2
- data/lib/stripe_model_callbacks/models/stripe_discount.rb +10 -3
- data/lib/stripe_model_callbacks/models/stripe_dispute.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_invoice.rb +62 -7
- data/lib/stripe_model_callbacks/models/stripe_invoice_item.rb +2 -0
- data/lib/stripe_model_callbacks/models/stripe_order.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_order_item.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_payout.rb +2 -1
- data/lib/stripe_model_callbacks/models/stripe_plan.rb +4 -1
- data/lib/stripe_model_callbacks/models/stripe_price.rb +27 -0
- data/lib/stripe_model_callbacks/models/stripe_product.rb +17 -7
- data/lib/stripe_model_callbacks/models/stripe_recipient.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_refund.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_review.rb +1 -0
- data/lib/stripe_model_callbacks/models/stripe_sku.rb +13 -6
- data/lib/stripe_model_callbacks/models/stripe_source.rb +4 -2
- data/lib/stripe_model_callbacks/models/stripe_subscription.rb +56 -6
- data/lib/stripe_model_callbacks/models/stripe_subscription_default_tax_rate.rb +4 -0
- data/lib/stripe_model_callbacks/models/stripe_subscription_item.rb +19 -1
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule.rb +94 -0
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule_phase.rb +67 -0
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule_phase_plan.rb +37 -0
- data/lib/stripe_model_callbacks/models/stripe_tax_rate.rb +22 -0
- data/lib/stripe_model_callbacks/models/stripe_transfer.rb +1 -0
- data/lib/stripe_model_callbacks/version.rb +1 -1
- data/lib/stripe_model_callbacks.rb +1 -0
- metadata +90 -13
@@ -9,6 +9,8 @@ class StripeSource < StripeModelCallbacks::ApplicationRecord
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def assign_from_stripe(object)
|
12
|
+
check_object_is_stripe_class(object)
|
13
|
+
|
12
14
|
self.stripe_type = object.type
|
13
15
|
self.amount = Money.new(object.amount, object.currency) if object.respond_to?(:amount)
|
14
16
|
|
@@ -28,7 +30,7 @@ class StripeSource < StripeModelCallbacks::ApplicationRecord
|
|
28
30
|
|
29
31
|
private
|
30
32
|
|
31
|
-
def assign_owner(object)
|
33
|
+
def assign_owner(object) # rubocop:disable Metrics/AbcSize
|
32
34
|
assign_attributes(
|
33
35
|
owner_address_city: object.owner.address&.city,
|
34
36
|
owner_address_country: object.owner.address&.country,
|
@@ -42,7 +44,7 @@ private
|
|
42
44
|
)
|
43
45
|
end
|
44
46
|
|
45
|
-
def assign_owner_verified(object)
|
47
|
+
def assign_owner_verified(object) # rubocop:disable Metrics/AbcSize
|
46
48
|
assign_attributes(
|
47
49
|
owner_verified_address_city: object.owner.verified_address&.city,
|
48
50
|
owner_verified_address_country: object.owner.verified_address&.country,
|
@@ -4,7 +4,11 @@ class StripeSubscription < StripeModelCallbacks::ApplicationRecord
|
|
4
4
|
belongs_to :stripe_plan, optional: true, primary_key: "stripe_id"
|
5
5
|
has_many :stripe_invoices, primary_key: "stripe_id"
|
6
6
|
has_many :stripe_discounts, primary_key: "stripe_id"
|
7
|
+
has_many :stripe_subscription_default_tax_rates, autosave: true, dependent: :destroy
|
7
8
|
has_many :stripe_subscription_items, autosave: true, primary_key: "stripe_id"
|
9
|
+
has_many :stripe_subscription_schedules, primary_key: "stripe_id"
|
10
|
+
has_many :stripe_plans, through: :stripe_subscription_items
|
11
|
+
has_many :default_tax_rates, source: :stripe_tax_rate, through: :stripe_subscription_default_tax_rates
|
8
12
|
|
9
13
|
STATES = %w[trialing active past_due canceled unpaid].freeze
|
10
14
|
|
@@ -18,13 +22,16 @@ class StripeSubscription < StripeModelCallbacks::ApplicationRecord
|
|
18
22
|
end
|
19
23
|
|
20
24
|
def assign_from_stripe(object)
|
25
|
+
check_object_is_stripe_class(object)
|
21
26
|
assign_attributes(
|
22
27
|
canceled_at: object.canceled_at ? Time.zone.at(object.canceled_at) : nil,
|
23
|
-
stripe_customer_id: object.customer,
|
24
28
|
ended_at: object.ended_at ? Time.zone.at(object.ended_at) : nil,
|
25
|
-
|
29
|
+
latest_stripe_invoice_id: latest_invoice_id(object),
|
30
|
+
stripe_customer_id: object.customer,
|
31
|
+
stripe_plan_id: object.respond_to?(:plan) ? object.plan&.id : nil
|
26
32
|
)
|
27
33
|
|
34
|
+
assign_default_tax_rates(object)
|
28
35
|
assign_discount(object)
|
29
36
|
assign_items(object)
|
30
37
|
assign_periods(object)
|
@@ -63,6 +70,32 @@ class StripeSubscription < StripeModelCallbacks::ApplicationRecord
|
|
63
70
|
|
64
71
|
private
|
65
72
|
|
73
|
+
def assign_default_tax_rates(object)
|
74
|
+
return unless object.try(:default_tax_rates)
|
75
|
+
|
76
|
+
found_ids = []
|
77
|
+
|
78
|
+
object.default_tax_rates.each do |default_tax_rate|
|
79
|
+
tax_rate = StripeModelCallbacks::TaxRate::UpdatedService.execute!(object: default_tax_rate)
|
80
|
+
|
81
|
+
if new_record?
|
82
|
+
stripe_subscription_default_tax_rates.build(stripe_subscription_id: object.id, stripe_tax_rate_id: tax_rate.id)
|
83
|
+
else
|
84
|
+
default_tax_rate = StripeSubscriptionDefaultTaxRate.find_by(stripe_subscription_id: object.id, stripe_tax_rate_id: tax_rate.id)
|
85
|
+
stripe_subscription_default_tax_rates.build(stripe_subscription_id: object.id, stripe_tax_rate_id: tax_rate.id) unless default_tax_rate
|
86
|
+
found_ids << default_tax_rate.id if default_tax_rate
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
clean_up_default_tax_rates_not_found(found_ids) if persisted?
|
91
|
+
end
|
92
|
+
|
93
|
+
def clean_up_default_tax_rates_not_found(found_ids)
|
94
|
+
stripe_subscription_default_tax_rates.select(&:persisted?).each do |subscription_default_tax_rate|
|
95
|
+
subscription_default_tax_rate.mark_for_destruction if found_ids.exclude?(subscription_default_tax_rate.stripe_tax_rate_id)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
66
99
|
def assign_discount(object)
|
67
100
|
return if object.discount.blank?
|
68
101
|
|
@@ -80,10 +113,16 @@ private
|
|
80
113
|
end
|
81
114
|
|
82
115
|
def assign_periods(object)
|
116
|
+
start_date = if object.respond_to?(:start_date)
|
117
|
+
object.start_date
|
118
|
+
else
|
119
|
+
object.start
|
120
|
+
end
|
121
|
+
|
83
122
|
assign_attributes(
|
84
123
|
current_period_start: Time.zone.at(object.current_period_start),
|
85
124
|
current_period_end: Time.zone.at(object.current_period_end),
|
86
|
-
|
125
|
+
start_date: Time.zone.at(start_date),
|
87
126
|
trial_start: object.trial_start ? Time.zone.at(object.trial_start) : nil,
|
88
127
|
trial_end: object.trial_end ? Time.zone.at(object.trial_end) : nil
|
89
128
|
)
|
@@ -91,11 +130,22 @@ private
|
|
91
130
|
|
92
131
|
def find_item_by_stripe_item(item)
|
93
132
|
stripe_subscription_items.find do |sub_item|
|
94
|
-
if item.plan
|
95
|
-
|
133
|
+
if item.try(:plan)
|
134
|
+
if item.plan.is_a?(String)
|
135
|
+
sub_item.stripe_plan_id == item.plan
|
136
|
+
else
|
137
|
+
sub_item.stripe_plan_id == item.plan.id
|
138
|
+
end
|
96
139
|
else
|
97
|
-
sub_item.
|
140
|
+
sub_item.stripe_price_id == item.price.id
|
98
141
|
end
|
99
142
|
end
|
100
143
|
end
|
144
|
+
|
145
|
+
def latest_invoice_id(object)
|
146
|
+
return unless object.respond_to?(:latest_invoice)
|
147
|
+
return object.latest_invoice if object.latest_invoice.is_a?(String)
|
148
|
+
|
149
|
+
object.latest_invoice&.id
|
150
|
+
end
|
101
151
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
class StripeSubscriptionItem < StripeModelCallbacks::ApplicationRecord
|
2
2
|
belongs_to :stripe_plan, optional: true, primary_key: "stripe_id"
|
3
|
+
belongs_to :stripe_price, optional: true, primary_key: "stripe_id"
|
3
4
|
belongs_to :stripe_subscription, optional: true, primary_key: "stripe_id"
|
4
5
|
|
5
6
|
has_many :stripe_invoice_items, primary_key: "stripe_id"
|
@@ -9,8 +10,11 @@ class StripeSubscriptionItem < StripeModelCallbacks::ApplicationRecord
|
|
9
10
|
end
|
10
11
|
|
11
12
|
def assign_from_stripe(object)
|
13
|
+
check_object_is_stripe_class(object)
|
12
14
|
self.stripe_subscription_id = object.subscription if object.respond_to?(:subscription)
|
13
|
-
self.stripe_plan_id = object.plan.id if object.plan.respond_to?(:id)
|
15
|
+
self.stripe_plan_id = object.plan.id if object.try(:plan).respond_to?(:id)
|
16
|
+
|
17
|
+
assign_price_from_stripe(object)
|
14
18
|
|
15
19
|
StripeModelCallbacks::AttributesAssignerService.execute!(
|
16
20
|
model: self, stripe_model: object,
|
@@ -33,4 +37,18 @@ class StripeSubscriptionItem < StripeModelCallbacks::ApplicationRecord
|
|
33
37
|
stripe_subscription.reload_from_stripe!
|
34
38
|
nil
|
35
39
|
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def assign_price_from_stripe(object)
|
44
|
+
# Older versions doesn't support price
|
45
|
+
return unless object.try(:price).respond_to?(:id)
|
46
|
+
|
47
|
+
# Make sure price is created
|
48
|
+
price = StripePrice.find_by(stripe_id: object.price.id)
|
49
|
+
StripePrice.create_from_stripe!(object.price) unless price
|
50
|
+
|
51
|
+
# Set stripe ID on the subscription item
|
52
|
+
self.stripe_price_id = object.price.id
|
53
|
+
end
|
36
54
|
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
class StripeSubscriptionSchedule < StripeModelCallbacks::ApplicationRecord
|
2
|
+
MATCHING_STRIPE_ATTRIBUTES = %w[
|
3
|
+
billing created collection_method
|
4
|
+
default_payment_method default_source
|
5
|
+
end_behavior id metadata livemode
|
6
|
+
renewal_behavior renewal_interval
|
7
|
+
status
|
8
|
+
].freeze
|
9
|
+
private_constant :MATCHING_STRIPE_ATTRIBUTES
|
10
|
+
|
11
|
+
belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
|
12
|
+
belongs_to :stripe_subscription, optional: true, primary_key: "stripe_id"
|
13
|
+
|
14
|
+
has_many :stripe_subscription_schedule_phases, primary_key: "stripe_id", dependent: :destroy
|
15
|
+
|
16
|
+
def self.stripe_class
|
17
|
+
Stripe::SubscriptionSchedule
|
18
|
+
end
|
19
|
+
|
20
|
+
def assign_from_stripe(object)
|
21
|
+
check_object_is_stripe_class(object)
|
22
|
+
assign_attributes(
|
23
|
+
released_stripe_subscription_id: object.released_subscription,
|
24
|
+
stripe_customer_id: object.customer,
|
25
|
+
stripe_subscription_id: object.subscription
|
26
|
+
)
|
27
|
+
|
28
|
+
assign_billing_thresholds(object)
|
29
|
+
assign_current_phase(object)
|
30
|
+
assign_invoice_settings(object)
|
31
|
+
assign_timestamps(object)
|
32
|
+
|
33
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
34
|
+
model: self,
|
35
|
+
stripe_model: object,
|
36
|
+
attributes: MATCHING_STRIPE_ATTRIBUTES
|
37
|
+
)
|
38
|
+
|
39
|
+
assign_subscription_schedule_phases(object)
|
40
|
+
end
|
41
|
+
|
42
|
+
def cancel_on_stripe
|
43
|
+
to_stripe.cancel
|
44
|
+
update!(canceled_at: Time.zone.now) if respond_to?(:canceled_at)
|
45
|
+
reload_from_stripe!
|
46
|
+
true
|
47
|
+
end
|
48
|
+
|
49
|
+
def cancel_on_stripe!
|
50
|
+
raise ActiveRecord::RecordInvalid, self unless cancel_on_stripe
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def assign_billing_thresholds(object)
|
56
|
+
billing_thresholds = object.billing_thresholds
|
57
|
+
|
58
|
+
self.billing_thresholds_amount_gte = billing_thresholds&.amount_gte
|
59
|
+
self.billing_thresholds_reset_billing_cycle_anchor = billing_thresholds&.reset_billing_cycle_anchor
|
60
|
+
end
|
61
|
+
|
62
|
+
def assign_current_phase(object)
|
63
|
+
current_phase = object.current_phase
|
64
|
+
|
65
|
+
start_date = current_phase&.start_date
|
66
|
+
self.current_phase_start_date = Time.zone.at(start_date) if start_date
|
67
|
+
|
68
|
+
end_date = current_phase&.end_date
|
69
|
+
self.current_phase_end_date = Time.zone.at(end_date) if end_date
|
70
|
+
end
|
71
|
+
|
72
|
+
def assign_invoice_settings(object)
|
73
|
+
self.invoice_settings_days_until_due = object.invoice_settings&.days_until_due
|
74
|
+
end
|
75
|
+
|
76
|
+
def assign_timestamps(object)
|
77
|
+
%i[canceled_at completed_at released_at].each do |timestamp|
|
78
|
+
object_timestamp = object.__send__(timestamp)
|
79
|
+
__send__("#{timestamp}=", Time.zone.at(object_timestamp)) if object_timestamp
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def assign_subscription_schedule_phases(object)
|
84
|
+
self.stripe_subscription_schedule_phases = new_stripe_subscription_schedule_phases(object)
|
85
|
+
end
|
86
|
+
|
87
|
+
def new_stripe_subscription_schedule_phases(object)
|
88
|
+
@new_stripe_subscription_schedule_phases ||= object.phases.collect do |phase|
|
89
|
+
subscription_schedule_phase = StripeSubscriptionSchedulePhase.new(stripe_subscription_schedule: self)
|
90
|
+
subscription_schedule_phase.assign_from_stripe(phase)
|
91
|
+
subscription_schedule_phase
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
class StripeSubscriptionSchedulePhase < StripeModelCallbacks::ApplicationRecord
|
2
|
+
MATCHING_STRIPE_ATTRIBUTES = %w[
|
3
|
+
application_fee_percent
|
4
|
+
collection_method
|
5
|
+
default_payment_method
|
6
|
+
prorate
|
7
|
+
proration_behavior
|
8
|
+
].freeze
|
9
|
+
private_constant :MATCHING_STRIPE_ATTRIBUTES
|
10
|
+
|
11
|
+
belongs_to :stripe_subscription_schedule, primary_key: "stripe_id"
|
12
|
+
|
13
|
+
has_many :stripe_subscription_schedule_phase_plans, dependent: :destroy
|
14
|
+
|
15
|
+
def self.stripe_class
|
16
|
+
Stripe::SubscriptionSchedulePhase
|
17
|
+
end
|
18
|
+
|
19
|
+
def assign_from_stripe(object)
|
20
|
+
check_object_is_stripe_class(object)
|
21
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
22
|
+
model: self,
|
23
|
+
stripe_model: object,
|
24
|
+
attributes: MATCHING_STRIPE_ATTRIBUTES
|
25
|
+
)
|
26
|
+
|
27
|
+
assign_billing_thresholds(object)
|
28
|
+
assign_invoice_settings(object)
|
29
|
+
assign_timestamps(object)
|
30
|
+
|
31
|
+
assign_subscription_schedule_phase_plans(object)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def assign_billing_thresholds(object)
|
37
|
+
billing_thresholds = object.billing_thresholds
|
38
|
+
|
39
|
+
self.billing_thresholds_amount_gte = billing_thresholds&.amount_gte
|
40
|
+
self.billing_thresholds_reset_billing_cycle_anchor = billing_thresholds&.reset_billing_cycle_anchor
|
41
|
+
end
|
42
|
+
|
43
|
+
def assign_invoice_settings(object)
|
44
|
+
invoice_settings = object.invoice_settings
|
45
|
+
|
46
|
+
self.invoice_settings_days_until_due = invoice_settings&.days_until_due
|
47
|
+
end
|
48
|
+
|
49
|
+
def assign_timestamps(object)
|
50
|
+
%i[end_date start_date trial_end].each do |timestamp|
|
51
|
+
object_timestamp = object.__send__(timestamp)
|
52
|
+
__send__("#{timestamp}=", Time.zone.at(object_timestamp)) if object_timestamp
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def assign_subscription_schedule_phase_plans(object)
|
57
|
+
self.stripe_subscription_schedule_phase_plans = new_stripe_subscription_schedule_phase_plans(object)
|
58
|
+
end
|
59
|
+
|
60
|
+
def new_stripe_subscription_schedule_phase_plans(object)
|
61
|
+
@new_stripe_subscription_schedule_phase_plans ||= object.plans.collect do |plan|
|
62
|
+
subscription_schedule_phase_plan = StripeSubscriptionSchedulePhasePlan.new(stripe_subscription_schedule_phase: self)
|
63
|
+
subscription_schedule_phase_plan.assign_from_stripe(plan)
|
64
|
+
subscription_schedule_phase_plan
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class StripeSubscriptionSchedulePhasePlan < StripeModelCallbacks::ApplicationRecord
|
2
|
+
MATCHING_STRIPE_ATTRIBUTES = %w[quantity].freeze
|
3
|
+
private_constant :MATCHING_STRIPE_ATTRIBUTES
|
4
|
+
|
5
|
+
belongs_to :stripe_plan, optional: true, primary_key: "stripe_id"
|
6
|
+
belongs_to :stripe_subscription_schedule_phase
|
7
|
+
|
8
|
+
has_one :stripe_subscription_schedule, through: :stripe_subscription_schedule_phase
|
9
|
+
|
10
|
+
def self.stripe_class
|
11
|
+
Stripe::SubscriptionSchedulePhasePlan
|
12
|
+
end
|
13
|
+
|
14
|
+
def assign_from_stripe(object)
|
15
|
+
check_object_is_stripe_class(object)
|
16
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
17
|
+
model: self,
|
18
|
+
stripe_model: object,
|
19
|
+
attributes: MATCHING_STRIPE_ATTRIBUTES
|
20
|
+
)
|
21
|
+
|
22
|
+
assign_attributes(
|
23
|
+
stripe_plan_id: object.plan,
|
24
|
+
stripe_price_id: object.price
|
25
|
+
)
|
26
|
+
|
27
|
+
assign_billing_thresholds(object)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def assign_billing_thresholds(object)
|
33
|
+
billing_thresholds = object.billing_thresholds
|
34
|
+
|
35
|
+
self.billing_thresholds_usage_gte = billing_thresholds&.usage_gte
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class StripeTaxRate < StripeModelCallbacks::ApplicationRecord
|
2
|
+
has_many :stripe_subscription_default_tax_rates, dependent: :destroy
|
3
|
+
|
4
|
+
def self.stripe_class
|
5
|
+
Stripe::TaxRate
|
6
|
+
end
|
7
|
+
|
8
|
+
def assign_from_stripe(object)
|
9
|
+
check_object_is_stripe_class(object)
|
10
|
+
assign_attributes(
|
11
|
+
created: Time.zone.at(object.created),
|
12
|
+
inclusive: object.inclusive == true,
|
13
|
+
stripe_id: object.id
|
14
|
+
)
|
15
|
+
|
16
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
17
|
+
model: self,
|
18
|
+
stripe_model: object,
|
19
|
+
attributes: %w[display_name description jurisdiction percentage inclusive]
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
@@ -7,6 +7,7 @@ class StripeTransfer < StripeModelCallbacks::ApplicationRecord
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def assign_from_stripe(object)
|
10
|
+
check_object_is_stripe_class(object)
|
10
11
|
assign_attributes(
|
11
12
|
amount: Money.new(object.amount, object.currency),
|
12
13
|
amount_reversed: Money.new(object.amount_reversed, object.currency),
|