2025-05-01 16:47:14 -04:00
|
|
|
class Provider::Stripe::SubscriptionEventProcessor < Provider::Stripe::EventProcessor
|
|
|
|
Error = Class.new(StandardError)
|
|
|
|
|
|
|
|
def process
|
2025-05-06 14:05:21 -04:00
|
|
|
raise Error, "Family not found for Stripe customer ID: #{subscription.customer}" unless family
|
2025-05-01 16:47:14 -04:00
|
|
|
|
2025-05-06 14:05:21 -04:00
|
|
|
family.subscription.update(
|
|
|
|
stripe_id: subscription.id,
|
|
|
|
status: subscription.status,
|
|
|
|
interval: subscription_details.plan.interval,
|
|
|
|
amount: subscription_details.plan.amount / 100.0, # Stripe returns cents, we report dollars
|
|
|
|
currency: subscription_details.plan.currency.upcase,
|
|
|
|
current_period_ends_at: Time.at(subscription_details.current_period_end)
|
2025-05-01 16:47:14 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def family
|
2025-05-06 14:05:21 -04:00
|
|
|
Family.find_by(stripe_customer_id: subscription.customer)
|
2025-05-01 16:47:14 -04:00
|
|
|
end
|
|
|
|
|
2025-05-06 14:05:21 -04:00
|
|
|
def subscription_details
|
|
|
|
event_data.items.data.first
|
2025-05-01 16:47:14 -04:00
|
|
|
end
|
|
|
|
|
2025-05-06 14:05:21 -04:00
|
|
|
def subscription
|
|
|
|
event_data
|
2025-05-01 16:47:14 -04:00
|
|
|
end
|
|
|
|
end
|