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.
169 lines
8.5 KiB
Go
169 lines
8.5 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"github.com/hanzo-fi/payments/pkg/domain/metrics"
|
|
)
|
|
|
|
type EventCategory string
|
|
|
|
const (
|
|
EventCategoryBookTransferCompleted EventCategory = "book.transfer.completed"
|
|
EventCategoryBookTransferCanceled EventCategory = "book.transfer.canceled"
|
|
EventCategoryBookTransferUpdated EventCategory = "book.transfer.updated"
|
|
EventCategoryBookTransferHoldCreated EventCategory = "book.transfer.hold_created"
|
|
EventCategoryWireTransferInitiated EventCategory = "wire.outgoing_transfer.initiated"
|
|
EventCategoryWireTransferOutgoingCompleted EventCategory = "wire.outgoing_transfer.completed"
|
|
EventCategoryWireTransferIncomingCompleted EventCategory = "wire.incoming_transfer.completed"
|
|
EventCategoryWireTransferSubmitted EventCategory = "wire.outgoing_transfer.submitted"
|
|
EventCategoryWireTransferRejected EventCategory = "wire.outgoing_transfer.rejected"
|
|
EventCategoryWireTransferManualReview EventCategory = "wire.outgoing_transfer.manual_review"
|
|
EventCategoryACHTransferInitiated EventCategory = "ach.outgoing_transfer.initiated"
|
|
EventCategoryACHTransferSettled EventCategory = "ach.outgoing_transfer.settled"
|
|
EventCategoryACHTransferSubmitted EventCategory = "ach.outgoing_transfer.submitted"
|
|
EventCategoryACHTransferCompleted EventCategory = "ach.outgoing_transfer.completed"
|
|
EventCategoryACHTransferManualReview EventCategory = "ach.outgoing_transfer.manual_review"
|
|
EventCategoryACHTransferReturned EventCategory = "ach.outgoing_transfer.returned"
|
|
EventCategoryACHTransferCanceled EventCategory = "ach.outgoing_transfer.canceled"
|
|
EventCategoryACHTransferReturnDishonored EventCategory = "ach.outgoing_transfer.return_dishonored"
|
|
EventCategoryACHTransferReturnContested EventCategory = "ach.outgoing_transfer.return_contested"
|
|
EventCategoryACHTransferNOC EventCategory = "ach.outgoing_transfer.noc"
|
|
EventCategoryACHIncomingScheduled EventCategory = "ach.incoming_transfer.scheduled"
|
|
EventCategoryACHIncomingSettled EventCategory = "ach.incoming_transfer.settled"
|
|
EventCategoryACHIncomingNSF EventCategory = "ach.incoming_transfer.nsf"
|
|
EventCategoryACHIncomingCompleted EventCategory = "ach.incoming_transfer.completed"
|
|
EventCategoryACHIncomingReturned EventCategory = "ach.incoming_transfer.returned"
|
|
EventCategoryACHIncomingReturnDishonored EventCategory = "ach.incoming_transfer.return_dishonored"
|
|
EventCategoryACHIncomingReturnContested EventCategory = "ach.incoming_transfer.return_contested"
|
|
EventCategoryInternationalWireCompleted EventCategory = "swift.outgoing_transfer.completed"
|
|
EventCategorySwiftOutgoingInitiated EventCategory = "swift.outgoing_transfer.initiated"
|
|
EventCategorySwiftOutgoingManualReview EventCategory = "swift.outgoing_transfer.manual_review"
|
|
EventCategorySwiftOutgoingSubmitted EventCategory = "swift.outgoing_transfer.submitted"
|
|
EventCategorySwiftOutgoingPendingReturn EventCategory = "swift.outgoing_transfer.pending_return"
|
|
EventCategorySwiftOutgoingReturned EventCategory = "swift.outgoing_transfer.returned"
|
|
EventCategorySwiftOutgoingCancellationRequested EventCategory = "swift.outgoing_transfer.cancellation_requested"
|
|
EventCategorySwiftOutgoingCancellationAccepted EventCategory = "swift.outgoing_transfer.cancellation_accepted"
|
|
EventCategorySwiftOutgoingCancellationRejected EventCategory = "swift.outgoing_transfer.cancellation_rejected"
|
|
EventCategorySwiftOutgoingTrackingUpdated EventCategory = "swift.outgoing_transfer.tracking_updated"
|
|
EventCategorySwiftIncomingInitiated EventCategory = "swift.incoming_transfer.initiated"
|
|
EventCategorySwiftIncomingCompleted EventCategory = "swift.incoming_transfer.completed"
|
|
EventCategorySwiftIncomingPendingReturn EventCategory = "swift.incoming_transfer.pending_return"
|
|
EventCategorySwiftIncomingReturned EventCategory = "swift.incoming_transfer.returned"
|
|
EventCategorySwiftIncomingCancellationRequested EventCategory = "swift.incoming_transfer.cancellation_requested"
|
|
EventCategorySwiftIncomingCancellationAccepted EventCategory = "swift.incoming_transfer.cancellation_accepted"
|
|
EventCategorySwiftIncomingCancellationRejected EventCategory = "swift.incoming_transfer.cancellation_rejected"
|
|
EventCategorySwiftIncomingTrackingUpdated EventCategory = "swift.incoming_transfer.tracking_updated"
|
|
EventCategoryRealtimeTransferCompleted EventCategory = "realtime.outgoing_transfer.completed"
|
|
EventCategoryRealtimeTransferInitiated EventCategory = "realtime.outgoing_transfer.initiated"
|
|
EventCategoryRealtimeTransferManualReview EventCategory = "realtime.outgoing_transfer.manual_review"
|
|
EventCategoryRealtimeTransferManualReviewApproved EventCategory = "realtime.outgoing_transfer.manual_review_approved"
|
|
EventCategoryRealtimeTransferManualReviewRejected EventCategory = "realtime.outgoing_transfer.manual_review_rejected"
|
|
EventCategoryRealtimeTransferRejected EventCategory = "realtime.outgoing_transfer.rejected"
|
|
EventCategoryRealtimeIncomingTransferCompleted EventCategory = "realtime.incoming_transfer.completed"
|
|
)
|
|
|
|
type WebhookEvent[t any] struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
CreatedAt string `json:"created_at"`
|
|
Data t `json:"data"`
|
|
}
|
|
|
|
type EventSubscription struct {
|
|
ID string `json:"id"`
|
|
URL string `json:"url"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
Description string `json:"description"`
|
|
EnabledEvents []string `json:"enabled_events"`
|
|
Secret string `json:"secret"`
|
|
IsDisabled bool `json:"is_disabled"`
|
|
}
|
|
|
|
type CreateEventSubscriptionRequest struct {
|
|
EnabledEvents []string `json:"enabled_events"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
type ListWebhookResponseWrapper[t any] struct {
|
|
WebhookEndpoints t `json:"webhook_endpoints"`
|
|
}
|
|
|
|
func (c *client) CreateEventSubscription(ctx context.Context, es *CreateEventSubscriptionRequest) (*EventSubscription, error) {
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "create_event_subscription")
|
|
|
|
body, err := json.Marshal(es)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := c.newRequest(ctx, http.MethodPost, "webhook-endpoints", bytes.NewBuffer(body))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %v", ErrWebhookRequestFailed, err)
|
|
}
|
|
|
|
var res EventSubscription
|
|
var errRes columnError
|
|
_, err = c.httpClient.Do(ctx, req, &res, &errRes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create web hooks: %w %w", err, errRes.Error())
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (c *client) ListEventSubscriptions(ctx context.Context) (endpoints []*EventSubscription, err error) {
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "list_event_subscription")
|
|
endpoints = make([]*EventSubscription, 0)
|
|
|
|
for {
|
|
req, err := c.newRequest(ctx, http.MethodGet, "webhook-endpoints", http.NoBody)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %v", ErrWebhookRequestFailed, err)
|
|
}
|
|
|
|
q := req.URL.Query()
|
|
q.Add("limit", "100")
|
|
if len(endpoints) > 0 {
|
|
q.Add("starting_after", endpoints[len(endpoints)-1].ID)
|
|
}
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
var res ListWebhookResponseWrapper[[]*EventSubscription]
|
|
var errRes columnError
|
|
_, err = c.httpClient.Do(ctx, req, &res, &errRes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list web hooks: %w %w", err, errRes.Error())
|
|
}
|
|
|
|
if len(res.WebhookEndpoints) == 0 {
|
|
break
|
|
}
|
|
endpoints = append(endpoints, res.WebhookEndpoints...)
|
|
}
|
|
|
|
return endpoints, nil
|
|
}
|
|
|
|
func (c *client) DeleteEventSubscription(ctx context.Context, eventID string) (*EventSubscription, error) {
|
|
ctx = context.WithValue(ctx, metrics.MetricOperationContextKey, "delete_event_subscription")
|
|
|
|
req, err := c.newRequest(ctx, http.MethodDelete, fmt.Sprintf("webhook-endpoints/%s", eventID), http.NoBody)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %v", ErrWebhookRequestFailed, err)
|
|
}
|
|
|
|
var res EventSubscription
|
|
var errRes columnError
|
|
_, err = c.httpClient.Do(ctx, req, &res, &errRes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to delete web hooks: %w %w", err, errRes.Error())
|
|
}
|
|
return &res, nil
|
|
}
|