React Native IAP自动续订订阅检查到期

时间:2019-11-23 15:40:43

标签: ios react-native in-app-purchase react-native-ios in-app-subscription

我正在尝试使用https://www.npmjs.com/package/react-native-iap/v/4.0.8在iOS上恢复和检测自动续订的终止 通过调用getAvailablePurchases(),每次调用此函数时,我都会得到不同数量的购买(例如7,下一次23,然后4 ..)。这对我来说毫无意义。我知道订阅可以每5分钟续订一次。 我的想法是获取最新购买的商品,只需检查收据中的过期日期即可。 (目前,我正在解析所有这些,这不是最佳实践)。另一件事是,即使只有一个活跃交易,我仍会检测到具有不同transactionReceipts的多个活跃交易。

RNIap.getAvailablePurchases().then((purchases) => {
            if(purchases != undefined && purchases.length > 0){
                alert(purchases.length + ' purchases');
                purchases.forEach((purchase) => {
                    const receiptBody = {
                        'receipt-data': purchase.transactionReceipt,
                        'password': appSubscriptionId
                    };
                    RNIap.validateReceiptIos(receiptBody, true).then((result) => { // false for production
                        let from, to, lastRenewal = '';
                        if(result.status == 0){ // 0 means receipt is correct..
                            result.latest_receipt_info.forEach((info) => { 
                                if(this.dateIsInFuture(info.expires_date)){
                                    to = moment.utc(info.expires_date, "YYYY-MM-DD HH:mm:ss").toString();
                                    from = moment.utc(info.original_purchase_date, "YYYY-MM-DD HH:mm:ss").toString();
                                    lastRenewal = moment.utc(info.purchase_date, "YYYY-MM-DD HH:mm:ss").toString();
                                    this.setState({ premium: true, from: from, to: to, lastRenewal: lastRenewal});
                                }else{
                                    alert('nicht aktiv');
                                }
                            });
                        }
                    }); 
                }); 
            }else{
                alert('No purchases found');
            }
        });

1 个答案:

答案 0 :(得分:1)

此功能提供所有可用的购买,并且receipt: purchases[0].transactionReceipt,行还返回最新的订阅。我认为这可以解决您的问题。

getAvailablePurchases = async () => {
    try {
      console.info(
        'Get available purchases (non-consumable or unconsumed consumable)',
      );
      const purchases = await RNIap.getAvailablePurchases();
      console.info('Available purchases :: ', purchases);
      if (purchases && purchases.length > 0) {
        this.setState({
          availableItemsMessage: `Got ${purchases.length} items.`,
          receipt: purchases[0].transactionReceipt,
        });

        console.log(this.state.availableItemsMessage);
      }
    } catch (err) {
      console.warn(err.code, err.message);
      Alert.alert(err.message);
    }
  };