Files
Quentin-David-24andGitHub 1d0d7e5ae7 fix(tink): EN-543 Fix how to map tink amounts (#612)
* fix(tink): EN-543 Fix how to map tink amounts

* Validate currency code in the mapper

* Set transaction amount to the standard precision

* Fix import
2025-12-04 14:54:44 +00:00

54 lines
1.6 KiB
Go

package tink
import (
"context"
"encoding/json"
"github.com/formancehq/payments/internal/connectors/plugins/public/tink/client"
"github.com/formancehq/payments/internal/models"
)
func (p *Plugin) fetchNextBalances(ctx context.Context, req models.FetchNextBalancesRequest) (models.FetchNextBalancesResponse, error) {
var pspAccount models.PSPAccount
if err := json.Unmarshal(req.FromPayload, &pspAccount); err != nil {
return models.FetchNextBalancesResponse{}, err
}
pspBalance, err := toPSPBalance(pspAccount)
if err != nil {
return models.FetchNextBalancesResponse{}, err
}
pspBalances := make([]models.PSPBalance, 0, 1)
pspBalances = append(pspBalances, pspBalance)
return models.FetchNextBalancesResponse{
Balances: pspBalances,
}, nil
}
func toPSPBalance(
pspAccount models.PSPAccount,
) (models.PSPBalance, error) {
var account client.Account
if err := json.Unmarshal(pspAccount.Raw, &account); err != nil {
return models.PSPBalance{}, err
}
balance := account.Balances.Booked
amount, asset, err := MapTinkAmount(balance.Amount.Value.Value, balance.Amount.Value.Scale, balance.Amount.CurrencyCode)
if err != nil {
return models.PSPBalance{}, err
}
return models.PSPBalance{
AccountReference: account.ID,
CreatedAt: account.Dates.LastRefreshed.UTC(),
Amount: amount,
Asset: *asset,
PsuID: pspAccount.PsuID,
OpenBankingConnectionID: pspAccount.OpenBankingConnectionID, // Note -- currently Tink doesn't forward the connectionID, so this is mostly wishful thinking.
}, nil
}