Skip to content

[in_app_purchase ][iOS] When upgrading in_app_purchase 3.1.13 to 3.2.0, a fatal crash occurs if I manually call SKPaymentQueueWrapper().finishTransaction #154763

@himotou

Description

@himotou

What package does this bug report belong to?

in_app_purchase

What target platforms are you seeing this bug on?

iOS

Have you already upgraded your packages?

Yes

Dependency versions

pubspec.lock
 in_app_purchase:
    dependency: "direct main"
    description:
      name: in_app_purchase
      sha256: "960f26a08d9351fb8f89f08901f8a829d41b04d45a694b8f776121d9e41dcad6"
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "3.2.0"
  in_app_purchase_android:
    dependency: transitive
    description:
      name: in_app_purchase_android
      sha256: "4c3f46f736a31902ec5392a75f41ea1ad292f27db00ed51153f3912d076d0efa"
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.3.6+5"
  in_app_purchase_platform_interface:
    dependency: transitive
    description:
      name: in_app_purchase_platform_interface
      sha256: "1d353d38251da5b9fea6635c0ebfc6bb17a2d28d0e86ea5e083bf64244f1fb4c"
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.4.0"
  in_app_purchase_storekit:
    dependency: "direct main"
    description:
      name: in_app_purchase_storekit
      sha256: e9a408da11d055f9af9849859e1251b2b0a2f84f256fa3bf1285c5614a397079
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.3.18+1"

Steps to reproduce

I used "in_app_purchase: ^3.1.13 in_app_purchase_storekit: ^0.3.8+1" before, which has been running stably for more than half a year. Recently, the SDK was upgraded and the plug-in version was updated to "in_app_purchase: ^3.2.0 in_app_purchase_storekit: ^0.3.18+1", and then the app crashed when it was invoked to pay.

Expected results

After upgrading the version, I did not make any code changes and expected the payment to continue to work normally.

Actual results

在唤起支付后,程序崩溃。以下是报错内容:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot finish a purchasing transaction'
*** First throw call stack:
(0x190da0f20 0x188c2b2b8 0x190e9f6dc 0x1ba2127bc 0x101a33a2c 0x101a31888 0x101a42890 0x101a42b78 0x101a3d214 0x10599cb9c 0x10541c58c 0x10277cb98 0x10277e7bc 0x10278ed58 0x10278e90c 0x190d73710 0x190d70914 0x190d6fcd8 0x1d57bd1a8 0x1933a9ae8 0x19345dd98 0x1003d8e24 0x1b4547154)
libc++abi: terminating due to uncaught exception of type NSException

Code sample

Code sample
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter_evdo_app/entity/my.dart';
import 'package:flutter_evdo_app/generated/locales.g.dart';
import 'package:flutter_evdo_app/http/apis/my.dart';
import 'package:flutter_evdo_app/utils/function.dart';
import 'package:flutter_evdo_app/utils/shared_preferences.dart';
import 'package:flutter_evdo_app/values/defines.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:in_app_purchase_storekit/in_app_purchase_storekit.dart';
import 'package:in_app_purchase_storekit/store_kit_wrappers.dart';

class ApplePurchaseUtils {
  static void pay(
      {required PurchaseParam purchaseParam,
      required InAppPurchase inAppPurchase,
      required String order}) async {
    inAppPurchase.buyNonConsumable(purchaseParam: purchaseParam);
    var transactions = await SKPaymentQueueWrapper().transactions();
    for (var skPaymentTransactionWrapper in transactions) {
      SKPaymentQueueWrapper().finishTransaction(skPaymentTransactionWrapper);
    }
    SharedPreferencesUtils.setString(Defines.appleOrderInfo, order);
  }

  static dispose({required InAppPurchase inAppPurchase}) {
    //苹果取消代理
    final InAppPurchaseStoreKitPlatformAddition iosPlatformAddition =
        inAppPurchase
            .getPlatformAddition<InAppPurchaseStoreKitPlatformAddition>();
    iosPlatformAddition.setDelegate(null);
    ApplePurchaseUtils.subscription?.cancel();
    ApplePurchaseUtils.subscription = null;
  }

  static PurchaseParam? getPurchaseParam(
      {required List<ProductDetails> productDetails,
      required String productId}) {
    if (productDetails.isEmpty) {
      SmartDialog.showToast("${LocaleKeys.member_noAvailableProducts.tr}!");
      return null;
    }

    List<ProductDetails>? payProducts =
        productDetails.where((element) => element.id == productId).toList();
    if (payProducts.isEmpty) {
      SmartDialog.showToast(
          "${LocaleKeys.member_subscriptionPackageException.tr}!");
      return null;
    }
    PurchaseParam purchaseParam = PurchaseParam(
      productDetails: payProducts[0],
    );
    return purchaseParam;
  }

