From 9ebefe79caedd9c45d5530ddb0e1ff02d09f4005 Mon Sep 17 00:00:00 2001 From: albexk Date: Fri, 8 Nov 2024 11:12:56 +0300 Subject: [PATCH] refactor: modify response json --- .../src/main/kotlin/BillingException.kt | 2 +- .../src/main/kotlin/BillingProvider.kt | 32 ++++++++----------- .../utils/src/main/kotlin/ErrorCode.kt | 15 +++++---- client/core/defs.h | 13 ++++---- client/core/errorstrings.cpp | 1 + 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/client/android/billing/src/main/kotlin/BillingException.kt b/client/android/billing/src/main/kotlin/BillingException.kt index f1ebd519..0094669c 100644 --- a/client/android/billing/src/main/kotlin/BillingException.kt +++ b/client/android/billing/src/main/kotlin/BillingException.kt @@ -18,7 +18,7 @@ internal class BillingException( ) : Exception(billingResult.toString()) { constructor(msg: String) : this(BillingResult.newBuilder() - .setResponseCode(9999) + .setResponseCode(DEVELOPER_ERROR) .setDebugMessage(msg) .build()) diff --git a/client/android/billing/src/main/kotlin/BillingProvider.kt b/client/android/billing/src/main/kotlin/BillingProvider.kt index 59fe5cab..4893eb30 100644 --- a/client/android/billing/src/main/kotlin/BillingProvider.kt +++ b/client/android/billing/src/main/kotlin/BillingProvider.kt @@ -26,14 +26,12 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.withContext +import org.amnezia.vpn.util.ErrorCode import org.amnezia.vpn.util.Log import org.json.JSONArray import org.json.JSONObject private const val TAG = "BillingProvider" -private const val RESULT_OK = 1 -private const val RESULT_CANCELED = 0 -private const val RESULT_ERROR = -1 private const val PRODUCT_ID = "premium" class BillingProvider(context: Context) : AutoCloseable { @@ -87,20 +85,18 @@ class BillingProvider(context: Context) : AutoCloseable { } catch (e: BillingException) { if (e.isCanceled) { Log.w(TAG, "Billing canceled") - return JSONObject().put("result", RESULT_CANCELED) + return JSONObject().put("responseCode", ErrorCode.BillingCanceled) } else if (e.isRetryable && attemptCount < numberAttempts) { Log.d(TAG, "Retryable error: $e") ++attemptCount delay(1000) } else { Log.e(TAG, "Billing error: $e") - return JSONObject() - .put("result", RESULT_ERROR) - .put("errorCode", e.errorCode) + return JSONObject().put("responseCode", e.errorCode) } } catch (_: CancellationException) { Log.w(TAG, "Billing coroutine canceled") - return JSONObject().put("result", RESULT_CANCELED) + return JSONObject().put("responseCode", ErrorCode.BillingCanceled) } } } @@ -109,7 +105,7 @@ class BillingProvider(context: Context) : AutoCloseable { Log.v(TAG, "Get subscription plans") val productDetailsList = getProductDetails() - val resultJson = JSONObject().put("result", RESULT_OK) + val resultJson = JSONObject().put("responseCode", ErrorCode.NoError) val productArray = JSONArray().also { resultJson.put("products", it) } productDetailsList?.forEach { productDetails -> val product = JSONObject().also { productArray.put(it) } @@ -178,7 +174,7 @@ class BillingProvider(context: Context) : AutoCloseable { val countryCode = deferred.await() return JSONObject() - .put("result", RESULT_OK) + .put("responseCode", ErrorCode.NoError) .put("countryCode", countryCode) } @@ -233,7 +229,7 @@ class BillingProvider(context: Context) : AutoCloseable { subscriptionPurchases.firstOrNull { it != null }?.let { (billingResult, purchases) -> if (!billingResult.isOk) throw BillingException(billingResult) return JSONObject() - .put("result", RESULT_OK) + .put("responseCode", ErrorCode.NoError) .put("purchases", processPurchases(purchases)) } ?: throw BillingException("Purchase failed") } @@ -241,20 +237,20 @@ class BillingProvider(context: Context) : AutoCloseable { private fun processPurchases(purchases: List?): JSONArray { val purchaseArray = JSONArray() purchases?.forEach { purchase -> - val purchaseJson = JSONObject().also { purchaseArray.put(it) } + /* val purchaseJson = */ JSONObject().also { purchaseArray.put(it) } .put("purchaseToken", purchase.purchaseToken) .put("purchaseTime", purchase.purchaseTime) .put("purchaseState", purchase.purchaseState) .put("isAcknowledged", purchase.isAcknowledged) .put("isAutoRenewing", purchase.isAutoRenewing) .put("orderId", purchase.orderId) - .put("productIds", JSONArray(purchase.products)) + // .put("productIds", JSONArray(purchase.products)) - purchase.pendingPurchaseUpdate?.let { purchaseUpdate -> + /* purchase.pendingPurchaseUpdate?.let { purchaseUpdate -> JSONObject() .put("purchaseToken", purchaseUpdate.purchaseToken) - .put("productIds", JSONArray(purchaseUpdate.products)) - }.also { purchaseJson.put("pendingPurchaseUpdate", it) } + // .put("productIds", JSONArray(purchaseUpdate.products)) + }.also { purchaseJson.put("pendingPurchaseUpdate", it) } */ } return purchaseArray } @@ -281,14 +277,14 @@ class BillingProvider(context: Context) : AutoCloseable { throw BillingException(result) } - return JSONObject().put("result", RESULT_OK) + return JSONObject().put("responseCode", ErrorCode.NoError) } suspend fun getPurchases(): JSONObject { Log.v(TAG, "Get purchases") val purchases = queryPurchases() return JSONObject() - .put("result", RESULT_OK) + .put("responseCode", ErrorCode.NoError) .put("purchases", processPurchases(purchases)) } diff --git a/client/android/utils/src/main/kotlin/ErrorCode.kt b/client/android/utils/src/main/kotlin/ErrorCode.kt index 14b32903..5626cabf 100644 --- a/client/android/utils/src/main/kotlin/ErrorCode.kt +++ b/client/android/utils/src/main/kotlin/ErrorCode.kt @@ -2,10 +2,13 @@ package org.amnezia.vpn.util // keep synchronized with client/core/defs.h error_code_ns::ErrorCode object ErrorCode { - const val BillingError = 1300 - const val BillingGooglePlayError = 1301 - const val BillingUnavailable = 1302 - const val SubscriptionAlreadyOwned = 1303 - const val SubscriptionUnavailable = 1304 - const val BillingNetworkError = 1305 + const val NoError = 0 + + const val BillingCanceled = 1300 + const val BillingError = 1301 + const val BillingGooglePlayError = 1302 + const val BillingUnavailable = 1303 + const val SubscriptionAlreadyOwned = 1304 + const val SubscriptionUnavailable = 1305 + const val BillingNetworkError = 1306 } diff --git a/client/core/defs.h b/client/core/defs.h index f871ae83..ed4c4d7a 100644 --- a/client/core/defs.h +++ b/client/core/defs.h @@ -119,12 +119,13 @@ namespace amnezia AbortError = 1205, // Billing errors - BillingError = 1300, - BillingGooglePlayError = 1301, - BillingUnavailable = 1302, - SubscriptionAlreadyOwned = 1303, - SubscriptionUnavailable = 1304, - BillingNetworkError = 1305, + BillingCanceled = 1300, + BillingError = 1301, + BillingGooglePlayError = 1302, + BillingUnavailable = 1303, + SubscriptionAlreadyOwned = 1304, + SubscriptionUnavailable = 1305, + BillingNetworkError = 1306, }; Q_ENUM_NS(ErrorCode) } diff --git a/client/core/errorstrings.cpp b/client/core/errorstrings.cpp index 3252c50c..4de4f506 100644 --- a/client/core/errorstrings.cpp +++ b/client/core/errorstrings.cpp @@ -73,6 +73,7 @@ QString errorString(ErrorCode code) { case(ErrorCode::AbortError): errorMessage = QObject::tr("QFile error: The operation was aborted"); break; // Billing errors + case(ErrorCode::BillingCanceled): errorMessage = QObject::tr("Transaction was canceled by the user"); break; case(ErrorCode::BillingError): errorMessage = QObject::tr("Billing error"); break; case(ErrorCode::BillingGooglePlayError): errorMessage = QObject::tr("Internal Google Play error, please try again later"); break; case(ErrorCode::BillingUnavailable): errorMessage = QObject::tr("Billing is unavailable, please try again later"); break;