【问题标题】:Ionic 4 - In-app purchase product information can't display before purchaseIonic 4 - 购买前无法显示应用内购买产品信息
【发布时间】:2020-02-20 12:43:34
【问题描述】:

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

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

其次,如果我在应用内购买的产品不止一种,如何修改我的代码?我看过一些在线资源,但对于 ionic 和 in-app-purchase2 来说似乎没有任何效果。

我已经为这个应用内购买工作了 2 周,尤其是那些在 Apple 中耗时的设置。对上述任何帮助都将非常感激!

升级.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();
}

升级.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>

【问题讨论】:

    标签: ios angular in-app-purchase


    【解决方案1】:

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

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

    其次,只注册一次产品,永远不要再取消注册。请勿尝试在其他页面再次注册产品。

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

    【讨论】:

    • 我正在尝试在 iOS 模拟器上进行测试。是否必须在真实设备上进行测试?
    • 苹果和安卓产品ID必须不同吗?
    • 是的,必须在真机上测试。
    • 苹果和安卓产品ID可以相同。
    • 我刚刚给出了我的答案,您可以在您的帖子中尝试。
    猜你喜欢
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2020-02-17
    相关资源
    最近更新 更多