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()) { ) : Exception(billingResult.toString()) {
constructor(msg: String) : this(BillingResult.newBuilder() constructor(msg: String) : this(BillingResult.newBuilder()
.setResponseCode(9999) .setResponseCode(DEVELOPER_ERROR)
.setDebugMessage(msg) .setDebugMessage(msg)
.build()) .build())

View file

@ -26,14 +26,12 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.amnezia.vpn.util.ErrorCode
import org.amnezia.vpn.util.Log import org.amnezia.vpn.util.Log
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
private const val TAG = "BillingProvider" 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" private const val PRODUCT_ID = "premium"
class BillingProvider(context: Context) : AutoCloseable { class BillingProvider(context: Context) : AutoCloseable {
@ -87,20 +85,18 @@ class BillingProvider(context: Context) : AutoCloseable {
} catch (e: BillingException) { } catch (e: BillingException) {
if (e.isCanceled) { if (e.isCanceled) {
Log.w(TAG, "Billing canceled") Log.w(TAG, "Billing canceled")
return JSONObject().put("result", RESULT_CANCELED) return JSONObject().put("responseCode", ErrorCode.BillingCanceled)
} else if (e.isRetryable && attemptCount < numberAttempts) { } else if (e.isRetryable && attemptCount < numberAttempts) {
Log.d(TAG, "Retryable error: $e") Log.d(TAG, "Retryable error: $e")
++attemptCount ++attemptCount
delay(1000) delay(1000)
} else { } else {
Log.e(TAG, "Billing error: $e") Log.e(TAG, "Billing error: $e")
return JSONObject() return JSONObject().put("responseCode", e.errorCode)
.put("result", RESULT_ERROR)
.put("errorCode", e.errorCode)
} }
} catch (_: CancellationException) { } catch (_: CancellationException) {
Log.w(TAG, "Billing coroutine canceled") 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") Log.v(TAG, "Get subscription plans")
val productDetailsList = getProductDetails() 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) } val productArray = JSONArray().also { resultJson.put("products", it) }
productDetailsList?.forEach { productDetails -> productDetailsList?.forEach { productDetails ->
val product = JSONObject().also { productArray.put(it) } val product = JSONObject().also { productArray.put(it) }
@ -178,7 +174,7 @@ class BillingProvider(context: Context) : AutoCloseable {
val countryCode = deferred.await() val countryCode = deferred.await()
return JSONObject() return JSONObject()
.put("result", RESULT_OK) .put("responseCode", ErrorCode.NoError)
.put("countryCode", countryCode) .put("countryCode", countryCode)
} }
@ -233,7 +229,7 @@ class BillingProvider(context: Context) : AutoCloseable {
subscriptionPurchases.firstOrNull { it != null }?.let { (billingResult, purchases) -> subscriptionPurchases.firstOrNull { it != null }?.let { (billingResult, purchases) ->
if (!billingResult.isOk) throw BillingException(billingResult) if (!billingResult.isOk) throw BillingException(billingResult)
return JSONObject() return JSONObject()
.put("result", RESULT_OK) .put("responseCode", ErrorCode.NoError)
.put("purchases", processPurchases(purchases)) .put("purchases", processPurchases(purchases))
} ?: throw BillingException("Purchase failed") } ?: throw BillingException("Purchase failed")
} }
@ -241,20 +237,20 @@ class BillingProvider(context: Context) : AutoCloseable {
private fun processPurchases(purchases: List<Purchase>?): JSONArray { private fun processPurchases(purchases: List<Purchase>?): JSONArray {
val purchaseArray = JSONArray() val purchaseArray = JSONArray()
purchases?.forEach { purchase -> purchases?.forEach { purchase ->
val purchaseJson = JSONObject().also { purchaseArray.put(it) } /* val purchaseJson = */ JSONObject().also { purchaseArray.put(it) }
.put("purchaseToken", purchase.purchaseToken) .put("purchaseToken", purchase.purchaseToken)
.put("purchaseTime", purchase.purchaseTime) .put("purchaseTime", purchase.purchaseTime)
.put("purchaseState", purchase.purchaseState) .put("purchaseState", purchase.purchaseState)
.put("isAcknowledged", purchase.isAcknowledged) .put("isAcknowledged", purchase.isAcknowledged)
.put("isAutoRenewing", purchase.isAutoRenewing) .put("isAutoRenewing", purchase.isAutoRenewing)
.put("orderId", purchase.orderId) .put("orderId", purchase.orderId)
.put("productIds", JSONArray(purchase.products)) // .put("productIds", JSONArray(purchase.products))
purchase.pendingPurchaseUpdate?.let { purchaseUpdate -> /* purchase.pendingPurchaseUpdate?.let { purchaseUpdate ->
JSONObject() JSONObject()
.put("purchaseToken", purchaseUpdate.purchaseToken) .put("purchaseToken", purchaseUpdate.purchaseToken)
.put("productIds", JSONArray(purchaseUpdate.products)) // .put("productIds", JSONArray(purchaseUpdate.products))
}.also { purchaseJson.put("pendingPurchaseUpdate", it) } }.also { purchaseJson.put("pendingPurchaseUpdate", it) } */
} }
return purchaseArray return purchaseArray
} }
@ -281,14 +277,14 @@ class BillingProvider(context: Context) : AutoCloseable {
throw BillingException(result) throw BillingException(result)
} }
return JSONObject().put("result", RESULT_OK) return JSONObject().put("responseCode", ErrorCode.NoError)
} }
suspend fun getPurchases(): JSONObject { suspend fun getPurchases(): JSONObject {
Log.v(TAG, "Get purchases") Log.v(TAG, "Get purchases")
val purchases = queryPurchases() val purchases = queryPurchases()
return JSONObject() return JSONObject()
.put("result", RESULT_OK) .put("responseCode", ErrorCode.NoError)
.put("purchases", processPurchases(purchases)) .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 // keep synchronized with client/core/defs.h error_code_ns::ErrorCode
object ErrorCode { object ErrorCode {
const val BillingError = 1300 const val NoError = 0
const val BillingGooglePlayError = 1301
const val BillingUnavailable = 1302 const val BillingCanceled = 1300
const val SubscriptionAlreadyOwned = 1303 const val BillingError = 1301
const val SubscriptionUnavailable = 1304 const val BillingGooglePlayError = 1302
const val BillingNetworkError = 1305 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, AbortError = 1205,
// Billing errors // Billing errors
BillingError = 1300, BillingCanceled = 1300,
BillingGooglePlayError = 1301, BillingError = 1301,
BillingUnavailable = 1302, BillingGooglePlayError = 1302,
SubscriptionAlreadyOwned = 1303, BillingUnavailable = 1303,
SubscriptionUnavailable = 1304, SubscriptionAlreadyOwned = 1304,
BillingNetworkError = 1305, SubscriptionUnavailable = 1305,
BillingNetworkError = 1306,
}; };
Q_ENUM_NS(ErrorCode) 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; case(ErrorCode::AbortError): errorMessage = QObject::tr("QFile error: The operation was aborted"); break;
// Billing errors // 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::BillingError): errorMessage = QObject::tr("Billing error"); break;
case(ErrorCode::BillingGooglePlayError): errorMessage = QObject::tr("Internal Google Play error, please try again later"); 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; case(ErrorCode::BillingUnavailable): errorMessage = QObject::tr("Billing is unavailable, please try again later"); break;