customer.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // CreateBillingCustomerHandler will create a new handler
  14. // for creating customers in the billing provider
  15. type CreateBillingCustomerHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. // NewCreateBillingCustomerIfNotExists will create a new CreateBillingCustomerIfNotExists
  19. func NewCreateBillingCustomerIfNotExists(
  20. config *config.Config,
  21. writer shared.ResultWriter,
  22. ) *CreateBillingCustomerHandler {
  23. return &CreateBillingCustomerHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  25. }
  26. }
  27. func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. ctx, span := telemetry.NewSpan(r.Context(), "create-billing-customer-endpoint")
  29. defer span.End()
  30. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  31. user, _ := r.Context().Value(types.UserScope).(*models.User)
  32. if proj.BillingID != "" {
  33. c.WriteResult(w, r, "")
  34. return
  35. }
  36. // Create customer in Stripe
  37. customerID, err := c.Config().BillingManager.CreateCustomer(ctx, user.Email, proj)
  38. if err != nil {
  39. err := telemetry.Error(ctx, span, err, "error creating billing customer")
  40. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating billing customer: %w", err)))
  41. return
  42. }
  43. telemetry.WithAttributes(span,
  44. telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
  45. telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
  46. telemetry.AttributeKV{Key: "user-email", Value: user.Email},
  47. )
  48. // Update the project record with the customer ID
  49. proj.BillingID = customerID
  50. _, err = c.Repo().Project().UpdateProject(proj)
  51. if err != nil {
  52. err := telemetry.Error(ctx, span, err, "error updating project")
  53. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error updating project: %w", err)))
  54. return
  55. }
  56. c.WriteResult(w, r, "")
  57. }
  58. // GetPublishableKeyHandler will return the configured publishable key
  59. type GetPublishableKeyHandler struct {
  60. handlers.PorterHandlerReadWriter
  61. }
  62. // NewGetPublishableKeyHandler will return the publishable key
  63. func NewGetPublishableKeyHandler(
  64. config *config.Config,
  65. decoderValidator shared.RequestDecoderValidator,
  66. writer shared.ResultWriter,
  67. ) *GetPublishableKeyHandler {
  68. return &GetPublishableKeyHandler{
  69. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  70. }
  71. }
  72. func (c *GetPublishableKeyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  73. ctx, span := telemetry.NewSpan(r.Context(), "get-publishable-key-endpoint")
  74. defer span.End()
  75. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  76. // There is no easy way to pass environment variables to the frontend,
  77. // so for now pass via the backend. This is acceptable because the key is
  78. // meant to be public
  79. publishableKey := c.Config().BillingManager.GetPublishableKey(ctx)
  80. telemetry.WithAttributes(span,
  81. telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
  82. telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
  83. )
  84. c.WriteResult(w, r, publishableKey)
  85. }