In app purchase android , ios

how to implement in app purchase in my android , ios app
i search a lot but found Little resources for this point
any help please
thanks

Right now, InAppPurchase2 looks like the best plugin to me, but I haven’t tested it yet in real life. I wrote a mock for it a couple days ago though, I should put it on Github somehow probably.

1 Like

Hi, would you be able to share it? I am also looking into this :slight_smile:

1 Like

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

@Walgermo I agree with @AaronSterling on this one. InAppPurchase2 is the way to go. I actually implemented InAppPurchase, but found that if a user had to go out of the app to fill in their credit card information, then they would lose their purchase.

I’ve written a blog post about it. If you need any information I would be happy to help out.

2 Likes

Thanks, will look at it soon :slight_smile:

@thielcole thanks for your blog post, I used it to implement IAP in my app :slight_smile:

There were 2 things i had to change, maybe due to the plugin changing since your post?

  1. store.ready() is not a promise, had to change the typing file in ionic native and use with a callback function

  2. in the product typing object: alias is actually optional

Thanks for the work you did with the ionic native integration for the plugin, should this be corrected? I could also make an PR if this helps but have to dive firsts in the code of the ionic native integration :slight_smile:

1 Like

Store.ready() should definitely return a promise, so if you would like feel free to submit a pull request for that.

I’d like to help but don’t know where to start for editing InAppPurchase2. Do you know where the original git repo is? It seems the files in the ionic native repo are already build for distribution.

The main thing to be done is to wrap the ready(callback) in a Promise style function if I understand correct.
https://github.com/j3k0/cordova-plugin-purchase/blob/b59315ddba16c6db5996d8bbaf48bed050fe7af2/www/store-android.js#L1218

If I understand this correct it is strange that the ionic native typings worked in the first place for other people. For me when I use ready as a promise it throws an error.

Actually there is already a PR. For further info:

2 Likes

I am also running into this issue now. Is a manual modification ourselves the only solution?

@aabdolla did you get it working? unfortunately you still have to make the manual modification

One way to avoid this would be to use the cordova plugin without the ionic-native layer

Someone posted their code where they used the cordova plugin straight up and bypassed ionic. That ended up working for me. Before using the ionic layer, the store.ready was never getting called. It was really annoying.

To do free in-app purchases download and install freedom app on your android device. Freedom allows you to do free of cost in-app purchase and also stop all the ads inside the app. Also you can use the app on you windows pc: https://freedomapkdownload.online/freedom-app-for-pc/