cordova-plugin-购买如何调用getPurchases?

时间:2015-10-12 10:40:28

标签: cordova in-app-purchase ionic

我已经为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);

1 个答案:

答案 0 :(得分:1)

首先,来自插件文档

  
      
  • 完成后,耗材将返回VALID状态,而其他产品将进入OWNED状态。
  •   
  • 购买过程中的任何错误都会使产品返回VALID状态。
  •   
  • 在应用程序启动期间,产品可能会立即从“已注册”更改为“已批准”或“已拥有”,例如,如果已购买   非消耗品或未过期的订阅。
  •   

这就是我目前正在为我工​​作的事情。

//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();

正如我所解释的那样,我发现n 消耗品会触发"批准"应用程序重启时回调。作为我的应用程序的一个特例,我不得不处理它。你需要做一些测试,看看哪些对你有用。