这是我第一次在Android应用中实现应用内计费,我直接从指南中获取了大部分代码,一切都很完美,直到我考虑退款。示例应用程序已经实现了退款,但方式有些奇怪!退款在应用程序上被视为购买,但状态为退款,这是完全可以理解的,但原始代码如下:
// Count the number of times the product was purchased
while (cursor.moveToNext()) {
int stateIndex = cursor.getInt(2);
PurchaseState state = PurchaseState.valueOf(stateIndex);
// Note that a refunded purchase is treated as a purchase. Such
// a friendly refund policy is nice for the user.
if (state == PurchaseState.PURCHASED || state == PurchaseState.REFUNDED) {
quantity += 1;
}
}
// Update the "purchased items" table
updatePurchasedItem(productId, quantity);
即使商品已退款,它仍会添加商品,我不知道为什么?退款商品是否有特殊的ID或者我遗漏了什么?到目前为止,我只尝试过测试产品,所以我不清楚。
updatePurchasedItem方法如果数量为0,则从表中删除条目,这似乎是完全正确的,因此我将我的代码更改为:
while (cursor.moveToNext()) {
int stateIndex = cursor.getInt(2);
PurchaseState state = PurchaseState.valueOf(stateIndex);
// Note that a refunded purchase is treated as a purchase. Such
// a friendly refund policy is nice for the user.
if(Consts.DEBUG)
Log.v(TAG, state == PurchaseState.PURCHASED ? "purchase" : "refund");
if (state == PurchaseState.PURCHASED) {
quantity += 1;
} else if(state == PurchaseState.REFUNDED) {
quantity = 0;
}
}
// Update the "purchased items" table
updatePurchasedItem(productId, quantity);
但是我怀疑示例应用程序中是否存在错误代码,因此我完全不确定自己是否做得正确!我该如何处理?请帮助我!