@@ -981,6 +981,22 @@ ErrorCode SubscriptionController::processPlayMarketPurchase(const QString &userC
981981 QObject::connect (&watcher, &QFutureWatcher<QPair<bool , QString>>::finished, &waitLoop, &QEventLoop::quit);
982982
983983 QFuture<QPair<bool , QString>> future = QtConcurrent::run ([androidController, productId]() {
984+ // If the user already has an active "premium" subscription, upgrade/replace it with proration
985+ // instead of stacking a second, independent purchase that Google Play would just queue behind it.
986+ QString oldPurchaseToken;
987+ QJsonObject existingPurchasesResult = androidController->queryPurchases ();
988+ if (existingPurchasesResult.value (" responseCode" ).toInt (-1 ) == 0 ) {
989+ const QJsonArray existingPurchases = existingPurchasesResult.value (" purchases" ).toArray ();
990+ for (const QJsonValue &purchaseValue : existingPurchases) {
991+ const QJsonObject existingPurchase = purchaseValue.toObject ();
992+ if (existingPurchase.value (" purchaseState" ).toInt (-1 ) == 1 ) { // PURCHASED
993+ oldPurchaseToken = existingPurchase.value (" purchaseToken" ).toString ();
994+ qInfo () << " [Billing] Found existing active subscription, will upgrade instead of purchasing a new one" ;
995+ break ;
996+ }
997+ }
998+ }
999+
9841000 QJsonObject plansResult = androidController->getSubscriptionPlans ();
9851001 int responseCode = plansResult.value (" responseCode" ).toInt (-1 );
9861002 if (responseCode != 0 ) {
@@ -1016,7 +1032,9 @@ ErrorCode SubscriptionController::processPlayMarketPurchase(const QString &userC
10161032 qWarning () << " [Billing] No offer token found for basePlanId:" << productId;
10171033 return qMakePair (false , QString ());
10181034 }
1019- QJsonObject purchaseResult = androidController->purchaseSubscription (offerToken);
1035+ QJsonObject purchaseResult = oldPurchaseToken.isEmpty ()
1036+ ? androidController->purchaseSubscription (offerToken)
1037+ : androidController->upgradeSubscription (offerToken, oldPurchaseToken);
10201038 responseCode = purchaseResult.value (" responseCode" ).toInt (-1 );
10211039 if (responseCode != 0 ) {
10221040 qWarning () << " [Billing] Purchase failed, responseCode:" << responseCode;
0 commit comments