|
|
@@ -22,11 +22,10 @@ type CreateBillingCustomerHandler struct {
|
|
|
// NewCreateBillingCustomerIfNotExists will create a new CreateBillingCustomerIfNotExists
|
|
|
func NewCreateBillingCustomerIfNotExists(
|
|
|
config *config.Config,
|
|
|
- decoderValidator shared.RequestDecoderValidator,
|
|
|
writer shared.ResultWriter,
|
|
|
) *CreateBillingCustomerHandler {
|
|
|
return &CreateBillingCustomerHandler{
|
|
|
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
|
|
|
+ PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -35,23 +34,15 @@ func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.
|
|
|
defer span.End()
|
|
|
|
|
|
proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
|
|
|
+ user, _ := r.Context().Value(types.UserScope).(*models.User)
|
|
|
|
|
|
- request := &types.CreateBillingCustomerRequest{}
|
|
|
- if ok := c.DecodeAndValidate(w, r, request); !ok {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- // There is no easy way to pass environment variables to the frontend,
|
|
|
- // so for now pass via the backend. This is acceptable because the key is
|
|
|
- // meant to be public
|
|
|
- publishableKey := c.Config().BillingManager.GetPublishableKey(ctx)
|
|
|
if proj.BillingID != "" {
|
|
|
- c.WriteResult(w, r, publishableKey)
|
|
|
+ c.WriteResult(w, r, "")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Create customer in Stripe
|
|
|
- customerID, err := c.Config().BillingManager.CreateCustomer(ctx, request.UserEmail, proj)
|
|
|
+ customerID, err := c.Config().BillingManager.CreateCustomer(ctx, user.Email, proj)
|
|
|
if err != nil {
|
|
|
err := telemetry.Error(ctx, span, err, "error creating billing customer")
|
|
|
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating billing customer: %w", err)))
|
|
|
@@ -61,6 +52,7 @@ func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.
|
|
|
telemetry.WithAttributes(span,
|
|
|
telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
|
|
|
telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
|
|
|
+ telemetry.AttributeKV{Key: "user-email", Value: user.Email},
|
|
|
)
|
|
|
|
|
|
// Update the project record with the customer ID
|
|
|
@@ -72,5 +64,40 @@ func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ c.WriteResult(w, r, "")
|
|
|
+}
|
|
|
+
|
|
|
+// GetPublishableKeyHandler will return the configured publishable key
|
|
|
+type GetPublishableKeyHandler struct {
|
|
|
+ handlers.PorterHandlerReadWriter
|
|
|
+}
|
|
|
+
|
|
|
+// NewGetPublishableKeyHandler will return the publishable key
|
|
|
+func NewGetPublishableKeyHandler(
|
|
|
+ config *config.Config,
|
|
|
+ decoderValidator shared.RequestDecoderValidator,
|
|
|
+ writer shared.ResultWriter,
|
|
|
+) *GetPublishableKeyHandler {
|
|
|
+ return &GetPublishableKeyHandler{
|
|
|
+ PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (c *GetPublishableKeyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
+ ctx, span := telemetry.NewSpan(r.Context(), "get-publishable-key-endpoint")
|
|
|
+ defer span.End()
|
|
|
+
|
|
|
+ proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
|
|
|
+
|
|
|
+ // There is no easy way to pass environment variables to the frontend,
|
|
|
+ // so for now pass via the backend. This is acceptable because the key is
|
|
|
+ // meant to be public
|
|
|
+ publishableKey := c.Config().BillingManager.GetPublishableKey(ctx)
|
|
|
+
|
|
|
+ telemetry.WithAttributes(span,
|
|
|
+ telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
|
|
|
+ telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
|
|
|
+ )
|
|
|
+
|
|
|
c.WriteResult(w, r, publishableKey)
|
|
|
}
|