Files
zeekay 7d727874d5 chore: rebrand Formance Payments -> Hanzo Payments (hanzo-fi)
- 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.
2026-07-05 13:08:55 -07:00

117 lines
2.9 KiB
Go

package mangopay
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/hanzo-fi/payments/ce/plugins/mangopay/client"
"github.com/hanzo-fi/payments/pkg/domain/models"
"github.com/hanzo-fi/payments/pkg/domain/pagination"
)
type externalAccountsState struct {
LastPage int `json:"lastPage"`
LastCreationDate time.Time `json:"lastCreationDate"`
}
func (p *Plugin) fetchNextExternalAccounts(ctx context.Context, req models.FetchNextExternalAccountsRequest) (models.FetchNextExternalAccountsResponse, error) {
var oldState externalAccountsState
if req.State != nil {
if err := json.Unmarshal(req.State, &oldState); err != nil {
return models.FetchNextExternalAccountsResponse{}, err
}
} else {
oldState = externalAccountsState{
// Mangopay pages start at 1
LastPage: 1,
}
}
var from client.User
if req.FromPayload == nil {
return models.FetchNextExternalAccountsResponse{}, errors.New("missing from payload when fetching external accounts")
}
if err := json.Unmarshal(req.FromPayload, &from); err != nil {
return models.FetchNextExternalAccountsResponse{}, err
}
newState := externalAccountsState{
LastPage: oldState.LastPage,
LastCreationDate: oldState.LastCreationDate,
}
var accounts []models.PSPAccount
needMore := false
hasMore := false
for page := oldState.LastPage; ; page++ {
newState.LastPage = page
pagedExternalAccounts, err := p.client.GetBankAccounts(ctx, from.ID, page, req.PageSize)
if err != nil {
return models.FetchNextExternalAccountsResponse{}, err
}
accounts, err = fillExternalAccounts(pagedExternalAccounts, accounts, from, oldState)
if err != nil {
return models.FetchNextExternalAccountsResponse{}, err
}
needMore, hasMore = pagination.ShouldFetchMore(accounts, pagedExternalAccounts, req.PageSize)
if !needMore || !hasMore {
break
}
}
if !needMore {
accounts = accounts[:req.PageSize]
}
if len(accounts) > 0 {
newState.LastCreationDate = accounts[len(accounts)-1].CreatedAt
}
payload, err := json.Marshal(newState)
if err != nil {
return models.FetchNextExternalAccountsResponse{}, err
}
return models.FetchNextExternalAccountsResponse{
ExternalAccounts: accounts,
NewState: payload,
HasMore: hasMore,
}, nil
}
func fillExternalAccounts(
pagedExternalAccounts []client.BankAccount,
accounts []models.PSPAccount,
from client.User,
oldState externalAccountsState,
) ([]models.PSPAccount, error) {
for _, bankAccount := range pagedExternalAccounts {
creationDate := time.Unix(bankAccount.CreationDate, 0)
if creationDate.Before(oldState.LastCreationDate) {
continue
}
raw, err := json.Marshal(bankAccount)
if err != nil {
return nil, err
}
accounts = append(accounts, models.PSPAccount{
Reference: bankAccount.ID,
CreatedAt: creationDate,
Name: &bankAccount.OwnerName,
Metadata: map[string]string{
"user_id": from.ID,
},
Raw: raw,
})
}
return accounts, nil
}