Skip to content

Commit d0bf954

Browse files
authored
[in_app_purchase_storekit] Complete StoreKit 2 finish when the transaction is not found (#13037)
StoreKit 2 `finish(id:completion:)` only called `completion` when `fetchTransaction` found the transaction. `fetchTransaction` searches `Transaction.all`, which by default does not contain finished consumables, so a second `completePurchase` for a consumable that was already finished (for example after a duplicate delivery) returned a `Future` that never completed. The StoreKit 1 path already treats a missing transaction as success (`testFinishTransactionSucceedsWithNilTransaction`). This PR: - Calls `completion(.success(Void()))` after the lookup whether or not a transaction was found. - Removes the `throws` from `fetchTransaction`, which has no throwing call. This rules out the second hang the issue describes (an error thrown inside the `Task` in `finish` and never reported) and clears the Xcode 27 warning `unstructured throwing task ... may accidentally ignore errors thrown inside the task`. The one test caller drops its now unneeded `try`. - Adds `testFinishTransactionSucceedsWhenAlreadyFinished`, which purchases a consumable and finishes it twice; the second `finish` times out without the fix and passes with it. - Makes `testFinishTransaction` also check that the purchase is no longer unfinished, since its completion alone no longer proves the transaction was found. Tested with the iOS `RunnerTests` on an iOS 26.2 simulator, the CI destination. This only makes the `Future` complete; it does not change which transactions StoreKit redelivers. Fixes flutter/flutter#192400 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 5d34573 commit d0bf954

4 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎packages/in_app_purchase/in_app_purchase_storekit/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## NEXT
1+
## 0.4.13+1
22

3+
* Fixes `completePurchase` never completing with StoreKit 2 when the transaction
4+
is not found, such as an already finished consumable.
35
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
46
* Fixes lint issues.
57

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,12 @@ extension InAppPurchasePlugin: InAppPurchase2API {
322322
/// Wrapper method around StoreKit2's finish() method https://developer.apple.com/documentation/storekit/transaction/3749694-finish
323323
func finish(id: Int64, completion: @escaping (Result<Void, Error>) -> Void) {
324324
Task {
325-
let transaction = try await fetchTransaction(by: UInt64(id))
326-
if let transaction = transaction {
325+
// Report success when no verified transaction matches, as StoreKit 1
326+
// does; a finished consumable is no longer in Transaction.all.
327+
if let transaction = await fetchTransaction(by: UInt64(id)) {
327328
await transaction.finish()
328-
completion(.success(Void()))
329329
}
330+
completion(.success(Void()))
330331
}
331332
}
332333

@@ -516,7 +517,7 @@ extension InAppPurchasePlugin: InAppPurchase2API {
516517
}
517518

518519
/// Helper function to fetch specific transaction
519-
func fetchTransaction(by id: UInt64) async throws -> Transaction? {
520+
func fetchTransaction(by id: UInt64) async -> Transaction? {
520521
for await result in Transaction.all {
521522
switch result {
522523
case .verified(let transaction):

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
175175

176176
await fulfillment(of: [expectation], timeout: 5)
177177

178-
let transaction = try await plugin.fetchTransaction(
178+
let transaction = await plugin.fetchTransaction(
179179
by: UInt64(session.allTransactions()[0].originalTransactionIdentifier))
180180

181181
guard let transaction = transaction else {
@@ -535,6 +535,53 @@ final class InAppPurchase2PluginTests: XCTestCase {
535535
}
536536

537537
await fulfillment(of: [finishExpectation], timeout: 5)
538+
539+
for await result in Transaction.unfinished {
540+
if case .verified(let transaction) = result {
541+
XCTAssertNotEqual(transaction.id, 0, "The purchase should be finished.")
542+
}
543+
}
544+
}
545+
546+
func testFinishTransactionSucceedsWhenAlreadyFinished() async throws {
547+
let purchaseExpectation = self.expectation(description: "Purchase should succeed")
548+
let finishExpectation = self.expectation(description: "Finishing purchase should succeed")
549+
let secondFinishExpectation = self.expectation(
550+
description: "Finishing an already finished purchase should succeed")
551+
552+
plugin.purchase(id: "consumable", options: nil) { result in
553+
switch result {
554+
case .success(_):
555+
purchaseExpectation.fulfill()
556+
case .failure(let error):
557+
XCTFail("Purchase should NOT fail. Failed with \(error)")
558+
}
559+
}
560+
561+
await fulfillment(of: [purchaseExpectation], timeout: 5)
562+
563+
plugin.finish(id: 0) { result in
564+
switch result {
565+
case .success():
566+
finishExpectation.fulfill()
567+
case .failure(let error):
568+
XCTFail("Finish purchases should NOT fail. Failed with \(error)")
569+
}
570+
}
571+
572+
await fulfillment(of: [finishExpectation], timeout: 5)
573+
574+
// A finished consumable is no longer in Transaction.all.
575+
plugin.finish(id: 0) { result in
576+
switch result {
577+
case .success():
578+
secondFinishExpectation.fulfill()
579+
case .failure(let error):
580+
XCTFail("Finish purchases should NOT fail. Failed with \(error)")
581+
}
582+
}
583+
584+
await fulfillment(of: [secondFinishExpectation], timeout: 5)
538585
}
539586

540587
@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.13
5+
version: 0.4.13+1
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)