Solved

Pending Payment Listener Status

  • 23 April 2024
  • 2 replies
  • 45 views

Badge

Hi, 

I would like to seek for support regarding to slow test card, approves after a few minutes. I would like to know how to get status success or failure after pending a few minutes. 

Thank you, 

icon

Best answer by kaitlin 25 April 2024, 18:31

View original

This post has been closed for comments

2 replies

Userlevel 4
Badge +6

Hey @peenon,

I’d recommend either calling `getCustomerInfo` periodically or setting up a listener for changes to a customer’s purchase info: https://www.revenuecat.com/docs/customers/customer-info#listening-for-customerinfo-updates

 

Badge

Hello @kaitlin 

Thank you so much for your answer, 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 = [];
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]['identifier'],
'description': filteredItems[0]['description'],
'title': filteredItems[0]['title'],
'price': filteredItems[0]['price'],
'priceString': filteredItems[0]['priceString'],
'currencyCode': filteredItems[0]['currencyCode']
};

final payloadGooglePay = {
'identifier': filteredItems[0]['identifier'],
'description': filteredItems[0]['description'],
'title': filteredItems[0]['title'],
'price': filteredItems[0]['price'],
'priceString': filteredItems[0]['priceString'],
'currencyCode': filteredItems[0]['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
.all[Platform.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(payload['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]['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
.all[Platform.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(payload['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]['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());
}
});
}
}
}