MCPcopy Create free account
hub / github.com/bytebase/bytebase / handleInvoicePaid

Method handleInvoicePaid

backend/api/stripe/webhook.go:230–348  ·  view source on GitHub ↗
(ctx context.Context, event *stripego.Event)

Source from the content-addressed store, hash-verified

228}
229
230func (h *WebhookHandler) handleInvoicePaid(ctx context.Context, event *stripego.Event) error {
231 var inv stripego.Invoice
232 if err := json.Unmarshal(event.Data.Raw, &inv); err != nil {
233 slog.Error("failed to unmarshal stripe invoice from webhook",
234 log.BBError(err),
235 slog.String("event_id", event.ID),
236 )
237 return errors.Wrap(err, "failed to unmarshal invoice")
238 }
239
240 if len(inv.Lines.Data) == 0 {
241 slog.Error("stripe invoice has no line items",
242 slog.String("invoice_id", inv.ID),
243 slog.String("event_id", event.ID),
244 )
245 return errors.Errorf("invoice %s has no line items", inv.ID)
246 }
247
248 metadata := inv.Lines.Data[0].Metadata
249 if metadata["source"] != stripeplugin.WebhookSource {
250 return nil
251 }
252
253 workspace := metadata["workspace"]
254 if workspace == "" {
255 slog.Error("missing workspace in invoice metadata",
256 slog.String("invoice_id", inv.ID),
257 slog.String("event_id", event.ID),
258 )
259 return nil
260 }
261
262 stripeCustomer, err := getStripeCustomer(inv.Customer)
263 if err != nil {
264 slog.Error("failed to get stripe customer from invoice",
265 log.BBError(err),
266 slog.String("workspace", workspace),
267 slog.String("invoice_id", inv.ID),
268 )
269 return err
270 }
271
272 payload, err := buildPayloadFromMetadata(metadata)
273 if err != nil {
274 slog.Error("failed to build payload from invoice metadata",
275 log.BBError(err),
276 slog.String("workspace", workspace),
277 slog.String("invoice_id", inv.ID),
278 )
279 return errors.Wrapf(err, "failed to build payload from invoice metadata for workspace %s", workspace)
280 }
281
282 // Get Stripe subscription ID from invoice parent.
283 var stripeSubID string
284 if inv.Parent != nil && inv.Parent.SubscriptionDetails != nil && inv.Parent.SubscriptionDetails.Subscription != nil {
285 stripeSubID = inv.Parent.SubscriptionDetails.Subscription.ID
286 }
287

Callers 1

processEventMethod · 0.95

Calls 12

updateLicenseMethod · 0.95
BBErrorFunction · 0.92
getStripeCustomerFunction · 0.85
buildPayloadFromMetadataFunction · 0.85
UnmarshalMethod · 0.80
ErrorfMethod · 0.80
InfoMethod · 0.80
UpsertSubscriptionMethod · 0.80
StringMethod · 0.65
ErrorMethod · 0.45
IntMethod · 0.45

Tested by

no test coverage detected