Skip to content

Commit

Permalink
fix(payment): missing check for account type (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Feb 7, 2024
1 parent 1b49aac commit 412f26d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ var (
},
},
}

sourceAccountID = models.AccountID{
Reference: "acc1",
ConnectorID: connectorDummyPay.ID,
}

destinationAccountID = models.AccountID{
Reference: "acc2",
ConnectorID: connectorDummyPay.ID,
}

destinationExternalAccountID = models.AccountID{
Reference: "acc3",
ConnectorID: connectorDummyPay.ID,
}
)

type MockStore struct {
Expand Down Expand Up @@ -151,6 +166,24 @@ func (m *MockStore) UpsertAccounts(ctx context.Context, accounts []*models.Accou
}

func (m *MockStore) GetAccount(ctx context.Context, id string) (*models.Account, error) {
switch id {
case sourceAccountID.String():
return &models.Account{
ID: sourceAccountID,
Type: models.AccountTypeInternal,
}, nil
case destinationAccountID.String():
return &models.Account{
ID: destinationAccountID,
Type: models.AccountTypeInternal,
}, nil
case destinationExternalAccountID.String():
return &models.Account{
ID: destinationAccountID,
Type: models.AccountTypeExternal,
}, nil
}

return nil, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,28 @@ func (s *Service) CreateTransferInitiation(ctx context.Context, req *CreateTrans
}
}

_, err := s.store.GetAccount(ctx, req.DestinationAccountID)
destinationAccount, err := s.store.GetAccount(ctx, req.DestinationAccountID)
if err != nil {
return nil, newStorageError(err, "getting destination account")
}

transferType := models.MustTransferInitiationTypeFromString(req.Type)

switch transferType {
case models.TransferInitiationTypeTransfer:
if destinationAccount.Type != models.AccountTypeInternal {
// account should be internal when doing a transfer, return an error
return nil, errors.Wrap(ErrValidation, "destination account must be internal when doing a transfer")
}
case models.TransferInitiationTypePayout:
switch destinationAccount.Type {
case models.AccountTypeExternal, models.AccountTypeExternalFormance:
default:
// account should be external when doing a payout, return an error
return nil, errors.Wrap(ErrValidation, "destination account must be external when doing a payout")
}
}

id := models.TransferInitiationID{
Reference: req.Reference,
ConnectorID: connectorID,
Expand All @@ -129,7 +146,7 @@ func (s *Service) CreateTransferInitiation(ctx context.Context, req *CreateTrans
DestinationAccountID: models.MustAccountIDFromString(req.DestinationAccountID),
ConnectorID: connectorID,
Provider: connectorID.Provider,
Type: models.MustTransferInitiationTypeFromString(req.Type),
Type: transferType,
Amount: req.Amount,
InitialAmount: req.Amount,
Asset: models.Asset(req.Asset),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ func TestCreateTransferInitiation(t *testing.T) {
},
},
},
{
name: "transfer with external account as destination",
req: &CreateTransferInitiationRequest{
Reference: "ref1",
ScheduledAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Description: "test",
SourceAccountID: sourceAccountID.String(),
DestinationAccountID: destinationExternalAccountID.String(),
ConnectorID: connectorDummyPay.ID.String(),
Provider: string(models.ConnectorProviderDummyPay),
Type: models.TransferInitiationTypeTransfer.String(),
Amount: big.NewInt(100),
Asset: "EUR/2",
Validated: false,
},
expectedError: ErrValidation,
},
{
name: "payout with internal account as destination",
req: &CreateTransferInitiationRequest{
Reference: "ref1",
ScheduledAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Description: "test",
SourceAccountID: sourceAccountID.String(),
DestinationAccountID: destinationAccountID.String(),
ConnectorID: connectorDummyPay.ID.String(),
Provider: string(models.ConnectorProviderDummyPay),
Type: models.TransferInitiationTypePayout.String(),
Amount: big.NewInt(100),
Asset: "EUR/2",
Validated: false,
},
expectedError: ErrValidation,
},
{
name: "invalid connector id",
req: &CreateTransferInitiationRequest{
Expand Down

0 comments on commit 412f26d

Please sign in to comment.