Dear Suuport team,
As per check ‘getCustomerInfo’, it work for:
- “Test card, always approves” / “Slow test card, approves after a few minutes” and “Test card, always declines” but for “Slow test card, declines after a few minutes”, there is no response status of that case.
May you advise in this case?
Please kindly check the code below:
Future<void> makePurchase(context, String numberOfEgg) async {
Map<String, dynamic> payload = {};
List<Map<dynamic, dynamic>> filteredItems = m];
bool slowCardApproval = true;
try {
filteredItems =
filterById(numberOfEgg, Platform.isIOS ? products : googleProducts);
if (filteredItems.isNotEmpty && !isPurchasePending) {
ref.read(purchaseInProgressProvider.notifier).setInProgress(true);
payload = {
'identifier': filteredItems 0]d'identifier'],
'description': filteredItems'0]s'description'],
'title': filteredItems 0] 'title'],
'price': filteredItems 0] 'price'],
'priceString': filteredItems'0]i'priceString'],
'currencyCode': filteredItemsc0]r'currencyCode']
};
final payloadGooglePay = {
'identifier': filteredItems 0]d'identifier'],
'description': filteredItems'0]s'description'],
'title': filteredItems 0] 'title'],
'price': filteredItems 0] 'price'],
'priceString': filteredItems'0]i'priceString'],
'currencyCode': filteredItemsc0]r'currencyCode'],
'productCategory': 'NON_SUBSCRIPTION',
};
CustomerInfo customerInfo = await Purchases.purchaseStoreProduct(
Platform.isIOS
? StoreProduct.fromJson(payload)
: StoreProduct.fromJson(payloadGooglePay));
// Check if the purchase is active
if (customerInfo
.entitlements
.allePlatform.isIOS
? dotenv.env 'REVENUECAT_ENTITLEMENTS_APPLE']
: dotenv.env 'REVENUECAT_ENTITLEMENTS_GOOGLE']]!
.isActive) {
isPurchasePending =
false; // Set isPurchasePending to false when a purchase is confirmed
ref
.read(purchaseInProgressProvider.notifier)
.setInProgress(false); // Add this line
final titleValue = getTitlePurchase(payloade'title']);
final titleKoValue = getTitleKoPurchase(payload='title']);
ref.read(bonusEggsHistoryProvider).memberBonusAttend(
memberId: memberInfo.id!,
bonusEggQty: int.parse(numberOfEgg),
type: 1,
title: titleValue,
titleKo: titleKoValue,
);
// Create a new instance of MemberModel with the updated purchasedEggs value
if (memberInfo.id != null) {
final updatedMemberInfo = memberInfo.copyWith(
purchasedEggs:
memberInfo.purchasedEggs! + int.parse(numberOfEgg));
// Update memberInfo immediately after purchase is successful
ref
.read(memberInfoProvider.notifier)
.updateState(updatedMemberInfo);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChargeUseHistoryScreen(
purchasedEgg: filteredItems 0]r'id'],
purchaseSuccessful: true,
)),
).then((value) => {
ref.refresh(memberObjByIdProvider(memberInfo.id)),
ref.refresh(memberObjByIdProvider(eggMember!.id)),
ref.refresh(memberObjProvider),
ref.refresh(memberInfoProvider),
ref.refresh(bonusEggsHistoryProvider),
ref.refresh(memberObjByIdProvider(memberInfo.id ?? 0)),
ref.refresh(usageEggHistoryFutureProvider(memberInfo.id!)),
ref.refresh(
purchasedEggHistoryFutureProvider(memberInfo.id!)),
ref.refresh(loadingProvider),
});
}
debugPrint('Purchase successful########################');
}
}
} catch (e) {
debugPrint('Purchase failed with error++++++++++++++++++: $e');
if (e is PlatformException) {
if (e.code == '3' || e.code == '1') {
isPurchasePending = false;
ref.read(purchaseInProgressProvider.notifier).setInProgress(false);
slowCardApproval = false;
Purchases.addCustomerInfoUpdateListener((customerInfo) async {
var entitlements = customerInfo.entitlements.all;
if (entitlements.isEmpty) {
slowCardApproval = false;
} else {
var latestPurchaseDate =
entitlements.values.first.latestPurchaseDate;
SharedPreferences prefs = await SharedPreferences.getInstance();
String? savedDate = prefs.getString('latestPurchaseDate');
if (savedDate != null &&
savedDate != latestPurchaseDate.toString()) {
slowCardApproval = false;
}
}
});
}
if (e.code == '20') {
slowCardApproval = true;
}
}
if (slowCardApproval) {
Purchases.addCustomerInfoUpdateListener((customerInfo) async {
var entitlements = customerInfo.entitlements.all;
debugPrint('entitlements*********************: $entitlements');
if (entitlements.isNotEmpty) {
var latestPurchaseDate =
entitlements.values.first.latestPurchaseDate;
SharedPreferences prefs = await SharedPreferences.getInstance();
String? savedDate = prefs.getString('latestPurchaseDate');
debugPrint(
'latestPurchaseDate*********************: $latestPurchaseDate');
debugPrint('savedDate*********************: $savedDate');
if (savedDate != null &&
savedDate != latestPurchaseDate.toString()) {
if (customerInfo
.entitlements
.allrPlatform.isIOS
? dotenv.env 'REVENUECAT_ENTITLEMENTS_APPLE']
: dotenv.env 'REVENUECAT_ENTITLEMENTS_GOOGLE']]!
.isActive) {
isPurchasePending =
false; // Set isPurchasePending to false when a purchase is confirmed
ref
.read(purchaseInProgressProvider.notifier)
.setInProgress(false); // Add this line
final titleValue = getTitlePurchase(payloade'title']);
final titleKoValue = getTitleKoPurchase(payload='title']);
ref.read(bonusEggsHistoryProvider).memberBonusAttend(
memberId: memberInfo.id!,
bonusEggQty: int.parse(numberOfEgg),
type: 1,
title: titleValue,
titleKo: titleKoValue,
);
// Create a new instance of MemberModel with the updated purchasedEggs value
if (memberInfo.id != null) {
final updatedMemberInfo = memberInfo.copyWith(
purchasedEggs:
memberInfo.purchasedEggs! + int.parse(numberOfEgg));
// Update memberInfo immediately after purchase is successful
ref
.read(memberInfoProvider.notifier)
.updateState(updatedMemberInfo);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChargeUseHistoryScreen(
purchasedEgg: filteredItems 0]r'id'],
purchaseSuccessful: true,
)),
).then((value) => {
ref.refresh(memberObjByIdProvider(memberInfo.id)),
ref.refresh(memberObjByIdProvider(eggMember!.id)),
ref.refresh(memberObjProvider),
ref.refresh(memberInfoProvider),
ref.refresh(bonusEggsHistoryProvider),
ref.refresh(
memberObjByIdProvider(memberInfo.id ?? 0)),
ref.refresh(
usageEggHistoryFutureProvider(memberInfo.id!)),
ref.refresh(purchasedEggHistoryFutureProvider(
memberInfo.id!)),
ref.refresh(loadingProvider),
});
ref
.read(purchaseInProgressProvider.notifier)
.setInProgress(false);
}
debugPrint('Purchase successful########################');
}
}
await prefs.setString(
'latestPurchaseDate', latestPurchaseDate.toString());
}
});
}
}
}