list.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package billing
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/porter-dev/porter/api/server/handlers"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/internal/models"
  11. "github.com/porter-dev/porter/internal/telemetry"
  12. )
  13. // ListBillingHandler is a handler for listing payment methods
  14. type ListBillingHandler struct {
  15. handlers.PorterHandlerWriter
  16. }
  17. // CheckPaymentEnabledHandler is a handler for checking if payment is setup
  18. type CheckPaymentEnabledHandler struct {
  19. handlers.PorterHandlerWriter
  20. }
  21. // NewListBillingHandler will create a new ListBillingHandler
  22. func NewListBillingHandler(
  23. config *config.Config,
  24. writer shared.ResultWriter,
  25. ) *ListBillingHandler {
  26. return &ListBillingHandler{
  27. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  28. }
  29. }
  30. func (c *ListBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. ctx, span := telemetry.NewSpan(r.Context(), "list-payment-endpoint")
  32. defer span.End()
  33. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  34. paymentMethods, err := c.Config().BillingManager.ListPaymentMethod(ctx, proj)
  35. if err != nil {
  36. err := telemetry.Error(ctx, span, err, "error listing payment method")
  37. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error listing payment method: %w", err)))
  38. return
  39. }
  40. c.WriteResult(w, r, paymentMethods)
  41. }
  42. // NewCheckPaymentEnabledHandler will create a new CheckPaymentEnabledHandler
  43. func NewCheckPaymentEnabledHandler(
  44. config *config.Config,
  45. writer shared.ResultWriter,
  46. ) *CheckPaymentEnabledHandler {
  47. return &CheckPaymentEnabledHandler{
  48. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  49. }
  50. }
  51. func (c *CheckPaymentEnabledHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  52. ctx, span := telemetry.NewSpan(r.Context(), "check-payment-endpoint")
  53. defer span.End()
  54. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  55. paymentEnabled, err := c.Config().BillingManager.CheckPaymentEnabled(ctx, proj)
  56. if err != nil {
  57. err := telemetry.Error(ctx, span, err, "error checking if payment enabled")
  58. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error checking if payment enabled: %w", err)))
  59. return
  60. }
  61. telemetry.WithAttributes(span,
  62. telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
  63. telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
  64. )
  65. c.WriteResult(w, r, paymentEnabled)
  66. }