2021-11-22 12:37:33 -03:00
|
|
|
//
|
|
|
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the MIT License (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
|
//
|
|
|
|
|
// BeginRefundRequestHelper.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Madeline Beyl on 10/13/21.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class responsible for handling any non-store-specific code involved in beginning a refund request.
|
|
|
|
|
* Delegates store-specific operations to `SK2BeginRefundRequestHelper`.
|
|
|
|
|
*/
|
|
|
|
|
class BeginRefundRequestHelper {
|
|
|
|
|
|
|
|
|
|
private let systemInfo: SystemInfo
|
2021-12-29 20:43:32 -05:00
|
|
|
private let customerInfoManager: CustomerInfoManager
|
2022-04-08 15:39:34 -07:00
|
|
|
private let currentUserProvider: CurrentUserProvider
|
2021-11-22 12:37:33 -03:00
|
|
|
|
2023-08-09 11:19:40 -07:00
|
|
|
#if os(iOS) || VISION_OS
|
2022-06-06 17:15:28 -07:00
|
|
|
|
|
|
|
|
private var _sk2Helper: Any?
|
|
|
|
|
|
2021-11-22 12:37:33 -03:00
|
|
|
@available(iOS 15.0, *)
|
2021-11-29 17:49:16 -03:00
|
|
|
@available(macOS, unavailable)
|
2021-11-22 12:37:33 -03:00
|
|
|
@available(watchOS, unavailable)
|
|
|
|
|
@available(tvOS, unavailable)
|
2024-01-25 15:29:31 -08:00
|
|
|
var sk2Helper: SK2BeginRefundRequestHelperType {
|
2022-06-06 17:15:28 -07:00
|
|
|
get {
|
2025-02-26 12:40:49 +01:00
|
|
|
// swiftlint:disable:next force_cast force_unwrapping
|
2024-01-25 15:29:31 -08:00
|
|
|
return self._sk2Helper! as! SK2BeginRefundRequestHelperType
|
2022-06-06 17:15:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set {
|
|
|
|
|
self._sk2Helper = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 12:37:33 -03:00
|
|
|
#endif
|
|
|
|
|
|
2022-04-08 15:39:34 -07:00
|
|
|
init(systemInfo: SystemInfo, customerInfoManager: CustomerInfoManager, currentUserProvider: CurrentUserProvider) {
|
2021-11-22 12:37:33 -03:00
|
|
|
self.systemInfo = systemInfo
|
2021-12-29 20:43:32 -05:00
|
|
|
self.customerInfoManager = customerInfoManager
|
2022-04-08 15:39:34 -07:00
|
|
|
self.currentUserProvider = currentUserProvider
|
2022-06-06 17:15:28 -07:00
|
|
|
|
2023-08-09 11:19:40 -07:00
|
|
|
#if os(iOS) || VISION_OS
|
2022-06-06 17:15:28 -07:00
|
|
|
if #available(iOS 15, *) {
|
|
|
|
|
self._sk2Helper = SK2BeginRefundRequestHelper()
|
|
|
|
|
} else {
|
|
|
|
|
self._sk2Helper = nil
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-11-22 12:37:33 -03:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 11:19:40 -07:00
|
|
|
#if os(iOS) || VISION_OS
|
2021-11-22 12:37:33 -03:00
|
|
|
/*
|
2021-12-29 20:43:32 -05:00
|
|
|
* Entry point for beginning the refund request. Handles getting the current windowScene and verifying the
|
|
|
|
|
* transaction before calling into `SK2BeginRefundRequestHelper`'s `initiateRefundRequest`.
|
2021-11-22 12:37:33 -03:00
|
|
|
*/
|
|
|
|
|
@available(iOS 15.0, *)
|
2021-11-29 17:49:16 -03:00
|
|
|
@available(macOS, unavailable)
|
2021-11-22 12:37:33 -03:00
|
|
|
@available(watchOS, unavailable)
|
|
|
|
|
@available(tvOS, unavailable)
|
2021-12-29 20:43:32 -05:00
|
|
|
@MainActor
|
|
|
|
|
func beginRefundRequest(forProduct productID: String) async throws -> RefundRequestStatus {
|
2023-06-22 07:31:03 -07:00
|
|
|
let windowScene = try self.systemInfo.currentWindowScene
|
2021-11-22 12:37:33 -03:00
|
|
|
|
2024-01-25 15:29:31 -08:00
|
|
|
let transactionID = try await self.sk2Helper.verifyTransaction(productID: productID)
|
|
|
|
|
return try await self.sk2Helper.initiateRefundRequest(transactionID: transactionID,
|
|
|
|
|
windowScene: windowScene)
|
2021-12-29 20:43:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@available(iOS 15.0, *)
|
|
|
|
|
@available(macOS, unavailable)
|
|
|
|
|
@available(watchOS, unavailable)
|
|
|
|
|
@available(tvOS, unavailable)
|
|
|
|
|
func beginRefundRequest(forEntitlement entitlementID: String) async throws -> RefundRequestStatus {
|
2022-01-31 10:48:05 -03:00
|
|
|
let entitlement = try await getEntitlement(entitlementID: entitlementID)
|
2021-12-29 20:43:32 -05:00
|
|
|
return try await self.beginRefundRequest(forProduct: entitlement.productIdentifier)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@available(iOS 15.0, *)
|
|
|
|
|
@available(macOS, unavailable)
|
|
|
|
|
@available(watchOS, unavailable)
|
|
|
|
|
@available(tvOS, unavailable)
|
|
|
|
|
func beginRefundRequestForActiveEntitlement() async throws -> RefundRequestStatus {
|
2022-01-31 10:48:05 -03:00
|
|
|
let activeEntitlement = try await getEntitlement(entitlementID: nil)
|
2021-12-29 20:43:32 -05:00
|
|
|
return try await self.beginRefundRequest(forProduct: activeEntitlement.productIdentifier)
|
2021-11-22 12:37:33 -03:00
|
|
|
}
|
2021-11-29 17:49:16 -03:00
|
|
|
#endif
|
2021-11-22 12:37:33 -03:00
|
|
|
}
|
|
|
|
|
|
2022-08-31 13:30:38 -07:00
|
|
|
// @unchecked because:
|
|
|
|
|
// - Class is not `final` (it's mocked). This implicitly makes subclasses `Sendable` even if they're not thread-safe.
|
|
|
|
|
// - It has mutable `_sk2Helper` which is necessary due to the availability annotations.
|
|
|
|
|
extension BeginRefundRequestHelper: @unchecked Sendable {}
|
|
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
2023-08-09 11:19:40 -07:00
|
|
|
#if os(iOS) || VISION_OS
|
2021-11-22 12:37:33 -03:00
|
|
|
@available(iOS 15.0, *)
|
2021-11-29 17:49:16 -03:00
|
|
|
@available(macOS, unavailable)
|
2021-11-22 12:37:33 -03:00
|
|
|
@available(watchOS, unavailable)
|
|
|
|
|
@available(tvOS, unavailable)
|
|
|
|
|
private extension BeginRefundRequestHelper {
|
|
|
|
|
|
|
|
|
|
/*
|
2021-12-29 20:43:32 -05:00
|
|
|
* Gets entitlement with the given `entitlementID` from customerInfo, or the active entitlement
|
|
|
|
|
* if no ID passed in.
|
2021-11-22 12:37:33 -03:00
|
|
|
*/
|
2022-01-31 10:48:05 -03:00
|
|
|
func getEntitlement(entitlementID: String?) async throws -> EntitlementInfo {
|
2022-03-15 15:17:04 -05:00
|
|
|
let customerInfo: CustomerInfo
|
2021-11-22 12:37:33 -03:00
|
|
|
|
2022-03-15 15:17:04 -05:00
|
|
|
do {
|
|
|
|
|
customerInfo = try await self.customerInfoManager.customerInfo(
|
2022-05-31 13:04:16 -07:00
|
|
|
appUserID: self.currentUserProvider.currentAppUserID,
|
|
|
|
|
fetchPolicy: .cachedOrFetched
|
2022-03-15 15:17:04 -05:00
|
|
|
)
|
|
|
|
|
} catch {
|
|
|
|
|
let message = Strings.purchase.begin_refund_customer_info_error(entitlementID: entitlementID)
|
|
|
|
|
.description
|
|
|
|
|
throw ErrorUtils.beginRefundRequestError(withMessage: message, error: error)
|
2021-11-22 12:37:33 -03:00
|
|
|
}
|
2022-03-15 15:17:04 -05:00
|
|
|
|
|
|
|
|
if let entitlementID = entitlementID {
|
|
|
|
|
guard let entitlement = customerInfo.entitlements[entitlementID] else {
|
|
|
|
|
let message = Strings.purchase
|
|
|
|
|
.begin_refund_no_entitlement_found(entitlementID: entitlementID)
|
|
|
|
|
.description
|
|
|
|
|
throw ErrorUtils.beginRefundRequestError(withMessage: message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entitlement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard customerInfo.entitlements.active.count < 2 else {
|
|
|
|
|
let message = Strings.purchase.begin_refund_multiple_active_entitlements.description
|
|
|
|
|
throw ErrorUtils.beginRefundRequestError(withMessage: message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let activeEntitlement = customerInfo.entitlements.active.first?.value else {
|
|
|
|
|
let message = Strings.purchase.begin_refund_no_active_entitlement.description
|
|
|
|
|
throw ErrorUtils.beginRefundRequestError(withMessage: message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return activeEntitlement
|
2021-11-22 12:37:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// Status codes for refund requests.
|
2022-08-31 13:30:38 -07:00
|
|
|
@objc(RCRefundRequestStatus) public enum RefundRequestStatus: Int, Sendable {
|
2021-11-22 12:37:33 -03:00
|
|
|
|
|
|
|
|
/// User canceled submission of the refund request.
|
|
|
|
|
@objc(RCRefundRequestUserCancelled) case userCancelled = 0
|
2021-11-29 17:49:16 -03:00
|
|
|
/// Apple has received the refund request.
|
2021-11-22 12:37:33 -03:00
|
|
|
@objc(RCRefundRequestSuccess) case success
|
2021-11-29 17:49:16 -03:00
|
|
|
/// There was an error with the request. See message for more details.
|
2021-11-22 12:37:33 -03:00
|
|
|
@objc(RCRefundRequestError) case error
|
|
|
|
|
|
|
|
|
|
}
|