proxy

package
v1.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BaseDashboardURL added in v1.21.6

func BaseDashboardURL(livemode bool, account string) string

BaseDashboardURL constructs a dashboard URL with (optionally) the test and account values appended

func GetSessionSecret added in v1.6.0

func GetSessionSecret(ctx context.Context, client stripe.RequestPerformer, deviceName string) (string, error)

GetSessionSecret creates a session and returns the webhook signing secret.

Types

type Config

type Config struct {
	// DeviceName is the name of the device sent to Stripe to help identify the device
	DeviceName  string
	DeviceToken *string

	// Client is a configured stripe client used to execute authenticated calls to the Stripe API.
	Client stripe.RequestPerformer

	// URL to which events are forwarded to
	ForwardURL string
	// URL to which Thin events are forwarded to
	ForwardThinURL string
	// Headers to inject when forwarding events
	ForwardHeaders []string
	// URL to which Connect events are forwarded to
	ForwardConnectURL string
	// URL to which Connect Thin events are forwarded to
	ForwardThinConnectURL string
	// Headers to inject when forwarding Connect events
	ForwardConnectHeaders []string
	// UseConfiguredWebhooks loads webhooks config from user's account
	UseConfiguredWebhooks bool

	// List of events to listen and proxy
	Events []string
	// List of Thin-type events to listen and proxy
	ThinEvents []string

	// WebSocketFeatures is the feature specified for the websocket connection
	WebSocketFeatures []string
	// Indicates whether to print full JSON objects to stdout
	PrintJSON bool

	// Specifies the format to print to stdout.
	Format string

	// Indicates whether to filter events formatted with the default or latest API version
	UseLatestAPIVersion bool
	// Indicates whether to skip certificate verification when forwarding webhooks to HTTPS endpoints
	SkipVerify bool
	// The logger used to log messages to stdin/err
	Log *log.Logger
	// Force use of unencrypted ws:// protocol instead of wss://
	NoWSS bool
	// Override default timeout
	Timeout int64

	// OutCh is the channel to send logs and statuses to for processing in other packages
	OutCh chan websocket.IElement
}

Config provides the configuration of a Proxy

type EndpointClient added in v0.2.6

type EndpointClient struct {
	// URL the client sends POST requests to
	URL string
	// contains filtered or unexported fields
}

EndpointClient is the client used to POST webhook requests to the local endpoint.

func NewEndpointClient added in v0.2.6

func NewEndpointClient(url string, headers []string, connect bool, events []string, isEventDestination bool, cfg *EndpointConfig) *EndpointClient

NewEndpointClient returns a new EndpointClient.

func (*EndpointClient) Post added in v0.2.6

func (c *EndpointClient) Post(evtCtx eventContext) error

Post sends a message to the local endpoint.

func (*EndpointClient) PostV2 added in v1.21.6

func (c *EndpointClient) PostV2(evtCtx eventContext) error

PostV2 sends a message to a local event destination

func (*EndpointClient) SupportsContext added in v1.21.6

func (c *EndpointClient) SupportsContext(context string) bool

SupportsContext takes the context string of an event, and determines whether the endpoint supports this context

func (*EndpointClient) SupportsEventType added in v0.2.6

func (c *EndpointClient) SupportsEventType(connect bool, eventType string) bool

SupportsEventType takes an event of a webhook and compares it to the internal list of supported events

type EndpointConfig added in v0.2.6

type EndpointConfig struct {
	HTTPClient *http.Client

	Log *log.Logger

	ResponseHandler EndpointResponseHandler

	// OutCh is the channel to send data and statuses to for processing in other packages
	OutCh chan websocket.IElement
}

EndpointConfig contains the optional configuration parameters of an EndpointClient.

type EndpointResponse added in v1.6.0

type EndpointResponse struct {
	Event   *StripeEvent
	V2Event *V2EventPayload
	Resp    *http.Response
}

EndpointResponse describes the response to a Stripe event from an endpoint

type EndpointResponseHandler added in v0.2.6

type EndpointResponseHandler interface {
	ProcessResponse(eventContext, string, *http.Response)
}

EndpointResponseHandler handles a response from the endpoint.

type EndpointResponseHandlerFunc added in v0.2.6

type EndpointResponseHandlerFunc func(eventContext, string, *http.Response)

EndpointResponseHandlerFunc is an adapter to allow the use of ordinary functions as response handlers. If f is a function with the appropriate signature, ResponseHandler(f) is a ResponseHandler that calls f.

func (EndpointResponseHandlerFunc) ProcessResponse added in v0.2.6

func (f EndpointResponseHandlerFunc) ProcessResponse(evtCtx eventContext, forwardURL string, resp *http.Response)

ProcessResponse calls f(evtCtx, forwardURL, resp).

type EndpointRoute added in v0.2.6

type EndpointRoute struct {
	// URL is the endpoint's URL.
	URL string

	// Headers to forward to endpoints
	ForwardHeaders []string

	// Connect indicates whether the endpoint should receive normal (when false) or Connect (when true) events.
	Connect bool

	// EventTypes is the list of event types that should be sent to the endpoint.
	EventTypes []string

	// Status is whether or not the endpoint is enabled.
	Status string

	// IsEventDestination indicates whether this is a Thin endpoint
	IsEventDestination bool
}

