invoices.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package billing
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. "github.com/porter-dev/porter/internal/telemetry"
  11. )
  12. // ListCustomerInvoicesHandler is a handler for listing payment methods
  13. type ListCustomerInvoicesHandler struct {
  14. handlers.PorterHandlerReadWriter
  15. }
  16. // NewListCustomerInvoicesHandler will create a new ListCustomerInvoicesHandler
  17. func NewListCustomerInvoicesHandler(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *ListCustomerInvoicesHandler {
  22. return &ListCustomerInvoicesHandler{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  24. }
  25. }
  26. func (c *ListCustomerInvoicesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. ctx, span := telemetry.NewSpan(r.Context(), "serve-list-payment-methods")
  28. defer span.End()
  29. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  30. telemetry.WithAttributes(span,
  31. telemetry.AttributeKV{Key: "lago-config-exists", Value: c.Config().BillingManager.LagoConfigLoaded},
  32. telemetry.AttributeKV{Key: "lago-enabled", Value: proj.GetFeatureFlag(models.LagoEnabled, c.Config().LaunchDarklyClient)},
  33. telemetry.AttributeKV{Key: "porter-cloud-enabled", Value: proj.EnableSandbox},
  34. )
  35. if !c.Config().BillingManager.LagoConfigLoaded || !proj.GetFeatureFlag(models.LagoEnabled, c.Config().LaunchDarklyClient) {
  36. c.WriteResult(w, r, "")
  37. return
  38. }
  39. invoices, err := c.Config().BillingManager.LagoClient.ListCustomerFinalizedInvoices(ctx, proj.ID, proj.EnableSandbox)
  40. if err != nil {
  41. err = telemetry.Error(ctx, span, err, "error listing invoices")
  42. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  43. return
  44. }
  45. c.WriteResult(w, r, invoices)
  46. }