Skip to content

Commit

Permalink
feat(transaction): enhance IStoreTransaction with price and currency …
Browse files Browse the repository at this point in the history
…fields
  • Loading branch information
M7md-Ebrahim committed Oct 15, 2024
1 parent ff78732 commit 9ce0740
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
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 9ce0740

Please sign in to comment.