равен null
object SubscriptionProvider : PurchasesUpdatedListener, BillingClientStateListener {
lateinit private var playStoreBillingClient: BillingClient
private lateinit var preferences: SharedPreferences
private const val SUBSCRIPTION_ID = "*********"
private const val HAS_SUBSCRIPTION = "has_subscription"
private const val IS_APPROVED = "is_approved"
var flag: Boolean = false
fun init(context: Context) {
preferences = context.getSharedPreferences("subscription", Context.MODE_PRIVATE)
playStoreBillingClient = BillingClient.newBuilder(context.applicationContext)
.enablePendingPurchases()
.setListener(this).build()
connectToPlayBillingService()
}
private fun connectToPlayBillingService(): Boolean {
if (!playStoreBillingClient.isReady) {
playStoreBillingClient.startConnection(this)
return true
}
return false
}
override fun onPurchasesUpdated(billingResult: BillingResult?, purchases: MutableList<Purchase>?) {
updatePurchases(billingResult)
}
private fun updatePurchases(billingResult: BillingResult?){
if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK) {
var hasSubscription = false
val result = playStoreBillingClient.queryPurchases(BillingClient.SkuType.SUBS)
if (result != null && result.purchasesList != null) {
result.purchasesList.forEach {
if (it.sku == SUBSCRIPTION_ID) {
hasSubscription = true
val params = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(it.purchaseToken).build()
playStoreBillingClient.acknowledgePurchase(params) { billingResult ->
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
preferences.edit().putBoolean(IS_APPROVED, true).apply()
flag = true
callback?.subscribe(true)
}
else -> {
preferences.edit().putBoolean(HAS_SUBSCRIPTION, false).apply()
callback?.subscribe(false)
}
}
}
}
}
}
preferences.edit().putBoolean(HAS_SUBSCRIPTION, hasSubscription).apply()
}
}
override fun onBillingServiceDisconnected() {
}
override fun onBillingSetupFinished(billingResult: BillingResult) {
updatePurchases(billingResult)
}
fun hasSubscription() = preferences.getBoolean(HAS_SUBSCRIPTION, false)
fun startSubscription(activity: Activity) {
val params = SkuDetailsParams.newBuilder().setSkusList(arrayListOf(SUBSCRIPTION_ID))
.setType(BillingClient.SkuType.SUBS).build()
playStoreBillingClient.querySkuDetailsAsync(params) { billingResult, skuDetailsList ->
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
if (skuDetailsList.orEmpty().isNotEmpty()) {
skuDetailsList.forEach {
val perchaseParams = BillingFlowParams.newBuilder().setSkuDetails(it).build()
playStoreBillingClient.launchBillingFlow(activity, perchaseParams)
}
}
}
else -> {
}
}
}
}
}
Код смотреть не буду. Почему что не так - см в BillingResult в onPurchase... . Там английским по белому. Обычно или сервер не запущен или приложение не выложено правильно или в нем версия старше/молже дебажной
Обсуждают сегодня