-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metronome business logic and a fix to the customer creation logic
- Loading branch information
Showing
27 changed files
with
751 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package billing | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/porter-dev/porter/api/server/handlers" | ||
"github.com/porter-dev/porter/api/server/shared" | ||
"github.com/porter-dev/porter/api/server/shared/apierrors" | ||
"github.com/porter-dev/porter/api/server/shared/config" | ||
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/internal/models" | ||
"github.com/porter-dev/porter/internal/telemetry" | ||
) | ||
|
||
// GetCreditsHandler is a handler for getting available credits | ||
type GetCreditsHandler struct { | ||
handlers.PorterHandlerWriter | ||
} | ||
|
||
// NewGetCreditsHandler will create a new GetCreditsHandler | ||
func NewGetCreditsHandler( | ||
config *config.Config, | ||
writer shared.ResultWriter, | ||
) *GetCreditsHandler { | ||
return &GetCreditsHandler{ | ||
PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer), | ||
} | ||
} | ||
|
||
func (c *GetCreditsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx, span := telemetry.NewSpan(r.Context(), "get-credits-endpoint") | ||
defer span.End() | ||
|
||
proj, _ := ctx.Value(types.ProjectScope).(*models.Project) | ||
|
||
if !proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) { | ||
c.WriteResult(w, r, "") | ||
} | ||
|
||
credits, err := c.Config().BillingManager.MetronomeClient.GetCustomerCredits(proj.UsageID) | ||
if err != nil { | ||
err := telemetry.Error(ctx, span, err, "error getting credits") | ||
c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) | ||
return | ||
} | ||
|
||
c.WriteResult(w, r, credits) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package billing | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/porter-dev/porter/api/server/handlers" | ||
"github.com/porter-dev/porter/api/server/shared" | ||
"github.com/porter-dev/porter/api/server/shared/config" | ||
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/internal/models" | ||
"github.com/porter-dev/porter/internal/telemetry" | ||
) | ||
|
||
// 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.StripeClient.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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.