Phase 4 part 1: the full Stripe sync, and margins per product
Customers, invoices with all their lines (both of Stripe's line shapes), credit notes, refunds (with their customer) and products: a backfill, then the Events API every hour, and a new backfill when events have expired. Metrics page with sync status. Gross margin per product: a default (60%) and overrides, with names from Stripe's products or the invoice lines. Schemas 18 and 19. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -165,3 +165,217 @@ func (c *Client) Check(ctx context.Context) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Raw is an object as Stripe sent it, with its ID and creation time read.
|
||||
type Raw struct {
|
||||
ID string
|
||||
Object string
|
||||
Created int64
|
||||
JSON json.RawMessage
|
||||
}
|
||||
|
||||
// ListRaw reads every page of a list endpoint, newest first as Stripe sends
|
||||
// them, keeping each object whole.
|
||||
func (c *Client) ListRaw(ctx context.Context, path string, q url.Values, max int) ([]Raw, error) {
|
||||
if q == nil {
|
||||
q = url.Values{}
|
||||
}
|
||||
q.Set("limit", "100")
|
||||
var all []Raw
|
||||
for {
|
||||
var page struct {
|
||||
Data []json.RawMessage `json:"data"`
|
||||
HasMore bool `json:"has_more"`
|
||||
}
|
||||
if err := c.get(ctx, path, q, &page); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, d := range page.Data {
|
||||
var head struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Created int64 `json:"created"`
|
||||
}
|
||||
if err := json.Unmarshal(d, &head); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
all = append(all, Raw{ID: head.ID, Object: head.Object, Created: head.Created, JSON: d})
|
||||
}
|
||||
if !page.HasMore || len(page.Data) == 0 || (max > 0 && len(all) >= max) {
|
||||
return all, nil
|
||||
}
|
||||
q.Set("starting_after", all[len(all)-1].ID)
|
||||
}
|
||||
}
|
||||
|
||||
// InvoiceLines reads all of an invoice's lines (for invoices whose embedded
|
||||
// list was cut short).
|
||||
func (c *Client) InvoiceLines(ctx context.Context, invoiceID string) ([]json.RawMessage, error) {
|
||||
raws, err := c.ListRaw(ctx, "/v1/invoices/"+url.PathEscape(invoiceID)+"/lines", nil, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]json.RawMessage, len(raws))
|
||||
for i, r := range raws {
|
||||
out[i] = r.JSON
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// EventTypes are the events the sync follows.
|
||||
var EventTypes = []string{
|
||||
"customer.created", "customer.updated", "customer.deleted",
|
||||
"invoice.created", "invoice.finalized", "invoice.updated", "invoice.paid", "invoice.voided", "invoice.marked_uncollectible",
|
||||
"credit_note.created", "credit_note.updated", "credit_note.voided",
|
||||
"refund.created", "refund.updated", "charge.refunded",
|
||||
}
|
||||
|
||||
// Event is one change, with the object as it was after it.
|
||||
type Event struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Created int64 `json:"created"`
|
||||
Data struct {
|
||||
Object json.RawMessage `json:"object"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// Events lists the events of the followed types after a time, oldest first.
|
||||
func (c *Client) Events(ctx context.Context, after time.Time) ([]Event, error) {
|
||||
q := url.Values{"created[gt]": {strconv.FormatInt(after.Unix(), 10)}}
|
||||
for _, t := range EventTypes {
|
||||
q.Add("types[]", t)
|
||||
}
|
||||
raws, err := c.ListRaw(ctx, "/v1/events", q, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Event, 0, len(raws))
|
||||
for i := len(raws) - 1; i >= 0; i-- {
|
||||
var e Event
|
||||
if err := json.Unmarshal(raws[i].JSON, &e); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, e)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Line is an invoice line, read from either shape Stripe has used: the
|
||||
// older one (price, type, proration on the line) and the 2025 one (pricing,
|
||||
// parent).
|
||||
type Line struct {
|
||||
ID string
|
||||
Amount int64 // after discounts, before tax
|
||||
Currency string
|
||||
PeriodStart int64
|
||||
PeriodEnd int64
|
||||
Subscription string
|
||||
Price string
|
||||
Product string
|
||||
Interval string // day, week, month, year; "" for one-off
|
||||
IntervalCount int
|
||||
Quantity int64
|
||||
Proration bool
|
||||
Recurring bool
|
||||
Description string
|
||||
}
|
||||
|
||||
// ParseLine reads an invoice line.
|
||||
func ParseLine(raw json.RawMessage) (Line, error) {
|
||||
var l struct {
|
||||
Description string `json:"description"`
|
||||
ID string `json:"id"`
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
Period struct {
|
||||
Start int64 `json:"start"`
|
||||
End int64 `json:"end"`
|
||||
} `json:"period"`
|
||||
Quantity int64 `json:"quantity"`
|
||||
Proration bool `json:"proration"`
|
||||
Type string `json:"type"`
|
||||
Subscription json.RawMessage `json:"subscription"`
|
||||
Price *struct {
|
||||
ID string `json:"id"`
|
||||
Product json.RawMessage `json:"product"`
|
||||
Recurring *struct {
|
||||
Interval string `json:"interval"`
|
||||
IntervalCount int `json:"interval_count"`
|
||||
} `json:"recurring"`
|
||||
} `json:"price"`
|
||||
Pricing *struct {
|
||||
PriceDetails *struct {
|
||||
Price json.RawMessage `json:"price"`
|
||||
Product string `json:"product"`
|
||||
} `json:"price_details"`
|
||||
} `json:"pricing"`
|
||||
Parent *struct {
|
||||
Type string `json:"type"`
|
||||
SubscriptionItemDetails *struct {
|
||||
Subscription string `json:"subscription"`
|
||||
Proration bool `json:"proration"`
|
||||
} `json:"subscription_item_details"`
|
||||
} `json:"parent"`
|
||||
DiscountAmounts []struct {
|
||||
Amount int64 `json:"amount"`
|
||||
} `json:"discount_amounts"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &l); err != nil {
|
||||
return Line{}, err
|
||||
}
|
||||
out := Line{ID: l.ID, Amount: l.Amount, Currency: l.Currency, PeriodStart: l.Period.Start, PeriodEnd: l.Period.End,
|
||||
Quantity: l.Quantity, Proration: l.Proration, Subscription: idOf(l.Subscription), Description: l.Description}
|
||||
for _, d := range l.DiscountAmounts {
|
||||
out.Amount -= d.Amount
|
||||
}
|
||||
if l.Price != nil {
|
||||
out.Price, out.Product = l.Price.ID, idOf(l.Price.Product)
|
||||
if l.Price.Recurring != nil {
|
||||
out.Interval, out.IntervalCount = l.Price.Recurring.Interval, l.Price.Recurring.IntervalCount
|
||||
}
|
||||
}
|
||||
if l.Pricing != nil && l.Pricing.PriceDetails != nil {
|
||||
out.Price = or(out.Price, idOf(l.Pricing.PriceDetails.Price))
|
||||
out.Product = or(out.Product, l.Pricing.PriceDetails.Product)
|
||||
}
|
||||
if l.Parent != nil && l.Parent.SubscriptionItemDetails != nil {
|
||||
out.Subscription = or(out.Subscription, l.Parent.SubscriptionItemDetails.Subscription)
|
||||
out.Proration = out.Proration || l.Parent.SubscriptionItemDetails.Proration
|
||||
}
|
||||
out.Recurring = l.Type == "subscription" || (l.Parent != nil && l.Parent.Type == "subscription_item_details") || out.Interval != ""
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func or(a, b string) string {
|
||||
if a != "" {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// idOf reads a field that is an ID string or an expanded object with an id.
|
||||
func idOf(raw json.RawMessage) string {
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
return ""
|
||||
}
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
return s
|
||||
}
|
||||
var o struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &o)
|
||||
return o.ID
|
||||
}
|
||||
|
||||
// GetRaw reads one object.
|
||||
func (c *Client) GetRaw(ctx context.Context, path string, q url.Values) (json.RawMessage, error) {
|
||||
var out json.RawMessage
|
||||
err := c.get(ctx, path, q, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
// IDOf reads a field that is an ID string or an expanded object with an id.
|
||||
func IDOf(raw json.RawMessage) string { return idOf(raw) }
|
||||
|
||||
Reference in New Issue
Block a user