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
3.4 KiB
Go
105 lines
3.4 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/hanzo-fi/payments/pkg/domain/metrics"
|
|
)
|
|
|
|
type Transaction struct {
|
|
ID string `json:"id"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
CompletedAt string `json:"completed_at"`
|
|
Status string `json:"status"`
|
|
Type string `json:"type"`
|
|
Amount int64 `json:"amount"`
|
|
CurrencyCode string `json:"currency_code"`
|
|
IsIncoming bool `json:"is_incoming"`
|
|
IdempotencyKey string `json:"idempotency_key"`
|
|
Description string `json:"description"`
|
|
SenderInternalAccount struct {
|
|
BankAccountID string `json:"bank_account_id"`
|
|
AccountNumberID string `json:"account_number_id"`
|
|
} `json:"sender_internal_account"`
|
|
ExternalSource struct {
|
|
BankName string `json:"bank_name"`
|
|
SenderName string `json:"sender_name"`
|
|
CounterpartyID string `json:"counterparty_id"`
|
|
} `json:"external_source"`
|
|
ReceiverInternalAccount struct {
|
|
BankAccountID string `json:"bank_account_id"`
|
|
AccountNumberID string `json:"account_number_id"`
|
|
} `json:"receiver_internal_account"`
|
|
ExternalDestination struct {
|
|
CounterpartyID string `json:"counterparty_id"`
|
|
} `json:"external_destination"`
|
|
}
|
|
|
|
type TransactionResponseWrapper[t any] struct {
|
|
Transfers t `json:"transfers"`
|
|
HasMore bool `json:"has_more"`
|
|
}
|
|
|
|
func (c *client) GetTransactions(ctx context.Context, t Timeline, pageSize int) (results []*Transaction, timeline Timeline, hasMore bool, err error) {
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "list_transactions")
|
|
var endpoint = "transfers"
|
|
|
|
timeline = t
|
|
results = make([]*Transaction, 0, pageSize+1)
|
|
|
|
// scroll back in time to find the oldest record so that we can fetch data in chronological order
|
|
if !timeline.IsCaughtUp() {
|
|
var oldest *Transaction
|
|
oldest, timeline, hasMore, err = c.scanForOldest(ctx, timeline, endpoint, pageSize)
|
|
if err != nil {
|
|
return results, timeline, false, err
|
|
}
|
|
// either there are no records or we haven't found the start yet
|
|
if !timeline.IsCaughtUp() {
|
|
return results, timeline, hasMore, nil
|
|
}
|
|
results = append(results, oldest)
|
|
}
|
|
|
|
// start fetching data chronologically
|
|
req, err := c.newRequest(ctx, http.MethodGet, endpoint, http.NoBody)
|
|
if err != nil {
|
|
return nil, timeline, false, fmt.Errorf("failed to create transactions request: %w", err)
|
|
}
|
|
|
|
q := req.URL.Query()
|
|
q.Add("limit", strconv.Itoa(pageSize))
|
|
if timeline.LastSeenID != "" {
|
|
q.Add("ending_before", timeline.LastSeenID)
|
|
}
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
var res TransactionResponseWrapper[[]*Transaction]
|
|
var errRes columnError
|
|
_, err = c.httpClient.Do(ctx, req, &res, &errRes)
|
|
if err != nil {
|
|
return nil, timeline, false, fmt.Errorf("failed to get transactions: %w %w", err, errRes.Error())
|
|
}
|
|
|
|
// Column returns data in reverse chronological order so we need to reverse the slice
|
|
transactions := reverseTransactions(res.Transfers)
|
|
results = append(results, transactions...)
|
|
if len(results) > 0 {
|
|
timeline.LastSeenID = results[len(results)-1].ID
|
|
}
|
|
return results, timeline, res.HasMore, nil
|
|
}
|
|
|
|
func reverseTransactions(in []*Transaction) []*Transaction {
|
|
out := make([]*Transaction, len(in))
|
|
copy(out, in)
|
|
for i, j := 0, len(out)-1; i < j; i, j = i+1, j-1 {
|
|
out[i], out[j] = out[j], out[i]
|
|
}
|
|
return out
|
|
}
|