refactor: modify response json

This commit is contained in:
albexk 2024-11-08 11:12:56 +03:00
parent d7581f29ba
commit 9ebefe79ca
5 changed files with 32 additions and 31 deletions

View file

@ -18,7 +18,7 @@ internal class BillingException(
) : Exception(billingResult.toString()) {
constructor(msg: String) : this(BillingResult.newBuilder()
.setResponseCode(9999)
.setResponseCode(DEVELOPER_ERROR)
.setDebugMessage(msg)
.build())

View file

@ -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<Purchase>?): 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))
}

View file

@ -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
}

View file

@ -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)
}

View file

@ -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;