Skip to content

[in_app_purchase] Write to the transactions update queue from the main thread #9068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.22+1

* Fix a channel thread-safety issue when StoreKit2 is enabled.

## 0.3.22

* Adds `sync()` and `countryCode()`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class InAppPurchasePlugin: NSObject, FlutterPlugin, FIAInAppPurchaseAPI {
self._updateListenerTask = task
}

var transactionCallbackAPI: InAppPurchase2CallbackAPI? = nil
var transactionCallbackAPI: InAppPurchase2CallbackAPIProtocol? = nil

public static func register(with registrar: FlutterPluginRegistrar) {
#if os(iOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,14 @@ extension InAppPurchasePlugin: InAppPurchase2API {
/// Sends an transaction back to Dart. Access these transactions with `purchaseStream`
private func sendTransactionUpdate(transaction: Transaction, receipt: String? = nil) {
let transactionMessage = transaction.convertToPigeon(receipt: receipt)
self.transactionCallbackAPI?.onTransactionsUpdated(newTransactions: [transactionMessage]) {
result in
switch result {
case .success: break
case .failure(let error):
print("Failed to send transaction updates: \(error)")
Task { @MainActor in
self.transactionCallbackAPI?.onTransactionsUpdated(newTransactions: [transactionMessage]) {
result in
switch result {
case .success: break
case .failure(let error):
print("Failed to send transaction updates: \(error)")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ import XCTest

@testable import in_app_purchase_storekit

@available(iOS 15.0, macOS 12.0, *)
final class FakeIAP2Callback: InAppPurchase2CallbackAPIProtocol {
func onTransactionsUpdated(
newTransactions newTransactionsArg: [in_app_purchase_storekit.SK2TransactionMessage],
completion: @escaping (Result<Void, in_app_purchase_storekit.PigeonError>) -> Void
) {
// We should only write to a flutter channel from the main thread.
XCTAssertTrue(Thread.isMainThread)
}
}

@available(iOS 15.0, macOS 12.0, *)
final class InAppPurchase2PluginTests: XCTestCase {
private var session: SKTestSession!
private var plugin: InAppPurchasePlugin!
Expand All @@ -24,6 +33,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
plugin = InAppPurchasePluginStub(receiptManager: FIAPReceiptManagerStub()) { request in
DefaultRequestHandler(requestHandler: FIAPRequestHandler(request: request))
}
plugin.transactionCallbackAPI = FakeIAP2Callback()
try plugin.startListeningToTransactions()
}

Expand Down Expand Up @@ -296,6 +306,8 @@ final class InAppPurchase2PluginTests: XCTestCase {
XCTFail("Purchase should NOT fail. Failed with \(error)")
}
}
await fulfillment(of: [purchaseExpectation], timeout: 5)

plugin.restorePurchases { result in
switch result {
case .success():
Expand All @@ -304,8 +316,7 @@ final class InAppPurchase2PluginTests: XCTestCase {
XCTFail("Restore purchases should NOT fail. Failed with \(error)")
}
}

await fulfillment(of: [restoreExpectation, purchaseExpectation], timeout: 5)
await fulfillment(of: [restoreExpectation], timeout: 5)
}

func testFinishTransaction() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: in_app_purchase_storekit
description: An implementation for the iOS and macOS platforms of the Flutter `in_app_purchase` plugin. This uses the StoreKit Framework.
repository: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase_storekit
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
version: 0.3.22
version: 0.3.22+1

environment:
sdk: ^3.4.0
Expand Down