eggheadgames / android-in-app-payments

Support Android Google Play and Amazon in-app billing (IAP) payments with one API
MIT License
58 stars 14 forks source link

Key manipulation InvalidKeySpecException #10

Closed jpardogo closed 4 years ago

jpardogo commented 7 years ago

I'm pretty curious about why you deal this way with the string key for google in app. I'm getting 'InvalidKeySpecException', an example project with the library would help.

https://github.com/eggheadgames/android-in-app-payments/blob/1.2.8/library/src/main/java/com/billing/google/GoogleBillingService.java#L40

mikemee commented 7 years ago

Hi @jpardogo! Yes, a sample project is a great idea.

Here's some code from one of our apps that lives in the activity that displays the items to purchase. Because we build both Amazon and Google from the same codebase, we rely on the Gradle flavor mechanism for that.

Hopefully this will get you started.

    private void initIap() {
        String iapKey;
        int iapBuildTarget;

        if ("amazon".equals(BuildConfig.FLAVOR)) {
            iapKey = "";
            iapBuildTarget = IAPManager.BUILD_TARGET_AMAZON;
        } else if ("google".equals(BuildConfig.FLAVOR)) {
            iapKey = "***** INSERT YOUR GOOGLE IAP KEY HERE ******";
            iapBuildTarget = IAPManager.BUILD_TARGET_GOOGLE;
        } else {
            return;
        }

        List<String> iapKeys = yourFunctionToGetIapKeys();
        IAPManager.build(this, iapBuildTarget, iapKeys);
        IAPManager.addPurchaseListener(this);
        IAPManager.init(iapKey, BuildConfig.DEBUG);
    }

Does that help?

mikemee commented 7 years ago

@jpardogo from further reading here, https://stackoverflow.com/questions/32428947/android-in-app-billing-error-invalidkeyspecexception, it sounds like your key is likely invalid somehow. As this library doesn't really do anything except pass it through, we don't have much control.

jpardogo commented 7 years ago

Hi, yes the library was modifying the string key I passed as a parameter (I debug it and confirmed it). I basically add the project as a module and remove the for loop and pass the key and it works. I don't see the need of the for loop.

Other improvements that could be good:

public static void init(boolean enableLogging) {
   init(null, enableLogging)
}

public static void init(String key, boolean enableLogging) {
   if(key!=null){        
      billingService.init(key);
   }
   billingService.enableDebugLogging(enableLogging);
}

https://github.com/eggheadgames/android-in-app-payments/blob/1.2.8/library/src/main/java/com/billing/google/GoogleBillingListener.java#L54

and here:

https://github.com/eggheadgames/android-in-app-payments/blob/1.2.8/library/src/main/java/com/billing/amazon/AmazonBillingListener.java#L59

mikemee commented 7 years ago

@jpardogo Thanks for the several great ideas there. I've created new issues:

I'm not sure why the for-loop is there either. As far as I know, this is the exact same sample code that Google provides for IAP in multiple places. I wonder why they bother to rebuild the key? I'm hesitant to change their code without understanding why. Do you know what section of your key it was changing and why?

jpardogo commented 7 years ago

I guess they where sending the IAP key in an specific format and the app was decoding it, with the for loop, this way the real inapp key wasn't store as a plain string on the source code. But again, It is just an idea, I would need to check the source code of this example to be sure.

Also we can follow the same init method strategy for other end points where some arguments are not required for amazon or Google.

jpardogo commented 7 years ago

I would send all callback in the main thread and the user will take care of doing anything they want with its own threading strategy.

Also the onError callback is useful for error user feedback, not only debuging.

Great lib by the way, with a bit of work it could be awesome.

mikemee commented 4 years ago

The only item left here is covered in #12, which we're not going to do in this round of releases (see comments there). Closing this for now.

@jpardogo Thanks again for your ideas and feedback. They really helped to improve this library!