gpt4 book ai didi

android - 为什么状态为已退款的订单在 Google 应用内支付系统中显示为已购买?

转载 作者:行者123 更新时间:2023-12-04 13:34:05 26 4
gpt4 key购买 nike

我在我的 Android 应用程序中使用 Google 应用内计费版本 3,我正在基于以下代码测试应用程序的订单模块。
在这种情况下,用户的订单已被 Google Play 退款,但是当我运行 mBilling.initBillingClient() 时, 我发现 purchase.purchaseState == Purchase.PurchaseState.PURCHASEDprivate fun processPurchases启动。
退款状态为 PURCHASED 的订单似乎已成功,我该如何解决?
FragmentBuy.kt

class FragmentBuy : Fragment() {
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DataBindingUtil.inflate(
inflater, R.layout.layout_buy, container, false
)

val mBilling=Billing.getInstance(requireActivity(), getString(R.string.skuRegisterApp))
mBilling.initBillingClient()
...
}

}
计费.kt
class Billing private constructor (private val mContext: Context, private val purchaseItem :String)
:PurchasesUpdatedListener, BillingClientStateListener {

private lateinit var playStoreBillingClient: BillingClient
private val mapSkuDetails = mutableMapOf<String,SkuDetails>()

fun initBillingClient() {
playStoreBillingClient = BillingClient
.newBuilder(mContext)
.enablePendingPurchases()
.setListener(this)
.build()

if (!playStoreBillingClient.isReady) {
playStoreBillingClient.startConnection(this) //It will launach override fun onBillingSetupFinished()
}
}

override fun onBillingSetupFinished(billingResult: BillingResult) {
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
queryAndProcessPurchasesAsync()
}
...
}
}

private fun queryAndProcessPurchasesAsync() {
val purchasesResult = HashSet<Purchase>()
var result = playStoreBillingClient.queryPurchases(BillingClient.SkuType.INAPP)
result?.purchasesList?.apply { purchasesResult.addAll(this) }

processPurchases(purchasesResult)
}


private fun processPurchases(purchasesResult: Set<Purchase>): Job {
val validPurchases = HashSet<Purchase>(purchasesResult.size)
purchasesResult.forEach { purchase ->
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
if (purchase.sku.equals(purchaseItem)) {
if (isSignatureValid(purchase)) {
validPurchases.add(purchase)
setIsRegisteredAppAsTrue(mContext) //It will be launched even if the status of the order is refunded.
}
}
}
...

}
acknowledgeNonConsumablePurchasesAsync(validPurchases.toList())

}


private fun acknowledgeNonConsumablePurchasesAsync(nonConsumables: List<Purchase>) {
...
}


override fun onPurchasesUpdated(billingResult: BillingResult, purchases: MutableList<Purchase>?) {
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
purchases?.apply { processPurchases(this.toSet()) }
}
...
}
}

fun purchaseProduct(activity: Activity) {
val skuDetails = mapSkuDetails[purchaseItem]
skuDetails?.let{
val purchaseParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build()
playStoreBillingClient.launchBillingFlow(activity, purchaseParams)
}
}

companion object {
private const val LOG_TAG = "BillingRepository"

private var INSTANCE: Billing? = null
fun getInstance(mContext: Context, purchaseItem: String): Billing =
INSTANCE ?: synchronized(this) {
INSTANCE ?: Billing(mContext, purchaseItem).also { INSTANCE = it }
}
}


}

最佳答案

您还可以在购买时检查 purchase.isAcknowledged() ,这样您就不会将其添加到 validPurchases 中,如果它是缓存问题的话。

关于android - 为什么状态为已退款的订单在 Google 应用内支付系统中显示为已购买?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63257164/

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