PeterStaev / nativescript-purchase

:moneybag: A NativeScript plugin for making in-app purchases!
Apache License 2.0
83 stars 28 forks source link

The init() method fails #102

Closed StefanNedelchev closed 3 years ago

StefanNedelchev commented 4 years ago

I have an Android app which is all setup with in-app purchases and subscriptions and testing track with a few tester accounts (also the app is in Published state). The problem is that I can't even start testing the purchases because the plugin fails to initialize. The promise from purchase.init([...]) always rejects and there's nothing in the response. I have both .then() and .catch() and I always enter in the 'catch' method. I followed the tutorials and double checked if I'm missing something. Unfortunately I can't provide you anything for testing so I guess the only thing that you can do is to run your app and see if it still works. I'll provide some of my code that implements the plugin. I use NS with Vue and I use the created and mounted hooks for initialization. Keep in mind that I also tried doing everything in the mounted hook without separation too but the result was the same. I also tried running the purchase.init() method on app launch and yet the promise still gets rejected.

    created() {
      this.purchaseInitPromise = purchase.init([
        '<hidden product id>',
        '<hidden product id>'
      ]);
    },
    mounted() {
      this.initializeProducts()
        .then(products => {
          // save products list so they can be used in a purcase
          this.productsList = products;
          // attach transaction update event listener
          purchase.on(purchase.transactionUpdatedEvent, transaction => this.onTransactionUpdate(transaction));
        });
    },
    beforeDestroy() {
      purchase.off(purchase.transactionUpdatedEvent);
    },
    methods: {
      initializeProducts() {
        return this.purchaseInitPromise
          .then(() => {
            this.displayDialog('Purchase plugin was initialized. Trying to get products with ids: ' + this.productIDs.toString());

            return purchase.getProducts()
              .then(products => {
                this.displayDialog('Product list was obtained: ' + JSON.stringify(products));
                products.forEach(product => {
                  if (applicationSettings.getBoolean(product.productIdentifier)) {
                    product.isPurchased = true;
                  }
                });
                return products;
              })
              .catch(error => {
                this.displayDialog('Couldn\'t get products list! ' + (typeof error === 'string' ? error : JSON.stringify(error)));
              });
          })
          .catch(error => {
            // I always end up here
            this.displayDialog('Couldn\'t initialize purchase module! ' + (typeof error === 'string' ? error : JSON.stringify(error)));
          });
      },
PeterStaev commented 4 years ago

Hey @hardmaster92 , I just tried and the ProPlugins demo works fine for Vanilla NS. Sadly I'm not a Vue developer so not sure if you are initializing the plugin correctly. Don't you get something in the catch method that could hint on what the problem is?

StefanNedelchev commented 4 years ago

Unfortunately no - there's nothing in the object returned by the catch method. Actually I discovered something else - the initialization seems to be successful after all. I tried running the init() metod without subscribing for the promise. Then I run getProducts() with 1 second timeout and I actually get the products (and all other methods of the plugin work fine). So it's weird how the plugin is initialized correctly but the promise returned by the init() method is rejected.

StefanNedelchev commented 3 years ago

I'll close this issue because I've never experienced this issue after chosing a different approach using Vue's plugin installation pattern.