Class: PurchaseKit::Pay::Webhooks::SubscriptionCreated

Inherits:
Base
  • Object
show all
Includes:
ActionView::RecordIdentifier, Turbo::Streams::ActionHelper
Defined in:
lib/purchasekit/pay/webhooks/subscription_created.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
30
31
32
33
34
35
36
# File 'lib/purchasekit/pay/webhooks/subscription_created.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"]
  )
  is_new = subscription.new_record?

  subscription.update!(
    name: event["subscription_name"] || ::Pay.default_product_name,
    processor_plan: event["store_product_id"],
    status: :active,
    quantity: 1,
    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"])
  )

  return unless is_new

  redirect_path = event["success_path"] || Rails.application.routes.url_helpers.root_path
  Turbo::StreamsChannel.broadcast_stream_to(
    dom_id(customer),
    content: turbo_stream_action_tag(:redirect, url: redirect_path)
  )
end