npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nativescript-iap

v8.0.7

Published

A NativeScript plugin for making in-app purchases

Downloads

31

Readme

In-app purchase plugin for NativeScript

Installation

Run the following command from the root of your project:

ns plugin add nativescript-iap

Configuration

In order for your in-app purchases to be recognized by the plugin, you must configure those on the Google/AppStore side.

Usage

Setup hook

Before starting purchase you need hook up to the purchaseUpdated event. This way you will receive information about the transaction state while it is executing and take necessary action when the transaction completes. You can set the hook once as global handler to process the all incoming purchases.

import inAppPurchase, { PurchaseEventData } from "nativescript-iap";

inAppPurchase.on("purchaseUpdated", async (data: PurchaseEventData) => {
    for (const transaction of data.transactions) {
        switch (transaction.state) {
            case TransactionState.purchasing:
                // ...
                break;
            case TransactionState.purchased:
            case TransactionState.restored:
                // Delivering the content
                break;
            case TransactionState.deferred:
                // ...
                break;
            case TransactionState.refunded:
                // ...
                break;
        }

        // You must finish transaction otherwise the purchase being refunded
        await inAppPurchase.finishTransaction(transaction);
    }
});

Getting the products

To get the actual products call getProducts with array of the products identifiers (in Google Play products ID calls SKU):

import inAppPurchase from "nativescript-iap";

const products = await inAppPurchase.getProducts(["your.product.id.1", "your.product.id.2"]);

Purchasing the product

import inAppPurchase, { PurchaseError } from "nativescript-iap";

try {
    await inAppPurchase.purchase(product);
} catch (error) {
    if (error instanceof PurchaseError) {
        switch (error.code) {
            switch (error.code) {
                case PurchaseErrorCode.canceled:
                    // ...
                    break;
                case PurchaseErrorCode.productAlreadyOwned:
                    // ...
                    break;
                case PurchaseErrorCode.productNotAvailable:
                    // ...
                    break;
                case PurchaseErrorCode.userNotAuthorized: // On iOS only
                    // ...
                    break;
                default: //Unknow error
                    // ...
                    break;
            }
        }
    }

    // Handle the error
}

Restoring the purchased products

import inAppPurchase from "nativescript-iap";
// All restored purchases will be handled by the "purchaseUpdated" hook.
await inAppPurchase.restorePurchases();

(ANDROID ONLY) Consuming the purchased product

import inAppPurchase from "nativescript-iap";

await inAppPurchase.consumePurchase(transaction);

Showing the price consent dialog

import inAppPurchase from "nativescript-iap";

const products = await inAppPurchase.getProducts(["product_id_with_updated_price"]);

await inAppPurchase.showPriceConsent(product[0]);

(iOS only) Listen for revoked family-shared purchases.

import inAppPurchase from "nativescript-iap";

inAppPurchase.on("productsRevoked", async (data: ProductsRevokedEventData) => {
    data.productIds.foreach((productId) => {
        // Notify user
    });
});

API

InAppPurchase - class Represents the class for making in-app purchase.

Product - class Represents information about a product.

ProductType - enum Represents the product type.

PurchaseError - class Represents the error class wich can throws in purchase method.

PurchaseErrorCode - enum Represents the error codes for PurchaseError.

PurchaseEventData - interface Represents the data of the purchaseUpdated event.

SubscriptionPeriod - enum Represents the subscription period duration.

Transaction - class Represents an in-app purchase transaction.

TransactionState - enum Represents the states of the transaction.