Skip to content
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

xrOS: added support for new purchase(confirmIn:options:) method #2683

Merged
merged 2 commits into from
Jun 22, 2023
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
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
Comment on lines +509 to +510
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the answer to your other question @tonidero @MarkVillacampa, I just didn’t want to duplicate this comment everywhere.

#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