Skip to content

Commit 9f90cc6

Browse files
Return success instead of error when transaction not found in finish()
If a transaction is not found in Transaction.unfinished, it means the transaction has already been finished. This should be treated as a success case rather than an error, since the desired outcome (transaction completed) has been achieved. This ensures that calling completePurchase() multiple times or on already-finished transactions works correctly, allowing consumable products to be repurchased.
1 parent d031e46 commit 9f90cc6

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

packages/in_app_purchase/in_app_purchase_storekit/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## 0.4.6+3
22

33
* Fixes `finish()` method to always call its completion handler in StoreKit2.
4-
* `finish()` now returns an error when the transaction is not found in unfinished transactions.
4+
* `finish()` now returns success when the transaction is not found in unfinished transactions (since it's already complete).
55
* Adds `fetchUnfinishedTransaction()` helper for looking up transactions that need to be completed.
66

77
## 0.4.6+2

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,10 @@ extension InAppPurchasePlugin: InAppPurchase2API {
267267
let transaction = try await fetchUnfinishedTransaction(by: UInt64(id))
268268
if let transaction = transaction {
269269
await transaction.finish()
270-
completion(.success(Void()))
271-
} else {
272-
// Transaction not found in unfinished transactions
273-
completion(
274-
.failure(
275-
PigeonError(
276-
code: "storekit2_transaction_not_found",
277-
message: "Transaction not found in unfinished transactions.",
278-
details: "Transaction ID: \(id)")))
279270
}
271+
// If transaction is not found, it means it's already been finished.
272+
// This is a success case - the transaction is complete.
273+
completion(.success(Void()))
280274
} catch {
281275
completion(
282276
.failure(

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,19 +415,18 @@ final class InAppPurchase2PluginTests: XCTestCase {
415415
await fulfillment(of: [finishExpectation], timeout: 5)
416416
}
417417

418-
func testFinishNonExistentTransactionReturnsError() async throws {
419-
// Test that finishing a non-existent transaction returns an error
418+
func testFinishNonExistentTransactionReturnsSuccess() async throws {
419+
// Test that finishing a non-existent transaction returns success
420+
// (since if it's not in unfinished transactions, it's already complete)
420421
let finishExpectation = self.expectation(
421-
description: "Finishing non-existent transaction should return error")
422+
description: "Finishing non-existent transaction should return success")
422423

423424
plugin.finish(id: 999999) { result in
424425
switch result {
425426
case .success():
426-
XCTFail("Finish should fail for non-existent transaction")
427-
case .failure(let error):
428-
let pigeonError = error as! PigeonError
429-
XCTAssertEqual(pigeonError.code, "storekit2_transaction_not_found")
430427
finishExpectation.fulfill()
428+
case .failure(let error):
429+
XCTFail("Finish should succeed for non-existent transaction: \(error)")
431430
}
432431
}
433432

0 commit comments

Comments
 (0)