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

@courthoang/nativescript-in-app-purchase

v1.0.4

Published

NativeScript plugin to handle in app purchases and subscriptions on Android and iOS.

Downloads

12

Readme

nativescript-in-app-purchase

NativeScript plugin to handle in app purchases and subscriptions on Android and iOS.

(Optional) Prerequisites / Requirements

Refer to the mobile ecosystem provider on how to
test in app purchases.

For Apple head to developer.apple.com

For Android Google Play Store head over to developer.android.com

Installation

Installing the plugin

tns plugin add nativescript-in-app-purchase

Usage

Use this typings definition for Typescript and adding IntelliSense support.

    /// <reference path="./node_modules/nativescript-in-app-purchase/index.d.ts" />

Initialization

First of all it is required to create an instance of InAppPurchaseManager.

    import { OnInit } from '@angular/core'
    import { InAppPurchaseManager, InAppPurchaseResultCode, InAppPurchaseStateUpdateListener, InAppPurchaseTransactionState, InAppPurchaseType } from 'nativescript-in-app-purchase'
    export class Test implements OnInit {
        private inAppPurchaseManager: InAppPurchaseManager
        constructor() { }
        ngOnInit(): void {
            const purchaseStateUpdateListener: InAppPurchaseStateUpdateListener = {
                onUpdate: (purchaseTransactionState: InAppPurchaseTransactionState): void => {
                    if (purchaseTransactionState.resultCode === InAppPurchaseResultCode.Purchased) {
                        // Item has been purchased, sync local items list ...
                    }
                },
                onUpdateHistory: (purchaseTransactionState: InAppPurchaseTransactionState): void => {
                    if (purchaseTransactionState.resultCode === InAppPurchaseResultCode.Restored) {
                        // Item has been  restored, sync local items list ...
                    }
                }
            }
            InAppPurchaseManager.bootStrapInstance(purchaseStateUpdateListener).then(inAppPurchaseManager => {
                this.inAppPurchaseManager = inAppPurchaseManager
            })
        }
    }

Product list

Get the list of in app products.
To retrieve the list of in app products you must query a known amount of product IDs.

    // additional imports required
    import { InAppPurchaseType, InAppListProductsResult, InAppProduct } from 'nativescript-in-app-purchase'

    // query products
    queryProducts() {
        const myProductIds = ['product_1', 'product_2']
        // For subscriptions change to `InAppPurchaseType.Subscription`
        const myProductType = InAppPurchaseType.InAppPurchase 

        this.inAppPurchaseManager.list(myProductIds, myProductType)
            .then((result: InAppListProductsResult) => {
                const products: InAppProduct[] = result.products
                for (const product of products) {
                    // get the products ...
                    console.log(product.title, product)
                }
            })
    }

Buy a product

When buying a product the result InAppOrderResult is only related to the order transaction it self.
The purchase state of the product will be called on the InAppPurchaseStateUpdateListener#onUpdate method.
This is where you have to confirm the purchase to finish the whole purchasing transaction.
The App Store and Google Play Store will automatically refund orders that haven't been confirmed.

Buying a product

    // additional imports required
    import { InAppOrderResult } from 'nativescript-in-app-purchase'

    // by product
    buy() {
        const myProducts: InAppProduct[] = []//...

        const productToBuy: InAppProduct = myProducts[0]
        this.inAppPurchaseManager.order(productToBuy)
            .then((result: InAppOrderResult) => {
                if (result.success) {
                    // order has been processed
                    // ... expecting confirmation ...
                    // handle confirmation in `InAppPurchaseStateUpdateListener.onUpdate(...)`
                }
            })
    }

Confirming a product

    // additional imports required
    import { InAppOrderConfirmResult } from 'nativescript-in-app-purchase'

    ngOnInit(): void {
        const purchaseStateUpdateListener: InAppPurchaseStateUpdateListener = {
            onUpdate: (purchaseTransactionState: InAppPurchaseTransactionState): void => {
                if (purchaseTransactionState.resultCode === InAppPurchaseResultCode.Purchased) {
                    // Item has been purchased, sync local items list ...
                    this.confirmOrder(purchaseTransactionState)
                }
            },
            onUpdateHistory: (purchaseTransactionState: InAppPurchaseTransactionState): void => {
                if (purchaseTransactionState.resultCode === InAppPurchaseResultCode.Restored) {
                    // Item has been  restored, sync local items list ...
                }
            }
        }
        // ...
    }

    confirmOrder(purchaseTransactionState: InAppPurchaseTransactionState) {
        const isConsumable = (productId: string): boolean => { 
            /* determine if is consumable and can be purchased more then once */
            return false }

        // only purchased products can be confirmed
        if (purchaseTransactionState.resultCode === InAppPurchaseResultCode.Purchased) {
            const consumable: boolean = isConsumable(purchaseTransactionState.productIdentifier)
            this.inAppPurchaseManager.orderConfirm(purchaseTransactionState, consumable)
                .then((result: InAppOrderConfirmResult) => {
                    if (result.success) {
                        // order confirmation has been processed
                    }
                })

        }
    }

Restore purchases

Restore purchases will get you all items the user already purchased.
The purchase state of the restored product will be called on the InAppPurchaseStateUpdateListener#onUpdateHistory method.

    // additional imports required
    import { InAppOrderHistoryResult } from 'nativescript-in-app-purchase'

    restoreProducts() {
        this.inAppPurchaseManager.purchaseHistory()
            .then((result: InAppOrderHistoryResult) => {
                if (result.success) {
                    // purchase history requested
                    // handle it in `InAppPurchaseStateUpdateListener.onUpdateHistory(...)`
                }
            })
    }

API

  • list(productIds: string[], productType?: InAppPurchaseType): Promise<InAppListProductsResult>
    List all products
  • order(product: InAppProduct): Promise<InAppOrderResult>
    Order a product
  • orderConfirm(purchaseTransaction: InAppPurchaseTransactionState, consumable: boolean): Promise<InAppOrderConfirmResult>
    Confirm the buy of a product to make it final
  • purchaseHistory(): Promise<InAppOrderHistoryResult>
    Load user's owened products
  • canMakePayment(): boolean
    Check wether billing is enabled or not
  • static bootStrapInstance(purchaseStateUpdateListener?: InAppPurchaseStateUpdateListener): Promise<InAppPurchaseManager>
    Create a new instance of the in app purchase manager
  • shutdown()
    Close connection to the unerlying OS billing API

DEMO App

There is a demo angular app project included.
Checkout this repo and read the DEMO Readme

License

Apache License Version 2.0, January 2020

Donation

Donate with Bitcoin
3GFxvCK4nnTvHcLpVtFDQhdjANzRGBV6G6
Open in Wallet
Open in Wallet