Skip to content

Commit fc1ca33

Browse files
authored
[stripe] Re-use constants for event types (#17161)
1 parent 7a8ed40 commit fc1ca33

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import (
1515

1616
const maxBodyBytes = int64(65536)
1717

18+
const (
19+
InvoiceFinalizedEventType = "invoice.finalized"
20+
CustomerSubscriptionDeletedEventType = "customer.subscription.deleted"
21+
ChargeDisputeCreatedEventType = "charge.dispute.created"
22+
)
23+
1824
type webhookHandler struct {
1925
billingService billingservice.Interface
2026
stripeWebhookSignature string
@@ -58,7 +64,7 @@ func (h *webhookHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
5864
}
5965

6066
switch event.Type {
61-
case "invoice.finalized":
67+
case InvoiceFinalizedEventType:
6268
invoiceID, ok := event.Data.Object["id"].(string)
6369
if !ok {
6470
log.Error("failed to find invoice id in Stripe event payload")
@@ -72,7 +78,7 @@ func (h *webhookHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
7278
w.WriteHeader(http.StatusInternalServerError)
7379
return
7480
}
75-
case "customer.subscription.deleted":
81+
case CustomerSubscriptionDeletedEventType:
7682
subscriptionID, ok := event.Data.Object["id"].(string)
7783
if !ok {
7884
log.Error("failed to find subscriptionId id in Stripe event payload")
@@ -85,7 +91,7 @@ func (h *webhookHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
8591
w.WriteHeader(http.StatusInternalServerError)
8692
return
8793
}
88-
case "charge.dispute.created":
94+
case ChargeDisputeCreatedEventType:
8995
log.Info("Handling charge dispute")
9096
disputeID, ok := event.Data.Object["id"].(string)
9197
if !ok {

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ import (
2323

2424
// https://stripe.com/docs/api/events/types
2525
const (
26-
invoiceUpdatedEventType = "invoice.updated"
27-
invoiceFinalizedEventType = "invoice.finalized"
28-
customerCreatedEventType = "customer.created"
29-
customerSubscriptionDeleted = "customer.subscription.deleted"
30-
chargeDisputeCreated = "charge.dispute.created"
26+
invoiceUpdatedEventType = "invoice.updated"
27+
customerCreatedEventType = "customer.created"
3128
)
3229

3330
const (
@@ -55,7 +52,7 @@ func TestWebhookAcceptsPostRequests(t *testing.T) {
5552

5653
srv := baseServerWithStripeWebhook(t, &billingservice.NoOpClient{})
5754

58-
payload := payloadForStripeEvent(t, invoiceFinalizedEventType)
55+
payload := payloadForStripeEvent(t, InvoiceFinalizedEventType)
5956

6057
url := fmt.Sprintf("%s%s", srv.HTTPAddress(), "/webhook")
6158

@@ -80,11 +77,11 @@ func TestWebhookIgnoresIrrelevantEvents_NoopClient(t *testing.T) {
8077
ExpectedStatusCode int
8178
}{
8279
{
83-
EventType: invoiceFinalizedEventType,
80+
EventType: InvoiceFinalizedEventType,
8481
ExpectedStatusCode: http.StatusOK,
8582
},
8683
{
87-
EventType: customerSubscriptionDeleted,
84+
EventType: CustomerSubscriptionDeletedEventType,
8885
ExpectedStatusCode: http.StatusOK,
8986
},
9087
{
@@ -96,7 +93,7 @@ func TestWebhookIgnoresIrrelevantEvents_NoopClient(t *testing.T) {
9693
ExpectedStatusCode: http.StatusBadRequest,
9794
},
9895
{
99-
EventType: chargeDisputeCreated,
96+
EventType: ChargeDisputeCreatedEventType,
10097
ExpectedStatusCode: http.StatusOK,
10198
},
10299
}
@@ -134,7 +131,7 @@ func TestWebhookInvokesFinalizeInvoiceRPC(t *testing.T) {
134131

135132
url := fmt.Sprintf("%s%s", srv.HTTPAddress(), "/webhook")
136133

137-
payload := payloadForStripeEvent(t, invoiceFinalizedEventType)
134+
payload := payloadForStripeEvent(t, InvoiceFinalizedEventType)
138135
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(payload))
139136
require.NoError(t, err)
140137

@@ -154,7 +151,7 @@ func TestWebhookInvokesCancelSubscriptionRPC(t *testing.T) {
154151

155152
url := fmt.Sprintf("%s%s", srv.HTTPAddress(), "/webhook")
156153

157-
payload := payloadForStripeEvent(t, customerSubscriptionDeleted)
154+
payload := payloadForStripeEvent(t, CustomerSubscriptionDeletedEventType)
158155
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(payload))
159156
require.NoError(t, err)
160157

0 commit comments

Comments
 (0)