mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
- go.mod module path + all self-imports: github.com/formancehq/payments -> github.com/hanzo-fi/payments (1590 .go files / 3152 refs; 20 modules: root, pkg/client, pkg/domain, 18 ce/plugins). External dep kept: formancehq/go-libs. - plugins: each ce/plugins/* consumed a *published* pkg/domain; added local 'replace .../pkg/domain => ../../../pkg/domain' (consistent with root) so every standalone plugin module builds in-monorepo. compile-plugin.sh MOD var rebranded too. - branding: README, LICENSE (dual MIT+EE; upstream copyright retained + Hanzo added), goreleaser (release images -> registry.hanzo.ai/hanzo-fi, OCI labels, homepage), proto go_package, renovate, speakeasy gen.yaml, connector-template *.gotpl. - CI: removed Numary/Formance workflows + composite action; added canonical hanzo.yml + cicd.yml (hanzoai/ci, gated HANZO_CI_ENABLED) + secretless ci.yml (go build/vet). - kept as data/compat keys: wire capability enums ALLOW_FORMANCE_*, generated SDK type client.Formance, domain field FormanceRedirectURL, OTEL_SERVICE_NAME, upstream base image. - verified: go build ./... (default + -tags ee) + all 20 nested modules green.
127 lines
3.2 KiB
Go
127 lines
3.2 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
|
|
"github.com/hanzo-fi/payments/pkg/domain/metrics"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
"github.com/stripe/stripe-go/v80"
|
|
)
|
|
|
|
const StripeConnectUrlPrefix = "connect_"
|
|
|
|
type endpointConfig struct {
|
|
eventTypes []*string
|
|
isConnect bool
|
|
}
|
|
|
|
var endpoints = []endpointConfig{
|
|
{
|
|
eventTypes: []*string{
|
|
stripe.String(string(stripe.EventTypeBalanceAvailable)),
|
|
},
|
|
isConnect: false,
|
|
},
|
|
{
|
|
eventTypes: []*string{stripe.String(string(stripe.EventTypeBalanceAvailable))},
|
|
isConnect: true,
|
|
},
|
|
}
|
|
|
|
func urlForEndpoint(webhookBaseURL string, conf endpointConfig) (string, error) {
|
|
path := "root"
|
|
if conf.isConnect {
|
|
path = "connect"
|
|
}
|
|
return url.JoinPath(webhookBaseURL, path)
|
|
}
|
|
|
|
func (c *client) CreateWebhookEndpoints(ctx context.Context, webhookBaseURL string) ([]*stripe.WebhookEndpoint, error) {
|
|
results := make([]*stripe.WebhookEndpoint, 0, len(endpoints))
|
|
|
|
// in case this is being run at app startup the webhooks might already exist
|
|
// remaining is a map of url -> endpointConfig
|
|
updated, remaining, err := c.updateExistingWebhookEndpoints(ctx, webhookBaseURL)
|
|
if err != nil {
|
|
return results, wrapSDKErr(err)
|
|
}
|
|
results = append(results, updated...)
|
|
|
|
for u, conf := range remaining {
|
|
params := &stripe.WebhookEndpointParams{
|
|
EnabledEvents: conf.eventTypes,
|
|
URL: stripe.String(u),
|
|
APIVersion: stripe.String(stripe.APIVersion),
|
|
Connect: stripe.Bool(conf.isConnect),
|
|
}
|
|
result, err := c.webhookEndpointClient.New(params)
|
|
if err != nil {
|
|
return results, wrapSDKErr(err)
|
|
}
|
|
results = append(results, result)
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
func (c *client) updateExistingWebhookEndpoints(ctx context.Context, webhookBaseURL string) (
|
|
updated []*stripe.WebhookEndpoint,
|
|
remaining map[string]endpointConfig,
|
|
err error,
|
|
) {
|
|
filters := stripe.ListParams{
|
|
Context: metrics.OperationContext(ctx, "list_webhook_endpoints"),
|
|
}
|
|
itr := c.webhookEndpointClient.List(&stripe.WebhookEndpointListParams{
|
|
ListParams: filters,
|
|
})
|
|
|
|
if err := itr.Err(); err != nil {
|
|
return nil, remaining, err
|
|
}
|
|
|
|
// prepopulate a list of endpoints we want to ensure are created/up-to-date
|
|
remaining = make(map[string]endpointConfig)
|
|
for _, conf := range endpoints {
|
|
u, err := urlForEndpoint(webhookBaseURL, conf)
|
|
if err != nil {
|
|
return nil, remaining, err
|
|
}
|
|
remaining[u] = conf
|
|
}
|
|
|
|
updated = make([]*stripe.WebhookEndpoint, 0)
|
|
for _, data := range itr.WebhookEndpointList().Data {
|
|
conf, ok := remaining[data.URL]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
result, err := c.webhookEndpointClient.Update(
|
|
data.ID,
|
|
&stripe.WebhookEndpointParams{
|
|
EnabledEvents: conf.eventTypes,
|
|
URL: stripe.String(data.URL),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, remaining, err
|
|
}
|
|
updated = append(updated, result)
|
|
|
|
// delete this record from remaining so we don't create a duplicate endpoint
|
|
delete(remaining, data.URL)
|
|
}
|
|
return updated, remaining, nil
|
|
}
|
|
|
|
func (c *client) DeleteWebhookEndpoints(configs []models.PSPWebhookConfig) error {
|
|
for _, conf := range configs {
|
|
_, err := c.webhookEndpointClient.Del(conf.Name, &stripe.WebhookEndpointParams{})
|
|
if err != nil {
|
|
return wrapSDKErr(err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|