Class: PurchaseKit::Pay::Webhooks::SubscriptionUpdated

Inherits:
Base
  • Object
show all
Includes:
ActionView::RecordIdentifier, Turbo::Streams::ActionHelper
Defined in:
lib/purchasekit/pay/webhooks/subscription_updated.rb

Instance Method Summary collapse

Instance Method Details

#call(event) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/purchasekit/pay/webhooks/subscription_updated.rb', line 8

def call(event)
  customer = ::Pay::Customer.find(event["customer_id"])

  subscription = ::Pay::Purchasekit::Subscription.find_or_initialize_by(
    customer: customer,
    processor_id: event["subscription_id"]
  )
  subscription.name ||= event["subscription_name"] || ::Pay.default_product_name
  subscription.quantity ||= 1

  subscription.update!(
    processor_plan: event["store_product_id"],
    status: event["status"],
    current_period_start: parse_time(event["current_period_start"]),
    current_period_end: parse_time(event["current_period_end"]),
    trial_ends_at: parse_time(event["trial_ends_at"]),
    ends_at: parse_time(event["ends_at"]),
    data: (subscription.data || {}).merge("store" => event["store"])
  )

  broadcast_redirect(customer, event) if event["success_path"].present?
end