mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/formancehq/payments/internal/connectors/metrics"
|
|
errorsutils "github.com/formancehq/payments/internal/utils/errors"
|
|
)
|
|
|
|
//nolint:tagliatelle // allow different styled tags in client
|
|
type Transaction struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
Amount json.Number `json:"amount"`
|
|
Credit bool `json:"credit"`
|
|
SourceID string `json:"sourceId"`
|
|
Description string `json:"description"`
|
|
PostedDate string `json:"postedDate"`
|
|
TransactionDate string `json:"transactionDate"`
|
|
Account Account `json:"account"`
|
|
AdditionalInfo interface{} `json:"additionalInfo"`
|
|
}
|
|
|
|
func (c *client) GetTransactions(ctx context.Context, accountID string, page, pageSize int, fromTransactionDate time.Time) ([]Transaction, error) {
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "list_transactions")
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.buildEndpoint("accounts/%s/transactions", accountID), http.NoBody)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create accounts request: %w", err)
|
|
}
|
|
|
|
q := req.URL.Query()
|
|
q.Add("page", strconv.Itoa(page))
|
|
q.Add("size", strconv.Itoa(pageSize))
|
|
if !fromTransactionDate.IsZero() {
|
|
q.Add("fromTransactionDate", fromTransactionDate.Format("2006-01-02T15:04:05-0700"))
|
|
}
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
var res responseWrapper[[]Transaction]
|
|
var errRes modulrErrors
|
|
_, err = c.httpClient.Do(ctx, req, &res, &errRes)
|
|
if err != nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to get transactions: %v", errRes.Error()),
|
|
err,
|
|
)
|
|
}
|
|
return res.Content, nil
|
|
}
|