Phase 5 billing: account-based annual sub + tag SKUs + SMS pool + tag lifecycle

- Schema: plans, orders subscription cols + plan_id + period_started_at,
  billing_events log, users.stripe_id + sms_credits, products.price_aud
  (db/schema.sql + Laravel migration, idempotent)
- Laravel/Cashier: Billable User, checkout (annual sub + one-off SKUs with
  Managed Payments tax_code), webhook controller (signature-verified,
  idempotent, BILLING_ENABLED kill-switch), account tag transitions,
  nightly reconcile, Stripe portal link, PlanResource + billing dashboard
- Go frontend: account-level gating (paid sub required), SMS pool
  (included 50/yr + credits, drawn after included), plan caps replace
  constants, 60s plan cache (credits fresh), 25-tag cap (plan max_tags)
- BillingSeeder: personal plan + 3 SKUs + dev paid orders
- Verified test-mode e2e: subscribe/paid/active/alerts, pool exhaust +
  credits resume, lapsed/suspended, cancelled/closed, recover/active,
  webhook idempotency, 25-cap, one-off SKUs, replacement, kill-switch,
  invalid signature 400
This commit is contained in:
2026-08-28 13:19:32 +10:00
parent 43ce4514c4
commit aabaa750f4
43 changed files with 2679 additions and 341 deletions

View File

@@ -0,0 +1,104 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Phase 5 billing schema — mirrors db/schema.sql (canonical shared schema).
* Applied to the shared Postgres via: php artisan migrate --path=database/migrations/<this file>
* Kept idempotent-friendly (fresh or existing DB).
*/
return new class extends Migration
{
public function up(): void
{
// Plans table (plan_type reserves the future business model).
if (! Schema::hasTable('plans')) {
Schema::create('plans', function (Blueprint $table) {
$table->id();
$table->string('plan_type')->default('personal');
$table->string('name');
$table->decimal('price_aud', 10, 2)->default(10.00);
$table->string('billing_interval')->default('year');
$table->integer('sms_included')->default(50);
$table->integer('max_tags')->default(25);
$table->integer('alerts_per_day')->default(5);
$table->integer('alerts_per_hour')->default(10);
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
});
}
// Order subscription columns + plan linkage + billing-period marker.
Schema::table('orders', function (Blueprint $table) {
if (! Schema::hasColumn('orders', 'stripe_id')) {
$table->string('stripe_id')->nullable();
}
if (! Schema::hasColumn('orders', 'pm_type')) {
$table->string('pm_type')->nullable();
}
if (! Schema::hasColumn('orders', 'pm_last_four')) {
$table->string('pm_last_four')->nullable();
}
if (! Schema::hasColumn('orders', 'trial_ends_at')) {
$table->timestamp('trial_ends_at')->nullable();
}
if (! Schema::hasColumn('orders', 'plan_id')) {
$table->foreignId('plan_id')->nullable()->constrained('plans')->nullOnDelete();
}
if (! Schema::hasColumn('orders', 'period_started_at')) {
$table->timestamp('period_started_at')->nullable();
}
});
// User billing columns.
Schema::table('users', function (Blueprint $table) {
if (! Schema::hasColumn('users', 'stripe_id')) {
$table->string('stripe_id')->nullable();
}
if (! Schema::hasColumn('users', 'sms_credits')) {
$table->integer('sms_credits')->default(0);
}
});
// Product SKU price (AUD) for one-off checkouts.
Schema::table('products', function (Blueprint $table) {
if (! Schema::hasColumn('products', 'price_aud')) {
$table->decimal('price_aud', 10, 2)->default(0);
}
});
// Webhook audit log.
if (! Schema::hasTable('billing_events')) {
Schema::create('billing_events', function (Blueprint $table) {
$table->id();
$table->string('stripe_event_id')->unique();
$table->string('event_type');
$table->foreignId('order_id')->nullable()->constrained('orders')->nullOnDelete();
$table->jsonb('payload')->nullable();
$table->timestamp('created_at')->useCurrent();
});
}
}
public function down(): void
{
Schema::dropIfExists('billing_events');
Schema::table('products', function (Blueprint $table) {
$table->dropColumn(['price_aud']);
});
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['stripe_id', 'sms_credits']);
});
Schema::table('orders', function (Blueprint $table) {
$table->dropForeign(['plan_id']);
$table->dropColumn(['stripe_id', 'pm_type', 'pm_last_four', 'trial_ends_at', 'plan_id', 'period_started_at']);
});
Schema::dropIfExists('plans');
}
};