mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
- go.mod module path + all self-imports: github.com/formancehq/payments -> github.com/hanzo-fi/payments (1590 .go files / 3152 refs; 20 modules: root, pkg/client, pkg/domain, 18 ce/plugins). External dep kept: formancehq/go-libs. - plugins: each ce/plugins/* consumed a *published* pkg/domain; added local 'replace .../pkg/domain => ../../../pkg/domain' (consistent with root) so every standalone plugin module builds in-monorepo. compile-plugin.sh MOD var rebranded too. - branding: README, LICENSE (dual MIT+EE; upstream copyright retained + Hanzo added), goreleaser (release images -> registry.hanzo.ai/hanzo-fi, OCI labels, homepage), proto go_package, renovate, speakeasy gen.yaml, connector-template *.gotpl. - CI: removed Numary/Formance workflows + composite action; added canonical hanzo.yml + cicd.yml (hanzoai/ci, gated HANZO_CI_ENABLED) + secretless ci.yml (go build/vet). - kept as data/compat keys: wire capability enums ALLOW_FORMANCE_*, generated SDK type client.Formance, domain field FormanceRedirectURL, OTEL_SERVICE_NAME, upstream base image. - verified: go build ./... (default + -tags ee) + all 20 nested modules green.
113 lines
2.8 KiB
Go
113 lines
2.8 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/hanzo-fi/payments/pkg/domain/httpwrapper"
|
|
"github.com/hanzo-fi/payments/pkg/domain/metrics"
|
|
errorsutils "github.com/hanzo-fi/payments/pkg/domain/errors"
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
|
)
|
|
|
|
// Cannot use "golang.org/x/oauth2/clientcredentials" lib because moneycorp
|
|
// is only accepting request with "application/json" content type, and the lib
|
|
// sets it as application/x-www-form-urlencoded, giving us a 415 error.
|
|
type apiTransport struct {
|
|
connectorName string
|
|
|
|
clientID string
|
|
apiKey string
|
|
endpoint string
|
|
|
|
accessToken string
|
|
accessTokenExpiresAt time.Time
|
|
|
|
underlying *otelhttp.Transport
|
|
}
|
|
|
|
func (t *apiTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
if err := t.ensureAccessTokenIsValid(req.Context()); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Header.Add("Authorization", "Bearer "+t.accessToken)
|
|
|
|
return t.underlying.RoundTrip(req)
|
|
}
|
|
|
|
func (t *apiTransport) ensureAccessTokenIsValid(ctx context.Context) error {
|
|
if t.accessTokenExpiresAt.After(time.Now().Add(5 * time.Second)) {
|
|
return nil
|
|
}
|
|
|
|
return t.login(ctx)
|
|
}
|
|
|
|
type loginRequest struct {
|
|
ClientID string `json:"loginId"`
|
|
APIKey string `json:"apiKey"`
|
|
}
|
|
|
|
type loginResponse struct {
|
|
Data struct {
|
|
AccessToken string `json:"accessToken"`
|
|
ExpiresIn int `json:"expiresIn"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
func (t *apiTransport) login(ctx context.Context) error {
|
|
lreq := loginRequest{
|
|
ClientID: t.clientID,
|
|
APIKey: t.apiKey,
|
|
}
|
|
|
|
requestBody, err := json.Marshal(lreq)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal login request: %w", err)
|
|
}
|
|
|
|
config := &httpwrapper.Config{
|
|
Transport: metrics.NewTransport(t.connectorName, metrics.TransportOpts{}),
|
|
}
|
|
httpClient := httpwrapper.NewClient(config)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
|
|
t.endpoint+"/login", bytes.NewBuffer(requestBody))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create login request: %w", err)
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "authenticate")
|
|
|
|
var res loginResponse
|
|
var errRes moneycorpErrors
|
|
statusCode, err := httpClient.Do(ctx, req, &res, &errRes)
|
|
if err != nil {
|
|
return errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to login: %v", errRes.Error()),
|
|
err,
|
|
)
|
|
}
|
|
|
|
if statusCode != http.StatusOK {
|
|
if statusCode >= http.StatusInternalServerError {
|
|
return toError(statusCode, errRes).Error()
|
|
}
|
|
return errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to login: %v", errRes.Error()),
|
|
toError(statusCode, errRes).Error(),
|
|
)
|
|
}
|
|
|
|
t.accessToken = res.Data.AccessToken
|
|
t.accessTokenExpiresAt = time.Now().Add(time.Duration(res.Data.ExpiresIn) * time.Second)
|
|
return nil
|
|
}
|