Repository navigation
[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
Copy link
Copy link
Closed
flutter/packages
#8071Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listc: fatal crashCrashes that terminate the processCrashes that terminate the processc: regressionIt was better in the past than it is nowIt was better in the past than it is nowfound in release: 3.24Found to occur in 3.24Found to occur in 3.24has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: in_app_purchasePlugin for in-app purchasePlugin for in-app purchasepackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.platform-iosiOS applications specificallyiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-iosOwned by iOS platform teamOwned by iOS platform teamtriaged-iosTriaged by iOS platform teamTriaged by iOS platform team
Description
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.Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listc: fatal crashCrashes that terminate the processCrashes that terminate the processc: regressionIt was better in the past than it is nowIt was better in the past than it is nowfound in release: 3.24Found to occur in 3.24Found to occur in 3.24has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: in_app_purchasePlugin for in-app purchasePlugin for in-app purchasepackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.platform-iosiOS applications specificallyiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-iosOwned by iOS platform teamOwned by iOS platform teamtriaged-iosTriaged by iOS platform teamTriaged by iOS platform team
Activity
huycozy commentedon Sep 9, 2024
Hi @davidchzh
For the issue to be workable, can you provide a completed and minimal reproducible code sample that doesn’t include 3rd party plugins or complex production code? (you may look into plugin sample code and use it to see if the issue happens on your side). And please provide the entire crash stack trace as well. Thank you!
himotou commentedon Sep 9, 2024
Thank you for your reply!
For this question, I already know where the problem lies, which is caused by calling the following code.
The embarrassing thing is that I have forgotten why I added this code. I would like to ask if deleting this logic will affect the process, or if there is a new method in the latest version to replace it.
huycozy commentedon Sep 10, 2024
From my point of view, since you invoked
completePurchase, it will callcompletePurchase. Perhaps manually calling finishTransaction is being used in case error handling. Please see #91164 if it assembles your case or not.himotou commentedon Sep 10, 2024
It seems that the two are the same problem. According to my current understanding, the purpose of this code is to "end the last transaction if the last call fails."
In my code, I will call this logic regardless of whether the last call failed.
In version 3.1.13, this did not cause an error. It caused an error when I upgraded to version 3.2.
huycozy commentedon Sep 11, 2024
#91164 is a documentation issue and the original comment showing it's a type cast error.
It could be a regression but need to know the
in_app_purchase_storekitversion as well. Please also try to narrow the sample code down into a minimal one that can produce the error as reported.himotou commentedon Sep 11, 2024
Sample code
himotou commentedon Sep 11, 2024
When running the demo, I found another problem.
After selecting the product to call up the payment page, click the x in the upper right corner to cancel the payment, and the page will remain in the loading state.
The code does not make any changes to the demo logic. It just changes the product id.
Sample code
The following is the error log
huycozy commentedon Sep 12, 2024
(I collapsed the sample code on your comments above since they are too long for others to grasp the issue.)
When using your sample code at #154763 (comment), I can see the fatal crash on iOS.
To find regression, I was forced to use
in_app_purchase: 3.1.13(withdependency_overrides) but the crash persists. I think it's becausein_app_purchase_storekitis still on the latest version0.3.18+1. I traced it by trying some olderin_app_purchase_storekitversions on release timeline ofin_app_purchaseandin_app_purchase_storekit, I found the crash also occurs within_app_purchase_storekit: 0.3.8+1but the app is not terminated as the latest version, I can see the trace, though.I haven't found a version of
in_app_purchase_storekitthat didn't cause this error, if you can find one, please share it.In the meantime, I will mark this as a regression.
17 remaining items
andywhite37 commentedon Sep 28, 2024
We are also seeing this issue in iOS as of the past couple weeks. We had a similar workaround mentioned above to complete pending transactions prior to attempting a purchase. I think I added this about a year ago because we were having problems with the purchase flow and someone else recommended doing it. It seems that it's now causing a "crash to desktop" native error in iOS.
I've tried removing this workaround, but am experiencing other strange behavior which I'm currently trying to debug.
Error:
Versions in
pubspec.lock:andywhite37 commentedon Sep 29, 2024
Downgrading from
in_app_purchase3.2.0 to either3.1.10or3.1.13fixed the issue for me.LouiseHsu commentedon Sep 30, 2024
Sorry for the late reply guys - let me investigate and Ill try to get a patch out soon.
[in_app_purchase_storekit] Fixes manual invocation of `finishTransact…
github-actions commentedon Nov 28, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.[in_app_purchase_storekit] Fixes manual invocation of `finishTransact…
[in_app_purchase_storekit] Fixes manual invocation of `finishTransact…