customer.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *CreateBillingCustomerHandler {
  24. return &CreateBillingCustomerHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. ctx, span := telemetry.NewSpan(r.Context(), "create-billing-customer-endpoint")
  30. defer span.End()
  31. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  32. request := &types.CreateBillingCustomerRequest{}
  33. if ok := c.DecodeAndValidate(w, r, request); !ok {
  34. return
  35. }
  36. // There is no easy way to pass environment variables to the frontend,
  37. // so for now pass via the backend. This is acceptable because the key is
  38. // meant to be public
  39. publishableKey := c.Config().BillingManager.GetPublishableKey(ctx)
  40. if proj.BillingID != "" {
  41. c.WriteResult(w, r, publishableKey)
  42. return
  43. }
  44. // Create customer in Stripe
  45. customerID, err := c.Config().BillingManager.CreateCustomer(ctx, request.UserEmail, proj)
  46. if err != nil {
  47. err := telemetry.Error(ctx, span, err, "error creating billing customer")
  48. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating billing customer: %w", err)))
  49. return
  50. }
  51. telemetry.WithAttributes(span,
  52. telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
  53. telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
  54. )
  55. // Update the project record with the customer ID
  56. proj.BillingID = customerID
  57. _, err = c.Repo().Project().UpdateProject(proj)
  58. if err != nil {
  59. err := telemetry.Error(ctx, span, err, "error updating project")
  60. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error updating project: %w", err)))
  61. return
  62. }
  63. c.WriteResult(w, r, publishableKey)
  64. }