Skip to content

Commit

Permalink
fix(mangopay): fix webhooks handler
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Sep 20, 2024
1 parent ff96ede commit de8c34e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion components/payments/internal/api/v3/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func newRouter(backend backend.Backend, a auth.Authenticator, debug bool) *chi.M

// Public routes
r.Group(func(r chi.Router) {
r.Post("/connectors/webhooks/{connectorID}/*", connectorsWebhooks(backend))
r.Handle("/connectors/webhooks/{connectorID}/*", connectorsWebhooks(backend))
})

// Authenticated routes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@ type Webhook struct {
EventType EventType `json:"EventType"`
}

func (c *Client) UnmarshalWebhooks(req string) (*Webhook, error) {
res := Webhook{}
err := json.Unmarshal([]byte(req), &res)
if err != nil {
return nil, err
}
return &res, nil
}

type Hook struct {
ID string `json:"Id"`
URL string `json:"Url"`
Expand Down Expand Up @@ -146,7 +137,6 @@ func (c *Client) CreateHook(ctx context.Context, eventType EventType, URL string
}
req.Header.Set("Content-Type", "application/json")

return nil
var errRes mangopayError
_, err = c.httpClient.Do(req, nil, &errRes)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mangopay
import (
"context"
"errors"
"strconv"

"github.com/formancehq/payments/internal/connectors/plugins"
"github.com/formancehq/payments/internal/connectors/plugins/public/mangopay/client"
Expand All @@ -26,9 +27,18 @@ func (p *Plugin) Install(_ context.Context, req models.InstallRequest) (models.I
p.client = client
p.initWebhookConfig()

configs := make([]models.PSPWebhookConfig, 0, len(webhookConfigs))
for name, config := range webhookConfigs {
configs = append(configs, models.PSPWebhookConfig{
Name: string(name),
URLPath: config.urlPath,
})
}

return models.InstallResponse{
Capabilities: capabilities,
Workflow: workflow(),
Capabilities: capabilities,
WebhooksConfigs: configs,
Workflow: workflow(),
}, nil
}

Expand Down Expand Up @@ -93,9 +103,29 @@ func (p Plugin) CreateWebhooks(ctx context.Context, req models.CreateWebhooksReq
}

func (p Plugin) TranslateWebhook(ctx context.Context, req models.TranslateWebhookRequest) (models.TranslateWebhookResponse, error) {
webhook, err := p.client.UnmarshalWebhooks(string(req.Webhook.Body))
// Mangopay does not send us the event inside the body, but using
// URL query.
eventType, ok := req.Webhook.QueryValues["EventType"]
if !ok || len(eventType) == 0 {
return models.TranslateWebhookResponse{}, errors.New("missing EventType query parameter")
}
resourceID, ok := req.Webhook.QueryValues["RessourceId"]
if !ok || len(resourceID) == 0 {
return models.TranslateWebhookResponse{}, errors.New("missing RessourceId query parameter")
}
v, ok := req.Webhook.QueryValues["Date"]
if !ok || len(v) == 0 {
return models.TranslateWebhookResponse{}, errors.New("missing Date query parameter")
}
date, err := strconv.ParseInt(v[0], 10, 64)
if err != nil {
return models.TranslateWebhookResponse{}, err
return models.TranslateWebhookResponse{}, errors.New("invalid Date query parameter")
}

webhook := client.Webhook{
ResourceID: resourceID[0],
Date: date,
EventType: client.EventType(eventType[0]),
}

config, ok := webhookConfigs[webhook.EventType]
Expand All @@ -105,7 +135,7 @@ func (p Plugin) TranslateWebhook(ctx context.Context, req models.TranslateWebhoo

webhookResponse, err := config.fn(ctx, webhookTranslateRequest{
req: req,
webhook: webhook,
webhook: &webhook,
})
if err != nil {
return models.TranslateWebhookResponse{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ func (p Plugin) createWebhooks(ctx context.Context, req models.CreateWebhooksReq

webhookURL := fmt.Sprintf("%s/api/payments/v3/connectors/webhooks/%s", stackPublicURL, req.ConnectorID)
for eventType, config := range webhookConfigs {
url := fmt.Sprintf("%s%s", webhookURL, config.urlPath)
if v, ok := activeHooks[eventType]; ok {
// Already created, continue

if v.URL != webhookURL {
// If the URL is different, update it
err := p.client.UpdateHook(ctx, v.ID, webhookURL)
err := p.client.UpdateHook(ctx, v.ID, url)
if err != nil {
return err
}
Expand All @@ -130,8 +131,6 @@ func (p Plugin) createWebhooks(ctx context.Context, req models.CreateWebhooksReq
continue
}

url := fmt.Sprintf("%s%s", webhookURL, config.urlPath)

// Otherwise, create it
err := p.client.CreateHook(ctx, eventType, url)
if err != nil {
Expand Down

0 comments on commit de8c34e

Please sign in to comment.