-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stripe transfers integration (#64)
* feat: stripe transfers integration Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * feat: export connector names Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * feat: add support for assets Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * fix: currency parameter Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * fix: forward context to stripe params & use context for config fetch Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com>
- Loading branch information
1 parent
461f5a0
commit 06ee7a9
Showing
24 changed files
with
257 additions
and
96 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
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,109 @@ | ||
package api | ||
|
||
import ( | ||
"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) | ||
|
||
return func(w http.ResponseWriter, r *http.Request) { | ||
var cfg stripeConnector.Config | ||
|
||
err := connectorStore.ReadConfig(r.Context(), stripeConnector.Name, &cfg) | ||
if err != nil { | ||
handleError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
stripe.Key = cfg.APIKey | ||
|
||
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{ | ||
Params: stripe.Params{ | ||
Context: r.Context(), | ||
}, | ||
Amount: stripe.Int64(transferRequest.Amount), | ||
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.