Skip to content

Commit d0b9ad6

Browse files
committed
fix(api,stripe): fix stripe-handlers for sub-updates from nonmobile to mobile
1 parent 2a827e2 commit d0b9ad6

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

db/frontend.go

+10
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ func InsertMobileSubscription(tx *sql.Tx, userID uint64, paymentDetails types.Mo
425425
return err
426426
}
427427

428+
func DeleteMobileSubscriptionBySubscriptionID(tx *sql.Tx, subID string) error {
429+
var err error
430+
if tx == nil {
431+
_, err = FrontendWriterDB.Exec("DELETE FROM users_app_subscriptions WHERE subscription_id = $1", subID)
432+
} else {
433+
_, err = tx.Exec("DELETE FROM users_app_subscriptions WHERE subscription_id = $1", subID)
434+
}
435+
return err
436+
}
437+
428438
func ChangeProductIDFromStripe(tx *sql.Tx, stripeSubscriptionID string, productID string) error {
429439
now := time.Now()
430440
nowTs := now.Unix()

handlers/stripe.go

+5-16
Original file line numberDiff line numberDiff line change
@@ -468,23 +468,12 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) {
468468
return
469469
}
470470
} else {
471-
appSubID, err := db.GetUserSubscriptionIDByStripe(subscription.ID)
471+
// make sure to delete all mobile subscriptions with the same subID
472+
err = db.DeleteMobileSubscriptionBySubscriptionID(tx, subscription.ID)
472473
if err != nil {
473-
if err == sql.ErrNoRows {
474-
// no mobile-subscription found for this stripe-subscription, nothing to do
475-
} else {
476-
logger.WithError(err).WithField("subscription.ID", subscription.ID).Error("error updating user subscription, calling db.GetUserSubscriptionIDByStripe")
477-
http.Error(w, "error updating subscription", http.StatusInternalServerError)
478-
return
479-
}
480-
} else {
481-
// subscription changed from mobile to nonmobile, deactivate mobile subscription
482-
err = db.UpdateUserSubscription(tx, appSubID, false, time.Now().Unix(), "user_canceled")
483-
if err != nil {
484-
logger.WithError(err).Error("error updating stripe mobile subscription (sub updated)", subscription.ID)
485-
http.Error(w, "error updating subscription", http.StatusInternalServerError)
486-
return
487-
}
474+
logger.WithError(err).Error("error updating stripe mobile subscription (sub updated)", subscription.ID)
475+
http.Error(w, "error updating subscription", http.StatusInternalServerError)
476+
return
488477
}
489478
}
490479

0 commit comments

Comments
 (0)