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

72 lines
1.7 KiB
Go

package stripe
import (
"encoding/json"
"fmt"
"math/big"
"strings"
"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 Balances", 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 balances", func() {
var (
accRef string
sampleBalance *stripesdk.Balance
)
BeforeEach(func() {
accRef = "abc"
sampleBalance = &stripesdk.Balance{
Available: []*stripesdk.Amount{
{
Currency: stripesdk.CurrencyAED,
Amount: 49999,
},
},
}
})
It("fetches next balances", func(ctx SpecContext) {
req := models.FetchNextBalancesRequest{
FromPayload: json.RawMessage(fmt.Sprintf(`{"reference": "%s"}`, accRef)),
State: json.RawMessage(`{}`),
}
m.EXPECT().GetAccountBalances(gomock.Any(), accRef).Return(
sampleBalance,
nil,
)
res, err := plg.FetchNextBalances(ctx, req)
Expect(err).To(BeNil())
Expect(res.Balances).To(HaveLen(len(sampleBalance.Available)))
for i, available := range sampleBalance.Available {
Expect(res.Balances[i].AccountReference).To(Equal(accRef))
Expect(res.Balances[i].Amount).To(BeEquivalentTo(big.NewInt(available.Amount)))
Expect(res.Balances[i].Asset).To(HavePrefix(strings.ToUpper(string(available.Currency))))
}
})
})
})