Skip to content

Commit

Permalink
fix(payments): fix stripe root account transfer initiation (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Dec 18, 2023
1 parent 1fbbcd5 commit 2780091
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type transferInitiationResponse struct {
ID string `json:"id"`
Reference string `json:"reference"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ScheduledAt time.Time `json:"scheduledAt"`
Expand Down Expand Up @@ -60,6 +61,7 @@ func readTransferInitiationHandler(b backend.Backend) http.HandlerFunc {
data := &readTransferInitiationResponse{
transferInitiationResponse: transferInitiationResponse{
ID: ret.ID.String(),
Reference: ret.ID.Reference,
CreatedAt: ret.CreatedAt,
UpdatedAt: ret.UpdatedAt,
ScheduledAt: ret.ScheduledAt,
Expand Down Expand Up @@ -115,6 +117,7 @@ func listTransferInitiationsHandler(b backend.Backend) http.HandlerFunc {
for i := range ret {
data[i] = &transferInitiationResponse{
ID: ret[i].ID.String(),
Reference: ret[i].ID.Reference,
CreatedAt: ret[i].CreatedAt,
UpdatedAt: ret[i].UpdatedAt,
ScheduledAt: ret[i].ScheduledAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func TestListTransferInitiations(t *testing.T) {
expectedTFsResponse := []*transferInitiationResponse{
{
ID: listTFsResponse[0].ID.String(),
Reference: listTFsResponse[0].ID.Reference,
CreatedAt: listTFsResponse[0].CreatedAt,
UpdatedAt: listTFsResponse[0].UpdatedAt,
ScheduledAt: listTFsResponse[0].ScheduledAt,
Expand All @@ -242,6 +243,7 @@ func TestListTransferInitiations(t *testing.T) {
},
{
ID: listTFsResponse[1].ID.String(),
Reference: listTFsResponse[1].ID.Reference,
CreatedAt: listTFsResponse[1].CreatedAt,
UpdatedAt: listTFsResponse[1].UpdatedAt,
ScheduledAt: listTFsResponse[1].ScheduledAt,
Expand Down Expand Up @@ -438,6 +440,7 @@ func TestGetTransferInitiation(t *testing.T) {
expectedTransferInitiationResponse = &readTransferInitiationResponse{
transferInitiationResponse: transferInitiationResponse{
ID: getTransferInitiationResponse.ID.String(),
Reference: getTransferInitiationResponse.ID.Reference,
CreatedAt: getTransferInitiationResponse.CreatedAt,
UpdatedAt: getTransferInitiationResponse.UpdatedAt,
ScheduledAt: getTransferInitiationResponse.ScheduledAt,
Expand Down Expand Up @@ -491,6 +494,7 @@ func TestGetTransferInitiation(t *testing.T) {
expectedTransferInitiationResponse = &readTransferInitiationResponse{
transferInitiationResponse: transferInitiationResponse{
ID: getTransferInitiationResponse.ID.String(),
Reference: getTransferInitiationResponse.ID.Reference,
CreatedAt: getTransferInitiationResponse.CreatedAt,
UpdatedAt: getTransferInitiationResponse.UpdatedAt,
ScheduledAt: getTransferInitiationResponse.ScheduledAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
"go.opentelemetry.io/otel/metric"
)

const (
rootAccountReference = "root"
)

var (
accountsAttrs = metric.WithAttributes(append(connectorAttrs, attribute.String(metrics.ObjectAttributeKey, "accounts"))...)
)
Expand Down Expand Up @@ -139,12 +143,12 @@ func registerRootAccount(
if err := ingester.IngestAccounts(ctx, ingestion.AccountBatch{
{
ID: models.AccountID{
Reference: "root",
Reference: rootAccountReference,
ConnectorID: connectorID,
},
ConnectorID: connectorID,
CreatedAt: time.Now().UTC(),
Reference: "root",
Reference: rootAccountReference,
Type: models.AccountTypeInternal,
},
}); err != nil {
Expand All @@ -153,7 +157,7 @@ func registerRootAccount(
balanceTask, err := models.EncodeTaskDescriptor(TaskDescriptor{
Name: "Fetch balance for the root account",
Key: taskNameFetchBalances,
Account: "root",
Account: rootAccountReference,
})
if err != nil {
return errors.Wrap(err, "failed to transform task descriptor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func balanceTask(account string, client *client.DefaultClient) func(ctx context.
}()

stripeAccount := account
if account == "root" {
if account == rootAccountReference {
// special case for root account
stripeAccount = ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func fetchPaymentsTask(config TimelineConfig, client *client.DefaultClient) func
logger,
NewIngester(
func(ctx context.Context, batch []*stripe.BalanceTransaction, commitState TimelineState, tail bool) error {
if err := ingestBatch(ctx, connectorID, "root", logger, ingester, batch, commitState, tail); err != nil {
if err := ingestBatch(ctx, connectorID, rootAccountReference, logger, ingester, batch, commitState, tail); err != nil {
return err
}
metricsRegistry.ConnectorObjects().Add(ctx, int64(len(batch)), paymentsAttrs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func initiatePaymentTask(transferID string, stripeClient *client.DefaultClient)
}

stripeClient := client.Client(stripeClient)
if transfer.SourceAccount != nil {
// If source account is nil, or equal to root (which is a special
// account we create for stripe for the balance platform), we don't need
// to set the stripe account.
if transfer.SourceAccount != nil && transfer.SourceAccount.Reference != rootAccountReference {
stripeClient = stripeClient.ForAccount(transfer.SourceAccountID.Reference)
}

Expand Down
3 changes: 3 additions & 0 deletions components/payments/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,7 @@ components:
type: object
required:
- id
- reference
- createdAt
- updatedAt
- scheduledAt
Expand All @@ -1471,6 +1472,8 @@ components:
id:
type: string
example: XXX
reference:
type: string
createdAt:
type: string
format: date-time
Expand Down
1 change: 1 addition & 0 deletions libs/clients/go/docs/models/shared/transferinitiation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
| `DestinationAccountID` | *string* | :heavy_check_mark: | N/A | |
| `Error` | *string* | :heavy_check_mark: | N/A | |
| `ID` | *string* | :heavy_check_mark: | N/A | XXX |
| `Reference` | *string* | :heavy_check_mark: | N/A | |
| `RelatedPayments` | [][TransferInitiationPayments](../../models/shared/transferinitiationpayments.md) | :heavy_minus_sign: | N/A | |
| `ScheduledAt` | [time.Time](https://pkg.go.dev/time#Time) | :heavy_check_mark: | N/A | |
| `SourceAccountID` | *string* | :heavy_check_mark: | N/A | |
Expand Down
8 changes: 8 additions & 0 deletions libs/clients/go/pkg/models/shared/transferinitiation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type TransferInitiation struct {
DestinationAccountID string `json:"destinationAccountID"`
Error string `json:"error"`
ID string `json:"id"`
Reference string `json:"reference"`
RelatedPayments []TransferInitiationPayments `json:"relatedPayments,omitempty"`
ScheduledAt time.Time `json:"scheduledAt"`
SourceAccountID string `json:"sourceAccountID"`
Expand Down Expand Up @@ -121,6 +122,13 @@ func (o *TransferInitiation) GetID() string {
return o.ID
}

func (o *TransferInitiation) GetReference() string {
if o == nil {
return ""
}
return o.Reference
}

func (o *TransferInitiation) GetRelatedPayments() []TransferInitiationPayments {
if o == nil {
return nil
Expand Down

1 comment on commit 2780091

@vercel
Copy link

@vercel vercel bot commented on 2780091 Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.