My goal here is to implement an In-app purchase in my app and also to check if Users owns an item as each app launch. Below is what I have tried so far and the issue I am seeing.
This is the code in my main LAUNCHER
Activity. I am using android.test.purchased as my ITEM_SKU (is this a problem?) to test the in-app purchase locally. It did work once initially and since them I am getting this error inside onQueryInventoryFinished
: Failed to query inventory: IabResult: Error refreshing inventory (querying owned items). (response: -1003:Purchase signature verification failed)
// Does the user have the premium upgrade?
boolean mIsaddFree = false;
private AdView adView;
private static final String TAG = "com.example.inappbilling";
IabHelper mHelper;
static final String ITEM_SKU = "android.test.purchased";
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase) {
if (result.isFailure()) {
// Handle error
return;
} else if (purchase.getSku().equals(ITEM_SKU)) {
consumeItem();
}
}
};
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
mLog.printToLog(className + " -> " + MethodName.methodName() + ": Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
if (result.isFailure()) {
mLog.printToLog(className + " -> " + MethodName.methodName() + "Failed to query inventory: " + result);
} else {
if (inventory.hasPurchase(ITEM_SKU)) {mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),mConsumeFinishedListener);
} else {
}
}
// Do we have the addFree upgrade?
Purchase premiumPurchase = inventory.getPurchase(ITEM_SKU);
mIsaddFree = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase,
IabResult result) {
if (result.isSuccess()) {
} else {
// handle error
}
}
};
/**
* Verifies the developer payload of a purchase.
*/
boolean verifyDeveloperPayload(Purchase p) {
String payload = p.getDeveloperPayload();
return true;
}
public void consumeItem() {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
private void setupIAB() {
String base64EncodedPublicKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgQEbYA2GEYavRb1oZz9aMyn86Wj09NCqFRbD936uKLJHKSDFdsfsdfklndfHKJHJKAHFKJDSFskdsfkjnsdfjkKJNJKSDF9ZclxWM3EuZTMM3kWDUfOnS0+u8c/Uq727cLBRqnRJy5pfWFZtPqXwAN4yaeCgxGka6eofn0jj7RxPEFJZLkFw2Ijvxl2uMIHSlxIvl+6ZwMtOKJHSDKJFHNDSllkadndfkasdlkasdlkasBOaBLKWFYY2to8g3aRx1HVTxLSi9Ms3KPMM3uPbbElCbKoIiqcNwIDAQAB";
// Create the helper, passing it our context and the public key to verify signatures with
mLog.printToLog(className + " -> " + MethodName.methodName() + ": Creating IAB helper.");
mHelper = new IabHelper(this, base64EncodedPublicKey);
// enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(!Constants.C_PRODUCTION_MODE);
mHelper.startSetup(new
IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
return;
} else {
mLog.printToLog(className + " -> " + MethodName.methodName() + ": Passed, result.isSuccess() = TRUE");
// 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.
mHelper.queryInventoryAsync(mGotInventoryListener);
}
}
});
}
/**
* 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) {
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
// Is it a failure?
if (result.isFailure()) {
return;
}
// Do we have the premium upgrade?
Purchase adFreePurchase = inventory.getPurchase(ITEM_SKU);
mIsaddFree = (adFreePurchase != null && verifyDeveloperPayload(adFreePurchase));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupIAB();
}
I have changed the function verifyPurchase(String base64PublicKey,
String signedData, String signature)
in Security.java
as per this thread