3

Implement InApp purchase(non-consumable) in flutter using flutter_inapp_purchase . I have three plan 1 month, 6 month and 1 year, now i am checking is if 1 month plan then check transactionData and add 30 day to check expire date same for other 2 plan, but problem is when user is on trial period then how can i check trial period expire date

List<String> storeItems = ['onemonth', 'sixmonth', 'oneyear'];
List<IAPItem> subscriptions = [];
List<PurchasedItem> purchasesSubscriptions = [];
StreamSubscription _purchaseUpdatedSubscription;
StreamSubscription _purchaseErrorSubscription;
StreamSubscription _conectionSubscription;
initPurchaseState(SportsProvider sportsProvider) async {

print("initPurchaseState");
await FlutterInappPurchase.instance.initConnection;
if (!mounted) return;

_conectionSubscription =
    FlutterInappPurchase.connectionUpdated.listen((connected) {
  print('connected: $connected');
});

_purchaseUpdatedSubscription =
    FlutterInappPurchase.purchaseUpdated.listen((productItem) {
  var data = jsonDecode(productItem.transactionReceipt);
  int purchaseState = data["purchaseState"];
  String productId = data["productId"];
  String purchaseToken = data["purchaseToken"];

  print("Data : ${data.toString()}, ${purchaseState}, ${productId}, ${purchaseToken}");
  print('purchase-updated: $productItem');
  FlutterInappPurchase.instance.finishTransaction(productItem);
});

_purchaseErrorSubscription =
    FlutterInappPurchase.purchaseError.listen((purchaseError) {
  print('purchase-error: $purchaseError');
});

subscriptions =
    await FlutterInappPurchase.instance.getSubscriptions(storeItems);
//print("subscriptions: ${subscriptions}");
//subscriptions.forEach((f){print("subscription : ${f.freeTrialPeriodAndroid}");});
subscriptions.forEach((f){print("subscriptionPeriodAndroid : ${f.subscriptionPeriodAndroid}");});
subscriptions.forEach((f){print("subscriptionPeriodAndroid : ${f.toString()}");});
subscriptions.forEach((f){print("subscriptionPeriodAndroid : ${f.originalJson}");});


//Here we check for 1 month subscription is available or not by passig productId and days
var isSubscribedForMonth = await FlutterInappPurchase.instance.checkSubscribed(sku:subscriptions[0].productId,duration: Duration(days: 30));
print(isSubscribedForMonth);
//Here we check for 1 month subscription is available or not by passig productId and days
var isSubscribedForSixMonth = await FlutterInappPurchase.instance.checkSubscribed(sku:subscriptions[1].productId,duration: Duration(days: 180));
print(isSubscribedForSixMonth);
//Same for one year 
//But how can identify the current subscription is on trial period?  

// code for already subscribed
}

Have any way to identify the the current period is trial or regular.

I have tried so may solution but not get any relevant solution for my question,

please help me for the problem

if any more clarification required then just comment

Kalpesh Dabhi
  • 575
  • 4
  • 12

0 Answers0