Android in-app payment

This is the first time that I am implementing billing in an Android app, and I took most of the code directly from the guideline , and everything forgets perfectly, until I thought about a refund. The sample application is already coming back, but it's weird! The return is received in the application as a purchase, but with the return condition, which is quite understandable, but the original source is as follows:

// 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 goods, even if it was returned, and I had no idea why this is so? Does the returned product have a special identifier or what am I missing? I just tried this with testing products, so I had no idea.

The updatePurchasedItem method removes the record from the table if the number is 0, which seems completely correct, so I changed the 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 that the sample application will have the wrong code, so Iโ€™m absolutely sure that I am doing it right!

How should I handle this? Please help me!

+6
source share
1 answer

There is one record per purchase in the history table. This means that with a purchase that was later returned, after the return, one history record will be recorded for the product with the status of โ€œrefundโ€.

When counting purchases, the Return status assumes that the item has been purchased. The developer must then decide whether the user should have access to the returned products. (There is an example on this developer's site that wants to return purchases made by people who have already donated money and would like to allow them to continue using this product).

If you do not want the amounts to be refunded, you must change your code to add a quantity for the purchase, but do nothing to return it. (Do not set the quantity to zero).

Applications include 3 varieties:

  • Managed Item: Google does not permit the purchase of such products more than once. The total may contain up to zero or only one.

  • Unmanaged item: users can buy such products many times. The total may be zero or more.

  • Subscriptions: same as managed items.

+5
source

Source: https://habr.com/ru/post/920216/


All Articles