Flutter: purchases stream is empty and attempted purchase throws 'pending transaction for the same product identifier'

Viewed 2391

I'm using the Flutter in_app_purchase plugin, v0.3.3+1.

While testing on iOS, I began a purchase but cancelled mid-way through. After that, whenever I try the purchase again, I get an exception with this message:

There is a pending transaction for the same product identifier

I have a listener setup on the purchase stream (code below) to complete the purchases. But the stream is not emitting any events.

_purchaseListener = InAppPurchaseConnection.instance.purchaseUpdatedStream.listen((purchases) {
  purchases.forEach((purchase) async {
    if (purchase.status == PurchaseStatus.purchased) //...
    if (purchase.pendingCompletePurchase) {
      //Complete purchase (retrying as Google Play refunds after 3 days if this does not succeed)
      retry<BillingResultWrapper>(() async {
        final completion = await InAppPurchaseConnection.instance.completePurchase(purchase);
        const errors = {BillingResponse.error, BillingResponse.serviceUnavailable};
        if (errors.contains(completion.responseCode)) throw Exception();
        return completion;
      });
    }
  });
});
2 Answers

you must clean the pending transaction first

var paymentWrapper = SKPaymentQueueWrapper();
var transactions = await paymentWrapper.transactions();
transactions.forEach((transaction) async {
  await paymentWrapper.finishTransaction(transaction);
});

The stream was not emitting any events in debug mode, so the purchase was never coming through and being completed.

Testing in release mode, it works perfectly.

Related