-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: stripe transfers integration #64
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4458976
feat: stripe transfers integration
darkmatterpool eb31686
feat: export connector names
darkmatterpool b5fdb93
feat: add support for assets
darkmatterpool 9ea22d4
fix: currency parameter
darkmatterpool 7a424d8
fix: forward context to stripe params & use context for config fetch
darkmatterpool File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,76 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/formancehq/payments/internal/pkg/integration" | ||
"github.com/formancehq/payments/internal/pkg/payments" | ||
|
||
"github.com/formancehq/go-libs/sharedauth" | ||
"github.com/gorilla/mux" | ||
"github.com/spf13/viper" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux" | ||
) | ||
|
||
func httpRouter(db *mongo.Database, client *mongo.Client, connectorHandlers []connectorHandler) (*mux.Router, error) { | ||
rootMux := mux.NewRouter() | ||
|
||
if viper.GetBool(otelTracesFlag) { | ||
rootMux.Use(otelmux.Middleware(serviceName)) | ||
} | ||
|
||
rootMux.Use(recoveryHandler(httpRecoveryFunc)) | ||
rootMux.Use(httpCorsHandler()) | ||
rootMux.Use(httpServeFunc) | ||
|
||
rootMux.Path("/_health").Handler(healthHandler(client)) | ||
rootMux.Path("/_live").Handler(liveHandler()) | ||
|
||
authGroup := rootMux.Name("authenticated").Subrouter() | ||
|
||
if methods := sharedAuthMethods(); len(methods) > 0 { | ||
authGroup.Use(sharedauth.Middleware(methods...)) | ||
} | ||
|
||
authGroup.HandleFunc("/connectors", readConnectorsHandler(db)) | ||
connectorGroup := authGroup.PathPrefix("/connectors").Subrouter() | ||
|
||
connectorGroup.Path("/configs").Handler(connectorConfigsHandler()) | ||
|
||
for _, h := range connectorHandlers { | ||
connectorGroup.PathPrefix("/" + h.Name).Handler( | ||
http.StripPrefix("/connectors", h.Handler), | ||
) | ||
} | ||
|
||
// TODO: It's not ideal to define it explicitly here | ||
// Refactor it when refactoring the HTTP lib. | ||
connectorGroup.Path("/stripe/transfers").Methods(http.MethodPost). | ||
Handler(handleStripeTransfers(db)) | ||
|
||
authGroup.PathPrefix("/").Handler(paymentsRouter(db)) | ||
|
||
return rootMux, nil | ||
} | ||
|
||
func connectorRouter[Config payments.ConnectorConfigObject, Descriptor payments.TaskDescriptor]( | ||
name string, | ||
manager *integration.ConnectorManager[Config, Descriptor], | ||
) *mux.Router { | ||
r := mux.NewRouter() | ||
|
||
r.Path("/" + name).Methods(http.MethodPost).Handler(install(manager)) | ||
|
||
r.Path("/" + name + "/reset").Methods(http.MethodPost).Handler(reset(manager)) | ||
|
||
r.Path("/" + name).Methods(http.MethodDelete).Handler(uninstall(manager)) | ||
|
||
r.Path("/" + name + "/config").Methods(http.MethodGet).Handler(readConfig(manager)) | ||
|
||
r.Path("/" + name + "/tasks").Methods(http.MethodGet).Handler(listTasks(manager)) | ||
|
||
r.Path("/" + name + "/tasks/{taskID}").Methods(http.MethodGet).Handler(readTask(manager)) | ||
|
||
return r | ||
} |
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,104 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/formancehq/payments/internal/pkg/integration" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
|
||
"github.com/pkg/errors" | ||
|
||
stripeConnector "github.com/formancehq/payments/internal/pkg/connectors/stripe" | ||
"github.com/stripe/stripe-go/v72" | ||
"github.com/stripe/stripe-go/v72/transfer" | ||
) | ||
|
||
type stripeTransferRequest struct { | ||
Amount int64 `json:"amount"` | ||
Asset string `json:"asset"` | ||
Destination string `json:"destination"` | ||
Metadata map[string]string `json:"metadata"` | ||
|
||
currency string | ||
} | ||
|
||
func (req *stripeTransferRequest) validate() error { | ||
if req.Amount <= 0 { | ||
return errors.New("amount must be greater than 0") | ||
} | ||
|
||
if req.Asset == "" { | ||
return errors.New("asset is required") | ||
} | ||
|
||
if req.Asset != "USD/2" && req.Asset != "EUR/2" { | ||
return errors.New("asset must be USD/2 or EUR/2") | ||
} | ||
|
||
req.currency = req.Asset[:3] | ||
|
||
if req.Destination == "" { | ||
return errors.New("destination is required") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func handleStripeTransfers(db *mongo.Database) http.HandlerFunc { | ||
connectorStore := integration.NewMongoDBConnectorStore(db) | ||
var cfg stripeConnector.Config | ||
|
||
err := connectorStore.ReadConfig(context.Background(), stripeConnector.Name, &cfg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
stripe.Key = cfg.APIKey | ||
|
||
return func(w http.ResponseWriter, r *http.Request) { | ||
var transferRequest stripeTransferRequest | ||
|
||
err = json.NewDecoder(r.Body).Decode(&transferRequest) | ||
if err != nil { | ||
handleError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
err = transferRequest.validate() | ||
if err != nil { | ||
handleError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
params := &stripe.TransferParams{ | ||
Amount: stripe.Int64(transferRequest.Amount), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, can you forward context on params? |
||
Currency: stripe.String(transferRequest.currency), | ||
Destination: stripe.String(transferRequest.Destination), | ||
} | ||
|
||
for k, v := range transferRequest.Metadata { | ||
params.AddMetadata(k, v) | ||
} | ||
|
||
transferResponse, err := transfer.New(params) | ||
if err != nil { | ||
handleServerError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
|
||
err = json.NewEncoder(w).Encode(transferResponse) | ||
if err != nil { | ||
handleServerError(w, r, err) | ||
|
||
return | ||
} | ||
} | ||
} |
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
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
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
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
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading config out of the request scope will "freeze" api key for the service instance.
Not allowing to update the api key if needed, without a restart of the service.
Right?