Class: Webhookdb::Subscription

Inherits:
Object
  • Object
show all
Extended by:
MethodUtilities
Includes:
Appydays::Configurable, Appydays::Loggable
Defined in:
lib/webhookdb/subscription.rb

Defined Under Namespace

Classes: Plan, Status

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodUtilities

attr_predicate, attr_predicate_accessor, singleton_attr_accessor, singleton_attr_reader, singleton_attr_writer, singleton_method_alias, singleton_predicate_accessor, singleton_predicate_reader

Constructor Details

#initializeSubscription

Returns a new instance of Subscription.



49
50
51
52
# File 'lib/webhookdb/subscription.rb', line 49

def initialize(*)
  super
  self[:stripe_json] ||= Sequel.pg_json({})
end

Class Method Details

.backfill_from_stripe(limit: 50, page_size: 50) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/webhookdb/subscription.rb', line 178

def self.backfill_from_stripe(limit: 50, page_size: 50)
  subs = Stripe::Subscription.list({limit: page_size})
  done = 0
  subs.auto_paging_each do |sub|
    self.create_or_update_from_stripe_hash(sub.as_json)
    done += 1
    break if !limit.nil? && done >= limit
  end
end

.billing_disabled?Boolean

Returns:

  • (Boolean)


34
# File 'lib/webhookdb/subscription.rb', line 34

def billing_disabled? = !self.billing_enabled?

.billing_enabled?Boolean

Returns:

  • (Boolean)


33
# File 'lib/webhookdb/subscription.rb', line 33

def billing_enabled? = self.billing_enabled

.create_or_update_from_id(id) ⇒ Object



118
119
120
121
# File 'lib/webhookdb/subscription.rb', line 118

def self.create_or_update_from_id(id)
  subscription_obj = Stripe::Subscription.retrieve(id)
  self.create_or_update_from_stripe_hash(subscription_obj.as_json)
end

.create_or_update_from_stripe_hash(obj) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/webhookdb/subscription.rb', line 62

def self.create_or_update_from_stripe_hash(obj)
  created = false
  orig_status = nil
  sub = self.update_or_create(stripe_id: obj.fetch("id")) do |o|
    o.stripe_customer_id = obj.fetch("customer")
    if o.new?
      created = true
    else
      orig_status = o.status
    end
    o.stripe_json = obj.to_json
  end
  common_fields = [
    {title: "Subscription", value: sub.id, short: true},
    {title: "Status", value: sub.status, short: true},
    {title: "Stripe ID", value: sub.stripe_id, short: true},
    {title: "Customer ID", value: sub.stripe_customer_id, short: true},
  ]
  if sub.organization.nil?
    Webhookdb::DeveloperAlert.new(
      subsystem: "Subscription Error",
      emoji: ":hook:",
      fallback: "Subscription with Stripe ID #{sub.stripe_id} has no organization",
      fields: common_fields + [
        {title: "Message", value: "Has no organization in WebhookDB", short: false},
      ],
    ).emit
  elsif created
    Webhookdb::DeveloperAlert.new(
      subsystem: "Subscription Created",
      emoji: ":hook:",
      fallback: "Subscription with Stripe ID #{sub.stripe_id} created",
      fields: common_fields + [
        {title: "Organization", value: sub.organization.display_string, short: true},
        {title: "Message", value: "Created", short: true},
      ],
    ).emit
    elsif orig_status != sub.status
      Webhookdb::DeveloperAlert.new(
        subsystem: "Subscription Status Change",
        emoji: ":hook:",
        fallback: "Subscription with Stripe ID #{sub.stripe_id} changed status",
        fields: common_fields + [
          {title: "Organization", value: sub.organization.display_string, short: true},
          {title: "Message", value: "Status updated", short: true},
        ],
      ).emit
  end
  return sub
end

.create_or_update_from_webhook(webhook_body) ⇒ Object



113
114
115
116
# File 'lib/webhookdb/subscription.rb', line 113

def self.create_or_update_from_webhook(webhook_body)
  obj = webhook_body["data"]["object"]
  self.create_or_update_from_stripe_hash(obj)
end

.list_plansObject



40
41
42
43
44
45
46
47
# File 'lib/webhookdb/subscription.rb', line 40

def self.list_plans
  return [] if self.billing_disabled?
  prices = Stripe::Price.list(active: true)
  monthly = prices.find { |pr| pr.recurring.interval == "month" }
  yearly = prices.find { |pr| pr.recurring.interval == "year" }
  raise "Expected month and year prices in: #{prices.to_json}" unless monthly && yearly
  return [Plan.new("monthly", monthly), Plan.new("yearly", yearly)]
end

.max_free_integrationsObject



35
# File 'lib/webhookdb/subscription.rb', line 35

def max_free_integrations = self.billing_enabled? ? self.free_integrations_with_billing : 9999

.status_for_org(org) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/webhookdb/subscription.rb', line 150

def self.status_for_org(org)
  service_integrations = org.service_integrations
  used = service_integrations.count
  data = {
    organization_name: org.name,
    organization_key: org.key,
    organization_formatted: org.display_string,
    billing_email: org.billing_email,
    integrations_used: used,
    integrations_used_formatted: used.to_s,
  }
  subscription = Webhookdb::Subscription[stripe_customer_id: org.stripe_customer_id]
  # TODO: Modify the Stripe JSON to store the values of the fields for paid plans,
  # rather than hard-coding them.
  if subscription.nil?
    data[:plan_name] = "Free"
    data[:integrations_remaining] = [0, Webhookdb::Subscription.max_free_integrations - used].max
    data[:integrations_remaining_formatted] = data[:integrations_remaining].to_s
    data[:sub_status] = ""
  else
    data[:plan_name] = subscription.plan_name
    data[:integrations_remaining] = 2_000_000_000
    data[:integrations_remaining_formatted] = "unlimited"
    data[:sub_status] = subscription.status
  end
  return Status.new(**data)
end

Instance Method Details

#plan_nameObject



58
59
60
# File 'lib/webhookdb/subscription.rb', line 58

def plan_name
  return self.stripe_json.dig("plan", "nickname") || ""
end

#statusObject



54
55
56
# File 'lib/webhookdb/subscription.rb', line 54

def status
  return self.stripe_json["status"]
end