Skip to content

Commit

Permalink
(NOBIDS) fix api-products, improve stripe-webhooks handling (#2897)
Browse files Browse the repository at this point in the history
* (NOBIDS) retry if webhooks come out of order

* (NOBIDS) fix api-products

* (NOBIDS) less retries on out-of-order stripe-webhooks
  • Loading branch information
guybrush authored Jun 18, 2024
1 parent 6d4fb29 commit 93a7930
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 17 additions & 1 deletion handlers/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,23 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) {
}
defer tx.Rollback()

err = db.StripeUpdateSubscriptionStatus(tx, invoice.Lines.Data[0].Subscription, true, nil)
// retry updating subs if webhooks come out of order
retries := 0
for {
err = db.StripeUpdateSubscriptionStatus(tx, invoice.Lines.Data[0].Subscription, true, nil)
if err == nil {
break
}
if err.Error() == "no rows affected" {
retries++
if retries > 3 {
break
}
time.Sleep(1000 * time.Millisecond)
continue
}
break
}
if err != nil {
logger.WithError(err).Error("error processing invoice failed to activate subscription for customer", invoice.Customer.ID)
http.Error(w, "error processing invoice failed to activate subscription for customer", http.StatusInternalServerError)
Expand Down
9 changes: 9 additions & 0 deletions utils/products.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const GROUP_MOBILE = "mobile"
const GROUP_ADDON = "addon"

var ProductsGroups = map[string]string{
"sapphire": GROUP_API,
"emerald": GROUP_API,
"diamond": GROUP_API,
"plankton": GROUP_MOBILE,
"goldfish": GROUP_MOBILE,
"whale": GROUP_MOBILE,
Expand Down Expand Up @@ -93,6 +96,12 @@ func ProductIsEligibleForAddons(productId string) bool {

func PriceIdToProductId(priceId string) string {
switch priceId {
case Config.Frontend.Stripe.Sapphire:
return "sapphire"
case Config.Frontend.Stripe.Emerald:
return "emerald"
case Config.Frontend.Stripe.Diamond:
return "diamond"
case Config.Frontend.Stripe.Plankton:
return "plankton"
case Config.Frontend.Stripe.Goldfish:
Expand Down

0 comments on commit 93a7930

Please sign in to comment.