Browse Source

case on webhook event type

Alexander Belanger 4 years ago
parent
commit
18a288fb42
3 changed files with 14 additions and 2 deletions
  1. 6 0
      ee/api/server/handlers/billing/webhook.go
  2. 5 0
      ee/billing/ironplans.go
  3. 3 2
      ee/billing/types.go

+ 6 - 0
ee/api/server/handlers/billing/webhook.go

@@ -55,6 +55,12 @@ func (c *BillingWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
 		return
 	}
 
+	// newUsage will be nil if webhook event type is not "subscription", so return without
+	// updating usage in this case
+	if newUsage == nil {
+		return
+	}
+
 	// update the project's usage
 	_, err = c.Repo().ProjectUsage().ReadProjectUsage(newUsage.ProjectID)
 	notFound := errors.Is(err, gorm.ErrRecordNotFound)

+ 5 - 0
ee/billing/ironplans.go

@@ -315,6 +315,11 @@ func (c *Client) ParseProjectUsageFromWebhook(payload []byte) (*cemodels.Project
 		return nil, err
 	}
 
+	// if event type is not subscription, return wrong webhook event type error
+	if subscription.EventType != "subscription" {
+		return nil, nil
+	}
+
 	// get the project id linked to that team
 	projBilling, err := c.repo.ProjectBilling().ReadProjectBillingByTeamID(subscription.TeamID)
 

+ 3 - 2
ee/billing/types.go

@@ -78,6 +78,7 @@ type CreateIDTokenResponse struct {
 }
 
 type SubscriptionWebhookRequest struct {
-	TeamID string `json:"team_id"`
-	Plan   Plan   `json:"plan"`
+	EventType string `json:"event_type"`
+	TeamID    string `json:"team_id"`
+	Plan      Plan   `json:"plan"`
 }