cordova-plugin-purchase 如何调用getPurchases?

cordova-plugin-purchase how to call getPurchases?

我已经为 iOS 和 Android 实现了 https://github.com/j3k0/cordova-plugin-purchase 插件。现在我需要知道用户以前的购买情况?我是否需要注册事件并将其包装在 setTimeout 或类似的东西中?

  store.when("product").updated(function(p) {
    if(p.loaded && p.valid && p.state === store.APPROVED){
      console.log("APPROVED", p)
      p.finish();
    }

    if(p.loaded && p.valid && p.state === store.OWNED){
      console.log('OWNED',p)
      var receipt =  JSON.parse(p.transaction.receipt)
      receipts.push(receipt)  
    }


    // never get here but it should be
    // if(p.loaded && p.valid && p.state === store.FINISHED){
    //   console.log('FINISHED',p)   
    // }

  });

  $timeout(function () {
    deferred.resolve(receipts)
  }, 10000);

首先,来自插件文档

  • When finished, a consumable product will get back to the VALID state, while other will enter the OWNED state.
  • Any error in the purchase process will bring a product back to the VALID state.
  • During application startup, products may go instantly from REGISTERED to APPROVED or OWNED, for example if they are purchased non-consumables or non-expired subscriptions.

这就是目前对我有用的东西。

//register callbacks 
store.when("hint_points_200").approved(function(product) {            
    //user gets the product and we finish. State goes back to "valid"
    product.finish();
});
store.when("hint_points_500").approved(function(product) {                
    //user gets the product and we finish. State goes to "owned". In my app, this gets called every time
    //the application restarts, the product seems to go back to the "approved" status
    product.finish();
});    


//Register "ready" callback to check the status of the products
store.ready(function(){
    p1 = store.get("200_coins");            
    p2 = store.get("500_coins");            
    console.log("p1.state:"+p1.state);
    console.log("p1.owned:"+p1.owned); //consumable. This should be "valid".
    console.log("p2.state:"+p2.state);
    console.log("p2.owned:"+p2.owned); //non-consumable. This should be "owned". (Doesn't work for me and "approved" callback gets called)

    //you should also have "p1.transactions" and "p2.transactions"
});

store.register({
    id:    "200_coins",     
    type:  store.CONSUMABLE
}); 
store.register({
    id:    "500_coins",
    type:  store.NON_CONSUMABLE
});

//refresh
store.refresh();

正如我所解释的,我发现 non-consumable 会在应用程序重新启动时触发 "approved" 回调 。我不得不将其作为我的应用程序的特例来处理。您需要进行一些测试,看看哪些适合您。