gpt4 book ai didi

android - 使用开发人员有效负载在应用程序购买中验证 android

转载 作者:行者123 更新时间:2023-11-29 00:53:08 24 4
gpt4 key购买 nike

计费库:'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="
}
})
}
}

尽管在 AcknowledgePurchaseParams 中设置了开发人员有效负载,但我还在确认后保存了 purchase,并尝试调用 purchase.developerPayload 一段时间后,它仍然是空白的,使用 developer payload 验证应用内购买的最佳实践是什么?

注意,我在 Play 控制台上使用内部测试轨道

最佳答案

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

关于android - 使用开发人员有效负载在应用程序购买中验证 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57603049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com