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.
187 lines
5.5 KiB
Go
187 lines
5.5 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
errorsutils "github.com/hanzo-fi/payments/pkg/domain/errors"
|
|
)
|
|
|
|
type WebhookDelivery struct {
|
|
Version string `json:"version"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
type webhookSubscription struct {
|
|
Name string `json:"name"`
|
|
TriggerOn string `json:"trigger_on"`
|
|
Delivery WebhookDelivery `json:"delivery"`
|
|
}
|
|
|
|
type WebhookSubscriptionResponse struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Delivery WebhookDelivery `json:"delivery"`
|
|
TriggerOn string `json:"trigger_on"`
|
|
Scope struct {
|
|
Domain string `json:"domain"`
|
|
} `json:"scope"`
|
|
CreatedBy struct {
|
|
Type string `json:"type"`
|
|
ID string `json:"id"`
|
|
} `json:"created_by"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
func (c *client) CreateWebhook(ctx context.Context, profileID uint64, name, triggerOn, url, version string) (*WebhookSubscriptionResponse, error) {
|
|
reqBody, err := json.Marshal(webhookSubscription{
|
|
Name: name,
|
|
TriggerOn: triggerOn,
|
|
Delivery: struct {
|
|
Version string `json:"version"`
|
|
URL string `json:"url"`
|
|
}{
|
|
Version: version,
|
|
URL: url,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
|
|
c.endpoint(fmt.Sprintf("/v3/profiles/%d/subscriptions", profileID)),
|
|
bytes.NewBuffer(reqBody),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
var res WebhookSubscriptionResponse
|
|
var errRes wiseErrors
|
|
statusCode, err := c.httpClient.Do(ctx, req, &res, &errRes)
|
|
if err != nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to create subscription: %v", errRes.Error(statusCode)),
|
|
err,
|
|
)
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (c *client) ListWebhooksSubscription(ctx context.Context, profileID uint64) ([]WebhookSubscriptionResponse, error) {
|
|
req, err := http.NewRequestWithContext(ctx,
|
|
http.MethodGet, c.endpoint(fmt.Sprintf("/v3/profiles/%d/subscriptions", profileID)), http.NoBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var res []WebhookSubscriptionResponse
|
|
var errRes wiseErrors
|
|
statusCode, err := c.httpClient.Do(ctx, req, &res, &errRes)
|
|
if err != nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to list subscriptions: %v", errRes.Error(statusCode)),
|
|
err,
|
|
)
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (c *client) DeleteWebhooks(ctx context.Context, profileID uint64, subscriptionID string) error {
|
|
req, err := http.NewRequestWithContext(ctx,
|
|
http.MethodDelete, c.endpoint(fmt.Sprintf("/v3/profiles/%d/subscriptions/%s", profileID, subscriptionID)), http.NoBody)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var errRes wiseErrors
|
|
statusCode, err := c.httpClient.Do(ctx, req, nil, &errRes)
|
|
if err != nil {
|
|
return errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to delete subscription: %v", errRes.Error(statusCode)),
|
|
err,
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type transferStateChangedWebhookPayload struct {
|
|
Data struct {
|
|
Resource struct {
|
|
Type string `json:"type"`
|
|
ID uint64 `json:"id"`
|
|
ProfileID uint64 `json:"profile_id"`
|
|
AccountID uint64 `json:"account_id"`
|
|
} `json:"resource"`
|
|
CurrentState string `json:"current_state"`
|
|
PreviousState string `json:"previous_state"`
|
|
OccurredAt string `json:"occurred_at"`
|
|
} `json:"data"`
|
|
SubscriptionID string `json:"subscription_id"`
|
|
EventType string `json:"event_type"`
|
|
SchemaVersion string `json:"schema_version"`
|
|
SentAt string `json:"sent_at"`
|
|
}
|
|
|
|
func (c *client) TranslateTransferStateChangedWebhook(ctx context.Context, payload []byte) (Transfer, error) {
|
|
var transferStatedChangedEvent transferStateChangedWebhookPayload
|
|
err := json.Unmarshal(payload, &transferStatedChangedEvent)
|
|
if err != nil {
|
|
return Transfer{}, err
|
|
}
|
|
|
|
transfer, err := c.GetTransfer(ctx, fmt.Sprint(transferStatedChangedEvent.Data.Resource.ID))
|
|
if err != nil {
|
|
return Transfer{}, err
|
|
}
|
|
|
|
transfer.Created = transferStatedChangedEvent.Data.OccurredAt
|
|
transfer.CreatedAt, err = time.Parse("2006-01-02 15:04:05", transfer.Created)
|
|
if err != nil {
|
|
return Transfer{}, fmt.Errorf("failed to parse created time: %w", err)
|
|
}
|
|
|
|
return *transfer, nil
|
|
}
|
|
|
|
type BalanceUpdateWebhookPayload struct {
|
|
Data BalanceUpdateWebhookData `json:"data"`
|
|
SubscriptionID string `json:"subscription_id"`
|
|
EventType string `json:"event_type"`
|
|
SchemaVersion string `json:"schema_version"`
|
|
SentAt string `json:"sent_at"`
|
|
}
|
|
|
|
type BalanceUpdateWebhookData struct {
|
|
Resource BalanceUpdateWebhookResource `json:"resource"`
|
|
Amount json.Number `json:"amount"`
|
|
BalanceID uint64 `json:"balance_id"`
|
|
Currency string `json:"currency"`
|
|
TransactionType string `json:"transaction_type"`
|
|
OccurredAt string `json:"occurred_at"`
|
|
TransferReference string `json:"transfer_reference"`
|
|
ChannelName string `json:"channel_name"`
|
|
}
|
|
|
|
type BalanceUpdateWebhookResource struct {
|
|
ID uint64 `json:"id"`
|
|
ProfileID uint64 `json:"profile_id"`
|
|
Type string `json:"type"`
|
|
}
|
|
|
|
func (c *client) TranslateBalanceUpdateWebhook(ctx context.Context, payload []byte) (BalanceUpdateWebhookPayload, error) {
|
|
var balanceUpdateEvent BalanceUpdateWebhookPayload
|
|
err := json.Unmarshal(payload, &balanceUpdateEvent)
|
|
if err != nil {
|
|
return BalanceUpdateWebhookPayload{}, err
|
|
}
|
|
|
|
return balanceUpdateEvent, nil
|
|
}
|