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.
212 lines
5.9 KiB
Go
212 lines
5.9 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/hanzo-fi/payments/pkg/domain/metrics"
|
|
errorsutils "github.com/hanzo-fi/payments/pkg/domain/errors"
|
|
)
|
|
|
|
type Transfer struct {
|
|
ID uint64 `json:"id"`
|
|
Reference string `json:"reference"`
|
|
Status string `json:"status"`
|
|
SourceAccount uint64 `json:"sourceAccount"`
|
|
SourceCurrency string `json:"sourceCurrency"`
|
|
SourceValue json.Number `json:"sourceValue"`
|
|
TargetAccount uint64 `json:"targetAccount"`
|
|
TargetCurrency string `json:"targetCurrency"`
|
|
TargetValue json.Number `json:"targetValue"`
|
|
Business uint64 `json:"business"`
|
|
Created string `json:"created"`
|
|
//nolint:tagliatelle // allow for clients
|
|
CustomerTransactionID string `json:"customerTransactionId"`
|
|
Details struct {
|
|
Reference string `json:"reference"`
|
|
} `json:"details"`
|
|
Rate float64 `json:"rate"`
|
|
User uint64 `json:"user"`
|
|
|
|
SourceBalanceID uint64 `json:"-"`
|
|
DestinationBalanceID uint64 `json:"-"`
|
|
|
|
CreatedAt time.Time `json:"-"`
|
|
}
|
|
|
|
func (t *Transfer) UnmarshalJSON(data []byte) error {
|
|
type Alias Transfer
|
|
|
|
aux := &struct {
|
|
Created string `json:"created"`
|
|
*Alias
|
|
}{
|
|
Alias: (*Alias)(t),
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
return err
|
|
}
|
|
|
|
var err error
|
|
|
|
t.CreatedAt, err = time.Parse("2006-01-02 15:04:05", aux.Created)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse created time: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *client) GetTransfers(ctx context.Context, profileID uint64, offset int, limit int) ([]Transfer, error) {
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "list_transfers")
|
|
|
|
req, err := http.NewRequestWithContext(ctx,
|
|
http.MethodGet, c.endpoint("v1/transfers"), http.NoBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := req.URL.Query()
|
|
q.Add("limit", fmt.Sprintf("%d", limit))
|
|
q.Add("profile", fmt.Sprintf("%d", profileID))
|
|
q.Add("offset", fmt.Sprintf("%d", offset))
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
var transfers []Transfer
|
|
var errRes wiseErrors
|
|
statusCode, err := c.httpClient.Do(ctx, req, &transfers, &errRes)
|
|
if err != nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to get transfers: %v", errRes.Error(statusCode)),
|
|
err,
|
|
)
|
|
}
|
|
|
|
for i, transfer := range transfers {
|
|
var sourceProfileID, targetProfileID uint64
|
|
if transfer.SourceAccount != 0 {
|
|
recipientAccount, err := c.GetRecipientAccount(ctx, transfer.SourceAccount)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get source profile id: %w", err)
|
|
}
|
|
|
|
sourceProfileID = recipientAccount.Profile
|
|
}
|
|
|
|
if transfer.TargetAccount != 0 {
|
|
recipientAccount, err := c.GetRecipientAccount(ctx, transfer.TargetAccount)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get target profile id: %w", err)
|
|
}
|
|
|
|
targetProfileID = recipientAccount.Profile
|
|
}
|
|
|
|
// TODO(polo): fetching balances for each transfer is not efficient
|
|
// and can be quite long. We should consider caching balances, but
|
|
// at the same time we will develop a feature soon to get balances
|
|
// for every accounts, so caching is not a solution.
|
|
switch {
|
|
case sourceProfileID == 0 && targetProfileID == 0:
|
|
// Do nothing
|
|
case sourceProfileID == targetProfileID && sourceProfileID != 0:
|
|
// Same profile id for target and source
|
|
balances, err := c.GetBalances(ctx, sourceProfileID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get balances: %w", err)
|
|
}
|
|
for _, balance := range balances {
|
|
if balance.Currency == transfer.SourceCurrency {
|
|
transfers[i].SourceBalanceID = balance.ID
|
|
}
|
|
|
|
if balance.Currency == transfer.TargetCurrency {
|
|
transfers[i].DestinationBalanceID = balance.ID
|
|
}
|
|
}
|
|
default:
|
|
if sourceProfileID != 0 {
|
|
balances, err := c.GetBalances(ctx, sourceProfileID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get balances: %w", err)
|
|
}
|
|
for _, balance := range balances {
|
|
if balance.Currency == transfer.SourceCurrency {
|
|
transfers[i].SourceBalanceID = balance.ID
|
|
}
|
|
}
|
|
}
|
|
|
|
if targetProfileID != 0 {
|
|
balances, err := c.GetBalances(ctx, targetProfileID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get balances: %w", err)
|
|
}
|
|
for _, balance := range balances {
|
|
if balance.Currency == transfer.TargetCurrency {
|
|
transfers[i].DestinationBalanceID = balance.ID
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
return transfers, nil
|
|
}
|
|
|
|
func (c *client) GetTransfer(ctx context.Context, transferID string) (*Transfer, error) {
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "get_transfer")
|
|
|
|
req, err := http.NewRequestWithContext(ctx,
|
|
http.MethodGet, c.endpoint("v1/transfers/"+transferID), http.NoBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var transfer Transfer
|
|
var errRes wiseErrors
|
|
statusCode, err := c.httpClient.Do(ctx, req, &transfer, &errRes)
|
|
if err != nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to get transfer: %v", errRes.Error(statusCode)),
|
|
err,
|
|
)
|
|
}
|
|
return &transfer, nil
|
|
}
|
|
|
|
func (c *client) CreateTransfer(ctx context.Context, quote Quote, targetAccount uint64, transactionID string) (*Transfer, error) {
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "initiate_transfer")
|
|
|
|
reqBody, err := json.Marshal(map[string]interface{}{
|
|
"targetAccount": targetAccount,
|
|
"quoteUuid": quote.ID.String(),
|
|
"customerTransactionId": transactionID,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx,
|
|
http.MethodPost, c.endpoint("v1/transfers"), bytes.NewBuffer(reqBody))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var transfer Transfer
|
|
var errRes wiseErrors
|
|
statusCode, err := c.httpClient.Do(ctx, req, &transfer, &errRes)
|
|
if err != nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to create transfer: %v", errRes.Error(statusCode)),
|
|
err,
|
|
)
|
|
}
|
|
return &transfer, nil
|
|
}
|