Skip to content

Commit

Permalink
feat(transaction): enhance IStoreTransaction with price and currency … (
Browse files Browse the repository at this point in the history
#84)

* feat(transaction): enhance IStoreTransaction with price and currency fields

* update changelog.md

---------

Co-authored-by: M7md <[email protected]>
  • Loading branch information
M7md-Ebrahim and M7md-Ebrahim authored Oct 16, 2024
1 parent ff78732 commit b6dabf3
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Check failure on line 26 in Sources/Flare/Classes/Models/Internal/Protocols/IStoreTransaction.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
/// The raw JWS representation of the transaction.
///
/// - Note: This is only available for StoreKit 2 transactions.
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ extension SK1StoreTransaction: IStoreTransaction {
var environment: StoreEnvironment? {
nil
}
var price: Decimal? {
nil
}
var currency: String? {
nil
}
}
12 changes: 12 additions & 0 deletions Sources/Flare/Classes/Models/Internal/SK2StoreTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ extension SK2StoreTransaction: IStoreTransaction {
var environment: StoreEnvironment? {
StoreEnvironment(transaction: transaction)
}

Check failure on line 69 in Sources/Flare/Classes/Models/Internal/SK2StoreTransaction.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
var price: Decimal? {
transaction.price
}

Check failure on line 73 in Sources/Flare/Classes/Models/Internal/SK2StoreTransaction.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
var currency: String? {
if #available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) {
transaction.currency?.identifier
} else {
transaction.currencyCode
}
}
}
7 changes: 7 additions & 0 deletions Sources/Flare/Classes/Models/StoreTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ extension StoreTransaction: IStoreTransaction {
var environment: StoreEnvironment? {
storeTransaction.environment
}
var price: Decimal? {
storeTransaction.price
}

Check failure on line 86 in Sources/Flare/Classes/Models/StoreTransaction.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
var currency: String? {
storeTransaction.currency
}
}

// MARK: Equatable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit b6dabf3

Please sign in to comment.