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.



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

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

Class Method Details

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



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

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)


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

def billing_disabled? = !self.billing_enabled?

.billing_enabled?Boolean

Returns:

  • (Boolean)


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

def billing_enabled? = self.billing_enabled

.create_or_update_from_id(id) ⇒ Object



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

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



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
112
# File 'lib/webhookdb/subscription.rb', line 63

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



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

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



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

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



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

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

.status_for_org(org) ⇒ Object



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
177
# File 'lib/webhookdb/subscription.rb', line 151

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



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

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

#statusObject



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

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