使用开发者负载验证应用购买中的 android

Verifying android in app purchase using developer payload

计费库:'com.android.billingclient:billing:2.0.3'

成功后开始流程 startConnection ,

val skuList = ArrayList<String>()
skuList.add("buy4")
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP)

     //billingClient is declared and initialized earlier 
     billingClient.querySkuDetailsAsync(params.build()) 
          { billingResult, skuDetailsList ->
                val flowParams = BillingFlowParams.newBuilder()
                    .setSkuDetails(skuDetailsList.first())
                    .build()

                val responseCode = billingClient.launchBillingFlow(this, flowParams)
                println(responseCode.responseCode) //prints 0 ... OK
            }

MainActivity 实现 PurchasesUpdatedListener

override fun onPurchasesUpdated(billingResult: BillingResult?, purchases: MutableList<Purchase>?) {

    if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
      for (purchase in purchases) {
          acknowledgePurchase(purchase)
        }
  }
}
private fun acknowledgePurchase(purchase: Purchase) {
   if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
        // Grant entitlement to the user.
        // Acknowledge the purchase
        val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
                .setPurchaseToken(purchase.purchaseToken)
                .setDeveloperPayload("PayloadString")
                .build()


        billingClient.acknowledgePurchase(
          acknowledgePurchaseParams,
          object : AcknowledgePurchaseResponseListener {
            override fun onAcknowledgePurchaseResponse(billingResult: BillingResult?) {
               println("payload =${purchase.developerPayload}")  // prints "payload="
                    }
                })
    }
}

开发者payload是空的,尽管在AcknowledgePurchaseParams设置了,我在确认后也保存了purchase,过一会儿再试调用purchase.developerPayload,还是这样空白,使用 developer payload 验证应用内购买的最佳做法是什么?

N.B 我在 Play 控制台上使用内部测试轨道

在 onAcknowledgePurchaseResponse 中,您需要从缓存中刷新购买对象。缓存在调用 onAcknowledgePurchaseResponse 时更新,因此您可以通过调用 https://developer.android.com/reference/com/android/billingclient/api/BillingClient.html#querypurchases 来完成此操作。我们会考虑将更新购买添加到侦听器中,以供将来的库版本使用,以使其更加方便。