Skip to content

Commit 606db97

Browse files
committed
[in_app_purchase_storekit] Use purchase(confirmIn:) for StoreKit 2 purchases
Switches StoreKit 2 purchases to Product.purchase(confirmIn:options:) when the plugin has a view controller attached to a window scene (iOS 17+) or an NSWindow (macOS 15.2+). The scene-less purchase(options:) cannot locate a UI anchor on iOS 27 / iPadOS 27 and never completes. The StoreKit 2 tests anchor the plugin to the test host's root view controller so purchases exercise the confirmIn: path, look transactions up by SKTestTransaction.identifier, poll for the asynchronous subscription expiry, and skip the two tests whose purchase fails by design on iOS 27, where StoreKitTest shows an undismissable error dialog (Apple known issue 184255116). Fixes flutter/flutter#194373 Fixes flutter/flutter#195494
1 parent 0f3656e commit 606db97

4 files changed

Lines changed: 73 additions & 21 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.16
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.15
27

38
* Exposes `sk2Transaction` on `SK2PurchaseDetails`.

‎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):
@@ -565,4 +565,33 @@ extension InAppPurchasePlugin: InAppPurchase2API {
565565
}
566566
return nil
567567
}
568+
569+
/// Purchases `product`, anchoring the confirmation UI to the Flutter view when possible.
570+
///
571+
/// The scene-less `purchase(options:)` leaves StoreKit to find a UI anchor itself, which fails
572+
/// on iOS 27 and leaves the purchase pending forever; it remains the fallback for older OS
573+
/// versions and for engines whose view is not in a window.
574+
@MainActor
575+
private func confirmPurchase(
576+
of product: Product,
577+
options: Set<Product.PurchaseOption>
578+
) async throws -> Product.PurchaseResult {
579+
#if os(iOS)
580+
if let viewController = self.registrar?.viewController,
581+
let windowScene = viewController.view.window?.windowScene
582+
{
583+
if #available(iOS 18.2, *) {
584+
return try await product.purchase(confirmIn: viewController, options: options)
585+
}
586+
if #available(iOS 17.0, *) {
587+
return try await product.purchase(confirmIn: windowScene, options: options)
588+
}
589+
}
590+
#elseif os(macOS)
591+
if #available(macOS 15.2, *), let window = self.registrar?.view?.window {
592+
return try await product.purchase(confirmIn: window, options: options)
593+
}
594+
#endif
595+
return try await product.purchase(options: options)
596+
}
568597
}

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

Lines changed: 37 additions & 19 deletions
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
@@ -193,7 +215,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
193215
await fulfillment(of: [expectation], timeout: purchaseTimeout)
194216

195217
let transaction = await plugin.fetchTransaction(
196-
by: UInt64(session.allTransactions()[0].originalTransactionIdentifier))
218+
by: UInt64(session.allTransactions()[0].identifier))
197219

198220
guard let transaction = transaction else {
199221
XCTFail("Transaction does not exist.")
@@ -223,6 +245,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
223245

224246
@available(iOS 17.0, macOS 14.0, *)
225247
func testFailedProductUnavilablePurchase() async throws {
248+
try skipIfFailedPurchasesHang()
226249
try await session.setSimulatedError(
227250
.purchase(.productUnavailable), forAPI: .purchase)
228251
let expectation = self.expectation(description: "Purchase request should succeed")
@@ -370,6 +393,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
370393
}
371394

372395
func testPurchaseWithIntroductoryOfferEligibilityJWS() async throws {
396+
try skipIfFailedPurchasesHang()
373397
let expectation = self.expectation(
374398
description: "Purchase with an introductory offer eligibility JWS should complete")
375399

@@ -593,24 +617,18 @@ final class InAppPurchase2PluginTests: XCTestCase {
593617

594618
try session.expireSubscription(productIdentifier: "subscription_silver")
595619

596-
let expectation = self.expectation(description: "Eligibility check should return true")
597-
598-
plugin.isWinBackOfferEligible(
599-
productId: "subscription_silver",
600-
offerId: "subscription_silver_winback_offer"
601-
) { result in
602-
switch result {
603-
case .success(let isEligible):
604-
XCTAssertTrue(isEligible)
605-
expectation.fulfill()
606-
case .failure(let error):
607-
XCTFail("Eligibility check failed: \(error.localizedDescription)")
608-
expectation.fulfill()
609-
}
610-
}
611-
612-
await fulfillment(of: [expectation], timeout: purchaseTimeout)
613-
620+
// The expiration is applied asynchronously on the iOS 27 simulator, so poll for it.
621+
let deadline = Date().addingTimeInterval(10)
622+
var isEligible = false
623+
repeat {
624+
isEligible = try await withCheckedThrowingContinuation { continuation in
625+
plugin.isWinBackOfferEligible(
626+
productId: "subscription_silver", offerId: "subscription_silver_winback_offer"
627+
) { continuation.resume(with: $0) }
628+
}
629+
if !isEligible { try await Task.sleep(nanoseconds: 100_000_000) }
630+
} while !isEligible && Date() < deadline
631+
XCTAssertTrue(isEligible)
614632
}
615633

616634
@available(iOS 18.0, macOS 15.0, *)

‎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.15
5+
version: 0.4.16
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)