diff --git a/CHANGELOG.md b/CHANGELOG.md index 81ac131ab..ac766e805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,7 +66,9 @@ Released on 2024-05-10. ## Updated - Update the `dependabot.yml` configuration - Updated in Pull Request [#34](https://github.com/space-code/flare/pull/34) - +- Update Transaction Model with price and currency + - Updated in Pull Request [#84](https://github.com/space-code/flare/pull/84) + ## Fixed - Fix handling of cancelling operations - Fixed in Pull Request [#26](https://github.com/space-code/flare/pull/26). diff --git a/Sources/Flare/Classes/Models/Internal/Protocols/IStoreTransaction.swift b/Sources/Flare/Classes/Models/Internal/Protocols/IStoreTransaction.swift index 4a346da3d..34ec5be54 100644 --- a/Sources/Flare/Classes/Models/Internal/Protocols/IStoreTransaction.swift +++ b/Sources/Flare/Classes/Models/Internal/Protocols/IStoreTransaction.swift @@ -19,7 +19,11 @@ protocol IStoreTransaction { var hasKnownTransactionIdentifier: Bool { get } /// The quantity of the product involved in the transaction. var quantity: Int { get } - + /// The price of the in-app purchase that the system records in the transaction. + var price: Decimal? { get } + /// The currency of the price of the product. + var currency: String? { get } + /// The raw JWS representation of the transaction. /// /// - Note: This is only available for StoreKit 2 transactions. @@ -30,3 +34,14 @@ protocol IStoreTransaction { /// - Note: This is only available for StoreKit 2 transactions. var environment: StoreEnvironment? { get } } + +/// Default implementation of the currency property for backward compatibility. +extension IStoreTransaction { + var currency: String? { + if #available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) { + return Locale.current.currency?.identifier + } else { + return Locale.current.currencyCode + } + } +} diff --git a/Sources/Flare/Classes/Models/Internal/SK1StoreTransaction.swift b/Sources/Flare/Classes/Models/Internal/SK1StoreTransaction.swift index ae44b14a5..49ea13089 100644 --- a/Sources/Flare/Classes/Models/Internal/SK1StoreTransaction.swift +++ b/Sources/Flare/Classes/Models/Internal/SK1StoreTransaction.swift @@ -62,4 +62,10 @@ extension SK1StoreTransaction: IStoreTransaction { var environment: StoreEnvironment? { nil } + var price: Decimal? { + nil + } + var currency: String? { + nil + } } diff --git a/Sources/Flare/Classes/Models/Internal/SK2StoreTransaction.swift b/Sources/Flare/Classes/Models/Internal/SK2StoreTransaction.swift index 75871ca13..d130bed1d 100644 --- a/Sources/Flare/Classes/Models/Internal/SK2StoreTransaction.swift +++ b/Sources/Flare/Classes/Models/Internal/SK2StoreTransaction.swift @@ -66,4 +66,16 @@ extension SK2StoreTransaction: IStoreTransaction { var environment: StoreEnvironment? { StoreEnvironment(transaction: transaction) } + + var price: Decimal? { + transaction.price + } + + var currency: String? { + if #available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) { + transaction.currency?.identifier + } else { + transaction.currencyCode + } + } } diff --git a/Sources/Flare/Classes/Models/StoreTransaction.swift b/Sources/Flare/Classes/Models/StoreTransaction.swift index 271af0d8d..52ae73a8e 100644 --- a/Sources/Flare/Classes/Models/StoreTransaction.swift +++ b/Sources/Flare/Classes/Models/StoreTransaction.swift @@ -80,6 +80,13 @@ extension StoreTransaction: IStoreTransaction { var environment: StoreEnvironment? { storeTransaction.environment } + var price: Decimal? { + storeTransaction.price + } + + var currency: String? { + storeTransaction.currency + } } // MARK: Equatable diff --git a/Tests/FlareTests/UnitTests/TestHelpers/Mocks/StoreTransactionMock.swift b/Tests/FlareTests/UnitTests/TestHelpers/Mocks/StoreTransactionMock.swift index 772c1b172..3b57e61bb 100644 --- a/Tests/FlareTests/UnitTests/TestHelpers/Mocks/StoreTransactionMock.swift +++ b/Tests/FlareTests/UnitTests/TestHelpers/Mocks/StoreTransactionMock.swift @@ -86,4 +86,24 @@ final class StoreTransactionMock: IStoreTransaction { invokedEnvironmentGetterCount += 1 return stubbedEnvironment } + + var invokedPriceGetter = false + var invokedPriceGetterCount = 0 + var stubbedPrice: Decimal! + + var price: Decimal? { + invokedPriceGetter = true + invokedPriceGetterCount += 1 + return stubbedPrice + } + + var invokedCurrencyGetter = false + var invokedCurrencyGetterCount = 0 + var stubbedCurrency: String! + + var currency: String { + invokedCurrencyGetter = true + invokedCurrencyGetterCount += 1 + return stubbedCurrency + } } diff --git a/Tests/FlareTests/UnitTests/TestHelpers/Stubs/StoreTransactionStub.swift b/Tests/FlareTests/UnitTests/TestHelpers/Stubs/StoreTransactionStub.swift index 516d7c05d..c35f69f81 100644 --- a/Tests/FlareTests/UnitTests/TestHelpers/Stubs/StoreTransactionStub.swift +++ b/Tests/FlareTests/UnitTests/TestHelpers/Stubs/StoreTransactionStub.swift @@ -54,4 +54,16 @@ final class StoreTransactionStub: IStoreTransaction { var environment: StoreEnvironment? { stubbedEnvironment } + + var stubbedPrice: Decimal! + + var price: Decimal? { + stubbedPrice + } + + var stubbedCurrency: String! + + var currency: String? { + stubbedCurrency + } }