Skip to content

Commit

Permalink
fix(api,stripe): fix stripe-handlers for sub-updates from nonmobile t…
Browse files Browse the repository at this point in the history
…o mobile
  • Loading branch information
guybrush committed Oct 22, 2024
1 parent 2a827e2 commit d0b9ad6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
10 changes: 10 additions & 0 deletions db/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 5 additions & 16 deletions handlers/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit d0b9ad6

Please sign in to comment.