diff --git a/db/frontend.go b/db/frontend.go index 1d8ea8108f..c7e406634e 100644 --- a/db/frontend.go +++ b/db/frontend.go @@ -425,6 +425,16 @@ func InsertMobileSubscription(tx *sql.Tx, userID uint64, paymentDetails types.Mo return err } +func DeleteMobileSubscriptionBySubscriptionID(tx *sql.Tx, subID string) error { + var err error + if tx == nil { + _, err = FrontendWriterDB.Exec("DELETE FROM users_app_subscriptions WHERE subscription_id = $1", subID) + } else { + _, err = tx.Exec("DELETE FROM users_app_subscriptions WHERE subscription_id = $1", subID) + } + return err +} + func ChangeProductIDFromStripe(tx *sql.Tx, stripeSubscriptionID string, productID string) error { now := time.Now() nowTs := now.Unix() diff --git a/handlers/stripe.go b/handlers/stripe.go index 8fbe206eee..89e21a947a 100644 --- a/handlers/stripe.go +++ b/handlers/stripe.go @@ -468,23 +468,12 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) { return } } else { - appSubID, err := db.GetUserSubscriptionIDByStripe(subscription.ID) + // make sure to delete all mobile subscriptions with the same subID + err = db.DeleteMobileSubscriptionBySubscriptionID(tx, subscription.ID) if err != nil { - if err == sql.ErrNoRows { - // no mobile-subscription found for this stripe-subscription, nothing to do - } else { - logger.WithError(err).WithField("subscription.ID", subscription.ID).Error("error updating user subscription, calling db.GetUserSubscriptionIDByStripe") - http.Error(w, "error updating subscription", http.StatusInternalServerError) - return - } - } else { - // subscription changed from mobile to nonmobile, deactivate mobile subscription - err = db.UpdateUserSubscription(tx, appSubID, false, time.Now().Unix(), "user_canceled") - if err != nil { - logger.WithError(err).Error("error updating stripe mobile subscription (sub updated)", subscription.ID) - http.Error(w, "error updating subscription", http.StatusInternalServerError) - return - } + logger.WithError(err).Error("error updating stripe mobile subscription (sub updated)", subscription.ID) + http.Error(w, "error updating subscription", http.StatusInternalServerError) + return } }