Ionic 4-购买前无法显示应用内购买产品信息

时间:2020-02-20 12:43:34

标签: ios angular in-app-purchase

我有google,并遵循有关应用内购买的在线教程。我已经完成了苹果平台上的所有设置,现在正在测试应用内购买功能。该功能似乎正常工作,但我现在正努力解决2个问题。

首先,我使用store.refresh()和store.get()命令从Apple的服务器中获取数据。但是,我只能在按“购买”按钮后才能看到获取的产品信息。从Xcode控制台中,我可以看到已经加载的产品信息(如定价等),但是在按下购买按钮之前无法在视图页面上显示。我有什么想念吗?

第二,如果我在应用内购买中有多个产品,该如何修改我的代码?我看到了一些在线资源,但对于适用于ionic和应用内购买2的产品似乎一无所获。

我已经进行了2个星期的应用内购买,尤其是那些耗时的Apple设置。上面的任何帮助将是巨大的,并高度赞赏!

upgrade.ts文件

 import { NavController, Platform } from '@ionic/angular';
 import { InAppPurchase2, IAPProduct } from '@ionic-native/in-app-purchase-2/ngx';

 @Component({
   selector: 'app-upgrade',
   templateUrl: './upgrade.page.html',
   styleUrls: ['./upgrade.page.scss'],
 })

 export class UpgradePage {

   setupReady = 'Not yet';
   public prod: any = {};

   public product: any = {
       name: 'Upgrade to Premium 12 months',
       appleProductId: 'upgrade_12months',
       googleProductId: 'android.test.purchased'
   };

  constructor(private store: InAppPurchase2,
            public platform: Platform,
             ) {
    this.platform.ready().then(() => {
        this.configurePurchasing();
        this.setupReady = 'Ready';
    });
  }

  ionViewDidEnter() {
    this.prod = this.store.get('upgrade_12months');
  }

configurePurchasing() {
  if (!this.platform.is('cordova')) { return; }
  let productId;
  try {
    if (this.platform.is('ios')) {
      productId = this.product.appleProductId;
    } else if (this.platform.is('android')) {
      productId = this.product.googleProductId;
    }

    // Register Product
    // Set Debug High
    this.store.verbosity = this.store.DEBUG;

    // Register the product with the store
    this.store.register({
        id: 'upgrade_12months',
        alias: 'upgrade_12months',
        type: this.store.PAID_SUBSCRIPTION
    });

    this.registerHandlers(productId);
    this.store.refresh();
    this.prod = this.store.get(productId);

    InAppPurchase2.getPlugin().ready().then((status) => {
      console.log(JSON.stringify(this.store.get(productId)));
      console.log('Store is Ready: ' + JSON.stringify(status));
      console.log('Products: ' + JSON.stringify(this.store.products));
    });

    // Errors On The Specific Product
    this.store.when(productId).error( (error) => {
      alert('An Error Occured' + JSON.stringify(error));
    });
    // Refresh Always
    console.log('Refresh Store');
    this.store.refresh();
  } catch (err) {
    console.log('Error On Store Issues' + JSON.stringify(err));
  }
}

 registerHandlers(productId) {
  // Handlers
  this.store.when(productId).approved( (product: IAPProduct) => {
    alert('Approved!');
    // Purchase was approved
    product.finish();
  });

  this.store.when(productId).registered( (product: IAPProduct) => {
    console.log('Registered: ' + JSON.stringify(product));
    console.log(` Registered2 ${product.owned}`);
  });

  this.store.when(productId).updated( (product: IAPProduct) => {
    console.log('Loaded' + JSON.stringify(product));
  });

  this.store.when(productId).cancelled( (product) => {
    alert('Purchase was Cancelled');
  });

  // Overall Store Error
  this.store.error( (err) => {
    console.log('Store Error ' + JSON.stringify(err));
  });
 }


async purchase() {
  if (!this.platform.is('cordova')) { return; }
  let productId;

  if (this.platform.is('ios')) {
    productId = this.product.appleProductId;
  } else if (this.platform.is('android')) {
    productId = this.product.googleProductId;
  }

  this.registerHandlers(productId);
  try {
    const product = this.store.get(productId);
    console.log('Product Info: ' + JSON.stringify(product));
    this.store.order(productId).then((p) => {
      alert('Purchase Action Detected');
      this.registerHandlers(productId);
    }).catch((e: string) => {
      alert('Error Ordering From Store' + e);
    });
  } catch (err) {
    console.log('Error Ordering ' + JSON.stringify(err));
  }
}

restore() {
    this.store.refresh();
}

upgrade.html

 <ion-content padding>
   <ion-row text-center>
     <ion-col>Status:  <b>{{ this.setupReady }}</b></ion-col>
   </ion-row> 

 <br>
 {{ ' Description: '}} {{ this.prod.description }}
 <br>
 {{ 'Price:' }} {{ this.prod.price }}
 <br>
 <div margin-vertical text-center>

          {{ this.prod.title }}
     <ion-button (click)='purchase()' expand="block">

        {{ ' Buy now - ' }} 
        {{ this.prod.price }}

     </ion-button>
 </div>   

   <ion-button full icon-left color="secondary" (click)="restore()">
       <ion-icon name="refresh"></ion-icon>Restore Purchases
   </ion-button>
 </ion-content>

1 个答案:

答案 0 :(得分:0)

我终于解决了这个问题,因为关于应用内购买的在线教程可能都没有提到这些关键点。

首先,您必须在应用启动后立即注册产品和处理程序,即在启动页面上,而不是在向客户显示应用内购买产品的页面上注册。

第二,仅一次注册产品,再也不要取消注册。请勿尝试在其他页面上再次注册产品。

这些应该可以解决您在应用内购买集成中遇到的许多问题。希望这对其他在应用内购买编码方面苦苦挣扎的人有所帮助。