Skip to content

[in_app_purchase] add Storefront.countryCode() and AppStore.sync() #8900

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 17 commits into from
Apr 17, 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

* Adds `sync()` and `countryCode()`.

## 0.3.21

* Adds Swift Package Manager compatibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import StoreKit

@available(iOS 15.0, macOS 12.0, *)
extension InAppPurchasePlugin: InAppPurchase2API {

// MARK: - Pigeon Functions

/// Wrapper method around StoreKit2's canMakePayments() method
Expand Down Expand Up @@ -146,6 +145,43 @@ extension InAppPurchasePlugin: InAppPurchase2API {
}
}

/// Wrapper method around StoreKit2's countryCode() method
/// https://developer.apple.com/documentation/storekit/storefront/countrycode
func countryCode(completion: @escaping (Result<String, Error>) -> Void) {
Task {
guard let currentStorefront = await Storefront.current else {
let error = PigeonError(
code: "storekit2_failed_to_fetch_country_code",
message: "Storekit has failed to fetch the country code.",
details: "Storefront.current returned nil.")
completion(.failure(error))
return
}
completion(.success(currentStorefront.countryCode))
return
}
}

/// Wrapper method around StoreKit2's sync() method
/// https://developer.apple.com/documentation/storekit/appstore/sync()
/// When called, a system prompt will ask users to enter their authentication details
func sync(completion: @escaping (Result<Void, Error>) -> Void) {
Task {
do {
try await AppStore.sync()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing completion on success.

completion(.success(()))
return
} catch {
let pigeonError = PigeonError(
code: "storekit2_failed_to_sync_to_app_store",
message: "Storekit has failed to sync to the app store.",
details: "\(error)")
completion(.failure(pigeonError))
return
}
}
}

/// This Task listens to Transation.updates as shown here
/// https://developer.apple.com/documentation/storekit/transaction/3851206-updates
/// This function should be called as soon as the app starts to avoid missing any Transactions done outside of the app.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v22.7.3), do not edit directly.
// Autogenerated from Pigeon (v25.1.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon

import Foundation
Expand All @@ -18,9 +18,9 @@ import Foundation
final class PigeonError: Error {
let code: String
let message: String?
let details: Any?
let details: Sendable?

init(code: String, message: String?, details: Any?) {
init(code: String, message: String?, details: Sendable?) {
self.code = code
self.message = message
self.details = details
Expand Down Expand Up @@ -521,6 +521,8 @@ protocol InAppPurchase2API {
func startListeningToTransactions() throws
func stopListeningToTransactions() throws
func restorePurchases(completion: @escaping (Result<Void, Error>) -> Void)
func countryCode(completion: @escaping (Result<String, Error>) -> Void)
func sync(completion: @escaping (Result<Void, Error>) -> Void)
}

/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
Expand Down Expand Up @@ -676,6 +678,41 @@ class InAppPurchase2APISetup {
} else {
restorePurchasesChannel.setMessageHandler(nil)
}
let countryCodeChannel = FlutterBasicMessageChannel(
name:
"dev.flutter.pigeon.in_app_purchase_storekit.InAppPurchase2API.countryCode\(channelSuffix)",
binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
countryCodeChannel.setMessageHandler { _, reply in
api.countryCode { result in
switch result {
case .success(let res):
reply(wrapResult(res))
case .failure(let error):
reply(wrapError(error))
}
}
}
} else {
countryCodeChannel.setMessageHandler(nil)
}
let syncChannel = FlutterBasicMessageChannel(
name: "dev.flutter.pigeon.in_app_purchase_storekit.InAppPurchase2API.sync\(channelSuffix)",
binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
syncChannel.setMessageHandler { _, reply in
api.sync { result in
switch result {
case .success:
reply(wrapResult(nil))
case .failure(let error):
reply(wrapError(error))
}
}
}
} else {
syncChannel.setMessageHandler(nil)
}
}
}
/// Generated protocol from Pigeon that represents Flutter messages that can be called from Swift.
Expand Down Expand Up @@ -713,7 +750,7 @@ class InAppPurchase2CallbackAPI: InAppPurchase2CallbackAPIProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(PigeonError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ class InAppPurchaseStoreKitPlatform extends InAppPurchasePlatform {
/// See: https://developer.apple.com/documentation/storekit/skstorefront?language=objc
@override
Future<String> countryCode() async {
if (_useStoreKit2) {
return Storefront().countryCode();
}
return (await _skPaymentQueueWrapper.storefront())?.countryCode ?? '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
// found in the LICENSE file.

import 'package:in_app_purchase_platform_interface/in_app_purchase_platform_interface.dart';
import '../in_app_purchase_storekit.dart';

import '../in_app_purchase_storekit.dart';
import '../store_kit_2_wrappers.dart';
import '../store_kit_wrappers.dart';

/// Contains InApp Purchase features that are only available on iOS.
class InAppPurchaseStoreKitPlatformAddition
extends InAppPurchasePlatformAddition {
/// Synchronizes your app’s transaction information and subscription status
/// with information from the App Store.
/// Storekit 2 only
Future<void> sync() {
return AppStore().sync();
}

/// Present Code Redemption Sheet.
///
/// Available on devices running iOS 14 and iPadOS 14 and later.
/// Available for StoreKit 1 and 2
Future<void> presentCodeRedemptionSheet() {
return SKPaymentQueueWrapper().presentCodeRedemptionSheet();
}
Expand Down
Loading