Files
CrimsonandGitHub c48bb246b5 feat(Stripe): EN-470 Add balance.available webhooks to Stripe connector (#590)
* Implement Balances Webhooks in Stripe to reduce polling workflows

* Create webhooks on install

* Remove webhook at uninstall time

* Add verifyWebhook function to Stripe

* Pin APIVersion in Stripe Webhook Create call

* Default to root account when receiving balance.update webhook

* Only make call do delete webhooks if webhooks were setup to begin with

* Set up two webhook endpoints - one for connect and one for root account

* Use event.Created to determine balance time in Stripe webhooks

* Start recording root account ID instead of dummy 'root' value

* Create unit test for Stripe create webhooks

* Add tests for Stripe Translate webhook

* Share logic between Stripe balance polling and webhook handle

* Use supported webhooks map

* Update existing stripe webhooks instead of creating new ones at app start time

* Run install sequence for each connector at app start of payments api pods

* Turn of balances periodically for Stripe

* Add nil checks for balance.Available

* Remove potentially sensitive information from error message

* Add migration tool to remove FETCH BALANCES schedules from stripe
2025-11-21 10:25:52 +00:00

85 lines
2.2 KiB
Go

package stripe
import (
"encoding/json"
"fmt"
"github.com/formancehq/payments/internal/connectors/plugins/public/stripe/client"
"github.com/formancehq/payments/internal/models"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
stripesdk "github.com/stripe/stripe-go/v80"
gomock "go.uber.org/mock/gomock"
)
var _ = Describe("Stripe Plugin Accounts", func() {
var (
ctrl *gomock.Controller
m *client.MockClient
plg models.Plugin
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
m = client.NewMockClient(ctrl)
plg = &Plugin{client: m}
})
AfterEach(func() {
ctrl.Finish()
})
Context("fetch next accounts", func() {
var (
pageSize int
sampleAccounts []*stripesdk.Account
)
BeforeEach(func() {
pageSize = 20
sampleAccounts = make([]*stripesdk.Account, 0)
for i := 0; i < pageSize-1; i++ {
sampleAccounts = append(sampleAccounts, &stripesdk.Account{
ID: fmt.Sprintf("some-reference-%d", i),
})
}
})
It("fetches next accounts", func(ctx SpecContext) {
req := models.FetchNextAccountsRequest{
State: json.RawMessage(`{}`),
PageSize: pageSize,
}
rootAccount := &stripesdk.Account{
ID: "root-account-id",
Settings: &stripesdk.AccountSettings{
Dashboard: &stripesdk.AccountSettingsDashboard{DisplayName: "displayName"}},
}
m.EXPECT().GetRootAccount().Return(
rootAccount,
nil,
)
// pageSize passed to client is less when we generate a root account
m.EXPECT().GetAccounts(gomock.Any(), gomock.Any(), int64(pageSize-1)).Return(
sampleAccounts,
client.Timeline{LatestID: sampleAccounts[len(sampleAccounts)-1].ID},
true,
nil,
)
res, err := plg.FetchNextAccounts(ctx, req)
Expect(err).To(BeNil())
Expect(res.HasMore).To(BeTrue())
Expect(res.Accounts).To(HaveLen(req.PageSize))
Expect(res.Accounts[0].Reference).To(Equal(rootAccount.ID))
Expect(res.Accounts[0].Name).To(Equal(&rootAccount.Settings.Dashboard.DisplayName))
var state accountsState
err = json.Unmarshal(res.NewState, &state)
Expect(err).To(BeNil())
Expect(state.Timeline.LatestID).To(Equal(res.Accounts[len(res.Accounts)-1].Reference))
})
})
})