stripe_model_callbacks 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +153 -0
- data/Rakefile +26 -0
- data/app/assets/config/stripe_model_callbacks_manifest.js +2 -0
- data/app/assets/javascripts/stripe_model_callbacks/application.js +13 -0
- data/app/assets/stylesheets/stripe_model_callbacks/application.css +15 -0
- data/app/controllers/stripe_model_callbacks/application_controller.rb +3 -0
- data/app/jobs/stripe_model_callbacks/application_job.rb +2 -0
- data/app/mailers/stripe_model_callbacks/application_mailer.rb +4 -0
- data/app/models/stripe_model_callbacks/application_record.rb +60 -0
- data/app/services/stripe_model_callbacks/account/external_account/updated_service.rb +13 -0
- data/app/services/stripe_model_callbacks/attributes_assigner_service.rb +35 -0
- data/app/services/stripe_model_callbacks/base_event_service.rb +9 -0
- data/app/services/stripe_model_callbacks/base_service.rb +26 -0
- data/app/services/stripe_model_callbacks/charge/dispute_updated_service.rb +29 -0
- data/app/services/stripe_model_callbacks/charge/updated_service.rb +40 -0
- data/app/services/stripe_model_callbacks/configure_service.rb +217 -0
- data/app/services/stripe_model_callbacks/coupon/updated_service.rb +18 -0
- data/app/services/stripe_model_callbacks/customer/bank_account/deleted_service.rb +13 -0
- data/app/services/stripe_model_callbacks/customer/deleted_service.rb +13 -0
- data/app/services/stripe_model_callbacks/customer/discount_updated_service.rb +32 -0
- data/app/services/stripe_model_callbacks/customer/source_updated_service.rb +25 -0
- data/app/services/stripe_model_callbacks/customer/subscription/updated_service.rb +28 -0
- data/app/services/stripe_model_callbacks/customer/updated_service.rb +13 -0
- data/app/services/stripe_model_callbacks/event_mocker_service.rb +38 -0
- data/app/services/stripe_model_callbacks/invoice/updated_service.rb +26 -0
- data/app/services/stripe_model_callbacks/invoice_item/updated_service.rb +13 -0
- data/app/services/stripe_model_callbacks/notifier_service.rb +6 -0
- data/app/services/stripe_model_callbacks/order/updated_service.rb +28 -0
- data/app/services/stripe_model_callbacks/payout/updated_service.rb +29 -0
- data/app/services/stripe_model_callbacks/plan/updated_service.rb +14 -0
- data/app/services/stripe_model_callbacks/product/updated_service.rb +14 -0
- data/app/services/stripe_model_callbacks/recipient/updated_service.rb +14 -0
- data/app/services/stripe_model_callbacks/refund/updated_service.rb +12 -0
- data/app/services/stripe_model_callbacks/review/updated_service.rb +13 -0
- data/app/services/stripe_model_callbacks/sku/updated_service.rb +14 -0
- data/app/services/stripe_model_callbacks/source/updated_service.rb +31 -0
- data/app/services/stripe_model_callbacks/subscription/state_checker_service.rb +21 -0
- data/app/services/stripe_model_callbacks/transfer/updated_service.rb +18 -0
- data/app/views/layouts/stripe_model_callbacks/application.html.erb +14 -0
- data/config/rails_best_practices.yml +49 -0
- data/config/routes.rb +2 -0
- data/db/migrate/20180206115801_create_stripe_charges.rb +54 -0
- data/db/migrate/20180206115802_create_stripe_coupons.rb +22 -0
- data/db/migrate/20180206115803_create_stripe_customers.rb +21 -0
- data/db/migrate/20180206115804_create_stripe_discounts.rb +27 -0
- data/db/migrate/20180206115805_create_stripe_invoice_items.rb +26 -0
- data/db/migrate/20180206115806_create_stripe_invoices.rb +57 -0
- data/db/migrate/20180206115807_create_stripe_order_items.rb +15 -0
- data/db/migrate/20180206115808_create_stripe_orders.rb +46 -0
- data/db/migrate/20180206115809_create_stripe_payouts.rb +27 -0
- data/db/migrate/20180206115810_create_stripe_plans.rb +20 -0
- data/db/migrate/20180206115811_create_stripe_products.rb +25 -0
- data/db/migrate/20180206115812_create_stripe_recipients.rb +18 -0
- data/db/migrate/20180206115813_create_stripe_refunds.rb +21 -0
- data/db/migrate/20180206115814_create_stripe_skus.rb +22 -0
- data/db/migrate/20180206115815_create_stripe_sources.rb +70 -0
- data/db/migrate/20180206115816_create_stripe_subscriptions.rb +29 -0
- data/db/migrate/20180206115817_create_stripe_transfers.rb +25 -0
- data/db/migrate/20180206130408_create_disputes.rb +76 -0
- data/db/migrate/20180206151132_change_activities_trackable_id_to_string.rb +5 -0
- data/db/migrate/20180207121808_create_stripe_subscription_items.rb +14 -0
- data/db/migrate/20180207171559_remove_name_from_stripe_plans.rb +5 -0
- data/db/migrate/20180207172011_add_stripe_product_id_to_stripe_plans.rb +6 -0
- data/db/migrate/20180207185604_add_index_on_refund_charge.rb +5 -0
- data/db/migrate/20180208091647_change_stripe_subscription_items_quantity_to_integer.rb +5 -0
- data/db/migrate/20180208102513_add_attempt_count_to_stripe_invoices.rb +7 -0
- data/db/migrate/20180208121235_create_stripe_reviews.rb +13 -0
- data/db/migrate/20180208122654_create_stripe_bank_accounts.rb +20 -0
- data/db/migrate/20180208160046_add_nickname_to_stripe_plans.rb +5 -0
- data/db/migrate/20180211092437_rename_stripe_invoice_items_subscription_item.rb +6 -0
- data/db/migrate/20180216224335_rename_stripe_subscriptions_discount.rb +5 -0
- data/db/migrate/20180217200350_change_stripe_invoices_to_reference_discounts.rb +9 -0
- data/db/migrate/20180217204628_create_stripe_cards.rb +29 -0
- data/db/migrate/20180218092600_add_deleted_at_to_stripe_cards.rb +9 -0
- data/db/migrate/20181219121712_add_new_primary_id_and_rename_old.rb +37 -0
- data/db/migrate/20190629112050_rename_stripe_invoice_date_to_created.rb +5 -0
- data/lib/stripe_model_callbacks.rb +19 -0
- data/lib/stripe_model_callbacks/autoload_models.rb +8 -0
- data/lib/stripe_model_callbacks/configuration.rb +23 -0
- data/lib/stripe_model_callbacks/engine.rb +5 -0
- data/lib/stripe_model_callbacks/event_mocker.rb +5 -0
- data/lib/stripe_model_callbacks/factory_bot_definitions.rb +7 -0
- data/lib/stripe_model_callbacks/models/stripe_bank_account.rb +17 -0
- data/lib/stripe_model_callbacks/models/stripe_card.rb +25 -0
- data/lib/stripe_model_callbacks/models/stripe_charge.rb +47 -0
- data/lib/stripe_model_callbacks/models/stripe_coupon.rb +24 -0
- data/lib/stripe_model_callbacks/models/stripe_customer.rb +23 -0
- data/lib/stripe_model_callbacks/models/stripe_discount.rb +53 -0
- data/lib/stripe_model_callbacks/models/stripe_dispute.rb +86 -0
- data/lib/stripe_model_callbacks/models/stripe_invoice.rb +88 -0
- data/lib/stripe_model_callbacks/models/stripe_invoice_item.rb +33 -0
- data/lib/stripe_model_callbacks/models/stripe_order.rb +63 -0
- data/lib/stripe_model_callbacks/models/stripe_order_item.rb +20 -0
- data/lib/stripe_model_callbacks/models/stripe_payout.rb +26 -0
- data/lib/stripe_model_callbacks/models/stripe_plan.rb +32 -0
- data/lib/stripe_model_callbacks/models/stripe_product.rb +27 -0
- data/lib/stripe_model_callbacks/models/stripe_recipient.rb +16 -0
- data/lib/stripe_model_callbacks/models/stripe_refund.rb +28 -0
- data/lib/stripe_model_callbacks/models/stripe_review.rb +18 -0
- data/lib/stripe_model_callbacks/models/stripe_sku.rb +28 -0
- data/lib/stripe_model_callbacks/models/stripe_source.rb +79 -0
- data/lib/stripe_model_callbacks/models/stripe_subscription.rb +101 -0
- data/lib/stripe_model_callbacks/models/stripe_subscription_item.rb +36 -0
- data/lib/stripe_model_callbacks/models/stripe_transfer.rb +25 -0
- data/lib/stripe_model_callbacks/require_models.rb +7 -0
- data/lib/stripe_model_callbacks/version.rb +3 -0
- data/lib/tasks/stripe_model_callbacks_tasks.rake +4 -0
- metadata +249 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
class StripeCoupon < StripeModelCallbacks::ApplicationRecord
|
2
|
+
has_many :stripe_discounts, primary_key: "stripe_id"
|
3
|
+
|
4
|
+
monetize :amount_off_cents, allow_nil: true
|
5
|
+
|
6
|
+
def self.stripe_class
|
7
|
+
Stripe::Coupon
|
8
|
+
end
|
9
|
+
|
10
|
+
def assign_from_stripe(object)
|
11
|
+
assign_attributes(
|
12
|
+
amount_off: object.amount_off ? Money.new(object.amount_off, object.currency) : nil,
|
13
|
+
stripe_valid: object.valid
|
14
|
+
)
|
15
|
+
|
16
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
17
|
+
model: self, stripe_model: object,
|
18
|
+
attributes: %w[
|
19
|
+
currency created duration duration_in_months id livemode max_redemptions metadata
|
20
|
+
percent_off redeem_by times_redeemed
|
21
|
+
]
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class StripeCustomer < StripeModelCallbacks::ApplicationRecord
|
2
|
+
has_many :stripe_cards, primary_key: "stripe_id"
|
3
|
+
has_many :stripe_charges, primary_key: "stripe_id"
|
4
|
+
has_many :stripe_discounts, primary_key: "stripe_id"
|
5
|
+
has_many :stripe_invoices, primary_key: "stripe_id"
|
6
|
+
has_many :stripe_invoice_items, primary_key: "stripe_id"
|
7
|
+
has_many :stripe_orders, primary_key: "stripe_id"
|
8
|
+
has_many :stripe_subscriptions, primary_key: "stripe_id"
|
9
|
+
|
10
|
+
def self.stripe_class
|
11
|
+
Stripe::Customer
|
12
|
+
end
|
13
|
+
|
14
|
+
def assign_from_stripe(object)
|
15
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
16
|
+
model: self, stripe_model: object,
|
17
|
+
attributes: %w[
|
18
|
+
account_balance currency created delinquent description discount email id
|
19
|
+
livemode metadata
|
20
|
+
]
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class StripeDiscount < StripeModelCallbacks::ApplicationRecord
|
2
|
+
belongs_to :stripe_coupon, optional: true, primary_key: "stripe_id"
|
3
|
+
belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
|
4
|
+
belongs_to :stripe_discount, optional: true, primary_key: "stripe_id"
|
5
|
+
belongs_to :stripe_subscription, optional: true, primary_key: "stripe_id"
|
6
|
+
|
7
|
+
has_many :stripe_invoices, primary_key: "stripe_id"
|
8
|
+
has_many :stripe_subscriptions, primary_key: "stripe_id"
|
9
|
+
|
10
|
+
monetize :coupon_amount_off_cents, allow_nil: true
|
11
|
+
|
12
|
+
def self.stripe_class
|
13
|
+
Stripe::Discount
|
14
|
+
end
|
15
|
+
|
16
|
+
def assign_from_stripe(object)
|
17
|
+
assign_attributes(
|
18
|
+
created: object.respond_to?(:created) ? Time.zone.at(object.created) : nil,
|
19
|
+
start: Time.zone.at(object.start),
|
20
|
+
end: object.end ? Time.zone.at(object.end) : nil,
|
21
|
+
stripe_coupon_id: object.coupon.id,
|
22
|
+
stripe_customer_id: object.customer,
|
23
|
+
stripe_subscription_id: object.subscription
|
24
|
+
)
|
25
|
+
|
26
|
+
assign_coupon_attributes(object)
|
27
|
+
assign_other_coupon_attributes(object)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def assign_coupon_attributes(object)
|
33
|
+
assign_attributes(
|
34
|
+
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),
|
36
|
+
coupon_currency: object.coupon.currency,
|
37
|
+
coupon_duration: object.coupon.duration,
|
38
|
+
coupon_duration_in_months: object.coupon.duration_in_months,
|
39
|
+
coupon_livemode: object.coupon.livemode
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def assign_other_coupon_attributes(object)
|
44
|
+
assign_attributes(
|
45
|
+
coupon_max_redemptions: object.coupon.max_redemptions,
|
46
|
+
coupon_metadata: JSON.generate(object.coupon.try(:metadata)),
|
47
|
+
coupon_percent_off: object.coupon.percent_off,
|
48
|
+
coupon_redeem_by: object.coupon.redeem_by ? Time.zone.at(object.coupon.redeem_by) : nil,
|
49
|
+
coupon_times_redeemed: object.coupon.times_redeemed,
|
50
|
+
coupon_valid: object.coupon.valid
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
class StripeDispute < StripeModelCallbacks::ApplicationRecord
|
2
|
+
monetize :amount_cents
|
3
|
+
|
4
|
+
def self.stripe_class
|
5
|
+
Stripe::Dispute
|
6
|
+
end
|
7
|
+
|
8
|
+
def assign_from_stripe(object)
|
9
|
+
assign_attributes(
|
10
|
+
amount: Money.new(object.amount, object.currency),
|
11
|
+
balance_transaction_id: object.balance_transaction,
|
12
|
+
stripe_charge_id: object.charge,
|
13
|
+
created: Time.zone.at(object.created),
|
14
|
+
metadata: JSON.generate(object.metadata)
|
15
|
+
)
|
16
|
+
|
17
|
+
assign_evidence_attributes(object)
|
18
|
+
assign_evidence_cancellation_duplicate_attributes(object)
|
19
|
+
assign_evidence_customer_attributes(object)
|
20
|
+
assign_evidence_shipping_attributes(object)
|
21
|
+
assign_evidence_details_attributes(object)
|
22
|
+
|
23
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
24
|
+
model: self, stripe_model: object,
|
25
|
+
attributes: %w[currency livemode reason status is_charge_refundable]
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def assign_evidence_attributes(object)
|
32
|
+
assign_attributes(
|
33
|
+
evidence_access_activity_log: object.evidence.access_activity_log,
|
34
|
+
evidence_billing_address: object.evidence.billing_address,
|
35
|
+
evidence_product_description: object.evidence.product_description,
|
36
|
+
evidence_receipt: object.evidence.receipt,
|
37
|
+
evidence_refund_policy: object.evidence.refund_policy,
|
38
|
+
evidence_refund_policy_disclosure: object.evidence.refund_policy_disclosure,
|
39
|
+
evidence_refund_refusal_explanation: object.evidence.refund_refusal_explanation,
|
40
|
+
evidence_service_date: object.evidence.service_date,
|
41
|
+
evidence_service_documentation: object.evidence.service_documentation,
|
42
|
+
evidence_uncategorized_file: object.evidence.uncategorized_file,
|
43
|
+
evidence_uncategorized_text: object.evidence.uncategorized_text
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
def assign_evidence_cancellation_duplicate_attributes(object)
|
48
|
+
assign_attributes(
|
49
|
+
evidence_cancellation_policy: object.evidence.cancellation_policy,
|
50
|
+
evidence_cancellation_policy_disclosure: object.evidence.cancellation_policy_disclosure,
|
51
|
+
evidence_cancellation_rebuttal: object.evidence.cancellation_rebuttal,
|
52
|
+
evidence_duplicate_charge_documentation: object.evidence.duplicate_charge_documentation,
|
53
|
+
evidence_duplicate_charge_explanation: object.evidence.duplicate_charge_explanation,
|
54
|
+
evidence_duplicate_charge_id: object.evidence.duplicate_charge_id
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
def assign_evidence_customer_attributes(object)
|
59
|
+
assign_attributes(
|
60
|
+
evidence_customer_communication: object.evidence.customer_communication,
|
61
|
+
evidence_customer_email_address: object.evidence.customer_email_address,
|
62
|
+
evidence_customer_name: object.evidence.customer_name,
|
63
|
+
evidence_customer_purchase_ip: object.evidence.customer_purchase_ip,
|
64
|
+
evidence_customer_signature: object.evidence.customer_signature
|
65
|
+
)
|
66
|
+
end
|
67
|
+
|
68
|
+
def assign_evidence_shipping_attributes(object)
|
69
|
+
assign_attributes(
|
70
|
+
evidence_shipping_address: object.evidence.shipping_address,
|
71
|
+
evidence_shipping_carrier: object.evidence.shipping_carrier,
|
72
|
+
evidence_shipping_date: object.evidence.shipping_date,
|
73
|
+
evidence_shipping_documentation: object.evidence.shipping_documentation,
|
74
|
+
evidence_shipping_tracking_number: object.evidence.shipping_tracking_number
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
def assign_evidence_details_attributes(object)
|
79
|
+
assign_attributes(
|
80
|
+
evidence_details_due_by: Time.zone.at(object.evidence_details.due_by),
|
81
|
+
evidence_details_has_evidence: object.evidence_details.has_evidence,
|
82
|
+
evidence_details_past_due: object.evidence_details.past_due,
|
83
|
+
evidence_details_submission_count: object.evidence_details.submission_count
|
84
|
+
)
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
class StripeInvoice < StripeModelCallbacks::ApplicationRecord
|
2
|
+
belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
|
3
|
+
belongs_to :stripe_discount, optional: true
|
4
|
+
belongs_to :stripe_subscription, optional: true, primary_key: "stripe_id"
|
5
|
+
|
6
|
+
has_many :stripe_invoice_items, autosave: true, primary_key: "stripe_id"
|
7
|
+
|
8
|
+
monetize :amount_due_cents, allow_nil: true
|
9
|
+
monetize :application_fee_cents, allow_nil: true
|
10
|
+
monetize :subtotal_cents, allow_nil: true
|
11
|
+
monetize :tax_cents, allow_nil: true
|
12
|
+
monetize :total_cents, allow_nil: true
|
13
|
+
|
14
|
+
def self.stripe_class
|
15
|
+
Stripe::Invoice
|
16
|
+
end
|
17
|
+
|
18
|
+
def assign_from_stripe(object)
|
19
|
+
assign_attributes(
|
20
|
+
stripe_charge_id: object.charge,
|
21
|
+
stripe_customer_id: object.customer,
|
22
|
+
stripe_subscription_id: object.subscription,
|
23
|
+
due_date: object.due_date ? Time.zone.at(object.due_date) : nil,
|
24
|
+
period_start: Time.zone.at(object.period_start),
|
25
|
+
period_end: Time.zone.at(object.period_end)
|
26
|
+
)
|
27
|
+
|
28
|
+
assign_created(object)
|
29
|
+
assign_amounts(object)
|
30
|
+
|
31
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
32
|
+
model: self, stripe_model: object,
|
33
|
+
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
|
37
|
+
]
|
38
|
+
)
|
39
|
+
|
40
|
+
assign_invoice_items(object)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def assign_amounts(object)
|
46
|
+
assign_attributes(
|
47
|
+
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),
|
50
|
+
subtotal: Money.new(object.subtotal, object.currency),
|
51
|
+
tax: object.tax ? Money.new(object.tax, object.currency) : nil,
|
52
|
+
total: object.total ? Money.new(object.total, object.currency) : nil
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
def assign_created(object)
|
57
|
+
# The date-field was renamed to created on 2019-03-14
|
58
|
+
if object.respond_to?(:date)
|
59
|
+
self.created = Time.zone.at(object.date)
|
60
|
+
else
|
61
|
+
self.created = Time.zone.at(object.created)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def assign_invoice_items(object)
|
66
|
+
object.lines.each do |item|
|
67
|
+
# Has to be found this way to actually update the values
|
68
|
+
invoice_item = stripe_invoice_items.find do |invoice_item_i|
|
69
|
+
invoice_item_i.stripe_id == item.id &&
|
70
|
+
invoice_item_i.stripe_subscription_item_id == item.subscription_item &&
|
71
|
+
invoice_item_i.stripe_plan_id == item.plan&.id
|
72
|
+
end
|
73
|
+
|
74
|
+
invoice_item ||= stripe_invoice_items.build
|
75
|
+
invoice_item.stripe_invoice_id = object.id
|
76
|
+
invoice_item&.assign_from_stripe(item)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def stripe_discount_id_from_object(object)
|
81
|
+
return nil unless object.discount
|
82
|
+
|
83
|
+
response = StripeModelCallbacks::Customer::DiscountUpdatedService.execute(object: object.discount)
|
84
|
+
raise response.errors.join(". ") if response.errors.any?
|
85
|
+
|
86
|
+
response.result.id
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class StripeInvoiceItem < StripeModelCallbacks::ApplicationRecord
|
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_subscription_item, optional: true, primary_key: "stripe_id"
|
5
|
+
belongs_to :stripe_plan, optional: true, primary_key: "stripe_id"
|
6
|
+
|
7
|
+
monetize :amount_cents
|
8
|
+
|
9
|
+
def self.stripe_class
|
10
|
+
Stripe::InvoiceItem
|
11
|
+
end
|
12
|
+
|
13
|
+
def assign_from_stripe(object)
|
14
|
+
assign_attributes(
|
15
|
+
amount: Money.new(object.amount, object.currency),
|
16
|
+
stripe_customer_id: object.try(:customer),
|
17
|
+
metadata: JSON.generate(object.metadata),
|
18
|
+
period_start: Time.zone.at(object.period.start),
|
19
|
+
period_end: Time.zone.at(object.period.end),
|
20
|
+
stripe_plan_id: object.plan&.id,
|
21
|
+
stripe_subscription_id: object.subscription
|
22
|
+
)
|
23
|
+
|
24
|
+
self.stripe_subscription_item_id = object.subscription_item if object.respond_to?(:subscription_item)
|
25
|
+
|
26
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
27
|
+
model: self, stripe_model: object,
|
28
|
+
attributes: %w[
|
29
|
+
currency description discountable id livemode proration quantity
|
30
|
+
]
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class StripeOrder < StripeModelCallbacks::ApplicationRecord
|
2
|
+
belongs_to :stripe_charge, optional: true, primary_key: "stripe_id"
|
3
|
+
belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
|
4
|
+
has_many :stripe_order_items, primary_key: "stripe_id"
|
5
|
+
|
6
|
+
monetize :amount_cents
|
7
|
+
monetize :amount_returned_cents, allow_nil: true
|
8
|
+
monetize :application_cents, allow_nil: true
|
9
|
+
|
10
|
+
def self.stripe_class
|
11
|
+
Stripe::Order
|
12
|
+
end
|
13
|
+
|
14
|
+
def assign_from_stripe(object)
|
15
|
+
assign_attributes(
|
16
|
+
stripe_charge_id: object.charge,
|
17
|
+
created: Time.zone.at(object.created),
|
18
|
+
currency: object.currency,
|
19
|
+
stripe_customer_id: object.customer,
|
20
|
+
email: object.email,
|
21
|
+
livemode: object.livemode,
|
22
|
+
metadata: JSON.generate(object.metadata),
|
23
|
+
selected_shipping_method: object.selected_shipping_method,
|
24
|
+
status: object.status,
|
25
|
+
updated: Time.zone.at(object.updated)
|
26
|
+
)
|
27
|
+
|
28
|
+
assign_amounts_from_stripe(object)
|
29
|
+
assign_shipping_address_from_stripe(object)
|
30
|
+
assign_shipping_from_stripe(object)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def assign_amounts_from_stripe(object)
|
36
|
+
assign_attributes(
|
37
|
+
amount: Money.new(object.amount, object.currency),
|
38
|
+
application: object.application ? Money.new(object.application, object.currency) : nil,
|
39
|
+
application_fee: object.application_fee
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def assign_shipping_address_from_stripe(object)
|
44
|
+
assign_attributes(
|
45
|
+
shipping_address_city: object.shipping.address.city,
|
46
|
+
shipping_address_country: object.shipping.address.country,
|
47
|
+
shipping_address_line1: object.shipping.address.line1,
|
48
|
+
shipping_address_line2: object.shipping.address.line2,
|
49
|
+
shipping_address_postal_code: object.shipping.address.postal_code,
|
50
|
+
shipping_address_state: object.shipping.address.state
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
def assign_shipping_from_stripe(object)
|
55
|
+
assign_attributes(
|
56
|
+
shipping_carrier: object.shipping.carrier,
|
57
|
+
shipping_name: object.shipping.name,
|
58
|
+
shipping_phone: object.shipping.phone,
|
59
|
+
shipping_tracking_number: object.shipping.tracking_number,
|
60
|
+
shipping_methods: object.shipping_methods
|
61
|
+
)
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class StripeOrderItem < StripeModelCallbacks::ApplicationRecord
|
2
|
+
belongs_to :stripe_order, optional: true, primary_key: "stripe_id"
|
3
|
+
|
4
|
+
monetize :amount_cents
|
5
|
+
|
6
|
+
def self.stripe_class
|
7
|
+
Stripe::OrderItem
|
8
|
+
end
|
9
|
+
|
10
|
+
def assign_from_stripe(object)
|
11
|
+
assign_attributes(
|
12
|
+
amount: Money.new(object.amount, object.currency),
|
13
|
+
currency: object.currency,
|
14
|
+
description: object.description,
|
15
|
+
parent_id: object.parent,
|
16
|
+
quantity: object.quantity,
|
17
|
+
order_item_type: object.type
|
18
|
+
)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class StripePayout < StripeModelCallbacks::ApplicationRecord
|
2
|
+
monetize :amount_cents, allow_nil: true
|
3
|
+
|
4
|
+
def self.stripe_class
|
5
|
+
Stripe::Subscription
|
6
|
+
end
|
7
|
+
|
8
|
+
def assign_from_stripe(object)
|
9
|
+
assign_attributes(
|
10
|
+
amount: Money.new(object.amount, object.currency),
|
11
|
+
arrival_date: Time.zone.at(object.arrival_date),
|
12
|
+
created: Time.zone.at(object.created),
|
13
|
+
metadata: JSON.generate(object.metadata),
|
14
|
+
stripe_method: object.method,
|
15
|
+
stripe_type: object.type
|
16
|
+
)
|
17
|
+
|
18
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
19
|
+
model: self, stripe_model: object,
|
20
|
+
attributes: %w[
|
21
|
+
automatic balance_transaction currency description destination failure_balance_transaction
|
22
|
+
failure_code failure_message livemode source_type statement_descriptor status
|
23
|
+
]
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class StripePlan < StripeModelCallbacks::ApplicationRecord
|
2
|
+
belongs_to :stripe_product, optional: true, primary_key: "stripe_id"
|
3
|
+
|
4
|
+
has_many :stripe_invoice_items, primary_key: "stripe_id"
|
5
|
+
has_many :stripe_subscriptions, primary_key: "stripe_id"
|
6
|
+
has_many :stripe_subscription_items, primary_key: "stripe_id"
|
7
|
+
|
8
|
+
scope :not_deleted, -> { where(deleted_at: nil) }
|
9
|
+
|
10
|
+
monetize :amount_cents
|
11
|
+
|
12
|
+
def self.stripe_class
|
13
|
+
Stripe::Plan
|
14
|
+
end
|
15
|
+
|
16
|
+
def assign_from_stripe(object)
|
17
|
+
assign_attributes(amount: Money.new(object.amount, object.currency))
|
18
|
+
self.stripe_product_id = object.product if object.respond_to?(:product)
|
19
|
+
|
20
|
+
StripeModelCallbacks::AttributesAssignerService.execute!(
|
21
|
+
model: self, stripe_model: object,
|
22
|
+
attributes: %w[
|
23
|
+
created currency id interval interval_count livemode metadata nickname statement_descriptor
|
24
|
+
trial_period_days
|
25
|
+
]
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
def name
|
30
|
+
nickname.presence || stripe_product&.name
|
31
|
+
end
|
32
|
+
end
|