11import StoreKit
22
3+ #if canImport(AppKit)
4+ import AppKit
5+ public typealias DisplayContext = NSViewController
6+ #elseif os(iOS)
7+ import class UIKit. UIWindowScene
8+ public typealias DisplayContext = UIWindowScene
9+ #endif
10+
311extension Mercato {
412
513 /// Promotional offer for a purchase.
@@ -173,53 +181,6 @@ public final class Mercato: Sendable {
173181 return await product. isEligibleForIntroOffer
174182 }
175183
176- /// Initiates the refund process for a specified product within a given `UIWindowScene`.
177- ///
178- /// This function attempts to begin a refund request for a transaction associated with the given product ID.
179- /// It handles different refund request outcomes, including user cancellation, success, and unknown errors.
180- /// The function is asynchronous and can throw errors if verification or the refund process fails.
181- ///
182- /// - Parameters:
183- /// - productID: The identifier of the product for which the refund process is being requested.
184- /// - scene: The `UIWindowScene` within which the refund request will be displayed to the user.
185- ///
186- /// - Throws:
187- /// - `MercatoError.failedVerification`: If the transaction cannot be verified.
188- /// - `MercatoError.canceledByUser`: If the user cancels the refund request.
189- /// - `MercatoError.unknown(error:)`: If an unknown error occurs during the refund process.
190- /// - `MercatoError.refund(error:)`: If a `Transaction.RefundRequestError` occurs during the refund process.
191- /// - `MercatoError.storeKit(error:)`: If a `StoreKitError` occurs during the refund process.
192- ///
193- /// - Availability:
194- /// - Not available on watchOS or tvOS.
195- ///
196- /// - Returns:
197- /// No return value. The function will either complete successfully or throw an error.
198- @available ( watchOS, unavailable)
199- @available ( watchOS, unavailable)
200- @available ( tvOS, unavailable)
201- func beginRefundProcess( for productID: String , in scene: UIWindowScene ) async throws {
202- guard case . verified( let transaction) = await Transaction . latest ( for: productID) else { throw MercatoError . failedVerification }
203-
204- do {
205- let status = try await transaction. beginRefundRequest ( in: scene)
206-
207- switch status {
208- case . userCancelled:
209- throw MercatoError . canceledByUser
210- case . success:
211- break
212- @unknown default :
213- throw MercatoError . unknown ( error: nil )
214- }
215- } catch let error as Transaction . RefundRequestError {
216- throw MercatoError . refund ( error: error)
217- } catch let error as StoreKitError {
218- throw MercatoError . storeKit ( error: error)
219- } catch {
220- throw MercatoError . unknown ( error: error)
221- }
222- }
223184}
224185
225186// MARK: - Mercato: Purchase
@@ -404,31 +365,6 @@ public extension Mercato {
404365 try await AppStore . sync ( )
405366 }
406367
407- /// Displays the subscription management interface for the user within the specified `UIWindowScene`.
408- ///
409- /// This function presents the App Store's manage subscriptions screen, allowing the user to view and manage their active subscriptions.
410- /// It is asynchronous and throws errors if there are issues invoking the subscription management screen.
411- ///
412- /// - Parameters:
413- /// - scene: The `UIWindowScene` within which the subscription management interface will be presented.
414- ///
415- /// - Throws:
416- /// - An error if the subscription management interface could not be presented.
417- ///
418- /// - Availability:
419- /// - Available on iOS 15.0 and later.
420- /// - Not available on macOS, watchOS, or tvOS.
421- ///
422- /// - Returns:
423- /// No return value. The function will either complete successfully or throw an error.
424- @available ( iOS 15 . 0 , * )
425- @available ( macOS, unavailable)
426- @available ( watchOS, unavailable)
427- @available ( tvOS, unavailable)
428- static func showManageSubscriptions( in scene: UIWindowScene ) async throws {
429- try await AppStore . showManageSubscriptions ( in: scene)
430- }
431-
432368 /// Requests product data from the App Store.
433369 /// - Parameter identifiers: A set of product identifiers to load from the App Store. If any
434370 /// identifiers are not found, they will be excluded from the return
@@ -453,35 +389,6 @@ public extension Mercato {
453389 try await shared. isEligibleForIntroOffer ( for: productId)
454390 }
455391
456- /// Initiates the refund process for a specified product within a given `UIWindowScene`.
457- ///
458- /// This function attempts to begin a refund request for a transaction associated with the given product ID.
459- /// It handles different refund request outcomes, including user cancellation, success, and unknown errors.
460- /// The function is asynchronous and can throw errors if verification or the refund process fails.
461- ///
462- /// - Parameters:
463- /// - productID: The identifier of the product for which the refund process is being requested.
464- /// - scene: The `UIWindowScene` within which the refund request will be displayed to the user.
465- ///
466- /// - Throws:
467- /// - `MercatoError.failedVerification`: If the transaction cannot be verified.
468- /// - `MercatoError.canceledByUser`: If the user cancels the refund request.
469- /// - `MercatoError.unknown(error:)`: If an unknown error occurs during the refund process.
470- /// - `MercatoError.refund(error:)`: If a `Transaction.RefundRequestError` occurs during the refund process.
471- /// - `MercatoError.storeKit(error:)`: If a `StoreKitError` occurs during the refund process.
472- ///
473- /// - Availability:
474- /// - Not available on watchOS or tvOS.
475- ///
476- /// - Returns:
477- /// No return value. The function will either complete successfully or throw an error.
478- @available ( watchOS, unavailable)
479- @available ( watchOS, unavailable)
480- @available ( tvOS, unavailable)
481- static func beginRefundProcess( for productID: String , in scene: UIWindowScene ) async throws {
482- try await shared. beginRefundProcess ( for: productID, in: scene)
483- }
484-
485392 /// Initiates a purchase for a specified productId with optional parameters.
486393 ///
487394 /// - Parameters:
@@ -555,3 +462,113 @@ public extension Mercato {
555462 try await shared. purchase ( product: product, options: options, finishAutomatically: finishAutomatically)
556463 }
557464}
465+
466+ #if os(macOS) || os(iOS)
467+ @available ( macOS 12 . 0 , * )
468+ @available ( iOS 15 . 0 , visionOS 1 . 0 , * )
469+ @available ( watchOS, unavailable)
470+ @available ( tvOS, unavailable)
471+ public extension Mercato {
472+ /// Initiates the refund process for a specified product within a given `UIWindowScene`.
473+ ///
474+ /// This function attempts to begin a refund request for a transaction associated with the given product ID.
475+ /// It handles different refund request outcomes, including user cancellation, success, and unknown errors.
476+ /// The function is asynchronous and can throw errors if verification or the refund process fails.
477+ ///
478+ /// - Parameters:
479+ /// - productID: The identifier of the product for which the refund process is being requested.
480+ /// - scene: The `UIWindowScene` within which the refund request will be displayed to the user.
481+ ///
482+ /// - Throws:
483+ /// - `MercatoError.failedVerification`: If the transaction cannot be verified.
484+ /// - `MercatoError.canceledByUser`: If the user cancels the refund request.
485+ /// - `MercatoError.unknown(error:)`: If an unknown error occurs during the refund process.
486+ /// - `MercatoError.refund(error:)`: If a `Transaction.RefundRequestError` occurs during the refund process.
487+ /// - `MercatoError.storeKit(error:)`: If a `StoreKitError` occurs during the refund process.
488+ ///
489+ /// - Availability:
490+ /// - Not available on watchOS or tvOS.
491+ ///
492+ /// - Returns:
493+ /// No return value. The function will either complete successfully or throw an error.
494+ @available ( macOS 12 . 0 , * )
495+ @available ( iOS 15 . 0 , visionOS 1 . 0 , * )
496+ @available ( watchOS, unavailable)
497+ @available ( tvOS, unavailable)
498+ func beginRefundProcess( for productID: String , in displayContext: DisplayContext ) async throws {
499+ guard case . verified( let transaction) = await Transaction . latest ( for: productID) else { throw MercatoError . failedVerification }
500+
501+ do {
502+ let status = try await transaction. beginRefundRequest ( in: displayContext)
503+
504+ switch status {
505+ case . userCancelled:
506+ throw MercatoError . canceledByUser
507+ case . success:
508+ break
509+ @unknown default :
510+ throw MercatoError . unknown ( error: nil )
511+ }
512+ } catch let error as Transaction . RefundRequestError {
513+ throw MercatoError . refund ( error: error)
514+ } catch let error as StoreKitError {
515+ throw MercatoError . storeKit ( error: error)
516+ } catch {
517+ throw MercatoError . unknown ( error: error)
518+ }
519+ }
520+
521+ @available ( macOS 12 . 0 , * )
522+ @available ( iOS 15 . 0 , visionOS 1 . 0 , * )
523+ @available ( watchOS, unavailable)
524+ @available ( tvOS, unavailable)
525+ static func beginRefundProcess( for productID: String , in displayContext: DisplayContext ) async throws {
526+ try await shared. beginRefundProcess ( for: productID, in: displayContext)
527+ }
528+ }
529+ #endif
530+
531+ // MARK: Manage Subscription
532+
533+ #if os(iOS)
534+ @available ( iOS 15 . 0 , visionOS 1 . 0 , * )
535+ @available ( macOS, unavailable)
536+ @available ( watchOS, unavailable)
537+ @available ( tvOS, unavailable)
538+ public extension Mercato {
539+ /// Displays the subscription management interface for the user within the specified `UIWindowScene`.
540+ ///
541+ /// This function presents the App Store's manage subscriptions screen, allowing the user to view and manage their active subscriptions.
542+ /// It is asynchronous and throws errors if there are issues invoking the subscription management screen.
543+ ///
544+ /// - Parameters:
545+ /// - scene: The `UIWindowScene` within which the subscription management interface will be presented.
546+ ///
547+ /// - Throws:
548+ /// - An error if the subscription management interface could not be presented.
549+ ///
550+ /// - Availability:
551+ /// - Available on iOS 15.0 and later.
552+ /// - Not available on macOS, watchOS, or tvOS.
553+ ///
554+ /// - Returns:
555+ /// No return value. The function will either complete successfully or throw an error.
556+ @available ( iOS 15 . 0 , visionOS 1 . 0 , * )
557+ @available ( macOS, unavailable)
558+ @available ( watchOS, unavailable)
559+ @available ( tvOS, unavailable)
560+ @MainActor
561+ static func showManageSubscriptions( in scene: UIWindowScene ) async throws {
562+ try await AppStore . showManageSubscriptions ( in: scene)
563+ }
564+
565+ @available ( iOS 17 . 0 , visionOS 1 . 0 , * )
566+ @available ( macOS, unavailable)
567+ @available ( watchOS, unavailable)
568+ @available ( tvOS, unavailable)
569+ @MainActor
570+ static func showManageSubscriptions( in scene: UIWindowScene , subscriptionGroupID: String ) async throws {
571+ try await AppStore . showManageSubscriptions ( in: scene, subscriptionGroupID: subscriptionGroupID)
572+ }
573+ }
574+ #endif
0 commit comments