Skip to content

Commit d2b9d20

Browse files
authored
[in_app_purchase_storekit] Use purchase(confirmIn:) for StoreKit 2 purchases (flutter#13410)
Fixes flutter#194373 Fixes flutter#195494 This PR switches StoreKit 2 purchases from the scene-less Product.purchase(options:) to Product.purchase(confirmIn:options:) whenever the plugin has a view controller attached to a window scene (iOS 17+) or an NSWindow (macOS 15.2+). `purchase(confirmIn:)` is Apple's recommended purchase API for UIKit/AppKit apps. The scene-less variant relies on StoreKit locating a UI anchor itself; on iOS 27 / iPadOS 27 that lookup fails (`Could not find a UI anchor for … purchase`) and the `await` never returns. In the field this surfaced as App Review rejecting iPad subscription purchases (flutter#194373); on the iOS 27 simulator it surfaced as every StoreKit 2 purchase test timing out (flutter#195494). The scene-less call remains the fallback for iOS < 17 / macOS < 15.2 and for headless engines whose registrar has no view in a window. **Test changes** The test `setUp` now hands the plugin a registrar stub backed by the test host's root view controller, mirroring how the plugin runs inside a real Flutter app, so the existing purchase tests exercise the `confirmIn:` path. Two further adjustments were needed for the iOS 27 ipad simulator: - `testGetTransactionJsonRepresentation` makes a purchase and then asks the plugin to look that transaction up by its ID. It took the ID from the test framework's `SKTestTransaction.originalTransactionIdentifier`, which only worked because, on earlier OS versions, that value happened to equal the StoreKit 2 `Transaction.id` the plugin matches on (both are 0 for the first purchase). On iOS 27 StoreKitTest numbers transactions differently (`identifier` is a non-zero counter, `originalTransactionIdentifier` is reported as 0), so the lookup found nothing. The test now uses `identifier`, which is the field that corresponds to `Transaction.id` on every OS version. - `testFailedProductUnavilablePurchase` and `testPurchaseWithIntroductoryOfferEligibilityJWS` are skipped on iOS 27+. Both make a purchase that fails by design, and on iOS 27 StoreKitTest shows an error dialog for failed purchases even with `disableDialogs` ([iOS 27 release notes, known issue 184255116](https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-27-release-notes)), so the purchase never completes. Since StoreKit serializes purchases per process, each of those also blocked every later purchase test, which is the cascade you see in the CI log. ## Pre-Review Checklist
1 parent 2c2b1e7 commit d2b9d20

4 files changed

Lines changed: 61 additions & 3 deletions

File tree

‎packages/in_app_purchase/in_app_purchase_storekit/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.4.17
2+
3+
* Uses `Product.purchase(confirmIn:options:)` for StoreKit 2 purchases when a view controller,
4+
window scene, or window is available. Fixes purchases never completing on iOS 27 and iPadOS 27.
5+
16
## 0.4.16
27

38
* Adds `SK2Transaction.currentEntitlements()` to query active subscriptions and non-consumable entitlements.

‎packages/in_app_purchase/in_app_purchase_storekit/darwin/in_app_purchase_storekit/Sources/in_app_purchase_storekit/StoreKit2/InAppPurchasePlugin+StoreKit2.swift‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extension InAppPurchasePlugin: InAppPurchase2API {
108108
}
109109
}
110110

111-
let result = try await product.purchase(options: purchaseOptions)
111+
let result = try await confirmPurchase(of: product, options: purchaseOptions)
112112

