Know when a suscription was cancelled - Play Billing 1.0 - android

I'm using this tuto to integrate Play Billing Library to my app: http://www.androidrey.com/implement-play-billing-library-in-android-application/ and all works good ... well, not at all. I have problems to know when a suscription was cancelled, I tested all the methods to find a resultCode or something to know this state, but have a method that I could not implement. Could be this the problem?
class: BillingManager.java
public void queryPurchases() {
Runnable queryToExecute = new Runnable() {
#Override
public void run() {
long time = System.currentTimeMillis();
Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP);
if (areSubscriptionsSupported()) {
Purchase.PurchasesResult subscriptionResult
= billingClient.queryPurchases(BillingClient.SkuType.SUBS);
System.out.println("QUERY 0");
if (subscriptionResult.getResponseCode() == BillingClient.BillingResponse.OK) {
purchasesResult.getPurchasesList().addAll(
subscriptionResult.getPurchasesList());
System.out.println("QUERY 1");
} else {
// Handle any error response codes.
}
} else if (purchasesResult.getResponseCode() == BillingClient.BillingResponse.OK) {
// Skip subscription purchases query as they are not supported.
System.out.println("QUERY 2");
} else {
// Handle any other error response codes.
System.out.println("QUERY 3");
}
onQueryPurchasesFinished(purchasesResult);
System.out.println("QUERY RESULT "+ purchasesResult);
}
};
executeServiceRequest(queryToExecute);
}
private void onQueryPurchasesFinished(Purchase.PurchasesResult result) {
// Have we been disposed of in the meantime? If so, or bad result code, then quit
if (billingClient == null || result.getResponseCode() != BillingClient.BillingResponse.OK) {
Log.w(TAG, "Billing client was null or result code (" + result.getResponseCode()
+ ") was bad – quitting");
return;
}
Log.d(TAG, "Query inventory was successful.");
// Update the UI and purchases inventory with new list of purchases
// mPurchases.clear();
onPurchasesUpdated(BillingClient.BillingResponse.OK, result.getPurchasesList());
}
public boolean areSubscriptionsSupported() {
int responseCode = billingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS);
if (responseCode != BillingClient.BillingResponse.OK) {
Log.w(TAG, "areSubscriptionsSupported() got an error response: " + responseCode);
}
return responseCode == BillingClient.BillingResponse.OK;
}
It is supposed to be called here: MyBillingUpdateListener.java
public class MyBillingUpdateListener implements BillingManager.BillingUpdatesListener {
//final BillingManager billingManager = new BillingManager(,this );
#Override
public void onBillingClientSetupFinished() {
//billingManager.queryPurchases(); THIS IS WHAT I COULD NOT IMPLEMENT
}
Any help is welcome, thanks!.

Play Billing 1.0 does not have the concept of purchase states (anymore), so there currently is no way to get this information using the Play Billing library.
My understanding is that queryPurchases is supposed to return actual valid purchases only. However, it gets the information from a long living cache and you have no way of updating it manually.
onBillingClientSetupFinished is completely unrelated.
Here is an active discussion on the subject: https://github.com/googlesamples/android-play-billing/issues/122

Related

Unity Android IAP "Purchase failed because Purchasing was not initialized correctly"

What I have
IAP button is working perfectly in unity editor. After purchasing, i'm getting debug message "ProcessPurchase: PASS. Product: 'full_version'" . But the button does nothing when I try to make a purchase on the device.
Don't have any errors when purchasing product in editor
What have i done so far
Automatically initialize UnityPurchasing checkbox is switched ON screenshot from unity IAP catalog
I've checked that the product ID, and price are the same in unity and Play console
My IAP product is showing active in play console IAP in play console
setup the Codeless IAP like this
Unity IAP button component
LogCat error when I press the button on the device
2022-03-14 20:08:18.244 9293-9334/? E/Unity: Purchase failed because Purchasing was not initialized correctly
UnityEngine.Purchasing.CodelessIAPStoreListener:InitiatePurchase(String)
UnityEngine.Events.UnityAction:Invoke()
UnityEngine.Events.UnityEvent:Invoke()
UnityEngine.EventSystems.EventFunction`1:Invoke(T1, BaseEventData)
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
UnityEngine.EventSystems.StandaloneInputModule:Process()
IAP script
using System;
using UnityEngine;
using UnityEngine.Purchasing; //библиотека с покупками, будет доступна когда активируем сервисы
public class IAP : MonoBehaviour, IStoreListener //для получения сообщений из Unity Purchasing
{
private static IStoreController m_StoreController; //доступ к системе Unity Purchasing
private static IExtensionProvider m_StoreExtensionProvider; // подсистемы закупок для конкретных магазинов
public static string fullVersion = "full_version";
void Start()
{
if (m_StoreController == null) //если еще не инициализаровали систему Unity Purchasing, тогда инициализируем
{
InitializePurchasing();
}
}
public void InitializePurchasing()
{
if (IsInitialized()) //если уже подключены к системе - выходим из функции
{
return;
}
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(fullVersion, ProductType.NonConsumable);
UnityPurchasing.Initialize(this, builder);
}
public void Buy_full_version()
{
BuyProductID(fullVersion);
}
void BuyProductID(string productId)
{
if (IsInitialized()) //если покупка инициализирована
{
Product product = m_StoreController.products.WithID(productId); //находим продукт покупки
if (product != null && product.availableToPurchase) //если продукт найдет и готов для продажи
{
Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
m_StoreController.InitiatePurchase(product); //покупаем
}
else
{
Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
}
}
else
{
Debug.Log("BuyProductID FAIL. Not initialized.");
}
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) //контроль покупок
{
if (String.Equals(args.purchasedProduct.definition.id, fullVersion, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
//действия при покупке
if (PlayerPrefs.HasKey("full_version") == false)
{
PlayerPrefs.SetInt("full_version", 1);
}
}
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
return PurchaseProcessingResult.Complete;
}
public void RestorePurchases() //Восстановление покупок (только для Apple). У гугл это автоматический процесс.
{
if (!IsInitialized())
{
Debug.Log("RestorePurchases FAIL. Not initialized.");
return;
}
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer) //если запущенно на эпл устройстве
{
Debug.Log("RestorePurchases started ...");
var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
apple.RestoreTransactions((result) =>
{
Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
});
}
else
{
Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
}
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
Debug.Log("OnInitialized: PASS");
m_StoreController = controller;
m_StoreExtensionProvider = extensions;
}
private bool IsInitialized()
{
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
}
}
The problem is that I don't understand what this error means, and subsequently how to solve it. Hoping for your help
You are mixing Codeless and Scripted IAP unfortunately. You should avoid Codeless IAP altogether, it has not been updated in some time and has issues, as you are seeing. Instead, use Scripted IAP (only). Read: this forum

Error You Already Own This Item

I have a new android app in which I am adding in-app billing and I am tearing my hair out with frustration.
I have uploaded a signed APK and published to alpha. I created a set of in-app products and activated them all. I have created a new gmail account and defined them as a tester for the app on the app apk page.
I have factory reset my android phone and initialised it with the new gmail account. I have entered the /apps/testing link in to chrome and signed up as a tester. I then downloaded and installed my app. Inside my app I asked for the in app products that were available and was shown the set i created above. I selected one to buy and went through the following purchase process.
1. Screen shows product to be purchased and price and requests press continue which i do
2. Screen shows payment methods and I select redeem code
3. Screen shows redeem your code and I enter one of the promotion codes I set up in the developer console earlier (not mentioned above - sorry) and press redeem
4. Screen shows product again, this time with price crossed out and offers option to add item which I select (very strange being asked to add again buy hey ho)
5. Screen shows item added
6. After a fews seconds screen shows Error you already own this item.
How can this be, this user did not exist before ten minutes ago and has only used this app once as described above.
I have seen many questions in stack overflow and elsewhere similar to this and tried everything, clearing google play store cache, clearing google play store data etc. This sequence described above is my latest attempt with a completely clean user on a completely clean phone.
I could upload my app code used but that misses the point, which is how can this gmail account already own an item when this gmail account have never purchased anything before from anyone. Surely this is a bug.
All clues very welcome as to how to proceed. Code now added, note this is a hybrid android app, with the user purchase decisions code in javascript/html and the in app actions in the wrapper code below
private void processCommand(JSONObject commandJSON) throws JSONException
{
String command = commandJSON.getString("method");
if ("GetInAppProducts".equals(command))
{
Log.d(TAG, "Querying Inventory");
InAppPurchaseSkuString = null ; // clear the purchased sku. Note this is tested in mConsumeFinishedListener
mHelper.queryInventoryAsync(true, itemSkus, new IabHelper.QueryInventoryFinishedListener()
{
#Override
public void onQueryInventoryFinished(IabResult iabResult, Inventory inventory)
{
InventoryRecord = inventory ;
if (iabResult.isFailure())
{
Log.d(TAG, "Query inventory failed");
SendEndItemsToApp ();
}
else
{
Log.d(TAG, "Query inventory was successful.");
InventoryCheckCount = 0 ; // seems that we cannot just fire off a whole lot of these checks at the same time, so do them in sequence
if (itemSkus.size()>0) { CheckForOwnedItems (); } else { SendEndItemsToApp (); }
}
}
});
}
else if ("BuyInAppProduct".equals(command))
{
JSONArray params = commandJSON.getJSONArray("parameters");
InAppPurchaseSkuString = params.getString(0);
Log.d(TAG, "User decision to purchase " + InAppPurchaseSkuString);
mHelper.launchPurchaseFlow( MainActivity.this, InAppPurchaseSkuString, InAppPurchaseActivityCode, mPurchaseFinishedListener, "mypurchasetoken"); // consider putting the user email address in the last field - need to get from app
};
}//end of ProcessCommand
public void CheckForOwnedItems ()
{
Log.d(TAG, "Pre Purchase Inventory Processing Started");
String sku = itemSkus.get(InventoryCheckCount);
if (InventoryRecord.getSkuDetails(sku) != null)
{
if (InventoryRecord.hasPurchase(sku))
{
consumeItem ();
}
else
{
SendItemToApp ();
InventoryCheckCount++;
if (InventoryCheckCount < itemSkus.size()) { CheckForOwnedItems (); } else { SendEndItemsToApp (); }
};
};
}//end of CheckForOwnedItems
public void SendItemToApp ()
{
String sku = itemSkus.get(InventoryCheckCount);
String priceString = InventoryRecord.getSkuDetails(sku).getPrice().replaceAll("[^\\d.]+", ""); // RegExp removes all characters except digits and periods
String infoString = "InAppProductDetails('" + sku + "','" + "dummy" + "','" + priceString + "');"; // dummy is a placeholder for product description which is not (yet?) used in the app
Log.d(TAG, infoString);
mWebView.evaluateJavascript (infoString, new ValueCallback<String>()
{
#Override
public void onReceiveValue(String s)
{
//Log.d(TAG,"Returned from InAppProductDetails:");
}
}
);
}
public void SendEndItemsToApp ()
{
String endString = "InAppProductsEnd();"; // name is a placeholder for now
Log.d(TAG, endString);
mWebView.evaluateJavascript(endString, new ValueCallback<String>()
{
#Override
public void onReceiveValue(String s)
{
//Log.d(TAG,"Returned from InAppProductsEnd:");
}
}
);
}
public void consumeItem()
{
Log.d(TAG,"Pre Purchase Inventory Query Started");
String sku = itemSkus.get(InventoryCheckCount);
mHelper.consumeAsync(InventoryRecord.getPurchase(sku), mConsumeFinishedListener);
}
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener()
{
public void onConsumeFinished (Purchase purchase, IabResult result)
{
if (result.isSuccess())
{
Log.d(TAG, "Pre Purchase Consume Item Completed");
SendItemToApp ();
InventoryCheckCount++;
if (InventoryCheckCount < itemSkus.size()) { CheckForOwnedItems (); } else { SendEndItemsToApp (); }
}
else
{
Log.d(TAG,"Pre Purchase Consume Item Failed");
}
}
};
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener()
{
public void onIabPurchaseFinished (IabResult result, Purchase purchase)
{
if (result.isFailure())
{
Log.d(TAG,"Purchase Scenario Failed");
}
else if (purchase.getSku().equals(InAppPurchaseSkuString))
{
Log.d(TAG,"Purchase Scenario Completed");
String evalString = "InAppProductPurchased('" + InAppPurchaseSkuString + "');";
Log.d(TAG, evalString);
mWebView.evaluateJavascript (evalString, new ValueCallback<String>()
{
#Override
public void onReceiveValue(String s)
{
Log.d(TAG, "Returned from InAppProductPurchased:");
}
}
);
}
}
};
I have found that this error does not occur when using paypal (i.e. real money) to make the purchase, so I believe that this "Error you already own this item" message is in some way connected to using a promotion code for the test. And (so far) my paypal account has not been charged (as I am a resgistered tester for the app).

How to debug in-app billing on Android 4.1 and 4.2?

New to Android dev and I have this very annoying, and familiar problem.
Short version: how can I test my app on multiple physical devices w/o buying them? Specifically, I'm trying to test on 4.1 and 4.2 running devices. I can't use an emulator because this involves in-app billing.
Long version:
Today, I got a crash report from a user (android version = 4.2, on device = A1-811 (mango)).
The issue is: IAB helper is not set up. Can't perform operation: queryInventory . I tested the app on 4.3, 5.0, 5.1 and it's fine.
I KNOW that the actual crash happened because I wasn't quitting when I was checking for (!result.isSuccess()) in mhelper.startSetup().
My question is, how do I solve the underlying issue of IabHelper not being set up?! I don't have access to Android 4.2 device...
MY CODE:
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener () {
public void onIabPurchaseFinished (IabResult result, Purchase purchase) {
if (mHelper == null)
return;
if (result.isFailure ()) {
Log.d (PURCHASE_TAG, "Google's response is a failure.");
} else {
Log.d (PURCHASE_TAG, "Response is successful. purchase.getSKU = " + purchase.getSku ());
premium = true;
}
}
};
IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener () {
public void onQueryInventoryFinished (IabResult result, Inventory inventory) {
Log.d (PURCHASE_TAG, "Processing Google's response.");
// Check if user has existing purchase.
if (result.isFailure ()) {
Log.d (PURCHASE_TAG, "Google's response is a failure. Response = ");
} else {
Log.d (PURCHASE_TAG, "Google's response is success! getPurchase() = " + inventory.getPurchase (THE_SKU));
if (inventory.getPurchase (THE_SKU) == null) {
premium = false;
} else {
premium = inventory.getPurchase (THE_SKU).getSku ().equals (THE_SKU);
}
}
// Show price if user is not premium, thank you note if the user is premium
if (premium == true) {
Log.d (PURCHASE_TAG, "3 premium = " + premium);
priceView.setText ("Thank you for the purchase!");
} else {
if (inventory != null) {
String pro_price = inventory.getSkuDetails (THE_SKU).getPrice ();
priceView.setText ("" + pro_price);
}
}
}
};
private void startPurchaseQuery () {
String base64EncodedPublicKey = "the key is generated ...";
mHelper = new IabHelper (this, base64EncodedPublicKey);
Log.d (PURCHASE_TAG, "Purchase query started.");
mHelper.startSetup (new IabHelper.OnIabSetupFinishedListener () {
public void onIabSetupFinished (IabResult result) {
Log.d (PURCHASE_TAG, "IabHelper Setup successful. Querying inventory.");
if (!result.isSuccess ()) {
Log.d (PURCHASE_TAG, "Error with IabHelper setup.");
return;
}
if (mHelper == null) return;
// IAB is fully set up. Now, let's get an inventory of stuff we own.
// Build the SKU list
List<String> additionalSkuList;
additionalSkuList = new ArrayList<String> ();
additionalSkuList.add (THE_SKU);
// Make the query
mHelper.queryInventoryAsync (true, additionalSkuList, mQueryFinishedListener);
Log.d (PURCHASE_TAG, "Query finished. Premium status = " + premium);
}
});
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Log.d (PURCHASE_TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult (requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult (requestCode, resultCode, data);
} else {
Log.d (PURCHASE_TAG, "onActivityResult handled by IABUtil.");
}
}
#Override
public void onDestroy () {
super.onDestroy ();
if (mHelper != null) mHelper.dispose ();
mHelper = null;
}
Answer from comments above
It's better to catch the error and patch own code first. Trying to reproduce exact issue why was IAB not set up may not be possible, because of all kinds of modifications user can do on his device (could be bug with specific version of distro user is running, which is almost impossible to find out, or similar). As you suggested, using Analytics to find out how many users have this issue is good approach.
To answer original question, you can create Alpha or Beta versions in Developer Console and invite users with specific versions of Android you'd like to target into testing group. This was you could test issues and crashes directly from Dev. Console even when not directly being owner of the device.

InAppBilling v3 IabResult response code BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED

Following the docs on developer site , i have implemented InAppBilling v3 in my app recently . I have used the classes in the utils package provided in the TRIVIAL DRIVE sample.
The problem i am facing is if a user has purchased an in app product already on launching purchse flow again on another device the play store dialog shows ITEM ALREADY OWNED but the response code returned by IabResult does not match to the constant IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED . The response code returned is actually one of the error codes in IabHelper class (-1005 User cancelled).
I would really like to know how can i get the actual response code instead of error code. Any help would be appreciated.
Below is the code for callback
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener =
new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
if (result.getResponse() ==
IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {
//already owned
boolean isPremium = true;
SharedPrefsUtils.setPremium(BaseActivity.this, isPremium);
EventBus.getDefault().post(new InAppBillingUiUpdateEvent(isPremium));
//setWaitScreen(false);
return;
}
//handle error
complain(result.getResponse() + " " + "Error purchasing: " + result);
//setWaitScreen(false);
return;
}
if (!verifyDeveloperPayload(purchase)) {
//corrupted
complain("Error purchasing. Authenticity verification failed.");
//setWaitScreen(false);
return;
}
//successful
if (purchase.getSku().equals(NO_ADS_PRODUCT_ID)) {
// bought the premium upgrade!
alert("Thank you for upgrading to premium!");
boolean isPremium = true;
SharedPrefsUtils.setPremium(BaseActivity.this, isPremium);
EventBus.getDefault().post(new InAppBillingUiUpdateEvent(isPremium));
//setWaitScreen(false);
}
}
};
I finally managed to find the problem in the IabHelper code , So here it goes whenever an Activity.RESULT_CANCELED result code is returned in handleActivityResult method the IabResult for all such cases is fixed with user cancelled (-1005) no matter what the reason is. So in order to get the correct actual response code replace the following code in handleActivityResult
else if (resultCode == Activity.RESULT_CANCELED) {
logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
}
with this
else if (resultCode == Activity.RESULT_CANCELED) {
logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
result = new IabResult(responseCode, null);
if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
}
hope it will saves someones time

Android In-app Purchase V3 Error: Authentication is required

I am implementing Google In-app Purchase V3 and followed all steps stated over here as well in official documentation here. I have uploaded my app in Google Playstore for Alpha Testing and I have downloaded that from playstore URL into my real device but it giving me error
Error
Authentication is required. You need to sign into your Google Account.
My code for In-app purchase is here:
public class BuyPointsFragment extends Fragment
//In app Billing variable start
// Debug tag, for logging
static final String TAG = "com.myApp";
// Does the user have the premium upgrade?
boolean mIsPremium = false;
// Does the user have an active subscription to the infinite gas plan?
boolean mSubscribedToInfiniteGas = false;
// SKUs for our products: the premium upgrade (non-consumable) and gas
// (consumable)
static final String SKU_PREMIUM = "premium";
static final String SKU_GAS = "gas";
// SKU for our subscription (infinite gas)
static final String SKU_INFINITE_GAS = "infinite_gas";
// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 10001;
// Graphics for the gas gauge
static int[] TANK_RES_IDS = {};
// How many units (1/4 tank is our unit) fill in the tank.
static final int TANK_MAX = 4;
// Current amount of gas in tank, in units
int mTank;
// The helper object
IabHelper mHelper;
//In app billing variable end
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inapp load game data
loadData();
String base64EncodedPublicKey = "Base64Key from publisher account";
// Some sanity checks to see if the developer (that's you!) really
// followed the
// instructions to run this sample (don't put these checks on your app!)
if (base64EncodedPublicKey.contains("CONSTRUCT_YOUR")) {
throw new RuntimeException(
"Please put your app's public key in MainActivity.java. See README.");
}
if (getActivity().getPackageName().startsWith("com.myApp.activity")) {
throw new RuntimeException(
"Please change the sample's package name! See README.");
}
// Create the helper, passing it our context and the public key to
// verify signatures with
Log.d(TAG, "Creating IAB helper.");
mHelper = new IabHelper(getActivity(), base64EncodedPublicKey);
// enable debug logging (for a production application, you should set
// this to false).
mHelper.enableDebugLogging(true);
// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
complain(getString(R.string.problem_setting_inapp_billing) + result);
return;
}
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null)
return;
// IAB is fully set up. Now, let's get an inventory of stuff we
// own.
Log.d(TAG, "Setup successful. Querying inventory.");
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
//In app billing code end here
}
//In app billing methods start here
public void inappCall(){
setWaitScreen(true);
Log.d(TAG, "Launching purchase flow for gas.");
/*
* TODO: for security, generate your payload here for verification. See
* the comments on verifyDeveloperPayload() for more info. Since this is
* a SAMPLE, we just use an empty string, but on a production app you
* should carefully generate this.
*/
String payload = "";
mHelper.launchPurchaseFlow(getActivity(), SKU_GAS, RC_REQUEST,
mPurchaseFinishedListener, payload);
}
// updates UI to reflect model
public void updateUi() {
// update the car color to reflect premium status or lack thereof
// ((ImageView)findViewById(R.id.free_or_premium)).setImageResource(mIsPremium
// ? R.drawable.premium : R.drawable.free);
// "Upgrade" button is only visible if the user is not premium
// findViewById(R.id.upgrade_button).setVisibility(mIsPremium ?
// View.GONE : View.VISIBLE);
// "Get infinite gas" button is only visible if the user is not
// subscribed yet
// (R.id.infinite_gas_button).setVisibility(mSubscribedToInfiniteGas ?
// View.GONE : View.VISIBLE);
// update gas gauge to reflect tank status
if (mSubscribedToInfiniteGas) {
// ((ImageView)findViewById(R.id.gas_gauge)).setImageResource(R.drawable.gas_inf);
} else {
int index = mTank >= TANK_RES_IDS.length ? TANK_RES_IDS.length - 1
: mTank;
// ((ImageView)findViewById(R.id.gas_gauge)).setImageResource(TANK_RES_IDS[index]);
}
}
// Enables or disables the "please wait" screen.
void setWaitScreen(boolean set) {
// findViewById(R.id.screen_main).setVisibility(set ? View.GONE :
// View.VISIBLE);
// findViewById(R.id.screen_wait).setVisibility(set ? View.VISIBLE :
// View.GONE);
}
void complain(String message) {
Log.e(TAG, "**** TrivialDrive Error: " + message);
alert("Error: " + message);
}
void alert(String message) {
AlertDialog.Builder bld = new AlertDialog.Builder(getActivity());
bld.setMessage(message);
bld.setNeutralButton("OK", null);
Log.d(TAG, "Showing alert dialog: " + message);
bld.create().show();
}
void saveData() {
/*
* WARNING: on a real application, we recommend you save data in a
* secure way to prevent tampering. For simplicity in this sample, we
* simply store the data using a SharedPreferences.
*/
SharedPreferences.Editor spe = getActivity().getPreferences(getActivity().MODE_PRIVATE).edit();
spe.putInt("tank", mTank);
spe.commit();
Log.d(TAG, "Saved data: tank = " + String.valueOf(mTank));
}
void loadData() {
SharedPreferences sp = getActivity().getPreferences(getActivity().MODE_PRIVATE);
mTank = sp.getInt("tank", 2);
Log.d(TAG, "Loaded data: tank = " + String.valueOf(mTank));
}
// We're being destroyed. It's important to dispose of the helper here!
#Override
public void onDestroy() {
super.onDestroy();
// very important:
Log.d(TAG, "Destroying helper.");
if (mHelper != null) {
mHelper.dispose();
mHelper = null;
}
}
// Listener that's called when we finish querying the items and
// subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null)
return;
// Is it a failure?
if (result.isFailure()) {
complain(getString(R.string.failed_to_query_inventory) + result);
return;
}
Log.d(TAG, "Query inventory was successful.");
/*
* Check for items we own. Notice that for each purchase, we check
* the developer payload to see if it's correct! See
* verifyDeveloperPayload().
*/
// Do we have the premium upgrade?
Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
// Do we have the infinite gas plan?
Purchase infiniteGasPurchase = inventory
.getPurchase(SKU_INFINITE_GAS);
mSubscribedToInfiniteGas = (infiniteGasPurchase != null && verifyDeveloperPayload(infiniteGasPurchase));
Log.d(TAG, "User "
+ (mSubscribedToInfiniteGas ? "HAS" : "DOES NOT HAVE")
+ " infinite gas subscription.");
if (mSubscribedToInfiniteGas)
mTank = TANK_MAX;
// Check for gas delivery -- if we own gas, we should fill up the
// tank immediately
Purchase gasPurchase = inventory.getPurchase(SKU_GAS);
if (gasPurchase != null && verifyDeveloperPayload(gasPurchase)) {
Log.d(TAG, "We have gas. Consuming it.");
mHelper.consumeAsync(inventory.getPurchase(SKU_GAS),
mConsumeFinishedListener);
return;
}
updateUi();
setWaitScreen(false);
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};
/** Verifies the developer payload of a purchase. */
boolean verifyDeveloperPayload(Purchase p) {
String payload = p.getDeveloperPayload();
/*
* TODO: verify that the developer payload of the purchase is correct.
* It will be the same one that you sent when initiating the purchase.
*
* WARNING: Locally generating a random string when starting a purchase
* and verifying it here might seem like a good approach, but this will
* fail in the case where the user purchases an item on one device and
* then uses your app on a different device, because on the other device
* you will not have access to the random string you originally
* generated.
*
* So a good developer payload has these characteristics:
*
* 1. If two different users purchase an item, the payload is different
* between them, so that one user's purchase can't be replayed to
* another user.
*
* 2. The payload must be such that you can verify it even when the app
* wasn't the one who initiated the purchase flow (so that items
* purchased by the user on one device work on other devices owned by
* the user).
*
* Using your own server to store and verify developer payloads across
* app installations is recommended.
*/
return true;
}
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: "
+ purchase);
// if we were disposed of in the meantime, quit.
if (mHelper == null)
return;
if (result.isFailure()) {
complain(getString(R.string.error_purchase) + result);
setWaitScreen(false);
return;
}
if (!verifyDeveloperPayload(purchase)) {
complain(getString(R.string.error_purchase_authenitcity_failed));
setWaitScreen(false);
return;
}
Log.d(TAG, "Purchase successful.");
if (purchase.getSku().equals(SKU_GAS)) {
// bought 1/4 tank of gas. So consume it.
Log.d(TAG, "Purchase is gas. Starting gas consumption.");
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
} else if (purchase.getSku().equals(SKU_PREMIUM)) {
// bought the premium upgrade!
Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
alert(getString(R.string.thank_you_updgraing_premium));
mIsPremium = true;
updateUi();
setWaitScreen(false);
} else if (purchase.getSku().equals(SKU_INFINITE_GAS)) {
// bought the infinite gas subscription
Log.d(TAG, "Infinite gas subscription purchased.");
alert("Thank you for subscribing to infinite gas!");
mSubscribedToInfiniteGas = true;
mTank = TANK_MAX;
updateUi();
setWaitScreen(false);
}
}
};
// Called when consumption is complete
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
Log.d(TAG, "Consumption finished. Purchase: " + purchase
+ ", result: " + result);
// if we were disposed of in the meantime, quit.
if (mHelper == null)
return;
// We know this is the "gas" sku because it's the only one we
// consume,
// so we don't check which sku was consumed. If you have more than
// one
// sku, you probably should check...
if (result.isSuccess()) {
// successfully consumed, so we apply the effects of the item in
// our
// game world's logic, which in our case means filling the gas
// tank a bit
Log.d(TAG, "Consumption successful. Provisioning.");
mTank = mTank == TANK_MAX ? TANK_MAX : mTank + 1;
saveData();
alert("You filled 1/4 tank. Your tank is now "
+ String.valueOf(mTank) + "/4 full!");
} else {
complain("Error while consuming: " + result);
}
updateUi();
setWaitScreen(false);
Log.d(TAG, "End consumption flow.");
}
};
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + ","
+ data);
if (mHelper == null)
return;
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
} else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
//In app billing method end here
My products are Managed products in developer account In-app Products
EDIT:
When I use android.test.purchased as a SKU then it works fine and as I change my sku with my product_id then it giving me Error Authentication is required. You need to sign into your Google Account.
Please make sure your product id on playstore for in app purchase which is "test_product" you should use the same sku in your code. And if you change the sku from your application, then all that possible sku names must exists on PlayStore as in-app Products. I once run into this problem and the reason was my SKU item was not exists on Google Playstore in-app products i just add it on Playstore and it was resolved.
Android IAB Error - Authentication required
It seems that you need to publish the APK. I stuck with this problem too.
It is a bit of a longshot, but your "Google Play store" app could be malfunctioning.
I have a report from a customer that he cannot make an In-App purchase and he is getting this same error. I also know that we are still getting purchases. So I don't think you necessarily configured anything wrong. IAB v3 could simply be malfunctioning with your "Google Play Store" app.
Try going to Settings open the Application manager and choose the "Google Play store" application in the list, and click on "Uninstall updates" button which appears there. Next try to do the purchase again. If your old Google Play application supports In-App billing v3 it will work. If it doesn't, try to update the "Google Play store" app again, perhaps you will get a newer/different version of the store app and you will be able to make the purchase.
As an alternative you can try and configure a second test device with a different Google account... or ask a friend to join a beta test group and make a test purchase.
I am not really happy with IAB v3. It feels a little clunky. V2 had fewer features but felt solid.

Categories

Resources