【问题标题】:Google billing library cancelled all subscriptions谷歌计费库取消了所有订阅
【发布时间】:2021-06-11 14:25:17
【问题描述】:

我正在使用 Google 的结算库在我的应用中进行订阅。

~~ 3 天后,google play 退还了我用户的所有订阅费用,这是为什么呢?

一些带有活动的代码,用于订阅:

    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()
        }
    }
}

【问题讨论】:

    标签: android google-play in-app-subscription play-billing-library


    【解决方案1】:

    因为您使用的是 Play 结算库 2.0 或更高版本。从 Play Billing Library 2.0 开始,所有购买必须在三天内得到确认。未能正确确认购买将导致购买被退款。 查看Play Billing Library v2.0 release notesProcessing Purchases 了解更多详情。

    【讨论】:

      【解决方案2】:

      在您的代码中,您使用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())
                  }
              }
           }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-11
        • 2019-07-02
        • 1970-01-01
        • 2016-01-24
        • 1970-01-01
        • 2018-10-26
        • 1970-01-01
        • 2020-04-05
        • 2018-01-30
        相关资源
        最近更新 更多