Files
2026-10-16 00:34:06 +02:00

57 lines
1.7 KiB
Go

package billing
import (
"context"
"errors"
"net/url"
"strings"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
// StripeInvoice contains only identifiers and URLs supplied by Stripe, never a
// locally invented financial document. Amounts are in the invoice currency's
// smallest unit, as returned by Stripe.
type StripeInvoice struct {
ID string `json:"id"`
Number string `json:"number"`
Status string `json:"status"`
Currency string `json:"currency"`
Created int64 `json:"created"`
AmountDue int64 `json:"amount_due"`
AmountPaid int64 `json:"amount_paid"`
AmountRemaining int64 `json:"amount_remaining"`
HostedInvoiceURL string `json:"hosted_invoice_url,omitempty"`
InvoicePDF string `json:"invoice_pdf,omitempty"`
}
func (s *StripeService) ListCompanyInvoices(ctx context.Context, companyID uuid.UUID) ([]StripeInvoice, error) {
ctx, cfg, err := s.bindCfg(ctx)
if err != nil {
return nil, err
}
if cfg.MockMode() {
return []StripeInvoice{}, nil
}
var customerID *string
err = s.Pool.QueryRow(ctx, `SELECT stripe_customer_id FROM companies WHERE id=$1`, companyID).Scan(&customerID)
if errors.Is(err, pgx.ErrNoRows) || customerID == nil || strings.TrimSpace(*customerID) == "" {
return []StripeInvoice{}, nil
}
if err != nil {
return nil, err
}
var result struct {
Data []StripeInvoice `json:"data"`
}
endpoint := "https://api.stripe.com/v1/invoices?limit=100&customer=" + url.QueryEscape(*customerID)
if err := s.stripeGET(ctx, endpoint, &result); err != nil {
return nil, err
}
if result.Data == nil {
result.Data = []StripeInvoice{}
}
return result.Data, nil
}