Hi,
I have created a custom action from which I am triggering in app purchase for a specific product id that I have created in appstoreconnect. I have a listener define in the custom action which listens to the purchase updated and ideally based on successful purchase it should return the purchase ID.
Issue: The In App purchase is getting triggered successfully and I am able to make the purchase also. But the custom action is not returning and retriggering the IAP again and again.
If you have done similar trigger with FF, please suggest what am I missing.
Here's the function:
import 'package:in_app_purchase/in_app_purchase.dart'; // Ensure this is imported
import 'dart:async'; // Required for Completer
Future<String> triggetIAP(String productId) async {
final InAppPurchase _iap = InAppPurchase.instance;
// Attach listener before initiating the purchase
final Stream<List<PurchaseDetails>> purchaseStream = _iap.purchaseStream;
final Completer<String> completer = Completer();
purchaseStream.listen((List<PurchaseDetails> purchaseDetailsList) {
for (var purchaseDetails in purchaseDetailsList) {
if (purchaseDetails.productID == productId) {
print('Purchase Stream Update: ${purchaseDetails.toString()}');
switch (purchaseDetails.status) {
case PurchaseStatus.purchased:
print(
'Purchase successful for product: ${purchaseDetails
.productID}');
// Acknowledge the purchase
_iap.completePurchase(purchaseDetails);
// Complete the purchase
completer.complete(purchaseDetails.purchaseID ??
'Purchase completed but ID is null');
break;
case PurchaseStatus.pending:
print('Purchase is pending...');
// Handle any UI updates needed for pending status, like showing a spinner
break;
case PurchaseStatus.error:
print('Purchase error: ${purchaseDetails.error}');
completer.complete('Purchase error: ${purchaseDetails.error}');
break;
case PurchaseStatus.restored:
print(
'Purchase restored for product: ${purchaseDetails.productID}');
// Handle the restoration if applicable
break;
default:
print('Unhandled purchase status: ${purchaseDetails.status}');
break;
}
}
}
});
// Check if the IAP service is available
final bool available = await _iap.isAvailable();
if (!available) {
print('In-app purchases are not available.');
return 'In-app purchases are not available';
}
// Query available products
final ProductDetailsResponse response =
await _iap.queryProductDetails({productId});
if (response.notFoundIDs.isNotEmpty) {
print('Product not found.');
return 'Product not found';
}
// Get the product details
final ProductDetails product = response.productDetails.first;
// Create a purchase param
final PurchaseParam purchaseParam = PurchaseParam(productDetails: product);
// Initiate the purchase
await _iap.buyConsumable(purchaseParam: purchaseParam, autoConsume: true);
// Return the result from the completer
return completer.future;
}
Note: My Apple app is rejected because I am using Razorpay for payment and not in-app purchases by apple. And I am new to flutter (not flutterflow)
I don't want to use revenuecat, I have my own backend to handle subscriptions.