Subscription
extends Model
in package
Model for managing client subscription billing.
Tags
Table of Contents
Constants
- TIER_AMOUNTS = ['free' => 0.0, 'basic' => 29.0, 'premium' => 79.0, 'enterprise' => 149.0]
- TIER_ANNUAL_AMOUNTS = ['free' => 0.0, 'basic' => 290.0, 'premium' => 790.0, 'enterprise' => 1490.0]
Properties
- $db : PDO
- $fillable : array<string|int, mixed>
- $primaryKey : string
- $table : string
Methods
- __construct() : mixed
- Initialize model with database connection.
- all() : array<string|int, mixed>
- Get all records from the table.
- cancel() : bool
- Cancel a subscription.
- count() : int
- Count records matching optional conditions.
- countByStatus() : int
- Count subscriptions by status.
- create() : int
- Create a new record.
- delete() : bool
- Delete a record by primary key.
- find() : array<string|int, mixed>|null
- Find a record by primary key.
- findBy() : array<string|int, mixed>|null
- Find a record by a specific field value.
- getActive() : array<string|int, mixed>
- Get all active subscriptions.
- getAllWithBusiness() : array<string|int, mixed>
- Get all subscriptions with business info.
- getByStripeSubscriptionId() : array<string|int, mixed>|null
- Find a subscription by Stripe subscription ID.
- getDb() : PDO
- Get the underlying PDO connection.
- getForBusiness() : array<string|int, mixed>|null
- Get the subscription for a business.
- getPastDue() : array<string|int, mixed>
- Get all past-due subscriptions.
- getTierAmounts() : array<string|int, mixed>
- Get tier pricing amounts.
- paginate() : array<string|int, mixed>
- Paginate records matching conditions.
- query() : PDOStatement
- Execute a raw SQL query with parameter binding.
- update() : bool
- Update a record by primary key.
- updatePeriod() : bool
- Update the billing period dates.
- updateStripeIds() : bool
- Update Stripe IDs for a subscription.
- where() : array<string|int, mixed>
- Find records matching conditions.
- filterFillable() : array<string|int, mixed>
- Filter data to only include fillable fields.
Constants
TIER_AMOUNTS
public
array<string|int, mixed>
TIER_AMOUNTS
= ['free' => 0.0, 'basic' => 29.0, 'premium' => 79.0, 'enterprise' => 149.0]
Monthly tier pricing
TIER_ANNUAL_AMOUNTS
public
array<string|int, mixed>
TIER_ANNUAL_AMOUNTS
= ['free' => 0.0, 'basic' => 290.0, 'premium' => 790.0, 'enterprise' => 1490.0]
Annual tier pricing (2 months free = 10/12 discount)
Properties
$db
protected
PDO
$db
Database connection
$fillable
protected
array<string|int, mixed>
$fillable
= ['business_id', 'tier', 'billing_cycle', 'status', 'monthly_amount', 'stripe_customer_id', 'stripe_subscription_id', 'stripe_payment_method_id', 'auto_pay', 'current_period_start', 'current_period_end', 'trial_ends_at', 'cancelled_at']
Mass-assignable fields
$primaryKey
protected
string
$primaryKey
= 'id'
Primary key column
$table
protected
string
$table
= 'subscriptions'
Database table name
Methods
__construct()
Initialize model with database connection.
public
__construct() : mixed
all()
Get all records from the table.
public
all([string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction (ASC or DESC)
Return values
array<string|int, mixed>cancel()
Cancel a subscription.
public
cancel(int $id) : bool
Parameters
- $id : int
-
Subscription ID
Return values
boolcount()
Count records matching optional conditions.
public
count([array<string|int, mixed> $conditions = [] ]) : int
Parameters
- $conditions : array<string|int, mixed> = []
-
Associative array of field => value pairs
Return values
intcountByStatus()
Count subscriptions by status.
public
countByStatus(string $status) : int
Parameters
- $status : string
-
Subscription status
Return values
intcreate()
Create a new record.
public
create(array<string|int, mixed> $data) : int
Parameters
- $data : array<string|int, mixed>
-
Associative array of field => value pairs
Return values
int —The ID of the newly created record
delete()
Delete a record by primary key.
public
delete(int $id) : bool
Parameters
- $id : int
-
Primary key value
Return values
boolfind()
Find a record by primary key.
public
find(int $id) : array<string|int, mixed>|null
Parameters
- $id : int
-
Primary key value
Return values
array<string|int, mixed>|nullfindBy()
Find a record by a specific field value.
public
findBy(string $field, mixed $value) : array<string|int, mixed>|null
Parameters
- $field : string
-
Column name
- $value : mixed
-
Value to match
Return values
array<string|int, mixed>|nullgetActive()
Get all active subscriptions.
public
getActive() : array<string|int, mixed>
Return values
array<string|int, mixed>getAllWithBusiness()
Get all subscriptions with business info.
public
getAllWithBusiness() : array<string|int, mixed>
Return values
array<string|int, mixed>getByStripeSubscriptionId()
Find a subscription by Stripe subscription ID.
public
getByStripeSubscriptionId(string $stripeSubscriptionId) : array<string|int, mixed>|null
Parameters
- $stripeSubscriptionId : string
-
Stripe subscription ID
Return values
array<string|int, mixed>|nullgetDb()
Get the underlying PDO connection.
public
getDb() : PDO
Return values
PDOgetForBusiness()
Get the subscription for a business.
public
getForBusiness(int $businessId) : array<string|int, mixed>|null
Parameters
- $businessId : int
-
Business ID
Return values
array<string|int, mixed>|nullgetPastDue()
Get all past-due subscriptions.
public
getPastDue() : array<string|int, mixed>
Return values
array<string|int, mixed>getTierAmounts()
Get tier pricing amounts.
public
getTierAmounts() : array<string|int, mixed>
Return values
array<string|int, mixed>paginate()
Paginate records matching conditions.
public
paginate(array<string|int, mixed> $conditions, int $page, int $perPage[, string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $conditions : array<string|int, mixed>
-
Associative array of field => value pairs
- $page : int
-
Current page number (1-based)
- $perPage : int
-
Records per page
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction (ASC or DESC)
Return values
array<string|int, mixed> —Pagination result with data, total, page, per_page, total_pages
query()
Execute a raw SQL query with parameter binding.
public
query(string $sql[, array<string|int, mixed> $params = [] ]) : PDOStatement
Parameters
- $sql : string
-
SQL query string
- $params : array<string|int, mixed> = []
-
Bound parameters
Return values
PDOStatementupdate()
Update a record by primary key.
public
update(int $id, array<string|int, mixed> $data) : bool
Parameters
- $id : int
-
Primary key value
- $data : array<string|int, mixed>
-
Associative array of field => value pairs
Return values
boolupdatePeriod()
Update the billing period dates.
public
updatePeriod(int $id, string $start, string $end) : bool
Parameters
- $id : int
-
Subscription ID
- $start : string
-
Period start date (Y-m-d)
- $end : string
-
Period end date (Y-m-d)
Return values
boolupdateStripeIds()
Update Stripe IDs for a subscription.
public
updateStripeIds(int $id, string $customerId, string $subscriptionId, string $paymentMethod) : bool
Parameters
- $id : int
-
Subscription ID
- $customerId : string
-
Stripe customer ID
- $subscriptionId : string
-
Stripe subscription ID
- $paymentMethod : string
-
Stripe payment method ID
Return values
boolwhere()
Find records matching conditions.
public
where(array<string|int, mixed> $conditions[, string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $conditions : array<string|int, mixed>
-
Associative array of field => value pairs
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction
Return values
array<string|int, mixed>filterFillable()
Filter data to only include fillable fields.
protected
filterFillable(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
-
Input data
Return values
array<string|int, mixed> —Filtered data