Admin Stripe invoices + recurring subscriptions (customers pay us) (#24)
Unit Tests / test (push) Successful in 11s

## Summary
- Closes #23
- Admin-only invoice dashboard under `/financial/invoices` (superuser)
- One-off Stripe hosted invoices + email pay link (customers pay us)
- Monthly (or weekly/yearly) Stripe Checkout subscriptions + email checkout link
- Webhook at `/financial/stripe/webhook/` syncs invoice/subscription status
- Env docs: `STRIPE_SECRET_KEY`, `STRIPE_PUBLISHABLE_KEY`, `STRIPE_WEBHOOK_SECRET`

## Test plan
- [ ] Set Stripe **test** keys in local `.env` and restart app
- [ ] As superuser: Dashboard → Invoices → create one-off invoice; confirm Stripe invoice + email
- [ ] As superuser: create monthly subscription; open Checkout link; complete with test card `4242…`
- [ ] Configure Stripe CLI or Dashboard webhook → confirm status updates to paid/active
- [ ] Non-admin user cannot open `/financial/invoices`
- [ ] Prod: add live keys + webhook secret to server `.env`, migrate, restart; point webhook to `https://aimloperations.com/financial/stripe/webhook/`

## Notes
Uses stdlib HTTP to Stripe API (no `stripe` PyPI package) so deploys stay dependency-light.Reviewed-on: #24
This commit was merged in pull request #24.
This commit is contained in:
2026-07-31 12:02:40 -07:00
parent 05aa0b96b1
commit b98f504afc
19 changed files with 1403 additions and 2 deletions
+102
View File
@@ -302,6 +302,108 @@ class TimeCardCell(IdMixin, TimeMixin):
charge_number = models.ForeignKey(ChargeNumber, on_delete=models.CASCADE, null=True, blank=True)
class BillingCustomer(IdMixin, TimeMixin):
"""Customer who pays us (one-off invoices or subscriptions)."""
name = models.CharField(max_length=200)
email = models.EmailField()
company = models.CharField(max_length=200, blank=True)
stripe_customer_id = models.CharField(max_length=255, blank=True, default="")
notes = models.TextField(blank=True)
class Meta:
ordering = ["name"]
def __str__(self):
return f"{self.name} <{self.email}>"
class Invoice(IdMixin, TimeMixin):
"""One-off invoice — customer pays us via Stripe hosted invoice URL."""
class Status(models.TextChoices):
DRAFT = "draft", "Draft"
OPEN = "open", "Open"
PAID = "paid", "Paid"
VOID = "void", "Void"
UNCOLLECTIBLE = "uncollectible", "Uncollectible"
FAILED = "failed", "Failed"
customer = models.ForeignKey(
BillingCustomer, on_delete=models.PROTECT, related_name="invoices"
)
description = models.CharField(max_length=500)
amount_cents = models.PositiveIntegerField(help_text="Amount in cents (USD)")
currency = models.CharField(max_length=3, default="usd")
status = models.CharField(
max_length=20, choices=Status.choices, default=Status.DRAFT
)
due_date = models.DateField(null=True, blank=True)
notes = models.TextField(blank=True)
stripe_invoice_id = models.CharField(max_length=255, blank=True, default="")
hosted_invoice_url = models.URLField(max_length=500, blank=True, default="")
invoice_pdf_url = models.URLField(max_length=500, blank=True, default="")
pay_link_emailed_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ["-created"]
def __str__(self):
return f"Invoice {self.pk}{self.customer}{self.status}"
@property
def amount_dollars(self):
return self.amount_cents / 100.0
class RecurringSubscription(IdMixin, TimeMixin):
"""Monthly (or custom) recurring charge — customer pays us via Stripe Subscription."""
class Status(models.TextChoices):
INCOMPLETE = "incomplete", "Incomplete"
ACTIVE = "active", "Active"
PAST_DUE = "past_due", "Past due"
CANCELED = "canceled", "Canceled"
UNPAID = "unpaid", "Unpaid"
TRIALING = "trialing", "Trialing"
PAUSED = "paused", "Paused"
class Interval(models.TextChoices):
MONTH = "month", "Monthly"
YEAR = "year", "Yearly"
WEEK = "week", "Weekly"
customer = models.ForeignKey(
BillingCustomer, on_delete=models.PROTECT, related_name="subscriptions"
)
description = models.CharField(max_length=500)
amount_cents = models.PositiveIntegerField(help_text="Amount per period in cents (USD)")
currency = models.CharField(max_length=3, default="usd")
interval = models.CharField(
max_length=10, choices=Interval.choices, default=Interval.MONTH
)
status = models.CharField(
max_length=20, choices=Status.choices, default=Status.INCOMPLETE
)
notes = models.TextField(blank=True)
stripe_product_id = models.CharField(max_length=255, blank=True, default="")
stripe_price_id = models.CharField(max_length=255, blank=True, default="")
stripe_subscription_id = models.CharField(max_length=255, blank=True, default="")
checkout_session_id = models.CharField(max_length=255, blank=True, default="")
checkout_url = models.URLField(max_length=500, blank=True, default="")
pay_link_emailed_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ["-created"]
def __str__(self):
return f"Subscription {self.pk}{self.customer}{self.status}"
@property
def amount_dollars(self):
return self.amount_cents / 100.0
def set_user_type(user, user_type):
"""Set user type and sync the Employee record (mutually exclusive types)."""
user.__dict__.pop("profile", None)