list.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package billing
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/google/uuid"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/telemetry"
  13. )
  14. // ListBillingHandler is a handler for listing payment methods
  15. type ListBillingHandler struct {
  16. handlers.PorterHandlerWriter
  17. }
  18. // CheckPaymentEnabledHandler is a handler for checking if payment is setup
  19. type CheckPaymentEnabledHandler struct {
  20. handlers.PorterHandlerWriter
  21. }
  22. // NewListBillingHandler will create a new ListBillingHandler
  23. func NewListBillingHandler(
  24. config *config.Config,
  25. writer shared.ResultWriter,
  26. ) *ListBillingHandler {
  27. return &ListBillingHandler{
  28. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  29. }
  30. }
  31. func (c *ListBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  32. ctx, span := telemetry.NewSpan(r.Context(), "list-payment-endpoint")
  33. defer span.End()
  34. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  35. paymentMethods, err := c.Config().BillingManager.StripeClient.ListPaymentMethod(ctx, proj.BillingID)
  36. if err != nil {
  37. err := telemetry.Error(ctx, span, err, "error listing payment method")
  38. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error listing payment method: %w", err)))
  39. return
  40. }
  41. c.WriteResult(w, r, paymentMethods)
  42. }
  43. // NewCheckPaymentEnabledHandler will create a new CheckPaymentEnabledHandler
  44. func NewCheckPaymentEnabledHandler(
  45. config *config.Config,
  46. writer shared.ResultWriter,
  47. ) *CheckPaymentEnabledHandler {
  48. return &CheckPaymentEnabledHandler{
  49. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  50. }
  51. }
  52. func (c *CheckPaymentEnabledHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  53. ctx, span := telemetry.NewSpan(r.Context(), "check-payment-endpoint")
  54. defer span.End()
  55. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  56. user, _ := ctx.Value(types.UserScope).(*models.User)
  57. // Create billing customer for project and set the billing ID if it doesn't exist
  58. var shouldUpdate bool
  59. if proj.BillingID == "" {
  60. billingID, err := c.Config().BillingManager.StripeClient.CreateCustomer(ctx, user.Email, proj.ID, proj.Name)
  61. if err != nil {
  62. err = telemetry.Error(ctx, span, err, "error creating billing customer")
  63. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  64. return
  65. }
  66. proj.BillingID = billingID
  67. shouldUpdate = true
  68. telemetry.WithAttributes(span,
  69. telemetry.AttributeKV{Key: "billing-id", Value: proj.BillingID},
  70. )
  71. }
  72. if c.Config().BillingManager.MetronomeEnabled && proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) && proj.UsageID == uuid.Nil {
  73. customerID, customerPlanID, err := c.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(ctx, user.Email, proj.Name, proj.ID, proj.BillingID, proj.EnableSandbox)
  74. if err != nil {
  75. err = telemetry.Error(ctx, span, err, "error creating Metronome customer")
  76. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  77. }
  78. proj.UsageID = customerID
  79. proj.UsagePlanID = customerPlanID
  80. shouldUpdate = true
  81. telemetry.WithAttributes(span,
  82. telemetry.AttributeKV{Key: "usage-id", Value: proj.UsageID},
  83. telemetry.AttributeKV{Key: "usage-plan-id", Value: proj.UsagePlanID},
  84. )
  85. }
  86. if shouldUpdate {
  87. _, err := c.Repo().Project().UpdateProject(proj)
  88. if err != nil {
  89. err := telemetry.Error(ctx, span, err, "error updating project")
  90. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  91. return
  92. }
  93. }
  94. paymentEnabled, err := c.Config().BillingManager.StripeClient.CheckPaymentEnabled(ctx, proj.BillingID)
  95. if err != nil {
  96. err := telemetry.Error(ctx, span, err, "error checking if payment enabled")
  97. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error checking if payment enabled: %w", err)))
  98. return
  99. }
  100. telemetry.WithAttributes(span,
  101. telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
  102. telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
  103. )
  104. c.WriteResult(w, r, paymentEnabled)
  105. }