In app purchase android , ios

I think it’s small enough to post here. I haven’t run tests on this, so if you see problems, please post. You can change the contents from nulls to something else, depending on what you want to simulate. Remember to declare the provider as {provide: InAppPurchase2, useClass: InAppPurchase2Mock}.

import { InAppPurchase2, IAPProduct, IAPProductEvents, IAPError, 
	     IAPQueryCallback, IAPProductOptions, IAPProducts } from '@ionic-native/in-app-purchase-2';

// InApp Purchase 2 Mock

const mockIAPProduct: IAPProduct = {
	additionalData: null,
	alias: null,
	canPurchase: null,
	currency: null,
	description: null,
	downloaded: null,
	downloading: null,
	finish: null,
	id: null,
	loaded: null,
	on: null,
	once: null,
	off: null,
	owned: null,
	price: null,
	priceMicros: null,
	set: null,
	state: null,
	stateChanged: null,
	title: null,
	transaction: null,
	trigger: null,
	type: null,
	valid: null,
	verify: null
}

const mockIAPProductEvents: IAPProductEvents = {
	approved: null,
	cancelled: null,
	downloaded: null,
	downloading: null,
	error: null,
	expired: null,
	finished: null,
	initiated: null,
	invalid: null,
	loaded: null,
	owned: null,
	refunded: null,
	registered: null,
	requested: null,
	unverified: null,
	updated: null,
	valid: null,
	verified: null
}

interface Error {
	code: number,
	message: string
}

export class InAppPurchase2Mock extends InAppPurchase2 {


	/**
     * Get product by id or alias
     * @param idOrAlias
     */
    get(idOrAlias: string): IAPProduct { return mockIAPProduct; }
    /**
     * Register error handler
     * @param onError {Function} function to call on error
     */
    error(onError: Function): void {}
    /**
     * Add or register a product
     * @param product {IAPProductOptions}
     */
    register(product: IAPProductOptions): void {}
    /**
     *
     * @param query
     * @param event
     * @param callback
     * @return {IAPProductEvents}
     */
    when(query: string | IAPProduct, event?: string, callback?: IAPQueryCallback): IAPProductEvents {
    	return mockIAPProductEvents;
    }
    /**
     * Identical to `when`, but the callback will be called only once. After being called, the callback will be unregistered.
     * @param query {string | IAPProduct}
     * @param [event] {event}
     * @param [callback] {IAPQueryCallback}
     * @return {IAPProductEvents}
     */
    once(query: string | IAPProduct, event?: string, callback?: IAPQueryCallback): IAPProductEvents {
    	return mockIAPProductEvents;
    }
    /**
     * Unregister a callback. Works for callbacks registered with ready, when, once and error.
     * @param callback {Function}
     */
    off(callback: Function): void {}
    
    order(product: string | IAPProduct, additionalData?: any): {
        then: Function;
        error: Function;
    } {
    	return {then: null, error: null};
    }
    /**
     *
     * @return {Promise<any>} returns a promise that resolves when the store is ready
     */
    ready(): Promise<void> {
    	return Promise.resolve(null);
    }
    refresh(): void {}

}
1 Like