mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
145 lines
3.5 KiB
Go
145 lines
3.5 KiB
Go
package adyen
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/observe/log"
|
|
"github.com/hanzo-fi/payments/ce/plugins/adyen/client"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
pkgplugins "github.com/hanzo-fi/payments/pkg/domain/plugins"
|
|
)
|
|
|
|
const ProviderName = "adyen"
|
|
|
|
var Registration = pkgplugins.Registration{
|
|
PluginType: models.PluginTypePSP,
|
|
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
|
|
client client.Client
|
|
config Config
|
|
supportedWebhooks map[string]supportedWebhook
|
|
}
|
|
|
|
func New(name string, logger logging.Logger, rawConfig json.RawMessage) (*Plugin, error) {
|
|
config, err := unmarshalAndValidateConfig(rawConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
client := client.New(
|
|
ProviderName,
|
|
config.APIKey,
|
|
config.WebhookUsername,
|
|
config.WebhookPassword,
|
|
config.CompanyID,
|
|
config.LiveEndpointPrefix,
|
|
)
|
|
|
|
p := &Plugin{
|
|
Plugin: pkgplugins.NewBasePlugin(),
|
|
|
|
name: name,
|
|
logger: logger,
|
|
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
|
|
}
|
|
|
|
err := p.client.DeleteWebhook(ctx, req.ConnectorID)
|
|
return models.UninstallResponse{}, err
|
|
}
|
|
|
|
func (p *Plugin) FetchNextAccounts(ctx context.Context, req models.FetchNextAccountsRequest) (models.FetchNextAccountsResponse, error) {
|
|
if p.client == nil {
|
|
return models.FetchNextAccountsResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
return p.fetchNextAccounts(ctx, req)
|
|
}
|
|
|
|
func (p *Plugin) CreateWebhooks(ctx context.Context, req models.CreateWebhooksRequest) (models.CreateWebhooksResponse, error) {
|
|
if p.client == nil {
|
|
return models.CreateWebhooksResponse{}, pkgplugins.ErrNotYetInstalled
|
|
}
|
|
configs, err := p.createWebhooks(ctx, req)
|
|
if err != nil {
|
|
return models.CreateWebhooksResponse{}, err
|
|
}
|
|
|
|
others := make([]models.PSPOther, 0, len(configs))
|
|
for _, config := range configs {
|
|
raw, err := json.Marshal(&config)
|
|
if err != nil {
|
|
return models.CreateWebhooksResponse{}, err
|
|
}
|
|
others = append(others, models.PSPOther{
|
|
ID: config.Name,
|
|
Other: raw,
|
|
})
|
|
}
|
|
|
|
return models.CreateWebhooksResponse{
|
|
Others: others,
|
|
Configs: configs,
|
|
}, 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
|
|
}
|
|
|
|
config, ok := p.supportedWebhooks[req.Name]
|
|
if !ok {
|
|
return models.TranslateWebhookResponse{}, errors.New("unknown webhook")
|
|
}
|
|
|
|
return config.fn(ctx, req)
|
|
}
|
|
|
|
var _ models.Plugin = &Plugin{}
|