|
@@ -2,6 +2,7 @@ package com.vpn.fastestvpnservice.viewmodels
|
|
|
|
|
|
import android.app.Activity
|
|
|
import android.content.Context
|
|
|
+import android.util.Base64
|
|
|
import android.util.Log
|
|
|
import androidx.compose.runtime.livedata.observeAsState
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
@@ -9,6 +10,7 @@ import androidx.core.app.ComponentActivity
|
|
|
import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
+import com.android.billingclient.api.AcknowledgePurchaseParams
|
|
|
import com.android.billingclient.api.BillingClient
|
|
|
import com.android.billingclient.api.BillingClientStateListener
|
|
|
import com.android.billingclient.api.BillingFlowParams
|
|
@@ -19,16 +21,21 @@ import com.android.billingclient.api.SkuDetails
|
|
|
import com.android.billingclient.api.SkuDetailsParams
|
|
|
import com.google.gson.Gson
|
|
|
import com.google.gson.reflect.TypeToken
|
|
|
+import com.vpn.fastestvpnservice.R
|
|
|
import com.vpn.fastestvpnservice.application.App
|
|
|
import com.vpn.fastestvpnservice.beans.DataResponse
|
|
|
+import com.vpn.fastestvpnservice.beans.DataResponseServers
|
|
|
import com.vpn.fastestvpnservice.beans.Features
|
|
|
import com.vpn.fastestvpnservice.beans.ProductFeatures
|
|
|
+import com.vpn.fastestvpnservice.beans.ServerData
|
|
|
import com.vpn.fastestvpnservice.beans.SubscriptionPackageList
|
|
|
import com.vpn.fastestvpnservice.customItems.getSelectedPosition
|
|
|
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
|
|
|
import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
|
|
|
import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
|
|
|
import com.vpn.fastestvpnservice.screens.accountScreensAll.SubscriptionScreen
|
|
|
+import org.json.JSONArray
|
|
|
+import org.json.JSONObject
|
|
|
import retrofit2.Call
|
|
|
|
|
|
class SubscriptionViewModel constructor(context: Context, activity: ComponentActivity): ViewModel(), PurchasesUpdatedListener {
|
|
@@ -36,6 +43,8 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
var mutableLiveDataProducts = MutableLiveData<DataResponse<ArrayList<ProductFeatures>>>()
|
|
|
var liveDataProducts: LiveData<DataResponse<ArrayList<ProductFeatures>>> = mutableLiveDataProducts
|
|
|
|
|
|
+ val mutableLiveDataSubscription = MutableLiveData<DataResponseServers<ArrayList<ServerData>>>()
|
|
|
+
|
|
|
var mutableLiveDataSkuDetails = MutableLiveData<MutableList<SkuDetails>>()
|
|
|
var liveDataSkuDetails: LiveData<MutableList<SkuDetails>> = mutableLiveDataSkuDetails
|
|
|
|
|
@@ -85,7 +94,7 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
fun setProductsData(products: DataResponse<ArrayList<ProductFeatures>>) {
|
|
|
mutableLiveDataProducts.value = products
|
|
|
}
|
|
|
- fun getProducts(){
|
|
|
+ fun getProducts(onProductResult: () -> Unit){
|
|
|
WebServiceFactory.getInstance().getProducts().enqueue(
|
|
|
RetrofitNetworkHandling<Any>(object :
|
|
|
RetrofitNetworkHandling.ResponseCallback<Any>{
|
|
@@ -97,12 +106,15 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
val type = object : TypeToken<DataResponse<ArrayList<ProductFeatures>>>() {}.type
|
|
|
val data = gson.fromJson<DataResponse<ArrayList<ProductFeatures>>>(jsonString, type)
|
|
|
|
|
|
- Log.d("test_api_response_p", "Products try = ${data.status} ${data.message}")
|
|
|
+ Log.d("test_api_response_p", "Products try = ${data.status} ${data.message} ${data.data?.size}")
|
|
|
|
|
|
data?.data?.let {
|
|
|
preferenceHelper.saveFeaturesData(it)
|
|
|
featuresList.value = it
|
|
|
}
|
|
|
+ if (data.data?.size == 0){
|
|
|
+ onProductResult()
|
|
|
+ }
|
|
|
mutableLiveDataProducts.value = data
|
|
|
mutableLiveDataProductsErrorStatus.value = false
|
|
|
|
|
@@ -129,6 +141,42 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
)
|
|
|
}
|
|
|
|
|
|
+ fun create_subscription(transaction_id: String, receipt_data: String, productId: String) {
|
|
|
+ Log.d("test_api_response", "create_subscription params = $transaction_id $receipt_data $productId")
|
|
|
+
|
|
|
+ WebServiceFactory.getInstance().createSubscription(transaction_id, receipt_data, productId).enqueue(
|
|
|
+ RetrofitNetworkHandling<Any>(object :
|
|
|
+ RetrofitNetworkHandling.ResponseCallback<Any> {
|
|
|
+ override fun onSuccess(call: Call<Any>?, response: Any?) {
|
|
|
+ try {
|
|
|
+ Log.d("test_api_response", "create_subscription try = $response")
|
|
|
+ val gson = Gson()
|
|
|
+ val jsonString = gson.toJson(response)
|
|
|
+ val type = object : TypeToken<DataResponseServers<ArrayList<ServerData>>>() {}.type
|
|
|
+ val data = gson.fromJson<DataResponseServers<ArrayList<ServerData>>>(jsonString, type)
|
|
|
+
|
|
|
+ Log.d("test_api_response", "create_subscription try = ${data.status} ${data.message}")
|
|
|
+
|
|
|
+ data?.let {
|
|
|
+ mutableLiveDataSubscription.value = it
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (ex: Exception) {
|
|
|
+ Log.d("test_api_response", "create_subscription catch = $response")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFail(call: Call<Any>?, response: Any?) {
|
|
|
+ Log.d("test_api_response", "create_subscription onFail = $response")
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onError(call: Call<Any>?, response: Any?) {
|
|
|
+ Log.d("test_api_response", "create_subscription onError = $response")
|
|
|
+ }
|
|
|
+ })
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
fun startConnection(selectedPosition: Int) {
|
|
|
SubscriptionViewModel.selectedPosition = selectedPosition
|
|
|
Log.d(tag, "startConnection Billing")
|
|
@@ -307,7 +355,83 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
}
|
|
|
|
|
|
|
|
|
- override fun onPurchasesUpdated(p0: BillingResult, p1: MutableList<Purchase>?) {
|
|
|
+ override fun onPurchasesUpdated(p0: BillingResult, purchases: MutableList<Purchase>?) {
|
|
|
+ if (purchases != null && purchases.size > 0) {
|
|
|
+ Log.d("purchases_product", purchases.toString())
|
|
|
+ parseDataAndMakeApiCall(purchases)
|
|
|
+ }
|
|
|
+ acknowledgePurchases(purchases)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ private fun parseDataAndMakeApiCall(purchases: MutableList<Purchase>?) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ val objectArray = JSONArray(Gson().toJson(purchases))
|
|
|
+ val obj: JSONObject = objectArray.getJSONObject(0)
|
|
|
+ val parsedJson = obj.getJSONObject("zzc")
|
|
|
+ val nameValuePairs = parsedJson.getJSONObject("nameValuePairs")
|
|
|
+ val orderId = nameValuePairs.getString("orderId")
|
|
|
+ val productId = nameValuePairs.getString("productId")
|
|
|
+ val packageName = nameValuePairs.getString("packageName")
|
|
|
+ val purchaseToken = nameValuePairs.getString("purchaseToken")
|
|
|
+
|
|
|
+ val billingCycle =
|
|
|
+ productId.substring(productId.lastIndexOf('.') + 1) //removing pacakge name
|
|
|
+ Log.d("purchases_product", billingCycle.toString())
|
|
|
+
|
|
|
+
|
|
|
+ val receiptData = JSONObject()
|
|
|
+ receiptData.put("orderId", orderId)
|
|
|
+ receiptData.put("productId", productId)
|
|
|
+ receiptData.put("packageName", packageName)
|
|
|
+ receiptData.put("purchaseToken", purchaseToken)
|
|
|
+
|
|
|
+ val receiptDataStr = receiptData.toString()
|
|
|
+ val receiptDataStrEncoded: String =
|
|
|
+ Base64.encodeToString(receiptDataStr.toByteArray(), Base64.NO_PADDING)
|
|
|
+
|
|
|
+ create_subscription_Product(orderId, receiptDataStrEncoded, productId)
|
|
|
+
|
|
|
+// createOrder(billingCycle, orderId, receiptData)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ e.printStackTrace()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun create_subscription_Product(orderId: String, receiptData: String,
|
|
|
+ productId: String) {
|
|
|
+ Log.d("purchases_product", "$orderId, $receiptData, $productId")
|
|
|
+
|
|
|
+ create_subscription(
|
|
|
+ orderId, receiptData, productId
|
|
|
+ )
|
|
|
+
|
|
|
+ }
|
|
|
+ private fun acknowledgePurchases(purchases: MutableList<Purchase>?) {
|
|
|
+ val purchase = purchases?.first()
|
|
|
+
|
|
|
+ if (purchase != null && !purchase.isAcknowledged
|
|
|
+ && purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
|
|
|
+ val acknowledgePurchaseParams =
|
|
|
+ AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.purchaseToken)
|
|
|
+ .build()
|
|
|
+ billingClient.acknowledgePurchase(acknowledgePurchaseParams) { billingResult ->
|
|
|
+ if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
|
|
|
+ Log.d(
|
|
|
+ "purchases_product",
|
|
|
+ "AcknowledgePurchases success, responseCode: ${billingResult.responseCode}"
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ Log.d(
|
|
|
+ "purchases_product",
|
|
|
+ "Can't allowMultiplePurchases, responseCode: ${billingResult.responseCode}"
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|