Google 计费库取消了所有订阅
Google billing library cancelled all subscriptions
我正在使用 Google 的计费库在我的应用程序中进行订阅。
~~ 3 天后google play 退还了我用户的每个订阅,为什么会这样?
一些带有 activity 的代码,用于订阅:
private var billingClient: BillingClient? = null
private val purchasesUpdateListener = PurchasesUpdatedListener { billingResult, purchases ->
val isSuccessResult = billingResult.responseCode == BillingClient.BillingResponseCode.OK
val hasPurchases = !purchases.isNullOrEmpty()
if (isSuccessResult && hasPurchases) {
purchases?.forEach(::confirmPurchase)
viewModel.hasSubscription.value = true
}
}
private fun confirmPurchase(purchase: Purchase) {
val consumeParams = ConsumeParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
billingClient?.consumeAsync(consumeParams) { billingResult, _ ->
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
//all done
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
billingClient = BillingClient.newBuilder(this)
.setListener(purchasesUpdateListener)
.enablePendingPurchases()
.build()
connectToBillingService()
}
private fun connectToBillingService() {
billingClient?.startConnection(this)
}
private fun getPurchases(): List<Purchase> {
val purchasesResult = billingClient?.queryPurchases(BillingClient.SkuType.SUBS)
return purchasesResult?.purchasesList.orEmpty()
}
override fun onBillingSetupFinished(result: BillingResult) {
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
updateSkuMap()
}
}
}
因为您使用的是 Play Billing Library 2.0 或更高版本。从 Play Billing Library 2.0 开始,所有购买都必须在三天内确认。未能正确确认购买将导致购买被退款。
正在检查 Play Billing Library v2.0 release notes and Processing Purchases 了解更多详情。
在您的代码中,您正在使用 consumeAsync()
确认购买,
这是一次性消耗品的正确方法,因为consumeAsync()
会自动为您确认。
但是对于订阅,您应该使用client.acknowledgePurchase()
方法而不是consumeAsync()
这是 Kotlin 的示例
val client: BillingClient = ...
val acknowledgePurchaseResponseListener: AcknowledgePurchaseResponseListener = ...
suspend fun handlePurchase() {
if (purchase.purchaseState === PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged) {
val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
val ackPurchaseResult = withContext(Dispatchers.IO) {
client.acknowledgePurchase(acknowledgePurchaseParams.build())
}
}
}
}
我正在使用 Google 的计费库在我的应用程序中进行订阅。
~~ 3 天后google play 退还了我用户的每个订阅,为什么会这样?
一些带有 activity 的代码,用于订阅:
private var billingClient: BillingClient? = null
private val purchasesUpdateListener = PurchasesUpdatedListener { billingResult, purchases ->
val isSuccessResult = billingResult.responseCode == BillingClient.BillingResponseCode.OK
val hasPurchases = !purchases.isNullOrEmpty()
if (isSuccessResult && hasPurchases) {
purchases?.forEach(::confirmPurchase)
viewModel.hasSubscription.value = true
}
}
private fun confirmPurchase(purchase: Purchase) {
val consumeParams = ConsumeParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
billingClient?.consumeAsync(consumeParams) { billingResult, _ ->
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
//all done
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
billingClient = BillingClient.newBuilder(this)
.setListener(purchasesUpdateListener)
.enablePendingPurchases()
.build()
connectToBillingService()
}
private fun connectToBillingService() {
billingClient?.startConnection(this)
}
private fun getPurchases(): List<Purchase> {
val purchasesResult = billingClient?.queryPurchases(BillingClient.SkuType.SUBS)
return purchasesResult?.purchasesList.orEmpty()
}
override fun onBillingSetupFinished(result: BillingResult) {
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
updateSkuMap()
}
}
}
因为您使用的是 Play Billing Library 2.0 或更高版本。从 Play Billing Library 2.0 开始,所有购买都必须在三天内确认。未能正确确认购买将导致购买被退款。 正在检查 Play Billing Library v2.0 release notes and Processing Purchases 了解更多详情。
在您的代码中,您正在使用 consumeAsync()
确认购买,
这是一次性消耗品的正确方法,因为consumeAsync()
会自动为您确认。
但是对于订阅,您应该使用client.acknowledgePurchase()
方法而不是consumeAsync()
这是 Kotlin 的示例
val client: BillingClient = ...
val acknowledgePurchaseResponseListener: AcknowledgePurchaseResponseListener = ...
suspend fun handlePurchase() {
if (purchase.purchaseState === PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged) {
val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
val ackPurchaseResult = withContext(Dispatchers.IO) {
client.acknowledgePurchase(acknowledgePurchaseParams.build())
}
}
}
}