Files
CrimsonandGitHub db0561d07e fix(column): Various fixes for Column Payments (#431)
* Set webhook ID as payment adjustment ID when adjustment created via webhook

* Fetch Column payments oldest to newest

* Ensure that webhook secrets are fetched when containers restart

* Regenerate plugin code for tests

* Add paging to fetch webhook request

* Add WorkerPool unit test to increase test coverage

* Guard against unhandled webhooks in Column plugin initiation

* Rename Timeline.LatestID to Timeline.LastSeenID for clarity

* Add metadata column to webhooks_configs table

* Persist webhook_configs.metadata

* Persist Column webhook secrets as metadata in webhooks_configs

* Remove PSPWebhookConfig from InstallResponse

* Stop passing context to initWebhookConfig()

* Don't pass context to RegisterPlugin func

* Don't pass context to plugins.RegisterPlugin
2025-05-12 09:24:59 +00:00

105 lines
3.4 KiB
Go

package client
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/formancehq/payments/internal/connectors/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
}