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.
105 lines
4.0 KiB
Go
105 lines
4.0 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzo-fi/payments/pkg/domain/httpwrapper"
|
|
"github.com/hanzo-fi/payments/pkg/domain/metrics"
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
|
)
|
|
|
|
type Client interface {
|
|
GetAccounts(ctx context.Context, pageSize int, cursor string, createdAtAfter time.Time) ([]*Account, string, error)
|
|
GetAccountBalance(ctx context.Context, accountID string) (*Balance, time.Time, error)
|
|
GetExternalAccounts(ctx context.Context, pageSize int, cursor string) ([]*ExternalAccount, string, error)
|
|
GetTransactions(ctx context.Context, pageSize int, timeline Timeline) ([]*Transaction, Timeline, bool, error)
|
|
GetTransaction(ctx context.Context, transactionID string) (*Transaction, error)
|
|
GetPendingTransactions(ctx context.Context, pageSize int, timeline Timeline) ([]*Transaction, Timeline, bool, error)
|
|
GetPendingTransaction(ctx context.Context, transactionID string) (*Transaction, error)
|
|
GetDeclinedTransactions(ctx context.Context, pageSize int, timeline Timeline) ([]*Transaction, Timeline, bool, error)
|
|
GetDeclinedTransaction(ctx context.Context, transactionID string) (*Transaction, error)
|
|
InitiateTransfer(ctx context.Context, tr *TransferRequest, idempotencyKey string) (*TransferResponse, error)
|
|
InitiateACHTransferPayout(ctx context.Context, pr *ACHPayoutRequest, idempotencyKey string) (*PayoutResponse, error)
|
|
InitiateRTPTransferPayout(ctx context.Context, pr *RTPPayoutRequest, idempotencyKey string) (*PayoutResponse, error)
|
|
InitiateCheckTransferPayout(ctx context.Context, pr *CheckPayoutRequest, idempotencyKey string) (*PayoutResponse, error)
|
|
InitiateWireTransferPayout(ctx context.Context, pr *WireTransferPayoutRequest, idempotencyKey string) (*PayoutResponse, error)
|
|
CreateBankAccount(ctx context.Context, pr *BankAccountRequest, idempotencyKey string) (*BankAccountResponse, error)
|
|
CreateEventSubscription(ctx context.Context, req *CreateEventSubscriptionRequest, idempotencyKey string) (*EventSubscription, error)
|
|
ListEventSubscriptions(ctx context.Context) ([]*EventSubscription, error)
|
|
UpdateEventSubscription(ctx context.Context, req *UpdateEventSubscriptionRequest, webhookID string) (*EventSubscription, error)
|
|
SetHttpClient(httpClient HTTPClient)
|
|
}
|
|
|
|
//go:generate mockgen -destination=http_generated.go -package=client . HTTPClient
|
|
type HTTPClient interface {
|
|
Do(context.Context, *http.Request, any, any) (int, error)
|
|
}
|
|
|
|
type client struct {
|
|
httpClient httpwrapper.Client
|
|
|
|
apiKey string
|
|
endpoint string
|
|
webhookSharedSecret string
|
|
}
|
|
|
|
type apiTransport struct {
|
|
client *client
|
|
underlying http.RoundTripper
|
|
}
|
|
|
|
func (t *apiTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", t.client.apiKey))
|
|
req.Header.Add("Content-Type", "application/json")
|
|
return t.underlying.RoundTrip(req)
|
|
}
|
|
|
|
type ResponseWrapper[t any] struct {
|
|
Data t `json:"data"`
|
|
NextCursor string `json:"next_cursor"`
|
|
ResponseMetadata struct {
|
|
NextCursor string `json:"next_cursor"`
|
|
} `json:"response_metadata"`
|
|
}
|
|
|
|
func New(connectorName, apiKey, endpoint, webhookSharedSecret string) *client {
|
|
client := &client{
|
|
apiKey: apiKey,
|
|
endpoint: endpoint,
|
|
webhookSharedSecret: webhookSharedSecret,
|
|
}
|
|
|
|
apiTransport := &apiTransport{
|
|
client: client,
|
|
underlying: otelhttp.NewTransport(http.DefaultTransport),
|
|
}
|
|
|
|
config := &httpwrapper.Config{
|
|
Transport: metrics.NewTransport("increase", metrics.TransportOpts{
|
|
Transport: apiTransport,
|
|
}),
|
|
}
|
|
|
|
client.httpClient = httpwrapper.NewClient(config)
|
|
|
|
return client
|
|
}
|
|
|
|
func (c *client) SetHttpClient(httpClient HTTPClient) {
|
|
c.httpClient = httpClient
|
|
}
|
|
|
|
func (c *client) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
|
|
endpoint := fmt.Sprintf("%s/%s", strings.TrimSuffix(c.endpoint, "/"), path)
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint, body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
return req, nil
|
|
}
|