j3k0 / cordova-plugin-purchase

In-App Purchase for Cordova on iOS, Android and Windows
https://purchase.cordova.fovea.cc
1.29k stars 530 forks source link

CdvPurchase is not defined v13 #1442

Closed pw-repo closed 8 months ago

pw-repo commented 10 months ago

Observed behavior

I am using Ionic 3 project with typescript 2.6.2. I added cordova-plugin-purchase@13.6.0 to the project and want to access global object CdvPurchase, but every time I try to do it, it say that there is no global object. Include logs with store.verbosity = store.DEBUG

Expected behavior

System Info

Output of cordova info.

ARIELOZAM commented 10 months ago

Does Somebody have an answer?

pw-repo commented 10 months ago

The only way I found to get it available, to copy manually(or with scripts) www/store.js file and add it to index.html file. Then global object is found.

I am also using admob plugin which exposes the global object too, and after installing plugin it is visible, but with purchase plugin, it requires some additional tricks

dferenc-netwizzy commented 10 months ago

Having the following

import { Injectable } from "@angular/core";
import 'cordova-plugin-purchase';

@Injectable({
    providedIn: 'root'
})
export class StoreService {
  constructor(private store: CdvPurchase.Store) {
    console.log(store);
  }
}

works, although i'm using

"@capacitor/core": "^5.0.0"
"cordova-plugin-purchase": "^13.5.0",
"@angular/core": "^15.0.4",
MarcelSchuermann commented 9 months ago

Same. Will there be an official fix? Did not work for me with Cordova: 11 cordova-plugin-purchase: 13.6.0

MarcelSchuermann commented 9 months ago

Any updates on this? @j3k0? We need to update to v13 according to google.

j3k0 commented 8 months ago

Have you tried the various suggestions? It looks like it works for some users.

pw-repo commented 8 months ago

Hello. When I created the issue, it didn't work and I resolved it by manually copying www/store.js(see the answer above). But then I removed node_modules, platforms and plugins folders and reinstalled the plugin again and now it works without any manual scripts

j3k0 commented 8 months ago

It sounds more of a platform issue than a plugin issue (?).. Maybe there's a better procedure to replace "in-app-purchase2" with "cordova-plugin-purchase" on ionic. I'll close, any feel free to contribute to the documentation if you find out how to do that upgrade.

loftyduck commented 7 months ago

I was having a similar issue. It turns out I was trying to reference the CdvPurchase namespace before it is initialized. In real terms here is some code (Ionic/Angular/Typescript):

import { Platform } from '@ionic/angular';
import 'cordova-plugin-purchase';

@Injectable()
export class AppStoreService {

  // DO NOT initialize to CdvPurchase.store here 
  store: CdvPurchase.Store;

  constructor(private platform: Platform) {
    this.platform.ready().then(() => {
      // MUST WAIT for Cordova to initialize before referencing CdvPurchase namespace
      this.store = CdvPurchase.store
    });
  }
tgleb commented 5 months ago

Indeed, ensuring that we reference store only after cordova initialized the plugin is a fix :) There are some posts that recommend adding to provider block in app.module.ts, no need to do that e.g. provide: CdvPurchase.Store, useFactory: () => { return window.CdvPurchase.Store }}, it will only cause webpack error

Code below worked. `Service.ts import 'cordova-plugin-purchase';

private store:any;

constructor(private platform: Platform, private ...) { this.platform.ready().then(() => {

        this.store = CdvPurchase.store - WORKED!

        });       

    console.log('SubscriptionService.constructor() this ' + this.toString());

}

`

j3k0 commented 4 months ago

Notice that I do not recommend storing CdvPurchase.store as this.store, in case the global object changes, you'll keep a reference to an old version of the object. I believe it's better to just use CdvPurchase.store directly when you need it.