cordova-plugin-purchase / iaptic - cant get in app purchase to work

283 Views Asked by At

I have had to update my in app purchase functionality, due to google billing forced updates, and am such using v13 of cordova-plugin-purchase.

I simply cannot get this to work or even return anything.

My current code:

import 'cordova-plugin-purchase';

const { store, ProductType, Platform } = CdvPurchase;

const iaptic = new CdvPurchase.Iaptic({
  appName: "com.app",
  apiKey: "XXX-XXX-XXX",
});

store.validator = iaptic.validator;

document.addEventListener("deviceready", function(){
  store.register([
    {
      id: "monthly",
      type: ProductType.PAID_SUBSCRIPTION,
      platform: Platform.GOOGLE_PLAY,
    }, {
    id: "annual",
    type: ProductType.PAID_SUBSCRIPTION,
    platform: Platform.GOOGLE_PLAY,
    }, {
      id: "quarterly",
      type: ProductType.PAID_SUBSCRIPTION,
      platform: Platform.GOOGLE_PLAY,
      }
  ])
  
  
  store.initialize();
 
  }, false);


const purchaseProduct = () => {
  const product = store.get('monthly', Platform.GOOGLE_PLAY);
  console.log("PRODUCT: ", product)
}

Button onclick is purchaseProduct ... in the console for Android, all I get is

Msg: PRODUCT:  undefined

As I am running 3 subscription products, I am trying to call the purchase when the click is made on the appropriate button - but again it is not doing anything.

Clearly I am missing something - can anyone assist?

2

There are 2 best solutions below

0
wrappingduke On

I've run into the same issue, so I'm not certain how much this will help but it appears that a Google Play Service account maybe needed for the iaptic API. Per iaptic, the service account is needed for their servers to communicate with Google. Although, Google states that "The API access page has gone away", here are a couple links that may help:

https://developers.google.com/android-publisher/getting_started#service-account https://www.iaptic.com/documentation/setup/connect-with-google-publisher-api

I'm currently trying the above and waiting 24 hours, per iaptic to see if it will work.

HTH

0
muzudre On

If you are using react js then you need to call:

import "cordova-plugin-purchase/www/store.js";

Also try to do this way:

const { store, ProductType, Platform } = window.CdvPurchase;

Using "window" as a global in case if a normal way will not work.


Here is a simple micro example which works - for NON_CONSUMABLE in-app purchase:

import React, { useState, useEffect } from "react";
import { Capacitor } from "@capacitor/core";
import "cordova-plugin-purchase/www/store.js";

const { store, ProductType, Platform } = window.CdvPurchase;
const platformName = Capacitor.getPlatform() === 'android' ? Platform.GOOGLE_PLAY : Platform.APPLE_APPSTORE;
const productId = "productid";

const PremiumContainer = () => {
  const [iapProduct, setIapProduct] = useState(null);

  useEffect(() => {
    store.verbosity = store.DEBUG;

    store.register({
      type: ProductType.NON_CONSUMABLE,
      id: productId,
      platform: platformName,
    });

    store.when().productUpdated(refreshUI).approved(finishPurchase);
    store.initialize([platformName, ProductType.NON_CONSUMABLE]);
  }, []);

  const finishPurchase = (p) => {
    p.finish();
    refreshUI();
  };

  const refreshUI = () => {
    setIapProduct(
      store.get(
        productId,
        platformName,
        ProductType.NON_CONSUMABLE
      )
    );
  };

  const orderProduct = () => {
    iapProduct.getOffer().order();
  };

  const restoreProduct = () => {
    store.restorePurchases();
  }

  return (
    <div>
      <h1>{iapProduct?.title}</h1>
      <p>{(iapProduct?.description ? `${iapProduct.description || ''}` : '')}</p>
      <pre>{iapProduct?.offers[0].pricingPhases[0].price}</pre>
      <button className="btn btn-primary btn-block mt-1" onClick={() => orderProduct()}>Purchase Product</button>
      <button className="btn btn-link btn-block mt-1" onClick={() => restoreProduct()}>Restore Product</button>

    </div>
  );
};

export default PremiumContainer;

Here are other helpful links where you can find code for subscription base code:

  1. GitHub issue discussion: https://github.com/j3k0/cordova-plugin-purchase/issues/1437
  2. A micro example from developer using PAID_SUBSCRIPTION: https://gist.github.com/j3k0/8b7e72b6af43572651328c613ac86d59
  3. Example from developer using .ts and more complex: https://github.com/j3k0/cordova-subscription-example/blob/main/with-server/www/ts/subscription-service.ts

I've found this resource useful in the past, so I thought you might too.