Class: Pay::Paddle::Webhooks::SubscriptionCreated

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/paddle/webhooks/subscription_created.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ SubscriptionCreated

Returns a new instance of SubscriptionCreated.



5
6
7
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
37
38
39
40
41
42
43
44
45
46
# File 'lib/pay/paddle/webhooks/subscription_created.rb', line 5

def initialize(data)
  # We may already have the subscription in the database, so we can update that record
  subscription = Pay.subscription_model.find_by(processor: :paddle, processor_id: data["subscription_id"])

  # Create the subscription in the database if we don't have it already
  if subscription.nil?

    # The customer could already be in the database
    owner = Pay.find_billable(processor: :paddle, processor_id: data["user_id"])

    if owner.nil?
      owner = owner_by_passtrough(data["passthrough"], data["subscription_plan_id"])
      owner&.update!(processor: "paddle", processor_id: data["user_id"])
    end

    if owner.nil?
      Rails.logger.error("[Pay] Unable to find Pay::Billable with owner: '#{data["passthrough"]}'. Searched these models: #{Pay.billable_models.join(", ")}")
      return
    end

    subscription = Pay.subscription_model.new(owner: owner, name: Pay.default_product_name, processor: "paddle", processor_id: data["subscription_id"], status: :active)
  end

  subscription.quantity = data["quantity"]
  subscription.processor_plan = data["subscription_plan_id"]
  subscription.paddle_update_url = data["update_url"]
  subscription.paddle_cancel_url = data["cancel_url"]
  subscription.trial_ends_at = Time.zone.parse(data["next_bill_date"]) if data["status"] == "trialing"

  # If user was on trial, their subscription ends at the end of the trial
  subscription.ends_at = if ["paused", "deleted"].include?(data["status"]) && subscription.on_trial?
    subscription.trial_ends_at

  # User wasn't on trial, so subscription ends at period end
  elsif ["paused", "deleted"].include?(data["status"])
    Time.zone.parse(data["next_bill_date"])

    # Subscription isn't marked to cancel at period end
  end

  subscription.save!
end