EndpointRoute describes a local endpoint's routing configuration.

type FailedToPostError added in v1.6.0

type FailedToPostError struct {
	Err error
}

FailedToPostError describes a failure to send a POST request to an endpoint

func (FailedToPostError) Error added in v1.6.0

func (f FailedToPostError) Error() string

type FailedToReadResponseError added in v1.6.0

type FailedToReadResponseError struct {
	Err error
}

FailedToReadResponseError describes a failure to read the response from an endpoint

func (FailedToReadResponseError) Error added in v1.6.0

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

A Proxy opens a websocket connection with Stripe, listens for incoming webhook events, forwards them to the local endpoint and sends the response back to Stripe.

func Init added in v1.6.0

func Init(ctx context.Context, cfg *Config) (*Proxy, error)

Init initializes a new Proxy

func (*Proxy) IsConnected added in v1.7.13

func (p *Proxy) IsConnected() <-chan struct{}

IsConnected returns a channel that signals the proxy has finished connecting. can only be called after webSocketClient is initialized

func (*Proxy) Run

func (p *Proxy) Run(ctx context.Context) error

Run sets the websocket connection and starts the Goroutines to forward incoming events to the local endpoint.

type StripeEvent added in v1.6.0

type StripeEvent struct {
	Account         string                 `json:"account"`
	APIVersion      string                 `json:"api_version"`
	Created         int                    `json:"created"`
	Data            map[string]interface{} `json:"data"`
	ID              string                 `json:"id"`
	Livemode        bool                   `json:"livemode"`
	PendingWebhooks int                    `json:"pending_webhooks"`
	Type            string                 `json:"type"`
	RequestData     interface{}            `json:"request"`
	Request         StripeRequest
}

StripeEvent is a representation of a Stripe `event` object we define RequestData as an interface for backwards compatibility Request will hold the deserialized request data

func (*StripeEvent) IsConnect added in v1.6.0

func (e *StripeEvent) IsConnect() bool

IsConnect return true or false if *StripeEvent is connect or not.

func (*StripeEvent) URLForEventID added in v1.6.0

func (e *StripeEvent) URLForEventID() string

URLForEventID builds a full URL from a StripeEvent ID.

func (*StripeEvent) URLForEventType added in v1.6.0

func (e *StripeEvent) URLForEventType() string

URLForEventType builds a full URL from a StripeEvent Type.

type StripeRequest added in v1.7.0

type StripeRequest struct {
	ID             string
	IdempotencyKey string
}

StripeRequest is a representation of the Request field in a Stripe `event` object

func ExtractRequestData added in v1.7.0

func ExtractRequestData(data interface{}) (StripeRequest, error)

ExtractRequestData takes an interface with request data from a Stripe event payload and properly parses it into a StripeRequest struct before returning it

type V2EventPayload added in v1.21.6

type V2EventPayload struct {
	Created       string               `json:"created"`
	Data          json.RawMessage      `json:"data,omitempty"`
	ID            string               `json:"id"`
	Object        string               `json:"object"`
	RelatedObject primaryRelatedObject `json:"related_object"`
	Type          string               `json:"type"`
	Context       string               `json:"context,omitempty"`
}

V2EventPayload describes the payload from the server for a v2 event

func (*V2EventPayload) IsConnect added in v1.21.6

func (e *V2EventPayload) IsConnect() bool

IsConnect returns true if this event is associated with a connected account

func (*V2EventPayload) URLForEventID added in v1.21.6

func (e *V2EventPayload) URLForEventID(cliEndpointID string) string

URLForEventID builds a full URL from a V2StripeEvent ID.

type WebhookEventProcessor added in v1.14.1

type WebhookEventProcessor struct {
	// contains filtered or unexported fields
}

WebhookEventProcessor encapsulates logic around processing and forwarding webhook events.

func NewWebhookEventProcessor added in v1.14.1

func NewWebhookEventProcessor(sendMessage func(*websocket.OutgoingMessage), routes []EndpointRoute, cfg *WebhookEventProcessorConfig) *WebhookEventProcessor

NewWebhookEventProcessor constructs a WebhookEventProcessor from the provided websocket delivery function, route table, and config.

func (*WebhookEventProcessor) ProcessEvent added in v1.14.1

func (p *WebhookEventProcessor) ProcessEvent(msg websocket.IncomingMessage)

ProcessEvent processes webhook events, notifying listeners via the configured OutCh, sending acknowledgements with the configured websocket sender, and forwarding events to configured endpoints.

ProcessEvent implements the websocket.EndpointResponseHandler interface.

type WebhookEventProcessorConfig added in v1.14.1

type WebhookEventProcessorConfig struct {
	// The logger used to log messages to stdin/err
	Log *log.Logger

	// List of events to listen and proxy
	Events []string

	// List of thin events to listen and proxy
	ThinEvents []string

	// OutCh is the channel to send logs and statuses to for processing in other packages
	OutCh chan websocket.IElement

	// Indicates whether to filter events formatted with the default or latest API version
	UseLatestAPIVersion bool

	// Indicates whether to skip certificate verification when forwarding webhooks to HTTPS endpoints
	SkipVerify bool

	// Override default timeout
	Timeout int64
}

WebhookEventProcessorConfig defines the external inputs that infuence the behavior of a WebhookEventProcessor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL