Android in-app billing refund

2020-07-09 10:16发布

问题:

This is the first time I am implementing in-app billing in android app and I took most of the code straightly from guideline and everything forget perfectly til I thought about refunding. The example app has already refunding implemented, but in a weird way! Refund is received on app as purchase but with state of refund which is totally understandable but the original source looks like this:

        // 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);

its adding item even if it was refunded and I got no idea why is this? Does refunded item has special id or what am i missing? I've only tried this yet with the testing products so I got no idea.

updatePurchasedItem method removes entry from table if quantity is 0 which seems totally right, so I changed my code to this

        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);

but I doubt there would be wrong code in the example app so I am totally unsure if I am doing it right!

How should i handle this? Please help me!

回答1:

History table has a single entry per purchase. This means that a purchase which was later refunded will have, after the refund, a single history record for the product with a status of "refunded".

When counting purchases, a "Refunded" state suggests the item has been purchased. It is then up to the developer to decide whether the user should have access to refunded products. (There is an example on this site of a developer wanting to refund purchases made by people who have already donated money, and would like to allow them to keep using the product).

If you don't want refunds to count, You should change your code to add quantity for a purchase, but do nothing for a refund. (Do not set quantity to zero).

In app products come in 3 varieties:

  1. Managed item: Google does not allow to purchase such products more than once. Total quantity can add up to zero or one only.

  2. Unmanaged item: Users can buy such products many times. Total quantity can be zero or more

  3. Subscriptions: The same as managed items.