mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package plaid
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/types/currency"
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/types/pointer"
|
|
"github.com/hanzo-fi/payments/ce/plugins/plaid/client"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
"github.com/google/uuid"
|
|
"github.com/plaid/plaid-go/v34/plaid"
|
|
)
|
|
|
|
func (p *Plugin) fetchNextAccounts(ctx context.Context, req models.FetchNextAccountsRequest) (models.FetchNextAccountsResponse, error) {
|
|
var from models.OpenBankingForwardedUserFromPayload
|
|
if err := json.Unmarshal(req.FromPayload, &from); err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
var baseWebhook client.BaseWebhooks
|
|
if err := json.Unmarshal(from.FromPayload, &baseWebhook); err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
resp, err := p.client.ListAccounts(
|
|
ctx,
|
|
from.OpenBankingConnection.AccessToken.Token,
|
|
)
|
|
if err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
accounts := make([]models.PSPAccount, 0, len(resp.Accounts))
|
|
for _, account := range resp.Accounts {
|
|
accounts = append(accounts, translatePlaidAccountToPSPAccount(account, from.PSUID, from.OpenBankingConnection.ConnectionID))
|
|
}
|
|
|
|
return models.FetchNextAccountsResponse{
|
|
Accounts: accounts,
|
|
}, nil
|
|
}
|
|
|
|
func translatePlaidAccountToPSPAccount(account plaid.AccountBase, psuID uuid.UUID, connectionID string) models.PSPAccount {
|
|
raw, err := json.Marshal(account)
|
|
if err != nil {
|
|
return models.PSPAccount{}
|
|
}
|
|
|
|
return models.PSPAccount{
|
|
Reference: account.AccountId,
|
|
CreatedAt: time.Now().UTC(),
|
|
Name: &account.Name,
|
|
DefaultAsset: pointer.For(currency.FormatAsset(supportedCurrenciesWithDecimal, account.Balances.GetIsoCurrencyCode())),
|
|
PsuID: &psuID,
|
|
OpenBankingConnectionID: &connectionID,
|
|
Metadata: map[string]string{
|
|
"accountType": string(account.Type),
|
|
},
|
|
Raw: raw,
|
|
}
|
|
}
|