Files
zeekay 73b7290e7f square: one webhook route and one verifier; secrets come from the host's KMS
Square keeps an event's created_at across every retry, and the receiver
refused anything older than five minutes, so every retry of an event
first sent while cloud was down (Recreate, ten rollouts a day) was refused
until Square gave up. The verifier now lives in payment/providers/square
and needs only the key and the URL; thirdparty/square is the API client
and no longer registers itself or verifies anything, so every registry
holds one Square type.

Replay is stopped by the event id instead: each verified event is
recorded once for the whole deployment, keyed on the verifying processor
and the event id with an insert-if-absent swap, so a repeat is a
duplicate under any path or org. The path names the one processor that
verifies, and only one that holds its webhook secret may: an unknown name,
or a verifier that needs no secret, is refused rather than tried.
The books an event lands in are the verifying account's, else the
deployment's brand — never a header or session, which on a replay are the
replayer's. An effect that did not land (a store or the ledger did not
answer) leaves the delivery open with a 503, so the provider redelivers;
it used to be acknowledged and never retried. A settlement reads the
payment index before the org's books, because an in-session charge's
receipt is in the payer's org.

POST /v1/commerce/webhooks/:provider is declared once, in setupRoutes, so
standalone and embedded commerce serve the same address; the
/v1/billing/webhooks spelling is gone.

thirdparty/kms read /v1/kms/orgs/<org>/secrets/..., which cloud no longer
serves, and was off in production anyway, so per-org payment credentials
never loaded. It is deleted. secrets.Org reads orgs/<org>/<name> through
the host's reader, the caller re-pointed at that org with zip.ActingAs so
the KMS tenancy check admits it, and payment.Hydrate loads an org's
credentials where the org receives the money: storefront checkout,
storefront subscriptions and wire, and the brand's bank for the billing
wire rail. Hanzo's own billing no longer reads the paying org's
credentials; it charges the deployment's account, as it did. The
standalone binary reads through hanzoai/kms/sdk/go.
2026-10-19 09:12:56 -07:00

148 lines
4.4 KiB
Go

package billing
import (
"github.com/zap-proto/zip"
"github.com/hanzoai/commerce/billing/engine"
"github.com/hanzoai/commerce/datastore"
"github.com/hanzoai/commerce/log"
"github.com/hanzoai/commerce/middleware"
"github.com/hanzoai/commerce/models/billinginvoice"
"github.com/hanzoai/commerce/models/paymentmethod"
"github.com/hanzoai/commerce/models/subscription"
"github.com/hanzoai/commerce/util/json/http"
)
// PortalOverview returns a billing summary for the authenticated customer.
//
// GET /v1/billing/portal/overview?customerId=...
func PortalOverview(c *zip.Ctx) error {
org := middleware.GetOrganization(c)
db := datastore.New(org.Namespaced(c.Context()))
customerId := c.Query("customerId")
if customerId == "" {
return http.Fail(c, 400, "customerId is required", nil)
}
// Get balance
cb, _ := engine.GetOrCreateCustomerBalance(db, customerId, "usd")
balance := int64(0)
if cb != nil {
balance = cb.Balance
}
// Count active subscriptions
rootKey := db.NewKey("synckey", "", 1, nil)
subs := make([]*subscription.Subscription, 0)
sq := subscription.Query(db).Ancestor(rootKey).
Filter("UserId=", customerId).
Filter("Status=", string(subscription.Active))
_, _ = sq.GetAll(&subs)
// Count payment methods
pms := make([]*paymentmethod.PaymentMethod, 0)
pmq := paymentmethod.Query(db).Ancestor(rootKey).
Filter("CustomerId=", customerId)
_, _ = pmq.GetAll(&pms)
return c.JSON(200, map[string]any{
"customerId": customerId,
"balance": balance,
"currency": "usd",
"activeSubscriptions": len(subs),
"paymentMethods": len(pms),
})
}
// PortalInvoices returns the customer's invoice list.
//
// GET /v1/billing/portal/invoices?customerId=...
func PortalInvoices(c *zip.Ctx) error {
org := middleware.GetOrganization(c)
db := datastore.New(org.Namespaced(c.Context()))
customerId := c.Query("customerId")
if customerId == "" {
return http.Fail(c, 400, "customerId is required", nil)
}
invoices := make([]*billinginvoice.BillingInvoice, 0)
q := billinginvoice.Query(db).
Filter("UserId=", customerId).
Order("-Created")
_, _ = q.GetAll(&invoices)
results := make([]map[string]any, len(invoices))
for i, inv := range invoices {
results[i] = map[string]any{
"id": inv.Id(),
"number": inv.NumberStr,
"status": inv.Status,
"amountDue": inv.AmountDue,
"currency": inv.Currency,
"created": inv.Created,
}
}
return c.JSON(200, results)
}
// PortalSubscriptions returns the customer's subscriptions.
//
// GET /v1/billing/portal/subscriptions?customerId=...
func PortalSubscriptions(c *zip.Ctx) error {
org := middleware.GetOrganization(c)
db := datastore.New(org.Namespaced(c.Context()))
customerId := c.Query("customerId")
if customerId == "" {
return http.Fail(c, 400, "customerId is required", nil)
}
rootKey := db.NewKey("synckey", "", 1, nil)
subs := make([]*subscription.Subscription, 0)
q := subscription.Query(db).Ancestor(rootKey).
Filter("UserId=", customerId).
Order("-Created")
_, _ = q.GetAll(&subs)
results := make([]map[string]any, len(subs))
for i, s := range subs {
results[i] = map[string]any{
"id": s.Id(),
"planId": s.PlanId,
"status": s.Status,
"periodStart": s.PeriodStart,
"periodEnd": s.PeriodEnd,
"created": s.Created,
}
}
return c.JSON(200, results)
}
// PortalPaymentMethods returns the customer's payment methods.
//
// The customer named here is a value the CALLER supplies, so the org namespace is
// what keeps one host's customerId from selecting another org's cards; asking for
// a foreign subject selects nothing inside your own namespace. It answers with
// the same ListMethods every other saved-card surface reads, because a portal
// that derived its own list would be a second answer to one question.
//
// GET /v1/billing/portal/methods?customerId=...
func PortalPaymentMethods(c *zip.Ctx) error {
org := middleware.GetOrganization(c)
customerId := c.Query("customerId")
if customerId == "" {
return http.Fail(c, 400, "customerId is required", nil)
}
methods, err := ListMethods(c.Context(), org, customerId, "")
if err != nil {
// A store that cannot be read has always answered an honest empty list
// here rather than a 500 — the portal renders no cards, not an error page.
log.Error("Failed to list portal payment methods: %v", err, c)
}
return c.JSON(200, methods)
}