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

1,925

you must clean the pending transaction first

var paymentWrapper = SKPaymentQueueWrapper();
var transactions = await paymentWrapper.transactions();
transactions.forEach((transaction) async {
  await paymentWrapper.finishTransaction(transaction);
});
Share:
1,925
Xander
Author by

Xander

Designer & Developer of exceptional user experiences. Creator of... Grammapp – an app for learning German grammar like a native. Concurrency – the currency converter for globetrotters. View full portfolio

Updated on December 05, 2022

Comments

  • Xander
    Xander over 1 year

    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;
          });
        }
      });
    });
    
  • Leonardo de Jesus
    Leonardo de Jesus almost 4 years
    how did you manage to test that in release mode with the sandbox user?
  • Zalán Meggyesi
    Zalán Meggyesi over 3 years
    Perhaps more to the point, how did you manage to clear the queue once you figured out what was going wrong?
  • Eray Hamurlu
    Eray Hamurlu over 2 years
    How can i acces SKPaymentQueueWrapper() . I have got an error The function 'SKPaymentQueueWrapper' isn't defined.
  • Eray Hamurlu
    Eray Hamurlu over 2 years
    I fixed -> import 'package:in_app_purchase_ios/store_kit_wrappers.dart';