PeterStaev / nativescript-purchase

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

purchase.on(purchase.transactionUpdatedEvent) is never called #108

Closed swissdude closed 3 years ago

swissdude commented 3 years ago

Hi

First an foremost: THANKS FOR THIS PLUGIN!

Alas, it doesn't really work for me.

Created a Class for the whole process.

The init-works, getProducts() works as well and when I call makePurchase(), I get the console-output that the purchase can be made.

But then it stops. purchase.on(purchase.transactionUpdatedEvent …) is never called.

const purchase = require("nativescript-purchase");
const Product = "nativescript-purchase/product";
//let purchase = new Purchase();
const purchaseInitPromise = purchase.init(["com.myApp.appsubscription"]);

const { Transaction, TransactionState } = require("nativescript-purchase/transaction");
const applicationSettings = require("application-settings");

class inAppPurchaseClass {

    listProducts(callback) {

        purchaseInitPromise.then(() => {

            purchase.getProducts().then((products) => {
                // products.forEach((product) => {
                //     console.log(product);
                // });

                //console.log(products);

                callback(products);
            });
        });
    }

    makePurchase(product, callback) {

        console.log("attempting purchase");

        if (purchase.canMakePayments()) {
            console.log("can make payments");

            purchase.on(purchase.transactionUpdatedEvent, (transaction) => {
                console.log("yo");
                console.log("transaction");
            });

            // NOTE: 'product' must be the same instance as the one returned from getProducts()
            purchase.buyProduct(product);
        } else {
            callback("error");
        }
    }

    setTransactionEventsHook(callback) {

        let result;
        let doConsume = false;
        console.log("init purchase-callback");
        purchase.on(purchase.transactionUpdatedEvent, (transaction) => {

            console.log("transaction hook");

            if (transaction.transactionState === TransactionState.Purchased) {
                //alert(`Congratulations you just bought ${transaction.productIdentifier}!`);
                console.log(transaction.transactionDate);
                console.log(transaction.transactionIdentifier);
                applicationSettings.setBoolean(transaction.productIdentifier, true);
                result = {
                    result: "ok",
                    product: transaction.productIdentifier,
                    trDate: transaction.transactionDate,
                    trId: transaction.transactionIdentifier
                };
            } else if (transaction.transactionState === TransactionState.Restored) {
                console.log(`Purchase of ${transaction.originalTransaction.productIdentifier} restored.`);
                console.log(transaction.originalTransaction);
                console.log(transaction.originalTransaction.transactionDate);
                applicationSettings.setBoolean(transaction.originalTransaction.productIdentifier, true);
                result = { result: "restored" };
            } else if (transaction.transactionState === TransactionState.Failed) {
                console.log(`Purchase of ${transaction.productIdentifier} failed!`);
                result = { result: "failed" };
            }

            if (transaction.transactionState === TransactionState.Purchased && transaction.productIdentifier.indexOf(".consume") >= 0) {

                doConsume = true;

                purchase.consumePurchase(transaction.transactionReceipt)
                    .then((responseCode) => {
                        console.log(responseCode);
                        callback({ result: "consumed" });
                    }) // If responseCode === 0 the purchase has been successfully consumed
                    .catch((e) => console.log(e));
            }

            if (doConsume === false) {
                callback(result);
            }
        });

    }

}

exports.inAppPurchaseClass = inAppPurchaseClass;

I'm calling it from my main-js like this:

iap.setTransactionEventsHook(iapPurchaseCallback);
iap.makePurchase(selectedProduct, iapPurchaseCallback);

As I said, this call works: iap.listProducts(iapCalllback);

I might have to add that I'm trying to buy a subscription. I read the other issues about this, but they all seem kinda outdated, so I'm not sure if this feature works or not.

Android, NS 7.x, v2.0.14 of your plugin

swissdude commented 3 years ago

Nevermind - I just realised that I'm using an old version and probably should use the pro-plugins feature...

PeterStaev commented 3 years ago

No further response so closing this one for now. In case you still have problems, please provide more details.