| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: billing.sql |
| 5 | |
| 6 | package billingdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgtype" |
| 12 | ) |
| 13 | |
| 14 | const applySubscriptionSnapshot = `-- name: ApplySubscriptionSnapshot :one |
| 15 | WITH state AS ( |
| 16 | INSERT INTO org_billing_states ( |
| 17 | org_id, |
| 18 | provider, |
| 19 | plan, |
| 20 | subscription_status, |
| 21 | stripe_subscription_id, |
| 22 | stripe_subscription_item_id, |
| 23 | current_period_start, |
| 24 | current_period_end, |
| 25 | cancel_at_period_end, |
| 26 | trial_end, |
| 27 | canceled_at, |
| 28 | last_webhook_event_id, |
| 29 | past_due_at, |
| 30 | locked_at, |
| 31 | lock_reason, |
| 32 | grace_until |
| 33 | ) |
| 34 | VALUES ( |
| 35 | $1::bigint, |
| 36 | 'stripe', |
| 37 | $2::org_plan, |
| 38 | $3::billing_subscription_status, |
| 39 | $4::text, |
| 40 | $5::text, |
| 41 | $6::timestamptz, |
| 42 | $7::timestamptz, |
| 43 | $8::boolean, |
| 44 | $9::timestamptz, |
| 45 | $10::timestamptz, |
| 46 | $11::text, |
| 47 | CASE |
| 48 | WHEN $3::billing_subscription_status = 'past_due' THEN now() |
| 49 | ELSE NULL |
| 50 | END, |
| 51 | NULL, |
| 52 | NULL, |
| 53 | NULL |
| 54 | ) |
| 55 | ON CONFLICT (org_id) DO UPDATE |
| 56 | SET plan = EXCLUDED.plan, |
| 57 | subscription_status = EXCLUDED.subscription_status, |
| 58 | stripe_subscription_id = EXCLUDED.stripe_subscription_id, |
| 59 | stripe_subscription_item_id = EXCLUDED.stripe_subscription_item_id, |
| 60 | current_period_start = EXCLUDED.current_period_start, |
| 61 | current_period_end = EXCLUDED.current_period_end, |
| 62 | cancel_at_period_end = EXCLUDED.cancel_at_period_end, |
| 63 | trial_end = EXCLUDED.trial_end, |
| 64 | canceled_at = EXCLUDED.canceled_at, |
| 65 | last_webhook_event_id = EXCLUDED.last_webhook_event_id, |
| 66 | past_due_at = CASE |
| 67 | WHEN EXCLUDED.subscription_status = 'past_due' THEN COALESCE(org_billing_states.past_due_at, now()) |
| 68 | ELSE NULL |
| 69 | END, |
| 70 | locked_at = NULL, |
| 71 | lock_reason = NULL, |
| 72 | grace_until = NULL, |
| 73 | updated_at = now() |
| 74 | RETURNING org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at |
| 75 | ), org_update AS ( |
| 76 | UPDATE orgs |
| 77 | SET plan = $2::org_plan, |
| 78 | updated_at = now() |
| 79 | WHERE id = $1::bigint |
| 80 | RETURNING id |
| 81 | ) |
| 82 | SELECT org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at FROM state |
| 83 | ` |
| 84 | |
| 85 | type ApplySubscriptionSnapshotParams struct { |
| 86 | OrgID int64 |
| 87 | Plan OrgPlan |
| 88 | SubscriptionStatus BillingSubscriptionStatus |
| 89 | StripeSubscriptionID pgtype.Text |
| 90 | StripeSubscriptionItemID pgtype.Text |
| 91 | CurrentPeriodStart pgtype.Timestamptz |
| 92 | CurrentPeriodEnd pgtype.Timestamptz |
| 93 | CancelAtPeriodEnd bool |
| 94 | TrialEnd pgtype.Timestamptz |
| 95 | CanceledAt pgtype.Timestamptz |
| 96 | LastWebhookEventID string |
| 97 | } |
| 98 | |
| 99 | type ApplySubscriptionSnapshotRow struct { |
| 100 | OrgID int64 |
| 101 | Provider BillingProvider |
| 102 | StripeCustomerID pgtype.Text |
| 103 | StripeSubscriptionID pgtype.Text |
| 104 | StripeSubscriptionItemID pgtype.Text |
| 105 | Plan OrgPlan |
| 106 | SubscriptionStatus BillingSubscriptionStatus |
| 107 | BillableSeats int32 |
| 108 | SeatSnapshotAt pgtype.Timestamptz |
| 109 | CurrentPeriodStart pgtype.Timestamptz |
| 110 | CurrentPeriodEnd pgtype.Timestamptz |
| 111 | CancelAtPeriodEnd bool |
| 112 | TrialEnd pgtype.Timestamptz |
| 113 | PastDueAt pgtype.Timestamptz |
| 114 | CanceledAt pgtype.Timestamptz |
| 115 | LockedAt pgtype.Timestamptz |
| 116 | LockReason NullBillingLockReason |
| 117 | GraceUntil pgtype.Timestamptz |
| 118 | LastWebhookEventID string |
| 119 | CreatedAt pgtype.Timestamptz |
| 120 | UpdatedAt pgtype.Timestamptz |
| 121 | } |
| 122 | |
| 123 | func (q *Queries) ApplySubscriptionSnapshot(ctx context.Context, db DBTX, arg ApplySubscriptionSnapshotParams) (ApplySubscriptionSnapshotRow, error) { |
| 124 | row := db.QueryRow(ctx, applySubscriptionSnapshot, |
| 125 | arg.OrgID, |
| 126 | arg.Plan, |
| 127 | arg.SubscriptionStatus, |
| 128 | arg.StripeSubscriptionID, |
| 129 | arg.StripeSubscriptionItemID, |
| 130 | arg.CurrentPeriodStart, |
| 131 | arg.CurrentPeriodEnd, |
| 132 | arg.CancelAtPeriodEnd, |
| 133 | arg.TrialEnd, |
| 134 | arg.CanceledAt, |
| 135 | arg.LastWebhookEventID, |
| 136 | ) |
| 137 | var i ApplySubscriptionSnapshotRow |
| 138 | err := row.Scan( |
| 139 | &i.OrgID, |
| 140 | &i.Provider, |
| 141 | &i.StripeCustomerID, |
| 142 | &i.StripeSubscriptionID, |
| 143 | &i.StripeSubscriptionItemID, |
| 144 | &i.Plan, |
| 145 | &i.SubscriptionStatus, |
| 146 | &i.BillableSeats, |
| 147 | &i.SeatSnapshotAt, |
| 148 | &i.CurrentPeriodStart, |
| 149 | &i.CurrentPeriodEnd, |
| 150 | &i.CancelAtPeriodEnd, |
| 151 | &i.TrialEnd, |
| 152 | &i.PastDueAt, |
| 153 | &i.CanceledAt, |
| 154 | &i.LockedAt, |
| 155 | &i.LockReason, |
| 156 | &i.GraceUntil, |
| 157 | &i.LastWebhookEventID, |
| 158 | &i.CreatedAt, |
| 159 | &i.UpdatedAt, |
| 160 | ) |
| 161 | return i, err |
| 162 | } |
| 163 | |
| 164 | const clearBillingLock = `-- name: ClearBillingLock :one |
| 165 | WITH state AS ( |
| 166 | UPDATE org_billing_states |
| 167 | SET plan = CASE |
| 168 | WHEN subscription_status = 'canceled' THEN 'free' |
| 169 | ELSE plan |
| 170 | END, |
| 171 | subscription_status = CASE |
| 172 | WHEN subscription_status = 'canceled' THEN 'none' |
| 173 | ELSE subscription_status |
| 174 | END, |
| 175 | locked_at = NULL, |
| 176 | lock_reason = NULL, |
| 177 | grace_until = NULL, |
| 178 | updated_at = now() |
| 179 | WHERE org_id = $1 |
| 180 | RETURNING org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at |
| 181 | ), org_update AS ( |
| 182 | UPDATE orgs |
| 183 | SET plan = state.plan, |
| 184 | updated_at = now() |
| 185 | FROM state |
| 186 | WHERE orgs.id = state.org_id |
| 187 | RETURNING orgs.id |
| 188 | ) |
| 189 | SELECT org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at FROM state |
| 190 | ` |
| 191 | |
| 192 | type ClearBillingLockRow struct { |
| 193 | OrgID int64 |
| 194 | Provider BillingProvider |
| 195 | StripeCustomerID pgtype.Text |
| 196 | StripeSubscriptionID pgtype.Text |
| 197 | StripeSubscriptionItemID pgtype.Text |
| 198 | Plan OrgPlan |
| 199 | SubscriptionStatus BillingSubscriptionStatus |
| 200 | BillableSeats int32 |
| 201 | SeatSnapshotAt pgtype.Timestamptz |
| 202 | CurrentPeriodStart pgtype.Timestamptz |
| 203 | CurrentPeriodEnd pgtype.Timestamptz |
| 204 | CancelAtPeriodEnd bool |
| 205 | TrialEnd pgtype.Timestamptz |
| 206 | PastDueAt pgtype.Timestamptz |
| 207 | CanceledAt pgtype.Timestamptz |
| 208 | LockedAt pgtype.Timestamptz |
| 209 | LockReason NullBillingLockReason |
| 210 | GraceUntil pgtype.Timestamptz |
| 211 | LastWebhookEventID string |
| 212 | CreatedAt pgtype.Timestamptz |
| 213 | UpdatedAt pgtype.Timestamptz |
| 214 | } |
| 215 | |
| 216 | func (q *Queries) ClearBillingLock(ctx context.Context, db DBTX, orgID int64) (ClearBillingLockRow, error) { |
| 217 | row := db.QueryRow(ctx, clearBillingLock, orgID) |
| 218 | var i ClearBillingLockRow |
| 219 | err := row.Scan( |
| 220 | &i.OrgID, |
| 221 | &i.Provider, |
| 222 | &i.StripeCustomerID, |
| 223 | &i.StripeSubscriptionID, |
| 224 | &i.StripeSubscriptionItemID, |
| 225 | &i.Plan, |
| 226 | &i.SubscriptionStatus, |
| 227 | &i.BillableSeats, |
| 228 | &i.SeatSnapshotAt, |
| 229 | &i.CurrentPeriodStart, |
| 230 | &i.CurrentPeriodEnd, |
| 231 | &i.CancelAtPeriodEnd, |
| 232 | &i.TrialEnd, |
| 233 | &i.PastDueAt, |
| 234 | &i.CanceledAt, |
| 235 | &i.LockedAt, |
| 236 | &i.LockReason, |
| 237 | &i.GraceUntil, |
| 238 | &i.LastWebhookEventID, |
| 239 | &i.CreatedAt, |
| 240 | &i.UpdatedAt, |
| 241 | ) |
| 242 | return i, err |
| 243 | } |
| 244 | |
| 245 | const createSeatSnapshot = `-- name: CreateSeatSnapshot :one |
| 246 | |
| 247 | WITH snapshot AS ( |
| 248 | INSERT INTO billing_seat_snapshots ( |
| 249 | org_id, |
| 250 | provider, |
| 251 | stripe_subscription_id, |
| 252 | active_members, |
| 253 | billable_seats, |
| 254 | source |
| 255 | ) |
| 256 | VALUES ( |
| 257 | $1::bigint, |
| 258 | 'stripe', |
| 259 | $2::text, |
| 260 | $3::integer, |
| 261 | $4::integer, |
| 262 | $5::text |
| 263 | ) |
| 264 | RETURNING id, org_id, provider, stripe_subscription_id, active_members, billable_seats, source, captured_at |
| 265 | ), state AS ( |
| 266 | INSERT INTO org_billing_states (org_id, billable_seats, seat_snapshot_at) |
| 267 | SELECT org_id, billable_seats, captured_at FROM snapshot |
| 268 | ON CONFLICT (org_id) DO UPDATE |
| 269 | SET billable_seats = EXCLUDED.billable_seats, |
| 270 | seat_snapshot_at = EXCLUDED.seat_snapshot_at, |
| 271 | updated_at = now() |
| 272 | RETURNING org_id |
| 273 | ) |
| 274 | SELECT id, org_id, provider, stripe_subscription_id, active_members, billable_seats, source, captured_at FROM snapshot |
| 275 | ` |
| 276 | |
| 277 | type CreateSeatSnapshotParams struct { |
| 278 | OrgID int64 |
| 279 | StripeSubscriptionID pgtype.Text |
| 280 | ActiveMembers int32 |
| 281 | BillableSeats int32 |
| 282 | Source string |
| 283 | } |
| 284 | |
| 285 | type CreateSeatSnapshotRow struct { |
| 286 | ID int64 |
| 287 | OrgID int64 |
| 288 | Provider BillingProvider |
| 289 | StripeSubscriptionID pgtype.Text |
| 290 | ActiveMembers int32 |
| 291 | BillableSeats int32 |
| 292 | Source string |
| 293 | CapturedAt pgtype.Timestamptz |
| 294 | } |
| 295 | |
| 296 | // ─── billing_seat_snapshots ──────────────────────────────────────── |
| 297 | func (q *Queries) CreateSeatSnapshot(ctx context.Context, db DBTX, arg CreateSeatSnapshotParams) (CreateSeatSnapshotRow, error) { |
| 298 | row := db.QueryRow(ctx, createSeatSnapshot, |
| 299 | arg.OrgID, |
| 300 | arg.StripeSubscriptionID, |
| 301 | arg.ActiveMembers, |
| 302 | arg.BillableSeats, |
| 303 | arg.Source, |
| 304 | ) |
| 305 | var i CreateSeatSnapshotRow |
| 306 | err := row.Scan( |
| 307 | &i.ID, |
| 308 | &i.OrgID, |
| 309 | &i.Provider, |
| 310 | &i.StripeSubscriptionID, |
| 311 | &i.ActiveMembers, |
| 312 | &i.BillableSeats, |
| 313 | &i.Source, |
| 314 | &i.CapturedAt, |
| 315 | ) |
| 316 | return i, err |
| 317 | } |
| 318 | |
| 319 | const createWebhookEventReceipt = `-- name: CreateWebhookEventReceipt :one |
| 320 | |
| 321 | INSERT INTO billing_webhook_events ( |
| 322 | provider, |
| 323 | provider_event_id, |
| 324 | event_type, |
| 325 | api_version, |
| 326 | payload |
| 327 | ) |
| 328 | VALUES ( |
| 329 | 'stripe', |
| 330 | $1::text, |
| 331 | $2::text, |
| 332 | $3::text, |
| 333 | $4::jsonb |
| 334 | ) |
| 335 | ON CONFLICT (provider, provider_event_id) DO NOTHING |
| 336 | RETURNING id, provider, provider_event_id, event_type, api_version, payload, received_at, processed_at, process_error, processing_attempts |
| 337 | ` |
| 338 | |
| 339 | type CreateWebhookEventReceiptParams struct { |
| 340 | ProviderEventID string |
| 341 | EventType string |
| 342 | ApiVersion string |
| 343 | Payload []byte |
| 344 | } |
| 345 | |
| 346 | // ─── billing_webhook_events ──────────────────────────────────────── |
| 347 | func (q *Queries) CreateWebhookEventReceipt(ctx context.Context, db DBTX, arg CreateWebhookEventReceiptParams) (BillingWebhookEvent, error) { |
| 348 | row := db.QueryRow(ctx, createWebhookEventReceipt, |
| 349 | arg.ProviderEventID, |
| 350 | arg.EventType, |
| 351 | arg.ApiVersion, |
| 352 | arg.Payload, |
| 353 | ) |
| 354 | var i BillingWebhookEvent |
| 355 | err := row.Scan( |
| 356 | &i.ID, |
| 357 | &i.Provider, |
| 358 | &i.ProviderEventID, |
| 359 | &i.EventType, |
| 360 | &i.ApiVersion, |
| 361 | &i.Payload, |
| 362 | &i.ReceivedAt, |
| 363 | &i.ProcessedAt, |
| 364 | &i.ProcessError, |
| 365 | &i.ProcessingAttempts, |
| 366 | ) |
| 367 | return i, err |
| 368 | } |
| 369 | |
| 370 | const getOrgBillingState = `-- name: GetOrgBillingState :one |
| 371 | |
| 372 | |
| 373 | SELECT org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at FROM org_billing_states WHERE org_id = $1 |
| 374 | ` |
| 375 | |
| 376 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 377 | // ─── org_billing_states ──────────────────────────────────────────── |
| 378 | func (q *Queries) GetOrgBillingState(ctx context.Context, db DBTX, orgID int64) (OrgBillingState, error) { |
| 379 | row := db.QueryRow(ctx, getOrgBillingState, orgID) |
| 380 | var i OrgBillingState |
| 381 | err := row.Scan( |
| 382 | &i.OrgID, |
| 383 | &i.Provider, |
| 384 | &i.StripeCustomerID, |
| 385 | &i.StripeSubscriptionID, |
| 386 | &i.StripeSubscriptionItemID, |
| 387 | &i.Plan, |
| 388 | &i.SubscriptionStatus, |
| 389 | &i.BillableSeats, |
| 390 | &i.SeatSnapshotAt, |
| 391 | &i.CurrentPeriodStart, |
| 392 | &i.CurrentPeriodEnd, |
| 393 | &i.CancelAtPeriodEnd, |
| 394 | &i.TrialEnd, |
| 395 | &i.PastDueAt, |
| 396 | &i.CanceledAt, |
| 397 | &i.LockedAt, |
| 398 | &i.LockReason, |
| 399 | &i.GraceUntil, |
| 400 | &i.LastWebhookEventID, |
| 401 | &i.CreatedAt, |
| 402 | &i.UpdatedAt, |
| 403 | ) |
| 404 | return i, err |
| 405 | } |
| 406 | |
| 407 | const listInvoicesForOrg = `-- name: ListInvoicesForOrg :many |
| 408 | SELECT id, org_id, provider, stripe_invoice_id, stripe_customer_id, stripe_subscription_id, status, number, currency, amount_due_cents, amount_paid_cents, amount_remaining_cents, hosted_invoice_url, invoice_pdf_url, period_start, period_end, due_at, paid_at, voided_at, created_at, updated_at FROM billing_invoices |
| 409 | WHERE org_id = $1 |
| 410 | ORDER BY created_at DESC, id DESC |
| 411 | LIMIT $2 |
| 412 | ` |
| 413 | |
| 414 | type ListInvoicesForOrgParams struct { |
| 415 | OrgID int64 |
| 416 | Limit int32 |
| 417 | } |
| 418 | |
| 419 | func (q *Queries) ListInvoicesForOrg(ctx context.Context, db DBTX, arg ListInvoicesForOrgParams) ([]BillingInvoice, error) { |
| 420 | rows, err := db.Query(ctx, listInvoicesForOrg, arg.OrgID, arg.Limit) |
| 421 | if err != nil { |
| 422 | return nil, err |
| 423 | } |
| 424 | defer rows.Close() |
| 425 | items := []BillingInvoice{} |
| 426 | for rows.Next() { |
| 427 | var i BillingInvoice |
| 428 | if err := rows.Scan( |
| 429 | &i.ID, |
| 430 | &i.OrgID, |
| 431 | &i.Provider, |
| 432 | &i.StripeInvoiceID, |
| 433 | &i.StripeCustomerID, |
| 434 | &i.StripeSubscriptionID, |
| 435 | &i.Status, |
| 436 | &i.Number, |
| 437 | &i.Currency, |
| 438 | &i.AmountDueCents, |
| 439 | &i.AmountPaidCents, |
| 440 | &i.AmountRemainingCents, |
| 441 | &i.HostedInvoiceUrl, |
| 442 | &i.InvoicePdfUrl, |
| 443 | &i.PeriodStart, |
| 444 | &i.PeriodEnd, |
| 445 | &i.DueAt, |
| 446 | &i.PaidAt, |
| 447 | &i.VoidedAt, |
| 448 | &i.CreatedAt, |
| 449 | &i.UpdatedAt, |
| 450 | ); err != nil { |
| 451 | return nil, err |
| 452 | } |
| 453 | items = append(items, i) |
| 454 | } |
| 455 | if err := rows.Err(); err != nil { |
| 456 | return nil, err |
| 457 | } |
| 458 | return items, nil |
| 459 | } |
| 460 | |
| 461 | const listSeatSnapshotsForOrg = `-- name: ListSeatSnapshotsForOrg :many |
| 462 | SELECT id, org_id, provider, stripe_subscription_id, active_members, billable_seats, source, captured_at FROM billing_seat_snapshots |
| 463 | WHERE org_id = $1 |
| 464 | ORDER BY captured_at DESC, id DESC |
| 465 | LIMIT $2 |
| 466 | ` |
| 467 | |
| 468 | type ListSeatSnapshotsForOrgParams struct { |
| 469 | OrgID int64 |
| 470 | Limit int32 |
| 471 | } |
| 472 | |
| 473 | func (q *Queries) ListSeatSnapshotsForOrg(ctx context.Context, db DBTX, arg ListSeatSnapshotsForOrgParams) ([]BillingSeatSnapshot, error) { |
| 474 | rows, err := db.Query(ctx, listSeatSnapshotsForOrg, arg.OrgID, arg.Limit) |
| 475 | if err != nil { |
| 476 | return nil, err |
| 477 | } |
| 478 | defer rows.Close() |
| 479 | items := []BillingSeatSnapshot{} |
| 480 | for rows.Next() { |
| 481 | var i BillingSeatSnapshot |
| 482 | if err := rows.Scan( |
| 483 | &i.ID, |
| 484 | &i.OrgID, |
| 485 | &i.Provider, |
| 486 | &i.StripeSubscriptionID, |
| 487 | &i.ActiveMembers, |
| 488 | &i.BillableSeats, |
| 489 | &i.Source, |
| 490 | &i.CapturedAt, |
| 491 | ); err != nil { |
| 492 | return nil, err |
| 493 | } |
| 494 | items = append(items, i) |
| 495 | } |
| 496 | if err := rows.Err(); err != nil { |
| 497 | return nil, err |
| 498 | } |
| 499 | return items, nil |
| 500 | } |
| 501 | |
| 502 | const markCanceled = `-- name: MarkCanceled :one |
| 503 | WITH state AS ( |
| 504 | UPDATE org_billing_states |
| 505 | SET plan = 'free', |
| 506 | subscription_status = 'canceled', |
| 507 | canceled_at = COALESCE(canceled_at, now()), |
| 508 | locked_at = now(), |
| 509 | lock_reason = 'canceled', |
| 510 | grace_until = NULL, |
| 511 | cancel_at_period_end = false, |
| 512 | last_webhook_event_id = $1::text, |
| 513 | updated_at = now() |
| 514 | WHERE org_id = $2::bigint |
| 515 | RETURNING org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at |
| 516 | ), org_update AS ( |
| 517 | UPDATE orgs |
| 518 | SET plan = 'free', |
| 519 | updated_at = now() |
| 520 | WHERE id = $2::bigint |
| 521 | RETURNING id |
| 522 | ) |
| 523 | SELECT org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at FROM state |
| 524 | ` |
| 525 | |
| 526 | type MarkCanceledParams struct { |
| 527 | LastWebhookEventID string |
| 528 | OrgID int64 |
| 529 | } |
| 530 | |
| 531 | type MarkCanceledRow struct { |
| 532 | OrgID int64 |
| 533 | Provider BillingProvider |
| 534 | StripeCustomerID pgtype.Text |
| 535 | StripeSubscriptionID pgtype.Text |
| 536 | StripeSubscriptionItemID pgtype.Text |
| 537 | Plan OrgPlan |
| 538 | SubscriptionStatus BillingSubscriptionStatus |
| 539 | BillableSeats int32 |
| 540 | SeatSnapshotAt pgtype.Timestamptz |
| 541 | CurrentPeriodStart pgtype.Timestamptz |
| 542 | CurrentPeriodEnd pgtype.Timestamptz |
| 543 | CancelAtPeriodEnd bool |
| 544 | TrialEnd pgtype.Timestamptz |
| 545 | PastDueAt pgtype.Timestamptz |
| 546 | CanceledAt pgtype.Timestamptz |
| 547 | LockedAt pgtype.Timestamptz |
| 548 | LockReason NullBillingLockReason |
| 549 | GraceUntil pgtype.Timestamptz |
| 550 | LastWebhookEventID string |
| 551 | CreatedAt pgtype.Timestamptz |
| 552 | UpdatedAt pgtype.Timestamptz |
| 553 | } |
| 554 | |
| 555 | func (q *Queries) MarkCanceled(ctx context.Context, db DBTX, arg MarkCanceledParams) (MarkCanceledRow, error) { |
| 556 | row := db.QueryRow(ctx, markCanceled, arg.LastWebhookEventID, arg.OrgID) |
| 557 | var i MarkCanceledRow |
| 558 | err := row.Scan( |
| 559 | &i.OrgID, |
| 560 | &i.Provider, |
| 561 | &i.StripeCustomerID, |
| 562 | &i.StripeSubscriptionID, |
| 563 | &i.StripeSubscriptionItemID, |
| 564 | &i.Plan, |
| 565 | &i.SubscriptionStatus, |
| 566 | &i.BillableSeats, |
| 567 | &i.SeatSnapshotAt, |
| 568 | &i.CurrentPeriodStart, |
| 569 | &i.CurrentPeriodEnd, |
| 570 | &i.CancelAtPeriodEnd, |
| 571 | &i.TrialEnd, |
| 572 | &i.PastDueAt, |
| 573 | &i.CanceledAt, |
| 574 | &i.LockedAt, |
| 575 | &i.LockReason, |
| 576 | &i.GraceUntil, |
| 577 | &i.LastWebhookEventID, |
| 578 | &i.CreatedAt, |
| 579 | &i.UpdatedAt, |
| 580 | ) |
| 581 | return i, err |
| 582 | } |
| 583 | |
| 584 | const markPastDue = `-- name: MarkPastDue :one |
| 585 | UPDATE org_billing_states |
| 586 | SET subscription_status = 'past_due', |
| 587 | past_due_at = COALESCE(past_due_at, now()), |
| 588 | locked_at = now(), |
| 589 | lock_reason = 'past_due', |
| 590 | grace_until = $1::timestamptz, |
| 591 | last_webhook_event_id = $2::text, |
| 592 | updated_at = now() |
| 593 | WHERE org_id = $3::bigint |
| 594 | RETURNING org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at |
| 595 | ` |
| 596 | |
| 597 | type MarkPastDueParams struct { |
| 598 | GraceUntil pgtype.Timestamptz |
| 599 | LastWebhookEventID string |
| 600 | OrgID int64 |
| 601 | } |
| 602 | |
| 603 | func (q *Queries) MarkPastDue(ctx context.Context, db DBTX, arg MarkPastDueParams) (OrgBillingState, error) { |
| 604 | row := db.QueryRow(ctx, markPastDue, arg.GraceUntil, arg.LastWebhookEventID, arg.OrgID) |
| 605 | var i OrgBillingState |
| 606 | err := row.Scan( |
| 607 | &i.OrgID, |
| 608 | &i.Provider, |
| 609 | &i.StripeCustomerID, |
| 610 | &i.StripeSubscriptionID, |
| 611 | &i.StripeSubscriptionItemID, |
| 612 | &i.Plan, |
| 613 | &i.SubscriptionStatus, |
| 614 | &i.BillableSeats, |
| 615 | &i.SeatSnapshotAt, |
| 616 | &i.CurrentPeriodStart, |
| 617 | &i.CurrentPeriodEnd, |
| 618 | &i.CancelAtPeriodEnd, |
| 619 | &i.TrialEnd, |
| 620 | &i.PastDueAt, |
| 621 | &i.CanceledAt, |
| 622 | &i.LockedAt, |
| 623 | &i.LockReason, |
| 624 | &i.GraceUntil, |
| 625 | &i.LastWebhookEventID, |
| 626 | &i.CreatedAt, |
| 627 | &i.UpdatedAt, |
| 628 | ) |
| 629 | return i, err |
| 630 | } |
| 631 | |
| 632 | const markWebhookEventFailed = `-- name: MarkWebhookEventFailed :one |
| 633 | UPDATE billing_webhook_events |
| 634 | SET process_error = $2, |
| 635 | processing_attempts = processing_attempts + 1 |
| 636 | WHERE provider = 'stripe' |
| 637 | AND provider_event_id = $1 |
| 638 | RETURNING id, provider, provider_event_id, event_type, api_version, payload, received_at, processed_at, process_error, processing_attempts |
| 639 | ` |
| 640 | |
| 641 | type MarkWebhookEventFailedParams struct { |
| 642 | ProviderEventID string |
| 643 | ProcessError string |
| 644 | } |
| 645 | |
| 646 | func (q *Queries) MarkWebhookEventFailed(ctx context.Context, db DBTX, arg MarkWebhookEventFailedParams) (BillingWebhookEvent, error) { |
| 647 | row := db.QueryRow(ctx, markWebhookEventFailed, arg.ProviderEventID, arg.ProcessError) |
| 648 | var i BillingWebhookEvent |
| 649 | err := row.Scan( |
| 650 | &i.ID, |
| 651 | &i.Provider, |
| 652 | &i.ProviderEventID, |
| 653 | &i.EventType, |
| 654 | &i.ApiVersion, |
| 655 | &i.Payload, |
| 656 | &i.ReceivedAt, |
| 657 | &i.ProcessedAt, |
| 658 | &i.ProcessError, |
| 659 | &i.ProcessingAttempts, |
| 660 | ) |
| 661 | return i, err |
| 662 | } |
| 663 | |
| 664 | const markWebhookEventProcessed = `-- name: MarkWebhookEventProcessed :one |
| 665 | UPDATE billing_webhook_events |
| 666 | SET processed_at = now(), |
| 667 | process_error = '', |
| 668 | processing_attempts = processing_attempts + 1 |
| 669 | WHERE provider = 'stripe' |
| 670 | AND provider_event_id = $1 |
| 671 | RETURNING id, provider, provider_event_id, event_type, api_version, payload, received_at, processed_at, process_error, processing_attempts |
| 672 | ` |
| 673 | |
| 674 | func (q *Queries) MarkWebhookEventProcessed(ctx context.Context, db DBTX, providerEventID string) (BillingWebhookEvent, error) { |
| 675 | row := db.QueryRow(ctx, markWebhookEventProcessed, providerEventID) |
| 676 | var i BillingWebhookEvent |
| 677 | err := row.Scan( |
| 678 | &i.ID, |
| 679 | &i.Provider, |
| 680 | &i.ProviderEventID, |
| 681 | &i.EventType, |
| 682 | &i.ApiVersion, |
| 683 | &i.Payload, |
| 684 | &i.ReceivedAt, |
| 685 | &i.ProcessedAt, |
| 686 | &i.ProcessError, |
| 687 | &i.ProcessingAttempts, |
| 688 | ) |
| 689 | return i, err |
| 690 | } |
| 691 | |
| 692 | const setStripeCustomer = `-- name: SetStripeCustomer :one |
| 693 | INSERT INTO org_billing_states (org_id, provider, stripe_customer_id) |
| 694 | VALUES ($1, 'stripe', $2) |
| 695 | ON CONFLICT (org_id) DO UPDATE |
| 696 | SET stripe_customer_id = EXCLUDED.stripe_customer_id, |
| 697 | provider = 'stripe', |
| 698 | updated_at = now() |
| 699 | RETURNING org_id, provider, stripe_customer_id, stripe_subscription_id, stripe_subscription_item_id, plan, subscription_status, billable_seats, seat_snapshot_at, current_period_start, current_period_end, cancel_at_period_end, trial_end, past_due_at, canceled_at, locked_at, lock_reason, grace_until, last_webhook_event_id, created_at, updated_at |
| 700 | ` |
| 701 | |
| 702 | type SetStripeCustomerParams struct { |
| 703 | OrgID int64 |
| 704 | StripeCustomerID pgtype.Text |
| 705 | } |
| 706 | |
| 707 | func (q *Queries) SetStripeCustomer(ctx context.Context, db DBTX, arg SetStripeCustomerParams) (OrgBillingState, error) { |
| 708 | row := db.QueryRow(ctx, setStripeCustomer, arg.OrgID, arg.StripeCustomerID) |
| 709 | var i OrgBillingState |
| 710 | err := row.Scan( |
| 711 | &i.OrgID, |
| 712 | &i.Provider, |
| 713 | &i.StripeCustomerID, |
| 714 | &i.StripeSubscriptionID, |
| 715 | &i.StripeSubscriptionItemID, |
| 716 | &i.Plan, |
| 717 | &i.SubscriptionStatus, |
| 718 | &i.BillableSeats, |
| 719 | &i.SeatSnapshotAt, |
| 720 | &i.CurrentPeriodStart, |
| 721 | &i.CurrentPeriodEnd, |
| 722 | &i.CancelAtPeriodEnd, |
| 723 | &i.TrialEnd, |
| 724 | &i.PastDueAt, |
| 725 | &i.CanceledAt, |
| 726 | &i.LockedAt, |
| 727 | &i.LockReason, |
| 728 | &i.GraceUntil, |
| 729 | &i.LastWebhookEventID, |
| 730 | &i.CreatedAt, |
| 731 | &i.UpdatedAt, |
| 732 | ) |
| 733 | return i, err |
| 734 | } |
| 735 | |
| 736 | const upsertInvoice = `-- name: UpsertInvoice :one |
| 737 | |
| 738 | INSERT INTO billing_invoices ( |
| 739 | org_id, |
| 740 | provider, |
| 741 | stripe_invoice_id, |
| 742 | stripe_customer_id, |
| 743 | stripe_subscription_id, |
| 744 | status, |
| 745 | number, |
| 746 | currency, |
| 747 | amount_due_cents, |
| 748 | amount_paid_cents, |
| 749 | amount_remaining_cents, |
| 750 | hosted_invoice_url, |
| 751 | invoice_pdf_url, |
| 752 | period_start, |
| 753 | period_end, |
| 754 | due_at, |
| 755 | paid_at, |
| 756 | voided_at |
| 757 | ) |
| 758 | VALUES ( |
| 759 | $1::bigint, |
| 760 | 'stripe', |
| 761 | $2::text, |
| 762 | $3::text, |
| 763 | $4::text, |
| 764 | $5::billing_invoice_status, |
| 765 | $6::text, |
| 766 | $7::text, |
| 767 | $8::bigint, |
| 768 | $9::bigint, |
| 769 | $10::bigint, |
| 770 | $11::text, |
| 771 | $12::text, |
| 772 | $13::timestamptz, |
| 773 | $14::timestamptz, |
| 774 | $15::timestamptz, |
| 775 | $16::timestamptz, |
| 776 | $17::timestamptz |
| 777 | ) |
| 778 | ON CONFLICT (provider, stripe_invoice_id) DO UPDATE |
| 779 | SET org_id = EXCLUDED.org_id, |
| 780 | stripe_customer_id = EXCLUDED.stripe_customer_id, |
| 781 | stripe_subscription_id = EXCLUDED.stripe_subscription_id, |
| 782 | status = EXCLUDED.status, |
| 783 | number = EXCLUDED.number, |
| 784 | currency = EXCLUDED.currency, |
| 785 | amount_due_cents = EXCLUDED.amount_due_cents, |
| 786 | amount_paid_cents = EXCLUDED.amount_paid_cents, |
| 787 | amount_remaining_cents = EXCLUDED.amount_remaining_cents, |
| 788 | hosted_invoice_url = EXCLUDED.hosted_invoice_url, |
| 789 | invoice_pdf_url = EXCLUDED.invoice_pdf_url, |
| 790 | period_start = EXCLUDED.period_start, |
| 791 | period_end = EXCLUDED.period_end, |
| 792 | due_at = EXCLUDED.due_at, |
| 793 | paid_at = EXCLUDED.paid_at, |
| 794 | voided_at = EXCLUDED.voided_at, |
| 795 | updated_at = now() |
| 796 | RETURNING id, org_id, provider, stripe_invoice_id, stripe_customer_id, stripe_subscription_id, status, number, currency, amount_due_cents, amount_paid_cents, amount_remaining_cents, hosted_invoice_url, invoice_pdf_url, period_start, period_end, due_at, paid_at, voided_at, created_at, updated_at |
| 797 | ` |
| 798 | |
| 799 | type UpsertInvoiceParams struct { |
| 800 | OrgID int64 |
| 801 | StripeInvoiceID string |
| 802 | StripeCustomerID string |
| 803 | StripeSubscriptionID pgtype.Text |
| 804 | Status BillingInvoiceStatus |
| 805 | Number string |
| 806 | Currency string |
| 807 | AmountDueCents int64 |
| 808 | AmountPaidCents int64 |
| 809 | AmountRemainingCents int64 |
| 810 | HostedInvoiceUrl string |
| 811 | InvoicePdfUrl string |
| 812 | PeriodStart pgtype.Timestamptz |
| 813 | PeriodEnd pgtype.Timestamptz |
| 814 | DueAt pgtype.Timestamptz |
| 815 | PaidAt pgtype.Timestamptz |
| 816 | VoidedAt pgtype.Timestamptz |
| 817 | } |
| 818 | |
| 819 | // ─── billing_invoices ────────────────────────────────────────────── |
| 820 | func (q *Queries) UpsertInvoice(ctx context.Context, db DBTX, arg UpsertInvoiceParams) (BillingInvoice, error) { |
| 821 | row := db.QueryRow(ctx, upsertInvoice, |
| 822 | arg.OrgID, |
| 823 | arg.StripeInvoiceID, |
| 824 | arg.StripeCustomerID, |
| 825 | arg.StripeSubscriptionID, |
| 826 | arg.Status, |
| 827 | arg.Number, |
| 828 | arg.Currency, |
| 829 | arg.AmountDueCents, |
| 830 | arg.AmountPaidCents, |
| 831 | arg.AmountRemainingCents, |
| 832 | arg.HostedInvoiceUrl, |
| 833 | arg.InvoicePdfUrl, |
| 834 | arg.PeriodStart, |
| 835 | arg.PeriodEnd, |
| 836 | arg.DueAt, |
| 837 | arg.PaidAt, |
| 838 | arg.VoidedAt, |
| 839 | ) |
| 840 | var i BillingInvoice |
| 841 | err := row.Scan( |
| 842 | &i.ID, |
| 843 | &i.OrgID, |
| 844 | &i.Provider, |
| 845 | &i.StripeInvoiceID, |
| 846 | &i.StripeCustomerID, |
| 847 | &i.StripeSubscriptionID, |
| 848 | &i.Status, |
| 849 | &i.Number, |
| 850 | &i.Currency, |
| 851 | &i.AmountDueCents, |
| 852 | &i.AmountPaidCents, |
| 853 | &i.AmountRemainingCents, |
| 854 | &i.HostedInvoiceUrl, |
| 855 | &i.InvoicePdfUrl, |
| 856 | &i.PeriodStart, |
| 857 | &i.PeriodEnd, |
| 858 | &i.DueAt, |
| 859 | &i.PaidAt, |
| 860 | &i.VoidedAt, |
| 861 | &i.CreatedAt, |
| 862 | &i.UpdatedAt, |
| 863 | ) |
| 864 | return i, err |
| 865 | } |
| 866 |