113113
switch result {
114114
case .success(let verification):
@@ -588,4 +588,33 @@ extension InAppPurchasePlugin: InAppPurchase2API {
588588
}
589589
return nil
590590
}
591+
592+
/// Purchases `product`, anchoring the confirmation UI to the Flutter view when possible.
593+
///
594+
/// The scene-less `purchase(options:)` leaves StoreKit to find a UI anchor itself, which fails
595+
/// on iOS 27 and leaves the purchase pending forever; it remains the fallback for older OS
596+
/// versions and for engines whose view is not in a window.
597+
@MainActor
598+
private func confirmPurchase(
599+
of product: Product,
600+
options: Set<Product.PurchaseOption>
601+
) async throws -> Product.PurchaseResult {
602+
#if os(iOS)
603+
if let viewController = self.registrar?.viewController,
604+
let windowScene = viewController.view.window?.windowScene
605+
{
606+
if #available(iOS 18.2, *) {
607+
return try await product.purchase(confirmIn: viewController, options: options)
608+
}
609+
if #available(iOS 17.0, *) {
610+
return try await product.purchase(confirmIn: windowScene, options: options)
611+
}
612+
}
613+
#elseif os(macOS)
614+
if #available(macOS 15.2, *), let window = self.registrar?.view?.window {
615+
return try await product.purchase(confirmIn: window, options: options)
616+
}
617+
#endif
618+
return try await product.purchase(options: options)
619+
}
591620
}

‎packages/in_app_purchase/in_app_purchase_storekit/example/shared/RunnerTests/InAppPurchaseStoreKit2PluginTests.swift‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,31 @@ final class InAppPurchase2PluginTests: XCTestCase {
5050
DefaultRequestHandler(requestHandler: FIAPRequestHandler(request: request))
5151
}
5252
plugin.transactionCallbackAPI = callback
53+
#if os(iOS)
54+
// Anchor purchases to the host app's window scene, as a real app does through its
55+
// FlutterViewController; the scene-less fallback never completes on the iOS 27 simulator.
56+
let registrar = FlutterPluginRegistrarStub()
57+
registrar.viewController = try await MainActor.run {
58+
try XCTUnwrap(
59+
UIApplication.shared.connectedScenes
60+
.compactMap { ($0 as? UIWindowScene)?.keyWindow?.rootViewController }.first)
61+
}
62+
plugin.registrar = registrar
63+
#endif
5364
try plugin.startListeningToTransactions()
5465
}
5566

67+
/// Skips tests whose purchase fails by design: on the iOS 27 simulator StoreKitTest shows an
68+
/// error dialog for failed purchases even with `disableDialogs` (Apple known issue 184255116),
69+
/// so the purchase never completes and blocks every later purchase in the process.
70+
private func skipIfFailedPurchasesHang() throws {
71+
#if os(iOS)
72+
try XCTSkipIf(
73+
ProcessInfo.processInfo.operatingSystemVersion.majorVersion >= 27,
74+
"Failed purchases hang on the iOS 27 simulator (Apple known issue 184255116).")
75+
#endif
76+
}
77+
5678
override func tearDown() async throws {
5779
try? plugin.stopListeningToTransactions()
5880
plugin = nil
@@ -238,7 +260,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
238260
await fulfillment(of: [expectation], timeout: purchaseTimeout)
239261

240262
let transaction = await plugin.fetchTransaction(
241-
by: UInt64(session.allTransactions()[0].originalTransactionIdentifier))
263+
by: UInt64(session.allTransactions()[0].identifier))
242264

243265
guard let transaction = transaction else {
244266
XCTFail("Transaction does not exist.")
@@ -268,6 +290,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
268290

269291
@available(iOS 17.0, macOS 14.0, *)
270292
func testFailedProductUnavilablePurchase() async throws {
293+
try skipIfFailedPurchasesHang()
271294
try await session.setSimulatedError(
272295
.purchase(.productUnavailable), forAPI: .purchase)
273296
let expectation = self.expectation(description: "Purchase request should succeed")
@@ -415,6 +438,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
415438
}
416439

417440
func testPurchaseWithIntroductoryOfferEligibilityJWS() async throws {
441+
try skipIfFailedPurchasesHang()
418442
let expectation = self.expectation(
419443
description: "Purchase with an introductory offer eligibility JWS should complete")
420444

‎packages/in_app_purchase/in_app_purchase_storekit/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: in_app_purchase_storekit
22
description: An implementation for the iOS and macOS platforms of the Flutter `in_app_purchase` plugin. This uses the StoreKit Framework.
33
repository: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase_storekit
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
5-
version: 0.4.16
5+
version: 0.4.17
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)