Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/webhooks/stripe.go
2500 views
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
"io"
9
"net/http"
10
11
"github.com/gitpod-io/gitpod/common-go/log"
12
"github.com/gitpod-io/gitpod/public-api-server/pkg/billingservice"
13
"github.com/sirupsen/logrus"
14
"github.com/stripe/stripe-go/v72/webhook"
15
)
16
17
const maxBodyBytes = int64(65536)
18
19
const (
20
InvoiceFinalizedEventType = "invoice.finalized"
21
CustomerSubscriptionDeletedEventType = "customer.subscription.deleted"
22
ChargeDisputeCreatedEventType = "charge.dispute.created"
23
CustomerUpdatedEventType = "customer.updated"
24
)
25
26
type webhookHandler struct {
27
billingService billingservice.Interface
28
stripeWebhookSignature string
29
}
30
31
func NewStripeWebhookHandler(billingService billingservice.Interface, stripeWebhookSignature string) *webhookHandler {
32
return &webhookHandler{
33
billingService: billingService,
34
stripeWebhookSignature: stripeWebhookSignature,
35
}
36
}
37
38
func (h *webhookHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
39
if req.Method != http.MethodPost {
40
log.Errorf("Bad HTTP method: %s", req.Method)
41
w.WriteHeader(http.StatusMethodNotAllowed)
42
return
43
}
44
45
stripeSignature := req.Header.Get("Stripe-Signature")
46
if stripeSignature == "" {
47
w.WriteHeader(http.StatusBadRequest)
48
return
49
}
50
51
req.Body = http.MaxBytesReader(w, req.Body, maxBodyBytes)
52
53
payload, err := io.ReadAll(req.Body)
54
if err != nil {
55
log.WithError(err).Error("Failed to read payload body.")
56
w.WriteHeader(http.StatusBadRequest)
57
return
58
}
59
60
// https://stripe.com/docs/webhooks/signatures#verify-official-libraries
61
event, err := webhook.ConstructEvent(payload, stripeSignature, h.stripeWebhookSignature)
62
if err != nil {
63
log.WithError(err).Error("Failed to verify webhook signature.")
64
w.WriteHeader(http.StatusBadRequest)
65
return
66
}
67
68
logger := log.WithFields(logrus.Fields{"event": event.ID})
69
70
switch event.Type {
71
case InvoiceFinalizedEventType:
72
logger.Info("Handling invoice finalization")
73
invoiceID, ok := event.Data.Object["id"].(string)
74
if !ok {
75
logger.Error("failed to find invoice id in Stripe event payload")
76
w.WriteHeader(http.StatusBadRequest)
77
return
78
}
79
80
err = h.billingService.FinalizeInvoice(req.Context(), invoiceID)
81
if err != nil {
82
logger.WithError(err).Error("Failed to finalize invoice")
83
w.WriteHeader(http.StatusInternalServerError)
84
return
85
}
86
case CustomerSubscriptionDeletedEventType:
87
logger.Info("Handling subscription cancellation")
88
subscriptionID, ok := event.Data.Object["id"].(string)
89
if !ok {
90
logger.Error("failed to find subscriptionId id in Stripe event payload")
91
w.WriteHeader(http.StatusBadRequest)
92
return
93
}
94
err = h.billingService.CancelSubscription(req.Context(), subscriptionID)
95
if err != nil {
96
logger.WithError(err).Error("Failed to cancel subscription")
97
w.WriteHeader(http.StatusInternalServerError)
98
return
99
}
100
case ChargeDisputeCreatedEventType:
101
logger.Info("Handling charge dispute")
102
disputeID, ok := event.Data.Object["id"].(string)
103
if !ok {
104
logger.Error("Failed to identify dispute ID from Stripe webhook.")
105
w.WriteHeader(http.StatusBadRequest)
106
return
107
}
108
109
if err := h.billingService.OnChargeDispute(req.Context(), disputeID); err != nil {
110
logger.WithError(err).Errorf("Failed to handle charge dispute event for dispute ID: %s", disputeID)
111
w.WriteHeader(http.StatusInternalServerError)
112
return
113
}
114
case CustomerUpdatedEventType:
115
logger.Info("Handling CustomerUpdatedEventType")
116
customerID, ok := event.Data.Object["id"].(string)
117
if !ok {
118
logger.Error("Failed to identify customer ID from Stripe webhook.")
119
w.WriteHeader(http.StatusBadRequest)
120
return
121
}
122
123
if _, ok := event.Data.PreviousAttributes["address"]; ok {
124
logger.Infof("Customer with ID %s address updated", customerID)
125
126
err = h.billingService.UpdateCustomerSubscriptionsTaxState(req.Context(), customerID)
127
if err != nil {
128
logger.WithError(err).Error("Failed to cancel subscription")
129
w.WriteHeader(http.StatusInternalServerError)
130
return
131
}
132
}
133
default:
134
logger.Errorf("Unexpected Stripe event type: %s", event.Type)
135
w.WriteHeader(http.StatusBadRequest)
136
return
137
}
138
139
}
140
141