Skip to content

Commit

Permalink
Rewrite wrong refund logic (#462)
Browse files Browse the repository at this point in the history
So, if both a purchase and a refund of feature `.foobar` existed,
whatever the dates, the purchase was incorrectly discarded.

Fixes #459, fixes #461
  • Loading branch information
keeshux authored Jan 10, 2024
1 parent 821d4c7 commit 2e989fd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 61 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- "Restore purchases" not working. [#459](https://github.com/passepartoutvpn/passepartout-apple/issues/459)
- Purchase is not credited if any refund was issued in the past. [#461](https://github.com/passepartoutvpn/passepartout-apple/issues/461)

## 2.3.2 (2024-01-09)

### Fixed
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ GEM
PLATFORMS
arm64-darwin-22
x86_64-darwin-19
x86_64-linux

DEPENDENCIES
dotenv
Expand Down
7 changes: 4 additions & 3 deletions Passepartout/App/Constants/Constants+App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ extension Constants {
private static let parentPath = "Library/Caches"

static let level: LoggerLevel = {
guard let levelString = ProcessInfo.processInfo.environment["LOG_LEVEL"], let levelNum = Int(levelString) else {
return .info
guard let levelString = ProcessInfo.processInfo.environment["LOG_LEVEL"],
let levelNum = Int(levelString) else {
return .debug
}
return .init(rawValue: levelNum) ?? .info
return .init(rawValue: levelNum) ?? .debug
}()

static let maxBytes = 100000
Expand Down
9 changes: 0 additions & 9 deletions Passepartout/App/Context/AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,6 @@ private extension AppContext {
self?.reviewer.reportEvent()
}
}.store(in: &cancellables)

productManager.didRefundProducts
.receive(on: DispatchQueue.main)
.sink { [weak self] in
Task {
pp_log.info("Refunds detected, uninstalling VPN profile")
await self?.coreContext.vpnManager.uninstall()
}
}.store(in: &cancellables)
}

// eligibility: ignore network settings if ineligible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public final class ProductManager: NSObject, ObservableObject {

public let buildProducts: BuildProducts

public let didRefundProducts = PassthroughSubject<Void, Never>()

@Published public private(set) var appType: AppType

@Published public private(set) var isRefreshingProducts = false
Expand All @@ -58,19 +56,6 @@ public final class ProductManager: NSObject, ObservableObject {

private var purchaseDates: [LocalProduct: Date]

private var cancelledPurchases: Set<LocalProduct>? {
willSet {
guard cancelledPurchases != nil else {
return
}
guard let newCancelledPurchases = newValue, newCancelledPurchases != cancelledPurchases else {
pp_log.debug("No purchase was refunded")
return
}
detectRefunds(newCancelledPurchases)
}
}

public init(inApp: any LocalInApp,
receiptReader: ReceiptReader,
overriddenAppType: AppType? = nil,
Expand All @@ -85,7 +70,6 @@ public final class ProductManager: NSObject, ObservableObject {
purchasedAppBuild = nil
purchasedFeatures = []
purchaseDates = [:]
cancelledPurchases = nil

super.init()

Expand Down Expand Up @@ -194,15 +178,24 @@ public final class ProductManager: NSObject, ObservableObject {

extension ProductManager {
public func isEligible(forFeature feature: LocalProduct) -> Bool {
if let purchasedAppBuild = purchasedAppBuild {
if let purchasedAppBuild {
if feature == .networkSettings && buildProducts.hasProduct(.networkSettings, atBuild: purchasedAppBuild) {
return true
}
}
pp_log.verbose("Eligibility: purchasedFeatures = \(purchasedFeatures)")
pp_log.verbose("Eligibility: purchaseDates = \(purchaseDates)")
pp_log.verbose("Eligibility: isIncludedInFullVersion(\(feature)) = \(isIncludedInFullVersion(feature))")
if isIncludedInFullVersion(feature) {
return isFullVersion() || isActivePurchase(feature)
let isFullVersion = isFullVersion()
let isActive = isActivePurchase(feature)
pp_log.verbose("Eligibility: isFullVersion() = \(isFullVersion)")
pp_log.verbose("Eligibility: isActivePurchase(\(feature)) = \(isActive)")
return isFullVersion || isActive
}
return isActivePurchase(feature)
let isActive = isActivePurchase(feature)
pp_log.verbose("Eligibility: isActivePurchase(\(feature)) = \(isActive)")
return isActive
}

public func isEligible(forProvider providerName: ProviderName) -> Bool {
Expand All @@ -219,11 +212,11 @@ extension ProductManager {

extension ProductManager {
func isActivePurchase(_ feature: LocalProduct) -> Bool {
purchasedFeatures.contains(feature) && cancelledPurchases?.contains(feature) != true
purchasedFeatures.contains(feature)
}

func isActivePurchase(where predicate: (LocalProduct) -> Bool) -> Bool {
purchasedFeatures.contains(where: predicate) && cancelledPurchases?.contains(where: predicate) != true
purchasedFeatures.contains(where: predicate)
}

func isCurrentPlatformVersion() -> Bool {
Expand All @@ -236,16 +229,19 @@ extension ProductManager {

public func isFullVersion() -> Bool {
if appType == .fullVersion {
pp_log.verbose("Eligibility: appType = .fullVersion")
return true
}
pp_log.verbose("Eligibility: isCurrentPlatformVersion() = \(isCurrentPlatformVersion())")
if isCurrentPlatformVersion() {
return true
}
pp_log.verbose("Eligibility: isActivePurchase(.fullVersion) = \(isActivePurchase(.fullVersion))")
return isActivePurchase(.fullVersion)
}

public func isPayingUser() -> Bool {
!purchasedFeatures.subtracting(cancelledPurchases ?? []).isEmpty
!purchasedFeatures.isEmpty
}
}

Expand All @@ -262,7 +258,6 @@ extension ProductManager {
purchasedAppBuild = originalBuildNumber
}
purchasedFeatures.removeAll()
var newCancelledPurchases: Set<LocalProduct> = []

if let buildNumber = purchasedAppBuild {
pp_log.debug("Original purchased build: \(buildNumber)")
Expand All @@ -282,7 +277,6 @@ extension ProductManager {
}
if let cancellationDate = $0.cancellationDate {
pp_log.debug("\t\(pid) [cancelled on: \(cancellationDate)]")
newCancelledPurchases.insert(product)
return
}
if let purchaseDate = $0.originalPurchaseDate {
Expand All @@ -296,7 +290,6 @@ extension ProductManager {
if andNotify {
objectWillChange.send()
}
cancelledPurchases = newCancelledPurchases
}
}

Expand All @@ -310,28 +303,4 @@ private extension ProductManager {
false
#endif
}

// FIXME: in-app, this is incomplete
func detectRefunds(_ refunds: Set<LocalProduct>) {
let isEligibleForFullVersion = isFullVersion()
let hasCancelledFullVersion: Bool
let hasCancelledTrustedNetworks: Bool

if isMac {
hasCancelledFullVersion = !isEligibleForFullVersion && (
refunds.contains(.fullVersion) || refunds.contains(.fullVersion_macOS)
)
hasCancelledTrustedNetworks = false
} else {
hasCancelledFullVersion = !isEligibleForFullVersion && (
refunds.contains(.fullVersion) || refunds.contains(.fullVersion_iOS)
)
hasCancelledTrustedNetworks = !isEligibleForFullVersion && refunds.contains(.trustedNetworks)
}

// review features and potentially revert them if they were used (Siri is handled in AppDelegate)
if hasCancelledFullVersion || hasCancelledTrustedNetworks {
didRefundProducts.send()
}
}
}

0 comments on commit 2e989fd

Please sign in to comment.