stripe_model_callbacks 0.1.7 → 0.1.9
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.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/Rakefile +1 -1
- data/app/models/stripe_model_callbacks/application_record.rb +28 -14
- data/app/services/stripe_model_callbacks/account/external_account/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/attributes_assigner_service.rb +154 -8
- data/app/services/stripe_model_callbacks/base_service.rb +9 -6
- data/app/services/stripe_model_callbacks/charge/dispute_updated_service.rb +3 -3
- data/app/services/stripe_model_callbacks/charge/updated_service.rb +5 -5
- data/app/services/stripe_model_callbacks/configure_service.rb +58 -27
- data/app/services/stripe_model_callbacks/customer/bank_account/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 +3 -3
- data/app/services/stripe_model_callbacks/customer/subscription/updated_service.rb +2 -2
- data/app/services/stripe_model_callbacks/customer/updated_service.rb +2 -2
- data/app/services/stripe_model_callbacks/event_mocker_service.rb +14 -8
- data/app/services/stripe_model_callbacks/invoice/updated_service.rb +4 -4
- data/app/services/stripe_model_callbacks/payment_intent/updated_service.rb +19 -0
- data/app/services/stripe_model_callbacks/payment_method/updated_service.rb +19 -0
- data/app/services/stripe_model_callbacks/payout/updated_service.rb +3 -3
- data/app/services/stripe_model_callbacks/plan/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/price/updated_service.rb +1 -1
- 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/review/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/setup_intent/updated_service.rb +8 -0
- data/app/services/stripe_model_callbacks/sku/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/source/transaction_created_service.rb +17 -0
- data/app/services/stripe_model_callbacks/source/updated_service.rb +4 -4
- data/app/services/stripe_model_callbacks/subscription/state_checker_service.rb +1 -1
- data/app/services/stripe_model_callbacks/subscription_schedule/updated_service.rb +1 -1
- data/app/services/stripe_model_callbacks/sync_everything.rb +17 -2
- data/app/services/stripe_model_callbacks/sync_from_stripe.rb +1 -1
- data/app/services/stripe_model_callbacks/tax_rate/updated_service.rb +3 -3
- data/app/services/stripe_model_callbacks/transfer/updated_service.rb +1 -1
- data/config/rails_best_practices.yml +1 -0
- data/db/migrate/20180206151132_change_activities_trackable_id_to_string.rb +1 -1
- data/db/migrate/20180208091647_change_stripe_subscription_items_quantity_to_integer.rb +1 -1
- data/db/migrate/20181219121712_add_new_primary_id_and_rename_old.rb +2 -2
- data/db/migrate/20230210081405_create_stripe_setup_intents.rb +28 -0
- data/db/migrate/20230331182902_add_amount_captured_to_stripe_charges.rb +6 -0
- data/db/migrate/20230420092307_create_stripe_payment_methods.rb +15 -0
- data/db/migrate/20230421072509_create_stripe_payment_intents.rb +44 -0
- data/db/migrate/20230422074329_add_payment_intent_to_stripe_charges.rb +6 -0
- data/db/migrate/20230422175529_add_payment_intent_to_stripe_refunds.rb +6 -0
- data/db/migrate/20230422180602_change_stripe_refunds_stripe_charge_id_to_nullable.rb +5 -0
- data/db/migrate/20230518072538_change_stripe_setup_intents_payment_method_to_string.rb +7 -0
- data/lib/stripe_model_callbacks/configuration.rb +1 -1
- data/lib/stripe_model_callbacks/event_mocker.rb +1 -1
- data/lib/stripe_model_callbacks/factories/stripe_charges.rb +10 -0
- data/lib/stripe_model_callbacks/factories/stripe_coupons.rb +10 -9
- data/lib/stripe_model_callbacks/factories/stripe_customers.rb +6 -4
- data/lib/stripe_model_callbacks/factories/stripe_invoice_items.rb +1 -0
- data/lib/stripe_model_callbacks/factories/stripe_invoices.rb +1 -0
- data/lib/stripe_model_callbacks/factories/stripe_payment_intents.rb +17 -0
- data/lib/stripe_model_callbacks/factories/stripe_payment_methods.rb +15 -0
- data/lib/stripe_model_callbacks/factories/stripe_plans.rb +9 -12
- data/lib/stripe_model_callbacks/factories/stripe_products.rb +9 -8
- data/lib/stripe_model_callbacks/factories/stripe_recipients.rb +2 -0
- data/lib/stripe_model_callbacks/factories/stripe_reviews.rb +2 -0
- data/lib/stripe_model_callbacks/factories/stripe_setup_intents.rb +7 -0
- data/lib/stripe_model_callbacks/factories/stripe_skus.rb +1 -0
- data/lib/stripe_model_callbacks/factories/stripe_subscription_items.rb +7 -7
- data/lib/stripe_model_callbacks/factories/stripe_subscriptions.rb +10 -12
- data/lib/stripe_model_callbacks/factories/stripe_tax_rates.rb +10 -0
- data/lib/stripe_model_callbacks/factories/stripe_transfers.rb +2 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/charge/charge.refunded.json +1 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/invoice/invoice.upcoming.json +0 -1
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.amount_capturable_updated.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.canceled.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.created.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.partially_funded.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.payment_failed.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.processing.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.requires_action.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_intent/payment_intent.succeeded.json +66 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.attached.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.automatically_updated.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.card_automatically_updated.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.detached.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/payment_method/payment_method.updated.json +63 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/setup_intent/setup_intent.created.json +47 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/setup_intent/setup_intent.updated.json +47 -0
- data/lib/stripe_model_callbacks/fixtures/stripe_events/source/{source.transaction_created.json → source.transaction.created.json} +2 -1
- data/lib/stripe_model_callbacks/models/stripe_charge.rb +51 -4
- data/lib/stripe_model_callbacks/models/stripe_coupon.rb +12 -0
- data/lib/stripe_model_callbacks/models/stripe_customer.rb +12 -0
- data/lib/stripe_model_callbacks/models/stripe_invoice.rb +17 -1
- data/lib/stripe_model_callbacks/models/stripe_payment_intent.rb +125 -0
- data/lib/stripe_model_callbacks/models/stripe_payment_method.rb +31 -0
- data/lib/stripe_model_callbacks/models/stripe_plan.rb +13 -0
- data/lib/stripe_model_callbacks/models/stripe_product.rb +10 -0
- data/lib/stripe_model_callbacks/models/stripe_recipient.rb +1 -1
- data/lib/stripe_model_callbacks/models/stripe_refund.rb +3 -1
- data/lib/stripe_model_callbacks/models/stripe_setup_intent.rb +21 -0
- data/lib/stripe_model_callbacks/models/stripe_source.rb +2 -1
- data/lib/stripe_model_callbacks/models/stripe_subscription.rb +23 -4
- data/lib/stripe_model_callbacks/models/stripe_subscription_item.rb +28 -2
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule.rb +2 -2
- data/lib/stripe_model_callbacks/models/stripe_subscription_schedule_phase.rb +1 -1
- data/lib/stripe_model_callbacks/models/stripe_tax_rate.rb +6 -0
- data/lib/stripe_model_callbacks/version.rb +1 -1
- data/lib/stripe_model_callbacks.rb +1 -1
- metadata +47 -14
|
@@ -5,6 +5,9 @@ class StripeCustomer < StripeModelCallbacks::ApplicationRecord
|
|
|
5
5
|
has_many :stripe_invoices, primary_key: "stripe_id"
|
|
6
6
|
has_many :stripe_invoice_items, primary_key: "stripe_id"
|
|
7
7
|
has_many :stripe_orders, primary_key: "stripe_id"
|
|
8
|
+
has_many :stripe_payment_intents, foreign_key: "customer", primary_key: "stripe_id"
|
|
9
|
+
has_many :stripe_payment_methods, foreign_key: "customer", primary_key: "stripe_id"
|
|
10
|
+
has_many :stripe_setup_intents, foreign_key: "customer", primary_key: "stripe_id"
|
|
8
11
|
has_many :stripe_subscriptions, primary_key: "stripe_id"
|
|
9
12
|
has_many :stripe_subscription_schedules, primary_key: "stripe_id"
|
|
10
13
|
|
|
@@ -31,4 +34,13 @@ class StripeCustomer < StripeModelCallbacks::ApplicationRecord
|
|
|
31
34
|
]
|
|
32
35
|
)
|
|
33
36
|
end
|
|
37
|
+
|
|
38
|
+
def create_stripe_mock!
|
|
39
|
+
mock_customer = Stripe::Customer.create(
|
|
40
|
+
id: stripe_id,
|
|
41
|
+
source: StripeMock.create_test_helper.generate_card_token
|
|
42
|
+
)
|
|
43
|
+
assign_from_stripe(mock_customer)
|
|
44
|
+
save!
|
|
45
|
+
end
|
|
34
46
|
end
|
|
@@ -4,6 +4,7 @@ class StripeInvoice < StripeModelCallbacks::ApplicationRecord
|
|
|
4
4
|
belongs_to :stripe_discount, optional: true
|
|
5
5
|
belongs_to :stripe_subscription, optional: true, primary_key: "stripe_id"
|
|
6
6
|
|
|
7
|
+
has_many :stripe_charges, primary_key: "stripe_id"
|
|
7
8
|
has_many :stripe_invoice_items, autosave: true, primary_key: "stripe_id"
|
|
8
9
|
|
|
9
10
|
validates :stripe_id, uniqueness: true
|
|
@@ -16,11 +17,13 @@ class StripeInvoice < StripeModelCallbacks::ApplicationRecord
|
|
|
16
17
|
monetize :tax_cents, allow_nil: true
|
|
17
18
|
monetize :total_cents, allow_nil: true
|
|
18
19
|
|
|
20
|
+
has_one_attached :invoice_pdf_file
|
|
21
|
+
|
|
19
22
|
def self.stripe_class
|
|
20
23
|
Stripe::Invoice
|
|
21
24
|
end
|
|
22
25
|
|
|
23
|
-
def assign_from_stripe(object)
|
|
26
|
+
def assign_from_stripe(object) # rubocop:disable Metrics/AbcSize
|
|
24
27
|
check_object_is_stripe_class(object)
|
|
25
28
|
assign_attributes(
|
|
26
29
|
stripe_charge_id: object.charge,
|
|
@@ -36,6 +39,7 @@ class StripeInvoice < StripeModelCallbacks::ApplicationRecord
|
|
|
36
39
|
|
|
37
40
|
assign_amounts(object)
|
|
38
41
|
assign_discount_item(object)
|
|
42
|
+
attach_invoice_pdf(object)
|
|
39
43
|
assign_tax(object)
|
|
40
44
|
|
|
41
45
|
assign_forgiven(object)
|
|
@@ -57,6 +61,7 @@ class StripeInvoice < StripeModelCallbacks::ApplicationRecord
|
|
|
57
61
|
|
|
58
62
|
private
|
|
59
63
|
|
|
64
|
+
|
|
60
65
|
def assign_amounts(object)
|
|
61
66
|
assign_attributes(
|
|
62
67
|
amount_due: Money.new(object.amount_due, object.currency),
|
|
@@ -114,6 +119,17 @@ private
|
|
|
114
119
|
end
|
|
115
120
|
end
|
|
116
121
|
|
|
122
|
+
def attach_invoice_pdf(object)
|
|
123
|
+
if object.respond_to?(:invoice_pdf) && object.invoice_pdf && !invoice_pdf_file.attached?
|
|
124
|
+
require "open-uri"
|
|
125
|
+
|
|
126
|
+
url = URI.parse(object.invoice_pdf)
|
|
127
|
+
filename = File.basename(url.path)
|
|
128
|
+
file = url.open
|
|
129
|
+
invoice_pdf_file.attach(io: file, filename: filename)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
117
133
|
def assign_status_transitions(object)
|
|
118
134
|
return unless object.respond_to?(:status_transitions)
|
|
119
135
|
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
class StripePaymentIntent < StripeModelCallbacks::ApplicationRecord
|
|
2
|
+
CANCELLABLE_STATUSES = %w[requires_payment_method requires_capture requires_confirmation requires_action].freeze
|
|
3
|
+
|
|
4
|
+
belongs_to :stripe_customer, foreign_key: "customer", optional: true, primary_key: "stripe_id"
|
|
5
|
+
belongs_to :stripe_latest_charge,
|
|
6
|
+
class_name: "StripeCharge",
|
|
7
|
+
foreign_key: "latest_charge",
|
|
8
|
+
inverse_of: :latest_charge_on_stripe_payment_intent,
|
|
9
|
+
optional: true,
|
|
10
|
+
primary_key: "stripe_id"
|
|
11
|
+
belongs_to :stripe_payment_method, foreign_key: "payment_method", optional: true, primary_key: "stripe_id"
|
|
12
|
+
|
|
13
|
+
has_many :stripe_charges, foreign_key: "payment_intent", primary_key: "stripe_id"
|
|
14
|
+
has_many :stripe_refunds, foreign_key: "payment_intent", primary_key: "stripe_id"
|
|
15
|
+
|
|
16
|
+
scope :cancellable, -> { where(status: CANCELLABLE_STATUSES) }
|
|
17
|
+
|
|
18
|
+
def self.stripe_class
|
|
19
|
+
Stripe::PaymentIntent
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def assign_from_stripe(object)
|
|
23
|
+
check_object_is_stripe_class(object)
|
|
24
|
+
|
|
25
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
|
26
|
+
model: self,
|
|
27
|
+
stripe_model: object,
|
|
28
|
+
attributes: %w[
|
|
29
|
+
amount
|
|
30
|
+
amount_capturable
|
|
31
|
+
amount_details
|
|
32
|
+
amount_received
|
|
33
|
+
application
|
|
34
|
+
application_fee_amount
|
|
35
|
+
automatic_payment_methods
|
|
36
|
+
canceled_at
|
|
37
|
+
cancellation_reason
|
|
38
|
+
capture_method
|
|
39
|
+
client_secret
|
|
40
|
+
confirmation_method
|
|
41
|
+
created
|
|
42
|
+
currency
|
|
43
|
+
customer
|
|
44
|
+
description
|
|
45
|
+
id
|
|
46
|
+
invoice
|
|
47
|
+
last_payment_error
|
|
48
|
+
latest_charge
|
|
49
|
+
livemode
|
|
50
|
+
metadata
|
|
51
|
+
next_action
|
|
52
|
+
on_behalf_of
|
|
53
|
+
payment_method
|
|
54
|
+
payment_method_options
|
|
55
|
+
payment_method_types
|
|
56
|
+
processing
|
|
57
|
+
receipt_email
|
|
58
|
+
review
|
|
59
|
+
setup_future_usage
|
|
60
|
+
shipping
|
|
61
|
+
statement_descriptor
|
|
62
|
+
statement_descriptor_suffix
|
|
63
|
+
status
|
|
64
|
+
transfer_data
|
|
65
|
+
transfer_group
|
|
66
|
+
]
|
|
67
|
+
)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def amount_money
|
|
71
|
+
Money.new(amount, currency)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def amount_capturable_money
|
|
75
|
+
Money.new(amount_capturable, currency)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def amount_received_money
|
|
79
|
+
Money.new(amount_received, currency)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def cancel(**args)
|
|
83
|
+
updated_payment_intent = Stripe::PaymentIntent.cancel(stripe_id, **args)
|
|
84
|
+
assign_from_stripe(updated_payment_intent)
|
|
85
|
+
save!
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def cancellable?
|
|
89
|
+
CANCELLABLE_STATUSES.include?(status)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def capture(**args)
|
|
93
|
+
updated_payment_intent = Stripe::PaymentIntent.capture(stripe_id, **args)
|
|
94
|
+
assign_from_stripe(updated_payment_intent)
|
|
95
|
+
save!
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def confirm(**args)
|
|
99
|
+
updated_payment_intent = Stripe::PaymentIntent.confirm(stripe_id, **args)
|
|
100
|
+
assign_from_stripe(updated_payment_intent)
|
|
101
|
+
save!
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def create_stripe_mock!
|
|
105
|
+
mock_payment_intent = Stripe::PaymentIntent.create(
|
|
106
|
+
amount:,
|
|
107
|
+
amount_capturable:,
|
|
108
|
+
amount_details:,
|
|
109
|
+
amount_received:,
|
|
110
|
+
application:,
|
|
111
|
+
application_fee_amount:,
|
|
112
|
+
automatic_payment_methods:,
|
|
113
|
+
canceled_at:,
|
|
114
|
+
cancellation_reason:,
|
|
115
|
+
capture_method:,
|
|
116
|
+
client_secret:,
|
|
117
|
+
currency:,
|
|
118
|
+
customer:,
|
|
119
|
+
id: stripe_id,
|
|
120
|
+
status:
|
|
121
|
+
)
|
|
122
|
+
assign_from_stripe(mock_payment_intent)
|
|
123
|
+
save!
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
class StripePaymentMethod < StripeModelCallbacks::ApplicationRecord
|
|
2
|
+
belongs_to :stripe_customer, foreign_key: "customer", optional: true, primary_key: "stripe_id"
|
|
3
|
+
|
|
4
|
+
has_many :stripe_payment_intents, foreign_key: "payment_method", primary_key: "stripe_id"
|
|
5
|
+
has_many :stripe_setup_intents, foreign_key: "payment_method", primary_key: "stripe_id"
|
|
6
|
+
|
|
7
|
+
def self.stripe_class
|
|
8
|
+
Stripe::PaymentMethod
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def assign_from_stripe(object)
|
|
12
|
+
check_object_is_stripe_class(object)
|
|
13
|
+
|
|
14
|
+
self.stripe_id = object.id
|
|
15
|
+
self.stripe_type = object.type
|
|
16
|
+
|
|
17
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
|
18
|
+
model: self,
|
|
19
|
+
stripe_model: object,
|
|
20
|
+
attributes: %w[
|
|
21
|
+
billing_details card created customer livemode metadata
|
|
22
|
+
]
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def create_stripe_mock!
|
|
27
|
+
mock_payment_method = Stripe::PaymentMethod.create(customer:, id: stripe_id, type: "card")
|
|
28
|
+
assign_from_stripe(mock_payment_method)
|
|
29
|
+
save!
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -30,6 +30,19 @@ class StripePlan < StripeModelCallbacks::ApplicationRecord
|
|
|
30
30
|
)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
def create_stripe_mock!
|
|
34
|
+
mock_plan = Stripe::Plan.create(
|
|
35
|
+
id: stripe_id,
|
|
36
|
+
amount: amount_cents,
|
|
37
|
+
currency:,
|
|
38
|
+
interval:,
|
|
39
|
+
interval_count:,
|
|
40
|
+
product: stripe_product.stripe_id
|
|
41
|
+
)
|
|
42
|
+
assign_from_stripe(mock_plan)
|
|
43
|
+
save!
|
|
44
|
+
end
|
|
45
|
+
|
|
33
46
|
def name
|
|
34
47
|
nickname.presence || stripe_product&.name
|
|
35
48
|
end
|
|
@@ -34,4 +34,14 @@ class StripeProduct < StripeModelCallbacks::ApplicationRecord
|
|
|
34
34
|
]
|
|
35
35
|
)
|
|
36
36
|
end
|
|
37
|
+
|
|
38
|
+
def create_stripe_mock!
|
|
39
|
+
mock_product = Stripe::Product.create(
|
|
40
|
+
id: stripe_id,
|
|
41
|
+
name:,
|
|
42
|
+
type: "service"
|
|
43
|
+
)
|
|
44
|
+
assign_from_stripe(mock_product)
|
|
45
|
+
save!
|
|
46
|
+
end
|
|
37
47
|
end
|
|
@@ -11,7 +11,7 @@ class StripeRecipient < StripeModelCallbacks::ApplicationRecord
|
|
|
11
11
|
|
|
12
12
|
StripeModelCallbacks::AttributesAssignerService.execute!(
|
|
13
13
|
model: self, stripe_model: object,
|
|
14
|
-
attributes: %w[active_account description email name migrated_to verified]
|
|
14
|
+
attributes: %w[active_account description email livemode name migrated_to verified]
|
|
15
15
|
)
|
|
16
16
|
end
|
|
17
17
|
end
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
class StripeRefund < StripeModelCallbacks::ApplicationRecord
|
|
2
2
|
belongs_to :stripe_charge, optional: true, primary_key: "stripe_id"
|
|
3
|
+
belongs_to :stripe_payment_intent, foreign_key: "payment_intent", optional: true, primary_key: "stripe_id"
|
|
3
4
|
|
|
4
5
|
monetize :amount_cents, allow_nil: true
|
|
5
6
|
|
|
@@ -12,6 +13,7 @@ class StripeRefund < StripeModelCallbacks::ApplicationRecord
|
|
|
12
13
|
assign_attributes(
|
|
13
14
|
amount: Money.new(object.amount, object.currency),
|
|
14
15
|
stripe_charge_id: object.charge,
|
|
16
|
+
stripe_id: object.id,
|
|
15
17
|
created: Time.zone.at(object.created),
|
|
16
18
|
livemode: object.try(:livemode) == true,
|
|
17
19
|
metadata: JSON.generate(object.metadata)
|
|
@@ -20,7 +22,7 @@ class StripeRefund < StripeModelCallbacks::ApplicationRecord
|
|
|
20
22
|
StripeModelCallbacks::AttributesAssignerService.execute!(
|
|
21
23
|
model: self,
|
|
22
24
|
stripe_model: object,
|
|
23
|
-
attributes: %w[balance_transaction currency reason receipt_number status]
|
|
25
|
+
attributes: %w[balance_transaction currency payment_intent reason receipt_number status]
|
|
24
26
|
)
|
|
25
27
|
|
|
26
28
|
self.failure_reason = object.failure_reason if object.respond_to?(:failure_reason)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class StripeSetupIntent < StripeModelCallbacks::ApplicationRecord
|
|
2
|
+
belongs_to :stripe_customer, foreign_key: "customer", optional: true, primary_key: "stripe_id"
|
|
3
|
+
belongs_to :stripe_payment_method, foreign_key: "payment_method", optional: true, primary_key: "stripe_id"
|
|
4
|
+
|
|
5
|
+
def self.stripe_class
|
|
6
|
+
Stripe::SetupIntent
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def assign_from_stripe(object)
|
|
10
|
+
check_object_is_stripe_class(object)
|
|
11
|
+
|
|
12
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
|
13
|
+
model: self,
|
|
14
|
+
stripe_model: object,
|
|
15
|
+
attributes: %w[
|
|
16
|
+
application cancellation_reason client_secret created customer description flow_directions last_setup_error latest_attempt livemode mandate metadata
|
|
17
|
+
next_action on_behalf_of payment_method payment_method_options payment_method_types single_use_mandate status usage
|
|
18
|
+
]
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -22,7 +22,8 @@ class StripeSource < StripeModelCallbacks::ApplicationRecord
|
|
|
22
22
|
assign_ach_credit_transfer(object)
|
|
23
23
|
|
|
24
24
|
StripeModelCallbacks::AttributesAssignerService.execute!(
|
|
25
|
-
model: self,
|
|
25
|
+
model: self,
|
|
26
|
+
stripe_model: object,
|
|
26
27
|
attributes: %w[
|
|
27
28
|
client_secret created currency flow livemode metadata statement_descriptor
|
|
28
29
|
status usage
|
|
@@ -47,9 +47,8 @@ class StripeSubscription < StripeModelCallbacks::ApplicationRecord
|
|
|
47
47
|
raise "Cannot reactivate unless cancel at period end has been set" unless cancel_at_period_end?
|
|
48
48
|
|
|
49
49
|
# https://stripe.com/docs/subscriptions/canceling-pausing
|
|
50
|
-
items =
|
|
51
|
-
|
|
52
|
-
items << {
|
|
50
|
+
items = stripe_subscription_items.map do |item|
|
|
51
|
+
{
|
|
53
52
|
id: item.stripe_id,
|
|
54
53
|
plan: item.stripe_plan_id,
|
|
55
54
|
quantity: item.quantity
|
|
@@ -64,11 +63,30 @@ class StripeSubscription < StripeModelCallbacks::ApplicationRecord
|
|
|
64
63
|
end
|
|
65
64
|
|
|
66
65
|
def cancel!(args = {})
|
|
67
|
-
to_stripe.
|
|
66
|
+
if to_stripe.respond_to?(:cancel)
|
|
67
|
+
to_stripe.cancel(args)
|
|
68
|
+
elsif to_stripe.respond_to?(:delete)
|
|
69
|
+
to_stripe.delete(args)
|
|
70
|
+
else
|
|
71
|
+
raise "Couldn't figure out how to cancel the Stripe subscription"
|
|
72
|
+
end
|
|
73
|
+
|
|
68
74
|
reload_from_stripe!
|
|
69
75
|
self
|
|
70
76
|
end
|
|
71
77
|
|
|
78
|
+
def create_stripe_mock!
|
|
79
|
+
cancel_at_period_end = cancel_at_period_end?
|
|
80
|
+
|
|
81
|
+
mock_subscription = Stripe::Subscription.create(
|
|
82
|
+
customer: stripe_customer.stripe_id,
|
|
83
|
+
plan: stripe_plan.stripe_id
|
|
84
|
+
)
|
|
85
|
+
assign_from_stripe(mock_subscription)
|
|
86
|
+
save!
|
|
87
|
+
cancel!(at_period_end: true) if cancel_at_period_end
|
|
88
|
+
end
|
|
89
|
+
|
|
72
90
|
private
|
|
73
91
|
|
|
74
92
|
def assign_default_tax_rates(object)
|
|
@@ -77,6 +95,7 @@ private
|
|
|
77
95
|
found_ids = []
|
|
78
96
|
|
|
79
97
|
object.default_tax_rates.each do |default_tax_rate|
|
|
98
|
+
default_tax_rate = Stripe::TaxRate.retrieve(default_tax_rate) if default_tax_rate.is_a?(String)
|
|
80
99
|
tax_rate = StripeModelCallbacks::TaxRate::UpdatedService.execute!(object: default_tax_rate)
|
|
81
100
|
|
|
82
101
|
if new_record?
|
|
@@ -15,18 +15,28 @@ class StripeSubscriptionItem < StripeModelCallbacks::ApplicationRecord
|
|
|
15
15
|
self.stripe_plan_id = object.plan.id if object.try(:plan).respond_to?(:id)
|
|
16
16
|
|
|
17
17
|
assign_price_from_stripe(object)
|
|
18
|
+
assign_deleted_from_stripe(object)
|
|
18
19
|
|
|
19
20
|
StripeModelCallbacks::AttributesAssignerService.execute!(
|
|
20
21
|
model: self, stripe_model: object,
|
|
21
|
-
attributes: %w[id created metadata quantity]
|
|
22
|
+
attributes: %w[id created deleted metadata quantity]
|
|
22
23
|
)
|
|
23
24
|
end
|
|
24
25
|
|
|
26
|
+
def create_stripe_mock!
|
|
27
|
+
mock_item = Stripe::SubscriptionItem.create(
|
|
28
|
+
subscription: stripe_subscription.stripe_id,
|
|
29
|
+
plan: stripe_plan.stripe_id,
|
|
30
|
+
quantity:
|
|
31
|
+
)
|
|
32
|
+
assign_from_stripe(mock_item)
|
|
33
|
+
end
|
|
34
|
+
|
|
25
35
|
def update_quantity_on_stripe!(new_quantity)
|
|
26
36
|
sub_object = stripe_subscription.to_stripe
|
|
27
37
|
|
|
28
38
|
new_items = [{
|
|
29
|
-
id
|
|
39
|
+
id:,
|
|
30
40
|
plan: stripe_plan_id,
|
|
31
41
|
quantity: new_quantity
|
|
32
42
|
}]
|
|
@@ -51,4 +61,20 @@ private
|
|
|
51
61
|
# Set stripe ID on the subscription item
|
|
52
62
|
self.stripe_price_id = object.price.id
|
|
53
63
|
end
|
|
64
|
+
|
|
65
|
+
def assign_deleted_from_stripe(object)
|
|
66
|
+
return if stripe_attribute_missing?(object, "deleted")
|
|
67
|
+
|
|
68
|
+
self.deleted = object.deleted == true
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def stripe_attribute_missing?(object, attribute)
|
|
72
|
+
return false unless object.respond_to?(:to_hash)
|
|
73
|
+
|
|
74
|
+
values = object.instance_variable_get(:@values)
|
|
75
|
+
return !values.key?(attribute.to_sym) && !values.key?(attribute.to_s) if values.is_a?(Hash)
|
|
76
|
+
|
|
77
|
+
stripe_values = object.to_hash
|
|
78
|
+
!stripe_values.key?(attribute.to_sym) && !stripe_values.key?(attribute.to_s)
|
|
79
|
+
end
|
|
54
80
|
end
|
|
@@ -39,7 +39,7 @@ class StripeSubscriptionSchedule < StripeModelCallbacks::ApplicationRecord
|
|
|
39
39
|
assign_subscription_schedule_phases(object)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
-
def cancel_on_stripe
|
|
42
|
+
def cancel_on_stripe # rubocop:disable Naming/PredicateMethod
|
|
43
43
|
to_stripe.cancel
|
|
44
44
|
update!(canceled_at: Time.zone.now) if respond_to?(:canceled_at)
|
|
45
45
|
reload_from_stripe!
|
|
@@ -76,7 +76,7 @@ private
|
|
|
76
76
|
def assign_timestamps(object)
|
|
77
77
|
%i[canceled_at completed_at released_at].each do |timestamp|
|
|
78
78
|
object_timestamp = object.__send__(timestamp)
|
|
79
|
-
__send__("#{timestamp}=", Time.zone.at(object_timestamp)) if object_timestamp
|
|
79
|
+
__send__(:"#{timestamp}=", Time.zone.at(object_timestamp)) if object_timestamp
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
|
|
@@ -49,7 +49,7 @@ private
|
|
|
49
49
|
def assign_timestamps(object)
|
|
50
50
|
%i[end_date start_date trial_end].each do |timestamp|
|
|
51
51
|
object_timestamp = object.__send__(timestamp)
|
|
52
|
-
__send__("#{timestamp}=", Time.zone.at(object_timestamp)) if object_timestamp
|
|
52
|
+
__send__(:"#{timestamp}=", Time.zone.at(object_timestamp)) if object_timestamp
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
|
|
@@ -5,6 +5,12 @@ class StripeTaxRate < StripeModelCallbacks::ApplicationRecord
|
|
|
5
5
|
Stripe::TaxRate
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
+
def create_stripe_mock!
|
|
9
|
+
mock_tax_rate = Stripe::TaxRate.create(description:, display_name:, id: stripe_id, percentage:)
|
|
10
|
+
assign_from_stripe(mock_tax_rate)
|
|
11
|
+
save!
|
|
12
|
+
end
|
|
13
|
+
|
|
8
14
|
def assign_from_stripe(object)
|
|
9
15
|
check_object_is_stripe_class(object)
|
|
10
16
|
assign_attributes(
|