Class: Billy::Account

Inherits:
ApplicationRecord show all
Defined in:
app/models/billy/account.rb

Instance Method Summary collapse

Instance Method Details

#deleted?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/billy/account.rb', line 64

def deleted?
  deleted_at.present?
end

#discord(kind:, desc: nil) ⇒ Object

Send a Discord event for this Account



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/billy/account.rb', line 89

def discord(kind:, desc: nil)
  return if Billy.discord_path.nil?

  case kind
  when :new_account
    Billy::DiscordJob.perform_now title: "πŸŽ‰  New Account", description: ["Account: #{owner.name}", desc].join("\n")
  when :payment_succeeded
    Billy::DiscordJob.perform_now title: "πŸ’Έ  Payment Succeeded", description: "Account: #{owner.name}\n#{desc}"
  when :subscription_created
    Billy::DiscordJob.perform_now title: "πŸ’°  Subscription Created", description: "Account: #{owner.name}\n#{desc}"
  when :subscription_cancelled
    Billy::DiscordJob.perform_now title: "🚫  Subscription Cancelled", description: "Account: #{owner.name}"
  when :subscription_paused
    Billy::DiscordJob.perform_now title: "⏸️  Subscription Paused", description: "Account: #{owner.name}"
  end
end

#free?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/models/billy/account.rb', line 20

def free?
  processor == "free"
end

#lemon_squeezy?Boolean

Lemon Squeezy accounts

Returns:

  • (Boolean)


35
36
37
# File 'app/models/billy/account.rb', line 35

def lemon_squeezy?
  processor == "lemon_squeezy"
end

#make_default!Object

Makes this the default Account



47
48
49
50
51
52
# File 'app/models/billy/account.rb', line 47

def make_default!
  return if default?

  owner.billy_accounts.update_all(default: false)
  update!(default: true)
end

#migrated?Boolean

Migrated accounts - from old payment processors

Returns:

  • (Boolean)


40
41
42
# File 'app/models/billy/account.rb', line 40

def migrated?
  processor == "migrated"
end

#paddle?Boolean

Paddle accounts - the main usage of this library

Returns:

  • (Boolean)


30
31
32
# File 'app/models/billy/account.rb', line 30

def paddle?
  processor == "paddle"
end

#passthroughObject

Generates a GlobalID for the Account owner



70
71
72
# File 'app/models/billy/account.rb', line 70

def passthrough
  owner.to_sgid.to_s
end

#subscribe(plan:, **options) ⇒ Object

Create a Subscription for this Account subscribe(plan: Billy::Plan.first, trial_ends_at: 14.days.from_now, ends_at: 14.days.from_now)



77
78
79
80
81
82
83
84
85
86
# File 'app/models/billy/account.rb', line 77

def subscribe(plan:, **options)
  attributes = options.merge(
    processor: processor,
    status: "active",
    plan: plan,
    default: true
  )
  
  subscriptions.create!(attributes)
end

#trial?Boolean

Trial accounts - e.g. 14 day trials

Returns:

  • (Boolean)


25
26
27
# File 'app/models/billy/account.rb', line 25

def trial?
  processor == "trial"
end