Skip to content

Commit

Permalink
xrOS: added support for new purchase(confirmIn:options:) method (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto authored Jun 22, 2023
1 parent 7a07b3f commit 4ab9d73
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import UIKit

extension UIApplication {

@available(iOS 13.0, *)
@available(macCatalystApplicationExtension 13.1, *)
@available(iOS 13.0, macCatalystApplicationExtension 13.1, *)
@available(macOS, unavailable)
@available(watchOS, unavailable)
@available(watchOSApplicationExtension, unavailable)
Expand Down
3 changes: 0 additions & 3 deletions Sources/Logging/Strings/ManageSubscriptionsStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ extension ManageSubscriptionsHelper {

case error_from_appstore_show_manage_subscription(error: Error)
case failed_to_get_management_url_error_unknown(error: Error)
case failed_to_get_window_scene
case management_url_nil_opening_default
case show_manage_subscriptions_called_in_unsupported_platform
case susbscription_management_sheet_dismissed
Expand All @@ -38,8 +37,6 @@ extension ManageSubscriptionsHelper.Strings: LogMessage {
return "Error when trying to show manage subscription: \(error.localizedDescription)"
case .failed_to_get_management_url_error_unknown(let error):
return "Failed to get managementURL from CustomerInfo. Details: \(error.localizedDescription)"
case .failed_to_get_window_scene:
return "Failed to get UIWindowScene"
case .management_url_nil_opening_default:
return "managementURL is nil, opening Apple's subscription management page"
case .susbscription_management_sheet_dismissed:
Expand Down
19 changes: 19 additions & 0 deletions Sources/Misc/SystemInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ class SystemInfo {

}

#if os(iOS)
extension SystemInfo {

@available(iOS 13.0, macCatalystApplicationExtension 13.1, *)
@available(macOS, unavailable)
@available(watchOS, unavailable)
@available(watchOSApplicationExtension, unavailable)
@available(tvOS, unavailable)
var currentWindowScene: UIWindowScene {
get throws {
let scene = self.sharedUIApplication?.currentWindowScene

return try scene.orThrow(ErrorUtils.storeProblemError(withMessage: "Failed to get UIWindowScene"))
}
}

}
#endif

extension SystemInfo: SandboxEnvironmentDetector {}

// @unchecked because:
Expand Down
21 changes: 20 additions & 1 deletion Sources/Purchasing/Purchases/PurchasesOrchestrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ final class PurchasesOrchestrator {

self.cachePresentedOfferingIdentifier(package: package, productIdentifier: sk2Product.id)

result = try await sk2Product.purchase(options: options)
result = try await self.purchase(sk2Product, options)
} catch StoreKitError.userCancelled {
guard !self.systemInfo.dangerousSettings.customEntitlementComputation else {
throw ErrorUtils.purchaseCancelledError()
Expand Down Expand Up @@ -501,6 +501,25 @@ final class PurchasesOrchestrator {
return (transaction, customerInfo, userCancelled)
}

@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
private func purchase(
_ product: SK2Product,
_ options: Set<Product.PurchaseOption>
) async throws -> Product.PurchaseResult {
// Note: this can be simplified as `#if swift(>=5.9) && os(xrOS)`
// once we drop support for Xcode 13.x
#if swift(>=5.9)
#if os(xrOS)
return try await product.purchase(confirmIn: try self.systemInfo.currentWindowScene,
options: options)
#else
return try await product.purchase(options: options)
#endif
#else
return try await product.purchase(options: options)
#endif
}

@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
func promotionalOffer(
forProductDiscount discount: StoreProductDiscountType,
Expand Down
4 changes: 1 addition & 3 deletions Sources/Support/BeginRefundRequestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ class BeginRefundRequestHelper {
@available(tvOS, unavailable)
@MainActor
func beginRefundRequest(forProduct productID: String) async throws -> RefundRequestStatus {
guard let windowScene = systemInfo.sharedUIApplication?.currentWindowScene else {
throw ErrorUtils.storeProblemError(withMessage: "Failed to get UIWindowScene")
}
let windowScene = try self.systemInfo.currentWindowScene

let transactionID = try await sk2Helper.verifyTransaction(productID: productID)
return try await sk2Helper.initiateRefundRequest(transactionID: transactionID,
Expand Down
10 changes: 6 additions & 4 deletions Sources/Support/ManageSubscriptionsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ private extension ManageSubscriptionsHelper {
@available(iOS 15.0, *)
@available(macOS, unavailable)
func showSK2ManageSubscriptions() async -> Result<Void, PurchasesError> {
guard let application = systemInfo.sharedUIApplication,
let windowScene = application.currentWindowScene else {
let message = Strings.failed_to_get_window_scene
return .failure(ErrorUtils.storeProblemError(withMessage: message.description))
let windowScene: UIWindowScene

do {
windowScene = try self.systemInfo.currentWindowScene
} catch {
return .failure(ErrorUtils.purchasesError(withUntypedError: error))
}

#if os(iOS)
Expand Down

0 comments on commit 4ab9d73

Please sign in to comment.