Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

restored purchases on Android have an undefined purchase.date field #145

Closed
NateEag opened this issue Jun 5, 2017 · 14 comments
Closed

restored purchases on Android have an undefined purchase.date field #145

NateEag opened this issue Jun 5, 2017 · 14 comments

Comments

@NateEag
Copy link

NateEag commented Jun 5, 2017

When I restore purchases on a Galaxy Tab S2 (running Android version 6.0.1), the date field for each item is undefined.

Example Code

inAppPurchase.restorePurchases()
    .then(function(purchaseHistory) {
        purchaseHistory.forEach(function(item) {
            console.log('Item:', item, 'purchase date:', item.date);
        });        

which, when run in my project (which uses Crosswalk, and thus Chromium), results in this (redacted) output:

Item: { ... }
purchase date: undefined

Type of product you are working with consumable/non-consumable/subscription

I noticed this working with subscriptions, where I actually need the subscription's start date, but it seems to apply to both subscriptions and consumable products.

Version of cordova

cordova --version says 6.3.1.

Version of iOS/Android

As mentioned above, this is on an Android device running 6.0.1.

Proposed Fix

Adding some console.log()s to the restorePurchases method helped me discover that the Google API response does not have state or date fields (as that code currently assumes), but instead has purchaseState and purchaseTime fields. This table in the Android docs appears to confirm that.

Updating purchase construction as follows seems to do the job in my sandbox:

return {
          productId: val.productId,
          state: val.purchaseState,
          transactionId: val.orderId,
          date: new Date(val.purchaseTime),
          ...
        };

Note that this change would also fix issue #112 .

If a PR implementing this fix would be accepted, I can submit one.

@c006
Copy link

c006 commented Jun 26, 2017

Try the following.

    inAppPurchase.restorePurchases()
        .then(function (purchaseHistory) {
            for(var i=0; i<purchaseHistory.length; i++) {
                var _r = JSON.parse(purchaseHistory[i].receipt);
                console.log('--------------------------------------');
                console.log('productId: '+ purchaseHistory[i].productId);
                console.log('transactionId: '+ purchaseHistory[i].transactionId);
                console.log('type: '+ purchaseHistory[i].type);
                console.log('productType: '+ purchaseHistory[i].productType);
                console.log('receipt: '+ purchaseHistory[i].receipt);
                console.log('receipt -> orderId: '+ _r.orderId);
                console.log('receipt -> packageName: '+ _r.packageName);
                console.log('receipt -> productId: '+ _r.productId);
                console.log('receipt -> purchaseTime: '+ _r.purchaseTime);
                console.log('receipt -> purchaseState: '+ _r.purchaseState);
                console.log('receipt -> purchaseToken: '+ _r.purchaseToken);
            }
        });

@NateEag
Copy link
Author

NateEag commented Jun 29, 2017

That is a useful workaround - thanks.

@NateEag
Copy link
Author

NateEag commented Aug 14, 2017

I would argue this issue should not be closed - a workaround is not the same as a fix.

As the situation currently stands, the plugin offers a broken API which the docs claim works.

The fact that someone found a usable workaround does not mean this issue is resolved.

@moritzgloeckl
Copy link
Collaborator

@NateEag If you send us a pull request we can merge your fix.

@NateEag
Copy link
Author

NateEag commented Aug 20, 2017

@moritzgloeckl Okay, thanks. I'll look for some time to do that.

@hirbod
Copy link

hirbod commented Oct 14, 2017

there is already #123 but its still unmerged. Yet another dead cordova plugin :(

@moritzgloeckl
Copy link
Collaborator

@hirbod It's unmerged because it breaks the build. If you fix it and the CI runs clean I can merge it!

@hirbod
Copy link

hirbod commented Oct 14, 2017

its a one-liner. You leave this Plugin broken because of this? You just need to change a single word

@hirbod
Copy link

hirbod commented Oct 14, 2017

PR has landed (for src and www)

@moritzgloeckl
Copy link
Collaborator

@hirbod Why did you send two PRs? You can include both changes into one. Have you tested your changes? Can you please add them to the tests as well?

I'm going to delete your PRs, so you can send me one PR with all three files edited.

Thanks.

@hirbod
Copy link

hirbod commented Oct 14, 2017

Cmon dude, I did a quick fix inside of githubs editor. I'm not going to repeat everything. Merge it or leave it

@hirbod
Copy link

hirbod commented Oct 14, 2017

I deleted the second one, as this was for "www" but I guess this will be generate anyway automatically when bundling up a release.

@moritzgloeckl
Copy link
Collaborator

@hirbod Well I know your changes are probably not breaking any existing code but nonetheless they should be tested and merged properly. "Merge it or leave it" is not an option because it's not just us two using this plugin.

Feel free to fork the project, implement and test your changes, then send us a PR.

Thanks.

@hirbod
Copy link

hirbod commented Oct 14, 2017

They are already tested locally and working fine. Before I just had an "undefined" state.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants