问题描述
我正在尝试使用Google计费库进行订阅,但是在启动购买流程时出现此错误。 此版本的应用程序未配置计费 但是当我从商店下载应用程序时,购买流程正常 我曾尝试将应用作为发行版运行,并安装了签名的APK,但我遇到了同样的问题。
private val application = App.appInstance
private lateinit var playStoreBillingClient:BillingClient
BillingManager类:PurchasesUpdatedListener,BillingClientStateListener {
suspend fun querySubscriptions(coroutineContext: CoroutineContext = dispatchers.IO): SkuDetailsResult = withContext(coroutineContext) {
val skuList = listof("1","2","3","4")
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS)
playStoreBillingClient.querySkuDetails(params.build())
}
fun startDataSourceConnections() {
instantiateAndConnectToPlayBillingService()
}
fun endDataSourceConnections() {
playStoreBillingClient.endConnection()
}
fun openSubscriptionFlow(activity: Activity,skuDetails: SkuDetails): Int {
val flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build()
return playStoreBillingClient.launchBillingFlow(activity,flowParams).responseCode
}
fun queryPurchasesAsync(): Purchase.PurchasesResult? {
return playStoreBillingClient.queryPurchases(BillingClient.SkuType.SUBS)
}
fun queryPurchasesHistoryAsync(): List<PurchaseHistoryRecord> {
val list = mutablelistof<PurchaseHistoryRecord>()
playStoreBillingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS) { billingResult,purchasesList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchasesList != null) {
purchasesList.forEach {
list.add(it)
}
}
}
return list
}
private fun processpurchases(purchasesResult: List<Purchase>) =
Coroutinescope(Job() + dispatchers.IO).launch {
val validPurchases = HashSet<Purchase>(purchasesResult.size)
purchasesResult.forEach { purchase ->
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
validPurchases.add(purchase)
} else if (purchase.purchaseState == Purchase.PurchaseState.PENDING) {
// handle pending purchases,e.g. confirm with users about the pending
// purchases,prompt them to complete it,etc.
}
}
ackNowledgeNonConsumablePurchasesAsync(validPurchases.toList())
}
private fun ackNowledgeNonConsumablePurchasesAsync(nonConsumables: List<Purchase>) {
nonConsumables.forEach { purchase ->
val params = AckNowledgePurchaseParams.newBuilder().setPurchasetoken(purchase
.purchasetoken).build()
playStoreBillingClient.ackNowledgePurchase(params) { billingResult ->
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
}
}
}
}
}
private fun instantiateAndConnectToPlayBillingService() {
playStoreBillingClient = BillingClient.newBuilder(application)
.setListener(this)
.enablePendingPurchases()
.build()
connectToPlayBillingService()
}
private fun connectToPlayBillingService(): Boolean {
if (!playStoreBillingClient.isReady) {
playStoreBillingClient.startConnection(this)
return true
}
return false
}
override fun onPurchasesUpdated(billingResult: BillingResult,purchases: MutableList<Purchase>?) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
processpurchases(purchases)
logD("purchases",BillingManager().queryPurchasesAsync()?.purchasesList.toString())
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
} else {
// Handle any other error codes.
}
}
override fun onBillingSetupFinished(billingResult: BillingResult) {
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
GlobalScope.launch(dispatchers.IO) { querySubscriptions() }
}
}
}
override fun onBillingServicedisconnected() {
connectToPlayBillingService()
}
}
然后我添加了结算权限
<uses-permission android:name="com.android.vending.BILLING" />
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)