Add finance app with Stripe Checkout subscriptions (#21) (#23)
Unit Tests / test (push) Successful in 10s

## Summary
- Closes #21 — new Django `finance` app with Stripe as payment provider
- Subscription price defaults to **$10 USD / month** via `SUBSCRIPTION_PRICE_AMOUNT_CENTS = 1000` in `settings.py` (env-overridable)
- Persists **Invoice** and **Payment** rows; both registered in Django admin (with payment inline on invoices)
- Checkout Session API redirects users to Stripe hosted payment; webhook verifies signatures and upserts ledger idempotently

## API
- `POST /api/finance/checkout/` — JWT auth → `{ checkout_url, session_id }`
- `GET /api/finance/invoices/` / `GET /api/finance/payments/` — own records
- `POST /api/finance/webhooks/stripe/` — Stripe signature-verified webhook

## Config
Documented in `.env.example` / `.env.prod.example`:
`STRIPE_SECRET_KEY`, `STRIPE_PUBLISHABLE_KEY`, `STRIPE_WEBHOOK_SECRET`, optional `STRIPE_PRICE_ID`, `FRONTEND_BASE_URL`

## Test plan
- [x] `uv run python manage.py test finance` (17 tests)
- [ ] Set Stripe test keys locally; create checkout session; complete payment in Stripe test mode
- [ ] Confirm Invoice/Payment appear in `/admin/`
- [ ] Point Stripe webhook to `/api/finance/webhooks/stripe/` and verify `checkout.session.completed` / `invoice.paid`Reviewed-on: #23
This commit was merged in pull request #23.
This commit is contained in:
2026-07-26 17:35:06 -07:00
parent 9984d1c340
commit ad44359804
23 changed files with 1540 additions and 0 deletions
+237
View File
@@ -0,0 +1,237 @@
# Generated by Django 6.0 on 2026-07-27 00:16
import django.db.models.deletion
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
("chat_backend", "0023_promptmetric_tokens_in_promptmetric_tokens_out"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="Invoice",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created", models.DateTimeField(default=django.utils.timezone.now)),
(
"last_modified",
models.DateTimeField(default=django.utils.timezone.now),
),
(
"provider",
models.CharField(
choices=[("stripe", "Stripe")], default="stripe", max_length=32
),
),
(
"status",
models.CharField(
choices=[
("draft", "Draft"),
("open", "Open"),
("paid", "Paid"),
("void", "Void"),
("uncollectible", "Uncollectible"),
("payment_failed", "Payment failed"),
],
db_index=True,
default="open",
max_length=32,
),
),
("currency", models.CharField(default="usd", max_length=8)),
(
"amount_due",
models.PositiveIntegerField(
default=0,
help_text="Amount due in the smallest currency unit (e.g. cents).",
),
),
(
"amount_paid",
models.PositiveIntegerField(
default=0,
help_text="Amount paid in the smallest currency unit (e.g. cents).",
),
),
("period_start", models.DateTimeField(blank=True, null=True)),
("period_end", models.DateTimeField(blank=True, null=True)),
(
"stripe_invoice_id",
models.CharField(
blank=True,
db_index=True,
max_length=255,
null=True,
unique=True,
),
),
(
"stripe_checkout_session_id",
models.CharField(
blank=True,
db_index=True,
max_length=255,
null=True,
unique=True,
),
),
(
"stripe_subscription_id",
models.CharField(
blank=True, db_index=True, max_length=255, null=True
),
),
(
"stripe_customer_id",
models.CharField(blank=True, default="", max_length=255),
),
("hosted_invoice_url", models.URLField(blank=True, default="")),
(
"description",
models.CharField(blank=True, default="", max_length=512),
),
(
"company",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="invoices",
to="chat_backend.company",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="invoices",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ["-created"],
},
),
migrations.CreateModel(
name="Payment",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created", models.DateTimeField(default=django.utils.timezone.now)),
(
"last_modified",
models.DateTimeField(default=django.utils.timezone.now),
),
(
"provider",
models.CharField(
choices=[("stripe", "Stripe")], default="stripe", max_length=32
),
),
(
"status",
models.CharField(
choices=[
("pending", "Pending"),
("succeeded", "Succeeded"),
("failed", "Failed"),
("canceled", "Canceled"),
("requires_action", "Requires action"),
],
db_index=True,
default="pending",
max_length=32,
),
),
("currency", models.CharField(default="usd", max_length=8)),
(
"amount",
models.PositiveIntegerField(
default=0,
help_text="Amount in the smallest currency unit (e.g. cents).",
),
),
(
"stripe_payment_intent_id",
models.CharField(
blank=True,
db_index=True,
max_length=255,
null=True,
unique=True,
),
),
(
"stripe_charge_id",
models.CharField(
blank=True,
db_index=True,
max_length=255,
null=True,
unique=True,
),
),
("paid_at", models.DateTimeField(blank=True, null=True)),
(
"failure_message",
models.CharField(blank=True, default="", max_length=512),
),
(
"company",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="payments",
to="chat_backend.company",
),
),
(
"invoice",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="payments",
to="finance.invoice",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="payments",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ["-created"],
},
),
]