  static void _listenToPurchaseUpdated(
      List<PurchaseDetails> purchaseDetailsList, ParamVoidCallback onSuccess,
      {required InAppPurchase inAppPurchase}) async {
    for (final PurchaseDetails purchaseDetails in purchaseDetailsList) {
      if (purchaseDetails.status == PurchaseStatus.pending) {
        SmartDialog.showLoading();
      } else {
        SmartDialog.dismiss();
        if (purchaseDetails.status == PurchaseStatus.error) {
          SmartDialog.showToast("${LocaleKeys.member_paymentException.tr}!");
        } else if (purchaseDetails.status == PurchaseStatus.purchased ||
            purchaseDetails.status == PurchaseStatus.restored) {
          AppStorePurchaseDetails appstoreDetail =
              purchaseDetails as AppStorePurchaseDetails;
          String appleOrderReceipt =
              appstoreDetail.verificationData.serverVerificationData;
          SharedPreferencesUtils.setString(
              Defines.appleOrderReceipt, appleOrderReceipt);
          String orderSn = await SharedPreferencesUtils.getString(
              Defines.appleOrderInfo, '');
          debugPrint("订单状态:${purchaseDetails.status}");
          if (orderSn.isEmpty) return;
          await MyApi.applePayVerify(
              orderSn, appleOrderReceipt, Defines.isDebug);
          SharedPreferencesUtils.setString(Defines.appleOrderInfo, "");
          SharedPreferencesUtils.setString(Defines.appleOrderReceipt, "");
          SmartDialog.showToast(LocaleKeys.member_paymentSuccessful.tr);

          onSuccess();
        }

        if (purchaseDetails.pendingCompletePurchase) {
          await inAppPurchase.completePurchase(purchaseDetails);
        }
      }
    }
  }

  static StreamSubscription<List<PurchaseDetails>>? subscription;
  static List<String> getProductIds(List<VipGoodListElement>? list) {
    List<String> products = [];
    if (list == null || list.isEmpty) return products;
    for (var i = 0; i < list.length; i++) {
      VipGoodListElement goods = list[i];
      products.add(goods.productId);
    }
    return products;
  }

  ///根据商品id获取苹果商品详情
  static initApplePay(
    List<VipGoodListElement>? goods, {
    required ParamVoidCallback onPaySuccess,
    required ParamSingleCallback<List<ProductDetails>> onInitSuccess,
    required InAppPurchase inAppPurchase,
  }) async {
    if (goods == null || goods.isEmpty) return;
    List<String> productIds = getProductIds(goods);
    final Stream<List<PurchaseDetails>> purchaseUpdated =
        inAppPurchase.purchaseStream;
    subscription =
        purchaseUpdated.listen((List<PurchaseDetails> purchaseDetailsList) {
      _listenToPurchaseUpdated(purchaseDetailsList, onPaySuccess,
          inAppPurchase: inAppPurchase);
    }, onDone: () {
      subscription?.cancel();
      subscription = null;
    }, onError: (Object error) {
      // handle error here.
    });
    final bool isAvailable = await inAppPurchase.isAvailable();
    if (!isAvailable) {
      SmartDialog.showToast(LocaleKeys.member_paymentUnavailable.tr);
    }
    final InAppPurchaseStoreKitPlatformAddition iosPlatformAddition =
        inAppPurchase
            .getPlatformAddition<InAppPurchaseStoreKitPlatformAddition>();
    await iosPlatformAddition.setDelegate(ExamplePaymentQueueDelegate());

    final ProductDetailsResponse productDetailResponse =
        await inAppPurchase.queryProductDetails(productIds.toSet());
    if (productDetailResponse.error != null) {
      SmartDialog.showToast(
          LocaleKeys.member_failedToRetrievePaymentProduct.tr);
      return;
    }

    if (productDetailResponse.productDetails.isEmpty) {
      return;
    }

    onInitSuccess(productDetailResponse.productDetails);
  }
}

class ExamplePaymentQueueDelegate implements SKPaymentQueueDelegateWrapper {
  @override
  bool shouldContinueTransaction(
      SKPaymentTransactionWrapper transaction, SKStorefrontWrapper storefront) {
    return true;
  }

  @override
  bool shouldShowPriceConsent() {
    return false;
  }
}

This is the encapsulated tool class. I commented out the following code and was able to invoke payment normally. But I'm not sure if commenting out this code will have any other impacts. Is there any alternative code for this code? I hope to get help.

    var transactions = await SKPaymentQueueWrapper().transactions();
    for (var skPaymentTransactionWrapper in transactions) {
      SKPaymentQueueWrapper().finishTransaction(skPaymentTransactionWrapper);
    }

Screenshots or Videos

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.24.1, on macOS 14.6.1 23G93 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.24.1 on channel stable at /Users/evdo/Desktop/development/flutter_sdk
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5874a72aa4 (2 weeks ago), 2024-08-20 16:46:00 -0500
    • Engine revision c9b9d5780d
    • Dart version 3.5.1
    • DevTools version 2.37.2
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/evdo/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C65
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.92.2)
    • VS Code at /Users/evdo/Desktop/Visual Studio Code.app/Contents
    • Flutter extension version 3.96.0

[✓] Connected device (4 available)
    • iPhone14 (mobile)               • 00008110-001A64CE2E40A01E • ios            • iOS 17.6.1 21G93
    • macOS (desktop)                 • macos                     • darwin-arm64   • macOS 14.6.1 23G93 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad     • darwin         • macOS 14.6.1 23G93 darwin-arm64
    • Chrome (web)                    • chrome                    • web-javascript • Google Chrome 128.0.6613.120

[✓] Network resources
    • All expected network resources are available.

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listc: fatal crashCrashes that terminate the processc: regressionIt was better in the past than it is nowfound in release: 3.24Found to occur in 3.24has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: in_app_purchasePlugin for in-app purchasepackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-iosOwned by iOS platform teamtriaged-iosTriaged by iOS platform team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions