From 3f59f23a3f20f87e24ee824999f66e3a0eb49493 Mon Sep 17 00:00:00 2001 From: NickVs2015 Date: Mon, 1 Jun 2026 22:09:09 +0300 Subject: [PATCH] fix: split payment to detect payment type --- .../api/subscriptionController.cpp | 54 ++++++++++++------- .../controllers/api/subscriptionController.h | 2 - .../api/subscriptionUiController.cpp | 2 - 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/client/core/controllers/api/subscriptionController.cpp b/client/core/controllers/api/subscriptionController.cpp index 4c5666ef6..d48d43592 100644 --- a/client/core/controllers/api/subscriptionController.cpp +++ b/client/core/controllers/api/subscriptionController.cpp @@ -333,7 +333,6 @@ ErrorCode SubscriptionController::importTrialFromGateway(const QString &userCoun ErrorCode SubscriptionController::importServiceFromMarket(const QString &userCountryCode, const QString &serviceType, const QString &serviceProtocol, const ProtocolData &protocolData, const QString &transactionId, bool isTestPurchase, - ServerConfig &serverConfig, int *duplicateServerIndex) { GatewayRequestData gatewayRequestData { QSysInfo::productType(), @@ -790,9 +789,8 @@ ErrorCode SubscriptionController::processAppStorePurchase(const QString &userCou bool isTestPurchase = IosController::Instance()->isTestFlight(); ProtocolData protocolData = generateProtocolData(serviceProtocol); - ServerConfig serverConfig; return importServiceFromMarket(userCountryCode, serviceType, serviceProtocol, protocolData, - originalTransactionId, isTestPurchase, serverConfig, duplicateServerIndex); + originalTransactionId, isTestPurchase, duplicateServerIndex); #else Q_UNUSED(userCountryCode); Q_UNUSED(serviceType); @@ -804,7 +802,6 @@ ErrorCode SubscriptionController::processAppStorePurchase(const QString &userCou ErrorCode SubscriptionController::processPlayMarketPurchase(const QString &userCountryCode, const QString &serviceType, const QString &serviceProtocol, const QString &productId, - ServerConfig &serverConfig, int *duplicateServerIndex) { #if defined(Q_OS_ANDROID) @@ -876,15 +873,40 @@ ErrorCode SubscriptionController::processPlayMarketPurchase(const QString &userC return ErrorCode::ApiPurchaseError; } + // First call: determine if this is a test purchase + GatewayRequestData checkRequestData { QSysInfo::productType(), + QString(APP_VERSION), + m_appSettingsRepository->getAppLanguage().name().split("_").first(), + m_appSettingsRepository->getInstallationUuid(true), + userCountryCode, + "", + serviceType, + serviceProtocol, + QJsonObject() }; + + QJsonObject checkPayload = checkRequestData.toJsonObject(); + checkPayload[apiDefs::key::transactionId] = purchaseToken; + + QByteArray checkResponse; + ErrorCode checkError = executeRequest(QString("%1v1/subscriptions"), checkPayload, checkResponse, false); + if (checkError != ErrorCode::NoError) { + qWarning().noquote() << "[Billing] Initial subscriptions check failed:" << static_cast(checkError); + return checkError; + } + + QJsonObject checkObject = QJsonDocument::fromJson(checkResponse).object(); + bool isTestPurchase = checkObject.value(apiDefs::key::isTestPurchase).toBool(false); + qInfo().noquote() << "[Billing] Purchase isTestPurchase =" << isTestPurchase; + + // Second call: import service with correct isTestPurchase flag ProtocolData protocolData = generateProtocolData(serviceProtocol); - return importServiceFromMarket(userCountryCode, serviceType, serviceProtocol, protocolData, //here we should now all about type payment - purchaseToken, false, serverConfig, duplicateServerIndex); + return importServiceFromMarket(userCountryCode, serviceType, serviceProtocol, protocolData, + purchaseToken, isTestPurchase, duplicateServerIndex); #else Q_UNUSED(userCountryCode); Q_UNUSED(serviceType); Q_UNUSED(serviceProtocol); Q_UNUSED(productId); - Q_UNUSED(serverConfig); return ErrorCode::ApiPurchaseError; #endif } @@ -944,9 +966,8 @@ SubscriptionController::AppStoreRestoreResult SubscriptionController::processApp ProtocolData protocolData = generateProtocolData(serviceProtocol); int currentDuplicateServerIndex = -1; - ServerConfig serverConfig; ErrorCode errorCode = importServiceFromMarket(userCountryCode, serviceType, serviceProtocol, protocolData, - originalTransactionId, isTestPurchase, serverConfig, + originalTransactionId, isTestPurchase, ¤tDuplicateServerIndex); if (errorCode == ErrorCode::ApiConfigAlreadyAdded) { @@ -1050,10 +1071,9 @@ SubscriptionController::PlayMarketRestoreResult SubscriptionController::processP } ProtocolData protocolData = generateProtocolData(serviceProtocol); - ServerConfig serverConfig; int currentDuplicateServerIndex = -1; ErrorCode errorCode = importServiceFromMarket(userCountryCode, serviceType, serviceProtocol, protocolData, - purchaseToken, false, serverConfig, + purchaseToken, false, ¤tDuplicateServerIndex); if (errorCode == ErrorCode::ApiConfigAlreadyAdded) { @@ -1085,18 +1105,14 @@ SubscriptionController::PlayMarketRestoreResult SubscriptionController::processP #endif } -ErrorCode SubscriptionController::getAccountInfo(int serverIndex, QJsonObject &accountInfo) +ErrorCode SubscriptionController::getAccountInfo(const QString &serverId, QJsonObject &accountInfo) { - ServerConfig serverConfigModel = m_serversRepository->server(serverIndex); - - if (!serverConfigModel.isApiV2()) { + auto apiV2Opt = m_serversRepository->apiV2Config(serverId); + if (!apiV2Opt.has_value()) { return ErrorCode::InternalError; } - const ApiV2ServerConfig* apiV2 = serverConfigModel.as(); - if (!apiV2) { - return ErrorCode::InternalError; - } + const ApiV2ServerConfig* apiV2 = &apiV2Opt.value(); bool isTestPurchase = apiV2->apiConfig.isTestPurchase; QJsonObject authDataJson = apiV2->authData.toJson(); diff --git a/client/core/controllers/api/subscriptionController.h b/client/core/controllers/api/subscriptionController.h index 776c8a561..17289703e 100644 --- a/client/core/controllers/api/subscriptionController.h +++ b/client/core/controllers/api/subscriptionController.h @@ -64,7 +64,6 @@ public: ErrorCode importServiceFromMarket(const QString &userCountryCode, const QString &serviceType, const QString &serviceProtocol, const ProtocolData &protocolData, const QString &transactionId, bool isTestPurchase, - ServerConfig &serverConfig, int *duplicateServerIndex = nullptr); ErrorCode updateServiceFromGateway(const QString &serverId, const QString &newCountryCode, bool isConnectEvent); @@ -115,7 +114,6 @@ public: ErrorCode processPlayMarketPurchase(const QString &userCountryCode, const QString &serviceType, const QString &serviceProtocol, const QString &productId, - ServerConfig &serverConfig, int *duplicateServerIndex = nullptr); AppStoreRestoreResult processAppStoreRestore(const QString &userCountryCode, const QString &serviceType, diff --git a/client/ui/controllers/api/subscriptionUiController.cpp b/client/ui/controllers/api/subscriptionUiController.cpp index 6cef7acd7..22773037a 100644 --- a/client/ui/controllers/api/subscriptionUiController.cpp +++ b/client/ui/controllers/api/subscriptionUiController.cpp @@ -235,14 +235,12 @@ bool SubscriptionUiController::importPremiumFromPlayMarket(const QString &storeP productId = QStringLiteral("premium"); } - ServerConfig serverConfig; int duplicateServerIndex = -1; ErrorCode errorCode = m_subscriptionController->processPlayMarketPurchase( m_apiServicesModel->getCountryCode(), m_apiServicesModel->getSelectedServiceType(), m_apiServicesModel->getSelectedServiceProtocol(), productId, - serverConfig, &duplicateServerIndex); if (errorCode != ErrorCode::NoError) {