Skip to content

Commit

Permalink
feat(fctl): add fixes with payments 1.0.0 (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Dec 1, 2023
1 parent 4fd415b commit ca677bd
Show file tree
Hide file tree
Showing 38 changed files with 826 additions and 200 deletions.
2 changes: 1 addition & 1 deletion components/fctl/cmd/payments/accounts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *ListController) Render(cmd *cobra.Command, args []string) error {
acc.ID,
acc.AccountName,
acc.CreatedAt.Format(time.RFC3339),
string(acc.ConnectorID),
acc.ConnectorID,
acc.DefaultAsset,
acc.DefaultCurrency,
acc.Reference,
Expand Down
18 changes: 17 additions & 1 deletion components/fctl/cmd/payments/bankaccounts/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/formancehq/fctl/cmd/payments/versions"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
"github.com/pkg/errors"
Expand All @@ -15,9 +16,15 @@ type CreateStore struct {
BankAccountID string `json:"bankAccountId"`
}
type CreateController struct {
PaymentsVersion versions.Version

store *CreateStore
}

func (c *CreateController) SetVersion(version versions.Version) {
c.PaymentsVersion = version
}

var _ fctl.Controller[*CreateStore] = (*CreateController)(nil)

func NewCreateStore() *CreateStore {
Expand All @@ -31,11 +38,12 @@ func NewCreateController() *CreateController {
}

func NewCreateCommand() *cobra.Command {
c := NewCreateController()
return fctl.NewCommand("create <file>|-",
fctl.WithShortDescription("Create a bank account"),
fctl.WithAliases("cr", "c"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithController[*CreateStore](NewCreateController()),
fctl.WithController[*CreateStore](c),
)
}

Expand All @@ -44,6 +52,14 @@ func (c *CreateController) GetStore() *CreateStore {
}

func (c *CreateController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
if err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
return nil, err
}

if c.PaymentsVersion < versions.V1 {
return nil, fmt.Errorf("bank accounts are only supported in >= v1.0.0")
}

soc, err := fctl.GetStackOrganizationConfig(cmd)
if err != nil {
return nil, err
Expand Down
18 changes: 17 additions & 1 deletion components/fctl/cmd/payments/bankaccounts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/formancehq/fctl/cmd/payments/versions"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
Expand All @@ -16,9 +17,15 @@ type ListStore struct {
}

type ListController struct {
PaymentsVersion versions.Version

store *ListStore
}

func (c *ListController) SetVersion(version versions.Version) {
c.PaymentsVersion = version
}

var _ fctl.Controller[*ListStore] = (*ListController)(nil)

func NewListStore() *ListStore {
Expand All @@ -38,6 +45,14 @@ func (c *ListController) GetStore() *ListStore {
}

func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
if err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
return nil, err
}

if c.PaymentsVersion < versions.V1 {
return nil, fmt.Errorf("bank accounts are only supported in >= v1.0.0")
}

cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
Expand Down Expand Up @@ -93,10 +108,11 @@ func (c *ListController) Render(cmd *cobra.Command, args []string) error {
}

func NewListCommand() *cobra.Command {
c := NewListController()
return fctl.NewCommand("list",
fctl.WithAliases("ls", "l"),
fctl.WithArgs(cobra.ExactArgs(0)),
fctl.WithShortDescription("List bank accounts"),
fctl.WithController[*ListStore](NewListController()),
fctl.WithController[*ListStore](c),
)
}
27 changes: 26 additions & 1 deletion components/fctl/cmd/payments/bankaccounts/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/formancehq/fctl/cmd/payments/versions"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
Expand All @@ -15,9 +16,15 @@ type ShowStore struct {
BankAccount *shared.BankAccount `json:"bankAccount"`
}
type ShowController struct {
PaymentsVersion versions.Version

store *ShowStore
}

func (c *ShowController) SetVersion(version versions.Version) {
c.PaymentsVersion = version
}

var _ fctl.Controller[*ShowStore] = (*ShowController)(nil)

func NewShowStore() *ShowStore {
Expand All @@ -31,11 +38,12 @@ func NewShowController() *ShowController {
}

func NewShowCommand() *cobra.Command {
c := NewShowController()
return fctl.NewCommand("get <bankAccountID>",
fctl.WithShortDescription("Get bank account"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithAliases("sh", "s"),
fctl.WithController[*ShowStore](NewShowController()),
fctl.WithController[*ShowStore](c),
)
}

Expand All @@ -44,6 +52,14 @@ func (c *ShowController) GetStore() *ShowStore {
}

func (c *ShowController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
if err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
return nil, err
}

if c.PaymentsVersion < versions.V1 {
return nil, fmt.Errorf("bank accounts are only supported in >= v1.0.0")
}

cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
Expand Down Expand Up @@ -87,6 +103,15 @@ func (c *ShowController) Render(cmd *cobra.Command, args []string) error {
tableData = append(tableData, []string{pterm.LightCyan("CreatedAt"), c.store.BankAccount.CreatedAt.Format(time.RFC3339)})
tableData = append(tableData, []string{pterm.LightCyan("Country"), c.store.BankAccount.Country})
tableData = append(tableData, []string{pterm.LightCyan("ConnectorID"), string(c.store.BankAccount.ConnectorID)})
if c.store.BankAccount.AccountNumber != nil {
tableData = append(tableData, []string{pterm.LightCyan("AccountNumber"), *c.store.BankAccount.AccountNumber})
}
if c.store.BankAccount.Iban != nil {
tableData = append(tableData, []string{pterm.LightCyan("Iban"), *c.store.BankAccount.Iban})
}
if c.store.BankAccount.SwiftBicCode != nil {
tableData = append(tableData, []string{pterm.LightCyan("SwiftBicCode"), *c.store.BankAccount.SwiftBicCode})
}

if err := pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
Expand Down
136 changes: 77 additions & 59 deletions components/fctl/cmd/payments/connectors/getconfig.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package connectors

import (
"errors"
"fmt"
"strings"

"github.com/formancehq/fctl/cmd/payments/connectors/internal"
"github.com/formancehq/fctl/cmd/payments/connectors/views"
"github.com/formancehq/fctl/cmd/payments/versions"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
Expand All @@ -19,10 +20,20 @@ var (

type PaymentsGetConfigStore struct {
ConnectorConfig *shared.ConnectorConfigResponse `json:"connectorConfig"`
Provider string `json:"provider"`
ConnectorID string `json:"connectorId"`
}
type PaymentsGetConfigController struct {
PaymentsVersion versions.Version

store *PaymentsGetConfigStore
args []string

providerNameFlag string
connectorIDFlag string
}

func (c *PaymentsGetConfigController) SetVersion(version versions.Version) {
c.PaymentsVersion = version
}

var _ fctl.Controller[*PaymentsGetConfigStore] = (*PaymentsGetConfigController)(nil)
Expand All @@ -33,17 +44,22 @@ func NewDefaultPaymentsGetConfigStore() *PaymentsGetConfigStore {

func NewPaymentsGetConfigController() *PaymentsGetConfigController {
return &PaymentsGetConfigController{
store: NewDefaultPaymentsGetConfigStore(),
store: NewDefaultPaymentsGetConfigStore(),
providerNameFlag: "provider",
connectorIDFlag: "connector-id",
}
}

func NewGetConfigCommand() *cobra.Command {
return fctl.NewCommand("get-config <connector-name>",
c := NewPaymentsGetConfigController()
return fctl.NewCommand("get-config",
fctl.WithAliases("getconfig", "getconf", "gc", "get", "g"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithArgs(cobra.ExactArgs(0)),
fctl.WithValidArgs(connectorsAvailable...),
fctl.WithStringFlag("provider", "", "Provider name"),
fctl.WithStringFlag("connector-id", "", "Connector ID"),
fctl.WithShortDescription(fmt.Sprintf("Read a connector config (Connectors available: %s)", connectorsAvailable)),
fctl.WithController[*PaymentsGetConfigStore](NewPaymentsGetConfigController()),
fctl.WithController[*PaymentsGetConfigStore](c),
)
}

Expand All @@ -52,6 +68,12 @@ func (c *PaymentsGetConfigController) GetStore() *PaymentsGetConfigStore {
}

func (c *PaymentsGetConfigController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
if err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
return nil, err
}

provider := fctl.GetString(cmd, c.providerNameFlag)
connectorID := fctl.GetString(cmd, c.connectorIDFlag)

cfg, err := fctl.GetConfig(cmd)
if err != nil {
Expand All @@ -73,20 +95,52 @@ func (c *PaymentsGetConfigController) Run(cmd *cobra.Command, args []string) (fc
return nil, err
}

response, err := client.Payments.ReadConnectorConfig(cmd.Context(), operations.ReadConnectorConfigRequest{
Connector: shared.Connector(args[0]),
})
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
switch c.PaymentsVersion {
case versions.V0:
if provider == "" {
return nil, fmt.Errorf("provider is required")
}

response, err := client.Payments.ReadConnectorConfig(cmd.Context(), operations.ReadConnectorConfigRequest{
Connector: shared.Connector(provider),
})
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.Provider = provider
c.store.ConnectorConfig = response.ConnectorConfigResponse

case versions.V1:
if provider == "" {
return nil, fmt.Errorf("provider is required")
}

if connectorID == "" {
return nil, fmt.Errorf("connector-id is required")
}

response, err := client.Payments.ReadConnectorConfigV1(cmd.Context(), operations.ReadConnectorConfigV1Request{
Connector: shared.Connector(provider),
ConnectorID: connectorID,
})
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.Provider = provider
c.store.ConnectorID = connectorID
c.store.ConnectorConfig = response.ConnectorConfigResponse
}

c.args = args
c.store.ConnectorConfig = response.ConnectorConfigResponse

return c, err

}
Expand All @@ -95,7 +149,7 @@ func (c *PaymentsGetConfigController) Run(cmd *cobra.Command, args []string) (fc
func (c *PaymentsGetConfigController) Render(cmd *cobra.Command, args []string) error {
var err error

switch c.args[0] {
switch strings.ToLower(c.store.Provider) {
case internal.StripeConnector:
err = views.DisplayStripeConfig(cmd, c.store.ConnectorConfig)
case internal.ModulrConnector:
Expand All @@ -106,50 +160,14 @@ func (c *PaymentsGetConfigController) Render(cmd *cobra.Command, args []string)
err = views.DisplayCurrencyCloudConfig(cmd, c.store.ConnectorConfig)
case internal.WiseConnector:
err = views.DisplayWiseConfig(cmd, c.store.ConnectorConfig)
case internal.MangoPayConnector:
err = views.DisplayMangopayConfig(cmd, c.store.ConnectorConfig)
case internal.MoneycorpConnector:
err = views.DisplayMoneycorpConfig(cmd, c.store.ConnectorConfig)
default:
pterm.Error.WithWriter(cmd.OutOrStderr()).Printfln("Connection unknown.")
}

return err

}

func displayMangoPayConfig(cmd *cobra.Command, connectorConfig *shared.ConnectorConfigResponse) error {
config, ok := connectorConfig.Data.(*shared.MangoPayConfig)
if !ok {
return errors.New("invalid currency cloud connector config")
}

tableData := pterm.TableData{}
tableData = append(tableData, []string{pterm.LightCyan("API key:"), config.APIKey})
tableData = append(tableData, []string{pterm.LightCyan("Client ID:"), config.ClientID})
tableData = append(tableData, []string{pterm.LightCyan("Endpoint:"), config.Endpoint})

if err := pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render(); err != nil {
return err
}
return nil
}

func displayMoneycorpConfig(cmd *cobra.Command, connectorConfig *shared.ConnectorConfigResponse) error {
config, ok := connectorConfig.Data.(*shared.MoneycorpConfig)
if !ok {
return errors.New("invalid currency cloud connector config")
}

tableData := pterm.TableData{}
tableData = append(tableData, []string{pterm.LightCyan("API key:"), config.APIKey})
tableData = append(tableData, []string{pterm.LightCyan("Client ID:"), config.ClientID})
tableData = append(tableData, []string{pterm.LightCyan("Endpoint:"), config.Endpoint})

if err := pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render(); err != nil {
return err
}
return nil
}
Loading

0 comments on commit ca677bd

Please sign in to comment.