list.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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().ServerConf.MetronomeAPIKey != "" && c.Config().ServerConf.PorterCloudPlanID != "" &&
  73. proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) && proj.UsageID == uuid.Nil {
  74. customerID, customerPlanID, err := c.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(ctx, user.Email, proj.Name, proj.ID, proj.BillingID, c.Config().ServerConf.PorterCloudPlanID)
  75. if err != nil {
  76. err = telemetry.Error(ctx, span, err, "error creating Metronome customer")
  77. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  78. }
  79. proj.UsageID = customerID
  80. proj.UsagePlanID = customerPlanID
  81. shouldUpdate = true
  82. telemetry.WithAttributes(span,
  83. telemetry.AttributeKV{Key: "usage-id", Value: proj.UsageID},
  84. telemetry.AttributeKV{Key: "usage-plan-id", Value: proj.UsagePlanID},
  85. )
  86. }
  87. if shouldUpdate {
  88. _, err := c.Repo().Project().UpdateProject(proj)
  89. if err != nil {
  90. err := telemetry.Error(ctx, span, err, "error updating project")
  91. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  92. return
  93. }
  94. }
  95. paymentEnabled, err := c.Config().BillingManager.StripeClient.CheckPaymentEnabled(ctx, proj.BillingID)
  96. if err != nil {
  97. err := telemetry.Error(ctx, span, err, "error checking if payment enabled")
  98. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error checking if payment enabled: %w", err)))
  99. return
  100. }
  101. telemetry.WithAttributes(span,
  102. telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
  103. telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
  104. )
  105. c.WriteResult(w, r, paymentEnabled)
  106. }