In App Purchase Guide V2

Im searching a guide to implement In App Purchases to my iOnic 2 App.
I only find older tutorials for iOnic 1 without TS etc.

Maybe someone can share an example to this in iOnic 2.

have you managed to implement in app purchase, i too am looking for some information about the proccess and can not find any guides for ionic 2,

the plugin docs requires an manifest.json file at the www folder. since rc0 they added that file but in assets, although its main use is for a PWA, should i add the “play_store_key” there?

Hi @taljacobson @LoLStats
could you manage to implement In-App-Purchase ?
happy if you could share your experience and some code example :slight_smile:
thank you

haven’t tested with a real Purchase yet but the test one passed on play store(have only tested on android)

the base inappPurchase plugin has built in polyfills which break with angular 2 /ionic 2 you should use a fork of it that does not have said polyfills like https://github.com/rsanchez-forks/cordova-plugin-inapppurchase
i made a issue on the ionic-site github page to change the docs to point to this fork, dont remember if they did

a few things that most be true,

  1. app is published on the play store, (alpha …)
  2. a manifest,json file in the root directory with the “play_store_key”. (this might be a problem with a PWA manifest file)
  3. app most be same id and version as the one in the store (config.xml)
  4. app most be signed for release
  5. phone play store app account email same as the testing email in play store console testing section.

note: in the docs it states that you should pass the whole productId (‘com.yourapp.prod1’) but for me it works with only the last part of it(prod1),

import { Injectable } from '@angular/core';
import { InAppPurchase } from 'ionic-native';
import { ToastController} from 'ionic-angular';


@Injectable()
export class PurchaseService {
  private productId: string[] = ['test0001']
  constructor(
    private toastCtrl: ToastController) { }

    /**
    * returns a array of Products
    */
  public getProducts():Promise<Iproducts[]> {
    return InAppPurchase
      .getProducts(this.productId)
      .then((products: Iproducts[]) => {
        return products
        //  [{ productId: 'com.yourapp.prod1', 'title': '...', description: '...', price: '...' }, ...]
      })
      .catch((err) => {
        this.presentToast("err" + err);
      });
  }

  /**
  * plugin is:  https://github.com/rsanchez-forks/cordova-plugin-inapppurchase
  * function that calls the
  *  prod - is the productId 'test0001'
  * using Toast for now since i cant view console, in a release build
  * @param {string} prod the productId .. "test0001"
  */
  public buyProduct(prod: string): void {
    InAppPurchase
      .buy(prod)
      .then((data) => {
        // alert("buy data: " + JSON.stringify(data))
        // ...then mark it as consumed:
        return InAppPurchase.consume(data.productType, data.receipt, data.signature);
      })
      .then(() => {
        this.presentToast('product was successfully consumed!');

        // this.receipt.CreateReceipt(data)
      })
      .catch((err) => {
        
      });
  }

export interface Iproducts {
  productId?: string;
  title?: string;
  description?: string;
  price?: string | number
}

1 Like

something i figured by trial and error. hope it will be a good tip for some of you.
before testing IAP, complete signing all contracts in iTunes Connect / Agreements, Tax, and Banking.
After i saved my info, it took Apple ~1h to finish my pending state.

InAppPurchase.getProducts will return empty / [] / nothing until you complete tax info (W8BEN form), contact info and banking info.

1 Like

@Frozenxis

how did you test IAP ? As proven app in the app store or is it also possible to test it with “Internal Testing”?

thank you

you have to make test users in itunes connect
logout from iPhone / Settings / iTunes&AppStore
run app on device and tap item to purchase. it will ask for sandbox account.

Hello @taljacobson

I wrote "play_store_key": "my key" on my www/manifest.json.
But when I run: ionic run android, this file is overwritten, because it deletes my “play_store_key”.

The same thing happens to you? Could you fix it?

Thanks in advance!

1 Like

You don’t need that file.

You only have to add the product ids in an array.

@LoLStats I asked because i’m having an error in the buy process. People are purchasing but the subscription is the “.then()” is not being executed. I have:

                    this.inAppPurchase
                        .buy(subscription.productId)
                        .then((data) => {

                            // Ex: logPurchase("US$ 10,00", "USD");
                            this.FBevents.logPurchase(subscription.price, subscription.currency);
                            this.saveSubscription(subscription.productId);
                        })
                        .catch((err) => {
                            console.log("error buy");
                            console.log(JSON.stringify(err));
                        });

buy.then() is not being executed, because the methods that are inside of it are not being executed.
So reading the repo I see that I didn’t include this “play_store_key”.

Do you know what could be the problem?

It may be because my products are subscriptions and I am trying to buy them with .buy() instead of .subscribe()?

@Colo9311
The plugin needs a manifest.json file in the root directory with the key of ‘play_store_key’, since ionic try to promote PWA it already has such a file, I deleted it and placed my own, not sure if you can just add the key to the existing file, ionic build scripts will clean the www directory and populate it with what is in the src directory.

You want to add the file with the key in the base of the src folder, the development folder

’src/manifest.json‘ not www

To test and see that it works you’ll need a device with play store,
A product in the play store,
Then upload a new version to the store
And test the product with the same version as the new version

1 Like

Thank you so much! I tried to put it in src/manifest.json and run: ionic run android
and ionic generated the manifest.json in www folder with my play store key.

As well I replaced the inAppPurchase.buy() method with .subscribe() because my products are subscriptions.

I’ll try and tell you how it was. Thank you very much!

It works, thank you!

in app purchase in ionic 2 for ios not working for me…it shows error as plugin not intalled in ios…help needed

1 Like