mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
200 lines
5.4 KiB
Go
200 lines
5.4 KiB
Go
package powens
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/observe/log"
|
|
"github.com/hanzo-fi/payments/ce/plugins/powens/client"
|
|
pkgplugins "github.com/hanzo-fi/payments/pkg/domain/plugins"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
)
|
|
|
|
const ProviderName = "powens"
|
|
|
|
var Registration = pkgplugins.Registration{
|
|
PluginType: models.PluginTypeOpenBanking,
|
|
CreateFunc: func(_ models.ConnectorID, name string, logger logging.Logger, rm json.RawMessage) (models.Plugin, error) {
|
|
return New(name, logger, rm)
|
|
},
|
|
Capabilities: capabilities,
|
|
RawConf: Config{},
|
|
PageSize: PAGE_SIZE,
|
|
}
|
|
|
|
type Plugin struct {
|
|
models.Plugin
|
|
|
|
name string
|
|
logger logging.Logger
|
|
clientID string
|
|
|
|
client client.Client
|
|
config Config
|
|
|
|
supportedWebhooks map[client.WebhookEventType]supportedWebhook
|
|
}
|
|
|
|
func New(name string, logger logging.Logger, rawConfig json.RawMessage) (*Plugin, error) {
|
|
config, err := unmarshalAndValidateConfig(rawConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
client, err := client.New(name, config.ClientID, config.ClientSecret, config.ConfigurationToken, config.Endpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
p := &Plugin{
|
|
Plugin: pkgplugins.NewBasePlugin(),
|
|
|
|
name: name,
|
|
logger: logger,
|
|
clientID: config.ClientID,
|
|
|
|
client: client,
|
|
config: config,
|
|
}
|
|
|
|
p.initWebhookConfig()
|
|
|
|
return p, nil
|
|
}
|
|
|
|
func (p *Plugin) Name() string {
|
|
return p.name
|
|
}
|
|
|
|
func (p *Plugin) Config() models.PluginInternalConfig {
|
|
return p.config
|
|
}
|
|
|
|
func (p *Plugin) Install(ctx context.Context, req models.InstallRequest) (models.InstallResponse, error) {
|
|
return models.InstallResponse{
|
|
Workflow: workflow(),
|
|
}, nil
|
|
}
|
|
|
|
func (p *Plugin) Uninstall(ctx context.Context, req models.UninstallRequest) (models.UninstallResponse, error) {
|
|
if p.client == nil {
|
|
return models.UninstallResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
if err := p.deleteWebhooks(ctx, req); err != nil {
|
|
return models.UninstallResponse{}, err
|
|
}
|
|
|
|
return models.UninstallResponse{}, nil
|
|
}
|
|
|
|
func (p *Plugin) CreateUser(ctx context.Context, req models.CreateUserRequest) (models.CreateUserResponse, error) {
|
|
if p.client == nil {
|
|
return models.CreateUserResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
return p.createUser(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) CreateUserLink(ctx context.Context, req models.CreateUserLinkRequest) (models.CreateUserLinkResponse, error) {
|
|
if p.client == nil {
|
|
return models.CreateUserLinkResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
return p.createUserLink(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) CompleteUserLink(ctx context.Context, req models.CompleteUserLinkRequest) (models.CompleteUserLinkResponse, error) {
|
|
if p.client == nil {
|
|
return models.CompleteUserLinkResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
return p.completeUserLink(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) UpdateUserLink(ctx context.Context, req models.UpdateUserLinkRequest) (models.UpdateUserLinkResponse, error) {
|
|
if p.client == nil {
|
|
return models.UpdateUserLinkResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
return p.updateUserLink(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) DeleteUser(ctx context.Context, req models.DeleteUserRequest) (models.DeleteUserResponse, error) {
|
|
if p.client == nil {
|
|
return models.DeleteUserResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
return p.deleteUser(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) DeleteUserConnection(ctx context.Context, req models.DeleteUserConnectionRequest) (models.DeleteUserConnectionResponse, error) {
|
|
if p.client == nil {
|
|
return models.DeleteUserConnectionResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
return p.deleteUserConnection(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) CreateWebhooks(ctx context.Context, req models.CreateWebhooksRequest) (models.CreateWebhooksResponse, error) {
|
|
if p.client == nil {
|
|
return models.CreateWebhooksResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
return p.createWebhooks(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) TrimWebhook(ctx context.Context, req models.TrimWebhookRequest) (models.TrimWebhookResponse, error) {
|
|
if p.client == nil {
|
|
return models.TrimWebhookResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
webhookTrimmer, ok := p.supportedWebhooks[client.WebhookEventType(req.Config.Name)]
|
|
if !ok {
|
|
return models.TrimWebhookResponse{}, fmt.Errorf("unsupported webhook event type: %s", req.Config.Name)
|
|
}
|
|
|
|
if webhookTrimmer.trimFunction == nil {
|
|
// Nothing to trim, return the webhook as is
|
|
return models.TrimWebhookResponse{
|
|
Webhooks: []models.PSPWebhook{req.Webhook},
|
|
}, nil
|
|
}
|
|
|
|
trimmedWebhook, err := webhookTrimmer.trimFunction(ctx, req)
|
|
if err != nil {
|
|
return models.TrimWebhookResponse{}, err
|
|
}
|
|
|
|
return trimmedWebhook, nil
|
|
}
|
|
|
|
func (p *Plugin) VerifyWebhook(ctx context.Context, req models.VerifyWebhookRequest) (models.VerifyWebhookResponse, error) {
|
|
if p.client == nil {
|
|
return models.VerifyWebhookResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
return p.verifyWebhook(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) TranslateWebhook(ctx context.Context, req models.TranslateWebhookRequest) (models.TranslateWebhookResponse, error) {
|
|
if p.client == nil {
|
|
return models.TranslateWebhookResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
|
|
webhookTranslator, ok := p.supportedWebhooks[client.WebhookEventType(req.Name)]
|
|
if !ok {
|
|
return models.TranslateWebhookResponse{}, fmt.Errorf("unsupported webhook event type: %s", req.Name)
|
|
}
|
|
|
|
resp, err := webhookTranslator.handleFunction(ctx, req)
|
|
if err != nil {
|
|
return models.TranslateWebhookResponse{}, err
|
|
}
|
|
|
|
return models.TranslateWebhookResponse{
|
|
Responses: resp,
|
|
}, nil
|
|
}
|