Skip to content

Commit a9958a0

Browse files
Andrew Farriesroboquat
Andrew Farries
authored andcommitted
Add Stripe webhook to public api server
1 parent 9f31143 commit a9958a0

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

components/public-api-server/pkg/server/server.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ package server
66

77
import (
88
"fmt"
9-
"github.com/gitpod-io/gitpod/public-api/config"
109
"net/url"
1110

11+
"github.com/gitpod-io/gitpod/public-api/config"
12+
"github.com/gorilla/handlers"
13+
1214
"github.com/gitpod-io/gitpod/common-go/baseserver"
1315
"github.com/gitpod-io/gitpod/public-api-server/pkg/apiv1"
1416
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
17+
"github.com/gitpod-io/gitpod/public-api-server/pkg/webhooks"
1518
v1 "github.com/gitpod-io/gitpod/public-api/v1"
1619
"github.com/prometheus/client_golang/prometheus"
1720
"github.com/sirupsen/logrus"
@@ -36,6 +39,10 @@ func Start(logger *logrus.Entry, cfg *config.Configuration) error {
3639
return fmt.Errorf("failed to initialize public api server: %w", err)
3740
}
3841

42+
srv.HTTPMux().Handle("/stripe/invoices/webhook",
43+
handlers.ContentTypeHandler(webhooks.NewStripeWebhookHandler(), "application/json"),
44+
)
45+
3946
if registerErr := register(srv, gitpodAPI, registry); registerErr != nil {
4047
return fmt.Errorf("failed to register services: %w", registerErr)
4148
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package webhooks
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"net/http"
11+
12+
"github.com/gitpod-io/gitpod/common-go/log"
13+
"github.com/stripe/stripe-go/v72"
14+
)
15+
16+
type webhookHandler struct{}
17+
18+
func NewStripeWebhookHandler() *webhookHandler {
19+
return &webhookHandler{}
20+
}
21+
22+
func (h *webhookHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
23+
const maxBodyBytes = int64(65536)
24+
25+
req.Body = http.MaxBytesReader(w, req.Body, maxBodyBytes)
26+
27+
event := stripe.Event{}
28+
err := json.NewDecoder(req.Body).Decode(&event)
29+
if err != nil {
30+
log.WithError(err).Error("Stripe webhook error while parsing event payload")
31+
w.WriteHeader(http.StatusBadRequest)
32+
return
33+
}
34+
35+
// TODO: verify webhook signature.
36+
// Conditional on there being a secret configured.
37+
38+
fmt.Fprintf(w, "event type: %s", event.Type)
39+
}

0 commit comments

Comments
 (0)